diff --git a/reflector/Client.cpp b/reflector/Client.cpp index 4f6c9c7..f026463 100644 --- a/reflector/Client.cpp +++ b/reflector/Client.cpp @@ -95,3 +95,15 @@ void CClient::WriteXml(std::ofstream &xmlFile) } xmlFile << "" << 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); +} diff --git a/reflector/Client.h b/reflector/Client.h index 28a3e4b..48fb022 100644 --- a/reflector/Client.h +++ b/reflector/Client.h @@ -18,6 +18,8 @@ #pragma once +#include + #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 diff --git a/reflector/Configure.h b/reflector/Configure.h index bb52d3f..1ae056a 100644 --- a/reflector/Configure.h +++ b/reflector/Configure.h @@ -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; diff --git a/reflector/Peer.cpp b/reflector/Peer.cpp index dca36cd..3a1638e 100644 --- a/reflector/Peer.cpp +++ b/reflector/Peer.cpp @@ -16,7 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . - +#include #include #include "Reflector.h" #include "Peer.h" @@ -124,3 +124,17 @@ void CPeer::WriteXml(std::ofstream &xmlFile) } xmlFile << "" << 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); +} diff --git a/reflector/Peer.h b/reflector/Peer.h index e91dfb4..bc1ddf9 100644 --- a/reflector/Peer.h +++ b/reflector/Peer.h @@ -68,6 +68,7 @@ public: // reporting virtual void WriteXml(std::ofstream &); + void JsonReport(nlohmann::json &report); protected: // data diff --git a/reflector/Reflector.cpp b/reflector/Reflector.cpp index 956758c..be99277 100644 --- a/reflector/Reflector.cpp +++ b/reflector/Reflector.cpp @@ -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 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) { diff --git a/reflector/Reflector.h b/reflector/Reflector.h index 30fce3c..efe22ea 100644 --- a/reflector/Reflector.h +++ b/reflector/Reflector.h @@ -90,6 +90,7 @@ protected: // xml helpers void WriteXmlFile(std::ofstream &); + void JsonReport(nlohmann::json &report); protected: // identity diff --git a/reflector/User.cpp b/reflector/User.cpp index e9b54ce..2110fdc 100644 --- a/reflector/User.cpp +++ b/reflector/User.cpp @@ -76,3 +76,18 @@ void CUser::WriteXml(std::ofstream &xmlFile) } xmlFile << "" << 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); +} diff --git a/reflector/User.h b/reflector/User.h index 617db1c..d5ed1d2 100644 --- a/reflector/User.h +++ b/reflector/User.h @@ -18,6 +18,8 @@ #pragma once +#include + #include "Callsign.h" #include "Buffer.h" @@ -41,6 +43,7 @@ public: // reporting void WriteXml(std::ofstream &); + void JsonReport(nlohmann::json &report); protected: // data