Simplify the JSON creation, remove duplication of effort.

This commit is contained in:
Jonathan Naylor
2022-12-29 16:01:27 +00:00
parent 5c12bbb7b0
commit 38a6dbbdeb
2 changed files with 59 additions and 100 deletions

View File

@@ -25,8 +25,6 @@
#include <cstring> #include <cstring>
#include <ctime> #include <ctime>
#include <nlohmann/json.hpp>
const unsigned int INTERLEAVER[] = { const unsigned int INTERLEAVER[] = {
0U, 137U, 90U, 227U, 180U, 317U, 270U, 39U, 360U, 129U, 82U, 219U, 172U, 309U, 262U, 31U, 352U, 121U, 74U, 211U, 164U, 0U, 137U, 90U, 227U, 180U, 317U, 270U, 39U, 360U, 129U, 82U, 219U, 172U, 309U, 262U, 31U, 352U, 121U, 74U, 211U, 164U,
301U, 254U, 23U, 344U, 113U, 66U, 203U, 156U, 293U, 246U, 15U, 336U, 105U, 58U, 195U, 148U, 285U, 238U, 7U, 328U, 97U, 301U, 254U, 23U, 344U, 113U, 66U, 203U, 156U, 293U, 246U, 15U, 336U, 105U, 58U, 195U, 148U, 285U, 238U, 7U, 328U, 97U,
@@ -932,28 +930,7 @@ void CM17Control::writeJSON(const char* action, RPT_RF_STATE state, const std::s
nlohmann::json json; nlohmann::json json;
json["timestamp"] = CUtils::createTimestamp(); writeJSON(json, action, state, source, dest);
json["source_callsign"] = source;
json["destination_callsign"] = dest;
json["source"] = "rf";
json["action"] = action;
switch (state)
{
case RS_RF_AUDIO:
json["traffic_type"] = "audio";
break;
case RS_RF_DATA_AUDIO:
json["traffic_type"] = "audio_data";
break;
case RS_RF_DATA:
json["traffic_type"] = "data";
break;
default:
break;
}
WriteJSON(json.dump()); WriteJSON(json.dump());
} }
@@ -964,31 +941,7 @@ void CM17Control::writeJSON(const char* action, RPT_RF_STATE state, const std::s
nlohmann::json json; nlohmann::json json;
json["timestamp"] = CUtils::createTimestamp(); writeJSON(json, action, state, source, dest, duration, ber);
json["source_callsign"] = source;
json["destination_callsign"] = dest;
json["source"] = "rf";
json["action"] = action;
switch (state)
{
case RS_RF_AUDIO:
json["traffic_type"] = "audio";
break;
case RS_RF_DATA_AUDIO:
json["traffic_type"] = "audio_data";
break;
case RS_RF_DATA:
json["traffic_type"] = "data";
break;
default:
break;
}
json["duration"] = duration;
json["ber"] = ber;
WriteJSON(json.dump()); WriteJSON(json.dump());
} }
@@ -999,31 +952,7 @@ void CM17Control::writeJSON(const char* action, RPT_RF_STATE state, const std::s
nlohmann::json json; nlohmann::json json;
json["timestamp"] = CUtils::createTimestamp(); writeJSON(json, action, state, source, dest, duration, ber);
json["source_callsign"] = source;
json["destination_callsign"] = dest;
json["source"] = "rf";
json["action"] = action;
switch (state)
{
case RS_RF_AUDIO:
json["traffic_type"] = "audio";
break;
case RS_RF_DATA_AUDIO:
json["traffic_type"] = "audio_data";
break;
case RS_RF_DATA:
json["traffic_type"] = "data";
break;
default:
break;
}
json["duration"] = duration;
json["ber"] = ber;
nlohmann::json rssi; nlohmann::json rssi;
rssi["minimumm"] = minRSSI; rssi["minimumm"] = minRSSI;
@@ -1041,28 +970,7 @@ void CM17Control::writeJSON(const char* action, RPT_NET_STATE state, const std::
nlohmann::json json; nlohmann::json json;
json["timestamp"] = CUtils::createTimestamp(); writeJSON(action, state, source, dest);
json["source_callsign"] = source;
json["destination_callsign"] = dest;
json["source"] = "network";
json["action"] = action;
switch (state)
{
case RS_NET_AUDIO:
json["traffic_type"] = "audio";
break;
case RS_NET_DATA_AUDIO:
json["traffic_type"] = "audio_data";
break;
case RS_NET_DATA:
json["traffic_type"] = "data";
break;
default:
break;
}
WriteJSON(json.dump()); WriteJSON(json.dump());
} }
@@ -1073,6 +981,55 @@ void CM17Control::writeJSON(const char* action, RPT_NET_STATE state, const std::
nlohmann::json json; nlohmann::json json;
writeJSON(action, state, source, dest);
json["duration"] = duration;
WriteJSON(json.dump());
}
void CM17Control::writeJSON(nlohmann::json& json, const char* action, RPT_RF_STATE state, const std::string& source, const std::string& dest)
{
assert(action != NULL);
json["timestamp"] = CUtils::createTimestamp();
json["source_callsign"] = source;
json["destination_callsign"] = dest;
json["source"] = "rf";
json["action"] = action;
switch (state)
{
case RS_RF_AUDIO:
json["traffic_type"] = "audio";
break;
case RS_RF_DATA_AUDIO:
json["traffic_type"] = "audio_data";
break;
case RS_RF_DATA:
json["traffic_type"] = "data";
break;
default:
break;
}
}
void CM17Control::writeJSON(nlohmann::json& json, const char* action, RPT_RF_STATE state, const std::string& source, const std::string& dest, float duration, float ber)
{
assert(action != NULL);
writeJSON(json, action, state, source, dest);
json["duration"] = duration;
json["ber"] = ber;
}
void CM17Control::writeJSON(nlohmann::json& json, const char* action, RPT_NET_STATE state, const std::string& source, const std::string& dest)
{
assert(action != NULL);
json["timestamp"] = CUtils::createTimestamp(); json["timestamp"] = CUtils::createTimestamp();
json["source_callsign"] = source; json["source_callsign"] = source;
@@ -1095,9 +1052,5 @@ void CM17Control::writeJSON(const char* action, RPT_NET_STATE state, const std::
default: default:
break; break;
} }
json["duration"] = duration;
WriteJSON(json.dump());
} }

