optional json report

pull/1/head
Tom Early 3 years ago
parent 0926767a60
commit b286079b73

@ -95,3 +95,15 @@ void CClient::WriteXml(std::ofstream &xmlFile)
}
xmlFile << "</NODE>" << std::endl;
}
void CClient::JsonReport(nlohmann::json &report)
{
nlohmann::json jclient;
jclient["Callsign"] = m_Callsign.GetCS();
jclient["OnModule"] = std::string(1, m_ReflectorModule);
jclient["Protocol"] = GetProtocolName();
char s[100];
if (std::strftime(s, sizeof(s), "%FT%TZ", std::gmtime(&m_ConnectTime)))
jclient["ConnectTime"] = s;
report["Clients"].push_back(jclient);
}

@ -18,6 +18,8 @@
#pragma once
#include <nlohmann/json.hpp>
#include "Defines.h"
#include "Timer.h"
#include "IP.h"
@ -72,6 +74,7 @@ public:
// reporting
virtual void WriteXml(std::ofstream &);
void JsonReport(nlohmann::json &report);
protected:
// data

@ -41,6 +41,7 @@ public:
ERefreshType GetRefreshType(const std::string &key) const;
bool IsString(const std::string &key) const;
char GetAutolinkModule(const std::string &key) const;
const nlohmann::json &GetData() { return data; }
private:
// CFGDATA data;

@ -16,7 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <nlohmann/json.hpp>
#include <string.h>
#include "Reflector.h"
#include "Peer.h"
@ -124,3 +124,17 @@ void CPeer::WriteXml(std::ofstream &xmlFile)
}
xmlFile << "</PEER>" << std::endl;
}
void CPeer::JsonReport(nlohmann::json &report)
{
nlohmann::json jpeer;
jpeer["Callsign"] = m_Callsign.GetCS();
jpeer["Modules"] = m_ReflectorModules;
jpeer["Protocol"] = GetProtocolName();
char s[100];
if (std::strftime(s, sizeof(s), "%FT%TZ", std::gmtime(&m_ConnectTime)))
{
jpeer["ConnectTime"] = s;
}
report["Peers"].push_back(jpeer);
}

@ -68,6 +68,7 @@ public:
// reporting
virtual void WriteXml(std::ofstream &);
void JsonReport(nlohmann::json &report);
protected:
// data

@ -298,23 +298,41 @@ void CReflector::RouterThread(const char ThisModule)
void CReflector::XmlReportThread()
{
const std::string path(g_Configure.GetString(g_Keys.files.xml));
const std::string xmlpath(g_Configure.GetString(g_Keys.files.xml));
const std::string jsnpath(g_Configure.GetString(g_Keys.files.json));
while (keep_running)
{
// report to xml file
std::ofstream xmlFile;
xmlFile.open(path, std::ios::out | std::ios::trunc);
if ( xmlFile.is_open() )
if (! xmlpath.empty())
{
// write xml file
WriteXmlFile(xmlFile);
std::ofstream xmlFile;
xmlFile.open(xmlpath, std::ios::out | std::ios::trunc);
if ( xmlFile.is_open() )
{
// write xml file
WriteXmlFile(xmlFile);
// and close file
xmlFile.close();
// and close file
xmlFile.close();
}
else
{
std::cout << "Failed to open " << xmlpath << std::endl;
}
}
else
// json report
if (! jsnpath.empty())
{
std::cout << "Failed to open " << path << std::endl;
nlohmann::json jreport;
JsonReport(jreport);
std::ofstream jsonFile;
jsonFile.open(jsnpath, std::ios::out | std::ios::trunc);
if (jsonFile.is_open())
{
jsonFile << jreport.dump(4);
jsonFile.close();
}
}
// and wait a bit
@ -404,7 +422,31 @@ char CReflector::GetStreamModule(std::shared_ptr<CPacketStream> stream)
}
////////////////////////////////////////////////////////////////////////////////////////
// xml helpers
// report helpers
void CReflector::JsonReport(nlohmann::json &report)
{
for (auto &item : g_Configure.GetData().items())
{
if (isupper(item.key().at(0)))
report["Configure"][item.key()] = item.value();
}
auto peers = GetPeers();
for (auto pit=peers->cbegin(); pit!=peers->cend(); pit++)
(*pit)->JsonReport(report);
ReleasePeers();
auto clients = GetClients();
for (auto cit=clients->cbegin(); cit!=clients->cend(); cit++)
(*cit)->JsonReport(report);
ReleaseClients();
auto users = GetUsers();
for (auto uid=users->begin(); uid!=users->end(); uid++)
(*uid).JsonReport(report);
ReleaseUsers();
}
void CReflector::WriteXmlFile(std::ofstream &xmlFile)
{

@ -90,6 +90,7 @@ protected:
// xml helpers
void WriteXmlFile(std::ofstream &);
void JsonReport(nlohmann::json &report);
protected:
// identity

@ -76,3 +76,18 @@ void CUser::WriteXml(std::ofstream &xmlFile)
}
xmlFile << "</STATION>" << std::endl;
}
void CUser::JsonReport(nlohmann::json &report)
{
nlohmann::json juser;
juser["Callsign"] = m_My.GetCS();
juser["Repeater"] = m_Rpt1.GetCS();
juser["OnModule"] = std::string(1, m_Rpt2.GetCSModule());
juser["ViaPeer"] = m_Xlx.GetCS();
char s[100];
if (std::strftime(s, sizeof(s), "%FT%TZ", std::gmtime(&m_LastHeardTime)))
{
juser["LastHeard"] = s;
}
report["Users"].push_back(juser);
}

@ -18,6 +18,8 @@
#pragma once
#include <nlohmann/json.hpp>
#include "Callsign.h"
#include "Buffer.h"
@ -41,6 +43,7 @@ public:
// reporting
void WriteXml(std::ofstream &);
void JsonReport(nlohmann::json &report);
protected:
// data

Loading…
Cancel
Save

Powered by TurnKey Linux.