summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarsh Sharma <harsh.sharma@multitech.com>2021-03-11 14:22:13 -0600
committerHarsh Sharma <harsh.sharma@multitech.com>2021-03-11 14:22:13 -0600
commit65b51c09edacb77f45c55c5488a90ec95346c414 (patch)
tree11071156e9302419f0fba08903993c18b980b63a
parentfb507bdaa76f0ca15fc22a876f2b2947d1359e2f (diff)
downloadlora-query-1.0.7.tar.gz
lora-query-1.0.7.tar.bz2
lora-query-1.0.7.zip
Removed jsoncpp dependancyHEAD1.0.7master
-rw-r--r--Makefile2
-rw-r--r--main.cpp179
2 files changed, 18 insertions, 163 deletions
diff --git a/Makefile b/Makefile
index 04de0ec..d45f683 100644
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ libdir ?= /usr/lib
CFLAGS += -Werror
-LIBS := -lmts -ljsoncpp -lpthread
+LIBS := -lmts -lpthread
OBJS += \
Version.o \
diff --git a/main.cpp b/main.cpp
index 9dd61f2..21e216f 100644
--- a/main.cpp
+++ b/main.cpp
@@ -19,7 +19,6 @@
*/
#include <mts/MTS_Text.h>
#include <mts/MTS_Logger.h>
-#include <json/json.h>
#include <getopt.h>
#include <sys/stat.h>
#include <arpa/inet.h>
@@ -65,10 +64,10 @@ char* command_args[20];
int timeout = TIMEOUT;
-const char* cmd_stats = "stats";
+const char* cmd_stats = "stats json_api";
const char* cmd_stats_reset = "stats reset";
const char* cmd_node_list = "node stats";
-const char* cmd_node_config = "node config";
+const char* cmd_node_list_json = "node stats json";
const char* cmd_node_update = "node update";
const char* cmd_node_delete = "node delete";
const char* cmd_nodeadd = "node add";
@@ -76,37 +75,14 @@ const std::string lora_command_output("/var/tmp/lora_command_output");
const std::string lora_network_stats_json("/var/tmp/lora_network_stats.json");
const std::string lora_network_nodelist("/var/tmp/lora_network_nodelist");
const std::string lora_network_nodelist_json("/var/tmp/lora_network_nodelist.json");
-const std::string lora_network_nodeconfig("/var/tmp/lora_network_nodeconfig");
-const std::string lora_network_nodeconfig_json("/var/tmp/lora_network_nodeconfig.json");
std::stringstream receiveStream;
const int nodeSize = 16;
const int configSize = 7;
-const std::string NODE_ADDR("nodeAddr");
-const std::string NODE_DEV_EUI("devEui");
-const std::string NODE_APP_EUI("appEui");
-const std::string NODE_APP_KEY("appKey");
-const std::string NODE_NS_KEY("nsKey");
-const std::string NODE_DS_KEY("dsKey");
-const std::string NODE_CLASS("class");
-const std::string NODE_JOINED("joined");
-const std::string NODE_SEQ_NUM("seqNum");
-const std::string NODE_PKTS_UP("pktsUp");
-const std::string NODE_PKTS_DOWN("pktsDown");
-const std::string NODE_PKTS_1ST("pkts1st");
-const std::string NODE_PKTS_2ND("pkts2nd");
-const std::string NODE_DROPPED("dropped");
-const std::string NODE_RSSI_MIN("rssiMin");
-const std::string NODE_RSSI_MAX("rssiMax");
-const std::string NODE_RSSI_AVR("rssiAvg");
-const std::string NODE_SNR_MIX("snrMin");
-const std::string NODE_SNR_MAX("snrMax");
-const std::string NODE_SNR_AVR("snrAvg");
void runCmd(const char *command);
void printStats(void);
void printNodeList(void);
-void printNodeConfig(void);
void saveToFile(const std::string& fileName, const std::string& buffer);
void parseOptions(int argc, char** argv);
void printHelp(const std::string& sApp);
@@ -128,7 +104,7 @@ int main(int argc, char**argv) {
runCmd(cmd.str().c_str());
if (receiveStream.str().empty()) {
- return 0;
+ return 0;
}
saveToFile(lora_command_output, receiveStream.str());
std::cout << receiveStream.str();
@@ -139,28 +115,20 @@ int main(int argc, char**argv) {
printStats();
}
if (opt_get_nodelist) {
- runCmd(cmd_node_list);
if (opt_get_json) {
- printNodeList();
- } else {
+ runCmd(cmd_node_list_json);
if (receiveStream.str().empty()) {
return 0;
}
- saveToFile(lora_network_nodelist, receiveStream.str());
+ saveToFile(lora_network_nodelist_json, receiveStream.str());
std::cout << receiveStream.str();
- }
- }
- if (opt_get_nodeconfig) {
- char buff[256];
- snprintf(buff, 256, "%s %s", cmd_node_config, node_config_addr);
- runCmd(buff);
- if (opt_get_json) {
- printNodeConfig();
} else {
- if (!receiveStream.str().empty()) {
- saveToFile(lora_network_nodelist, receiveStream.str());
- std::cout << receiveStream.str();
+ runCmd(cmd_node_list);
+ if (receiveStream.str().empty()) {
+ return 0;
}
+ saveToFile(lora_network_nodelist, receiveStream.str());
+ std::cout << receiveStream.str();
}
}
if (opt_add_node) {
@@ -274,125 +242,12 @@ void runCmd(const char *command) {
}
void printStats() {
- Json::Reader reader;
- Json::Value stats;
-
if (receiveStream.str().empty()) {
return;
}
- if (!reader.parse(receiveStream.str(), stats), false) {
- printError("Error parsing JSON: [%s]", receiveStream.str().c_str());
- }
-
- // Replace underscores keys with camel-case.
- if (stats.isObject()) {
- Json::Value::Members keys = stats.getMemberNames();
- std::string newKey;
- for (Json::Value::Members::iterator it = keys.begin(); it != keys.end(); ++it) {
- const std::string& key = *it;
- newKey.clear();
- for (size_t i = 0; i < key.length(); ++i) {
- if (('_' == key[i]) || ('-' == key[i])) {
- if ((i + 1) < key.length()) {
- newKey.push_back(std::toupper(key[++i]));
- }
- } else {
- newKey.push_back(key[i]);
- }
- }
- stats[newKey] = stats[key];
- stats.removeMember(key);
- }
- }
- saveToFile(lora_network_stats_json, stats.toStyledString());
- std::cout << stats.toStyledString();
-}
-
-void printNodeConfig() {
- std::string line;
- std::vector < std::string > parts;
- Json::Value nodeList = Json::ValueType::arrayValue;
-
- if (receiveStream.str().empty()) {
- return;
- }
-
- getline(receiveStream, line);
- while (1) {
- getline(receiveStream, line);
- if (line.size() == 0)
- break;
-
- line = trim(line);
- parts = MTS::Text::split(line, " ");
-
- if (configSize == (int)parts.size()) {
- Json::Value jNode(Json::objectValue);
- int index = 0;
- jNode[NODE_ADDR] = parts[index++];
- jNode[NODE_DEV_EUI] = parts[index++];
- jNode[NODE_CLASS] = parts[index++];
- jNode[NODE_APP_EUI] = parts[index++];
- jNode[NODE_APP_KEY] = parts[index++];
- jNode[NODE_NS_KEY] = parts[index++];
- jNode[NODE_DS_KEY] = parts[index++];
- nodeList.append(jNode);
- }
- else {
- printError("Incorrect Node Data! parts.size() = [%d]\n", parts.size());
- }
- }
- saveToFile(lora_network_nodelist_json, nodeList.toStyledString());
- std::cout << nodeList.toStyledString();
-}
-
-void printNodeList() {
- std::string line;
- std::vector < std::string > parts;
- Json::Value nodeList = Json::ValueType::arrayValue;
-
- if (receiveStream.str().empty()) {
- return;
- }
-
- getline(receiveStream, line);
- while (1) {
- getline(receiveStream, line);
- if (line.size() == 0)
- break;
-
- line = trim(line);
- parts = MTS::Text::split(line, " ");
-
- if (nodeSize == (int)parts.size()) {
- Json::Value jNode(Json::objectValue);
- int index = 0;
- jNode[NODE_ADDR] = parts[index++];
- jNode[NODE_DEV_EUI] = parts[index++];
- jNode[NODE_CLASS] = parts[index++];
- jNode[NODE_JOINED] = parts[index++];
- jNode[NODE_SEQ_NUM] = (uint32_t)std::strtoul(parts[index++].c_str(), nullptr, 10);
- jNode[NODE_PKTS_UP] = (uint32_t)std::strtoul(parts[index++].c_str(), nullptr, 10);
- jNode[NODE_PKTS_DOWN] = (uint32_t)std::strtoul(parts[index++].c_str(), nullptr, 10);
- jNode[NODE_PKTS_1ST] = (uint32_t)std::strtoul(parts[index++].c_str(), nullptr, 10);
- jNode[NODE_PKTS_2ND] = (uint32_t)std::strtoul(parts[index++].c_str(), nullptr, 10);
- jNode[NODE_DROPPED] = (uint32_t)std::strtoul(parts[index++].c_str(), nullptr, 10);
- jNode[NODE_RSSI_MIN] = atof(parts[index++].c_str());
- jNode[NODE_RSSI_MAX] = atof(parts[index++].c_str());
- jNode[NODE_RSSI_AVR] = atof(parts[index++].c_str());
- jNode[NODE_SNR_MIX] = atof(parts[index++].c_str());
- jNode[NODE_SNR_MAX] = atof(parts[index++].c_str());
- jNode[NODE_SNR_AVR] = atof(parts[index++].c_str());
-
- nodeList.append(jNode);
- }
- else {
- printError("Incorrect Node Data! parts.size() = [%d]\n", parts.size());
- }
- }
- saveToFile(lora_network_nodelist_json, nodeList.toStyledString());
- std::cout << nodeList.toStyledString();
+ saveToFile(lora_network_stats_json, receiveStream.str());
+ std::cout << receiveStream.str();
}
void saveToFile(const std::string& fileName, const std::string& buffer) {
@@ -428,14 +283,14 @@ void parseOptions(int argc, char** argv) {
while ((rez = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) {
switch (rez) {
- case 'x': {
- ++opt_command;
- command_args[command_count++] = optarg;
+ case 'x': {
+ ++opt_command;
+ command_args[command_count++] = optarg;
for (int i = optind; i < argc; i++) {
command_args[command_count++] = argv[i];
}
- break;
- }
+ break;
+ }
case 's': {
++opt_get_stats;
break;