View File

@@ -32,6 +32,8 @@
#include <string> #include <string>
#include <nlohmann/json.hpp>
class CM17Control { class CM17Control {
public: public:
CM17Control(const std::string& callsign, unsigned int can, bool selfOnly, bool allowEncryption, CM17Network* network, CDisplay* display, unsigned int timeout, bool duplex, CRSSIInterpolator* rssiMapper); CM17Control(const std::string& callsign, unsigned int can, bool selfOnly, bool allowEncryption, CM17Network* network, CDisplay* display, unsigned int timeout, bool duplex, CRSSIInterpolator* rssiMapper);
@@ -109,6 +111,10 @@ private:
void writeJSON(const char* action, RPT_NET_STATE state, const std::string& source, const std::string& dest); void writeJSON(const char* action, RPT_NET_STATE state, const std::string& source, const std::string& dest);
void writeJSON(const char* action, RPT_NET_STATE state, const std::string& source, const std::string& dest, float duration); void writeJSON(const char* action, RPT_NET_STATE state, const std::string& source, const std::string& dest, float duration);
void writeJSON(nlohmann::json& json, const char* action, RPT_RF_STATE state, const std::string& source, const std::string& dest);
void writeJSON(nlohmann::json& json, const char* action, RPT_RF_STATE state, const std::string& source, const std::string& dest, float duration, float ber);
void writeJSON(nlohmann::json& json, const char* action, RPT_NET_STATE state, const std::string& source, const std::string& dest);
bool openFile(); bool openFile();
bool writeFile(const unsigned char* data); bool writeFile(const unsigned char* data);
void closeFile(); void closeFile();