2016-04-11 10:52:21 +00:00
|
|
|
/*
|
2017-08-19 11:37:32 +00:00
|
|
|
* Copyright (C) 2016,2017 by Jonathan Naylor G4KLX
|
2016-04-11 10:52:21 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "DMRLookup.h"
|
2016-10-10 18:15:30 +00:00
|
|
|
#include "Timer.h"
|
2016-04-11 10:52:21 +00:00
|
|
|
#include "Log.h"
|
|
|
|
|
|
|
|
#include <cstdio>
|
2016-06-13 17:48:05 +00:00
|
|
|
#include <cstdlib>
|
2016-04-11 10:52:21 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <cctype>
|
|
|
|
|
2016-10-10 18:15:30 +00:00
|
|
|
CDMRLookup::CDMRLookup(const std::string& filename, unsigned int reloadTime) :
|
|
|
|
CThread(),
|
|
|
|
m_filename(filename),
|
|
|
|
m_reloadTime(reloadTime),
|
|
|
|
m_table(),
|
2021-01-01 13:54:03 +00:00
|
|
|
m_stop(false),
|
|
|
|
m_reload(false)
|
2016-04-11 10:52:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CDMRLookup::~CDMRLookup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CDMRLookup::read()
|
2016-10-10 18:15:30 +00:00
|
|
|
{
|
2020-03-07 07:52:11 +00:00
|
|
|
bool ret = m_table.load(m_filename);
|
2016-10-10 18:15:30 +00:00
|
|
|
|
|
|
|
if (m_reloadTime > 0U)
|
|
|
|
run();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-01-01 13:54:03 +00:00
|
|
|
void CDMRLookup::reload()
|
|
|
|
{
|
|
|
|
if (m_reloadTime == 0U)
|
|
|
|
m_table.load(m_filename);
|
|
|
|
else
|
|
|
|
m_reload = true;
|
|
|
|
}
|
|
|
|
|
2016-10-10 18:15:30 +00:00
|
|
|
void CDMRLookup::entry()
|
|
|
|
{
|
|
|
|
LogInfo("Started the DMR Id lookup reload thread");
|
|
|
|
|
|
|
|
CTimer timer(1U, 3600U * m_reloadTime);
|
|
|
|
timer.start();
|
|
|
|
|
|
|
|
while (!m_stop) {
|
|
|
|
sleep(1000U);
|
|
|
|
|
|
|
|
timer.clock();
|
2021-01-01 13:54:03 +00:00
|
|
|
if (timer.hasExpired() || m_reload) {
|
2020-03-07 07:52:11 +00:00
|
|
|
m_table.load(m_filename);
|
2016-10-10 18:15:30 +00:00
|
|
|
timer.start();
|
2021-01-01 13:54:03 +00:00
|
|
|
m_reload = false;
|
2016-10-10 18:15:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LogInfo("Stopped the DMR Id lookup reload thread");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDMRLookup::stop()
|
|
|
|
{
|
|
|
|
if (m_reloadTime == 0U) {
|
|
|
|
delete this;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_stop = true;
|
|
|
|
|
|
|
|
wait();
|
|
|
|
}
|
|
|
|
|
2020-03-07 23:06:27 +00:00
|
|
|
void CDMRLookup::findWithName(unsigned int id, class CUserDBentry *entry)
|
2016-10-10 18:15:30 +00:00
|
|
|
{
|
2020-03-07 23:06:27 +00:00
|
|
|
if (id == 0xFFFFFFU) {
|
|
|
|
entry->clear();
|
|
|
|
entry->set(keyCALLSIGN, "ALL");
|
|
|
|
return;
|
|
|
|
}
|
2016-10-10 18:15:30 +00:00
|
|
|
|
2020-03-07 23:06:27 +00:00
|
|
|
if (m_table.lookup(id, entry)) {
|
|
|
|
LogDebug("FindWithName =%s %s", entry->get(keyCALLSIGN).c_str(), entry->get(keyFIRST_NAME).c_str());
|
2020-03-07 07:52:11 +00:00
|
|
|
} else {
|
2020-03-07 23:06:27 +00:00
|
|
|
entry->clear();
|
|
|
|
|
2018-04-10 22:08:48 +00:00
|
|
|
char text[10U];
|
2020-03-07 07:52:11 +00:00
|
|
|
::snprintf(text, sizeof(text), "%u", id);
|
2020-03-07 23:06:27 +00:00
|
|
|
entry->set(keyCALLSIGN, text);
|
2018-04-10 22:08:48 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 23:06:27 +00:00
|
|
|
return;
|
2018-04-10 22:08:48 +00:00
|
|
|
}
|
2020-03-09 20:26:45 +00:00
|
|
|
|
2018-04-10 22:08:48 +00:00
|
|
|
std::string CDMRLookup::find(unsigned int id)
|
|
|
|
{
|
|
|
|
std::string callsign;
|
2020-03-09 13:16:21 +00:00
|
|
|
|
2018-04-10 22:08:48 +00:00
|
|
|
if (id == 0xFFFFFFU)
|
|
|
|
return std::string("ALL");
|
|
|
|
|
2020-03-07 07:52:11 +00:00
|
|
|
class CUserDBentry entry;
|
|
|
|
if (m_table.lookup(id, &entry)) {
|
|
|
|
callsign = entry.get(keyCALLSIGN);
|
|
|
|
} else {
|
2016-10-10 18:15:30 +00:00
|
|
|
char text[10U];
|
2020-03-07 07:52:11 +00:00
|
|
|
::snprintf(text, sizeof(text), "%u", id);
|
2016-10-10 18:15:30 +00:00
|
|
|
callsign = std::string(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
return callsign;
|
|
|
|
}
|
|
|
|
|
2017-08-19 11:37:32 +00:00
|
|
|
bool CDMRLookup::exists(unsigned int id)
|
|
|
|
{
|
2020-03-07 07:52:11 +00:00
|
|
|
return m_table.lookup(id, NULL);
|
2017-08-19 11:37:32 +00:00
|
|
|
}
|