jsonhandler.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. qInfo() << QString::fromStdString(buffer);
  30. }
  31. if (err.compare("") && cmd.compare("connectionerror")) {
  32. emit qmlHandler->footerSetError(QString::fromStdString(err));
  33. }
  34. CmdManager::executeCmd(cmd, root);
  35. }