jsonhandler.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <QDebug>
  2. #include "../include/cmdmanager.h"
  3. #include "../include/jsonhandler.h"
  4. using namespace std;
  5. namespace JsonHandler {
  6. QMLHandler *qmlHandler;
  7. }
  8. void JsonHandler::setQmlHandler(QMLHandler *q) { qmlHandler = q; }
  9. // This method gets a string and tries to read it as Json
  10. // If it fails to do so, return and do nothing, else handle the content
  11. void JsonHandler::parseJSON(string buffer) {
  12. Json::Value root;
  13. Json::CharReaderBuilder builder;
  14. Json::CharReader *reader = builder.newCharReader();
  15. string jsonError;
  16. // Try to parse the string as Json and store the result of the parsing in a boolean
  17. bool parsingSuccessful = reader->parse(buffer.c_str(), buffer.c_str() + buffer.size(), &root, &jsonError);
  18. delete (reader);
  19. // If the string is not correct Json, return
  20. if (!parsingSuccessful) {
  21. return;
  22. }
  23. string cmd = root["command"].asString();
  24. string err = root["error"].asString();
  25. // Filter the commands to not be printed
  26. if (cmd.compare("notifications") && cmd.compare("extendedlist") && cmd.compare("extendedstatus") && cmd.compare("status") && cmd.compare("putdata") &&
  27. cmd.compare("getdata")) {
  28. emit qmlHandler->log(QString::fromStdString(buffer));
  29. // TODO: Remove
  30. qInfo() << QString::fromStdString(buffer);
  31. }
  32. if (err.compare("")) {
  33. emit qmlHandler->footerSetError(QString::fromStdString(err));
  34. }
  35. CmdManager::executeCmd(cmd, root);
  36. }