You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.7 KiB

2 years ago
  1. #include "mqtt_config.hpp"
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <sstream>
  5. #include <string>
  6. using namespace std;
  7. using namespace nlohmann;
  8. using namespace iflytop;
  9. void MQTTConfig::initialize(string file_name) {
  10. std::ifstream jfile(file_name);
  11. jfile >> m_mqttConfigJson;
  12. jfile.close();
  13. try {
  14. m_address = m_mqttConfigJson["address"];
  15. m_username = m_mqttConfigJson["username"];
  16. m_password = m_mqttConfigJson["password"];
  17. m_clientid = m_mqttConfigJson["clientid"];
  18. m_qos = m_mqttConfigJson["qos"];
  19. m_topic = m_mqttConfigJson["topic"];
  20. m_timeout = m_mqttConfigJson["timeout"];
  21. } catch (const std::exception& e) {
  22. std::cerr << "mqtt_config_read error: " << e.what() << '\n';
  23. }
  24. }
  25. string MQTTConfig::dump() {
  26. sstream ret;
  27. ret << "==========================mqtt_config_dump_start===========================" << endl;
  28. ret << "address = " << getAddress() << endl;
  29. ret << "username = " << getUsername() << endl;
  30. ret << "password = " << getPassword() << endl;
  31. ret << "clientid = " << getClientid() << endl;
  32. ret << "qos = " << getQos() << endl;
  33. ret << "topic = " << getTopic() << endl;
  34. ret << "timeout = " << getTimeout() << endl;
  35. ret << "===========================mqtt_config_dump_end============================" << endl;
  36. return ret.str();
  37. }
  38. string MQTTConfig::getAddress() { return m_address; }
  39. string MQTTConfig::getUsername() { return m_username; }
  40. string MQTTConfig::getPassword() { return m_password; }
  41. string MQTTConfig::getClientid() { return m_clientid; }
  42. int MQTTConfig::getQos() { return m_qos; }
  43. string MQTTConfig::getTopic() { return m_topic; }
  44. int MQTTConfig::getTimeout() { return m_timeout; }