ClassFlowMQTT.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "ClassFlowMQTT.h"
  2. #include "Helper.h"
  3. #include "interface_mqtt.h"
  4. #include "ClassFlowPostProcessing.h"
  5. #include <time.h>
  6. ClassFlowMQTT::ClassFlowMQTT()
  7. {
  8. uri = "";
  9. topic = "";
  10. topicError = "";
  11. clientname = "watermeter";
  12. OldValue = "";
  13. flowpostprocessing = NULL;
  14. user = "";
  15. password = "";
  16. }
  17. ClassFlowMQTT::ClassFlowMQTT(std::vector<ClassFlow*>* lfc)
  18. {
  19. uri = "";
  20. topic = "";
  21. topicError = "";
  22. clientname = "watermeter";
  23. OldValue = "";
  24. flowpostprocessing = NULL;
  25. user = "";
  26. password = "";
  27. ListFlowControll = lfc;
  28. for (int i = 0; i < ListFlowControll->size(); ++i)
  29. {
  30. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  31. {
  32. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  33. }
  34. }
  35. }
  36. bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph)
  37. {
  38. std::vector<string> zerlegt;
  39. aktparamgraph = trim(aktparamgraph);
  40. if (aktparamgraph.size() == 0)
  41. if (!this->GetNextParagraph(pfile, aktparamgraph))
  42. return false;
  43. if (toUpper(aktparamgraph).compare("[MQTT]") != 0) // Paragraph passt nich zu MakeImage
  44. return false;
  45. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  46. {
  47. zerlegt = this->ZerlegeZeile(aktparamgraph);
  48. if ((toUpper(zerlegt[0]) == "USER") && (zerlegt.size() > 1))
  49. {
  50. this->user = zerlegt[1];
  51. }
  52. if ((toUpper(zerlegt[0]) == "PASSWORD") && (zerlegt.size() > 1))
  53. {
  54. this->password = zerlegt[1];
  55. }
  56. if ((toUpper(zerlegt[0]) == "URI") && (zerlegt.size() > 1))
  57. {
  58. this->uri = zerlegt[1];
  59. }
  60. if ((toUpper(zerlegt[0]) == "TOPIC") && (zerlegt.size() > 1))
  61. {
  62. this->topic = zerlegt[1];
  63. }
  64. if ((toUpper(zerlegt[0]) == "TOPICERROR") && (zerlegt.size() > 1))
  65. {
  66. this->topicError = zerlegt[1];
  67. }
  68. if ((toUpper(zerlegt[0]) == "CLIENTID") && (zerlegt.size() > 1))
  69. {
  70. this->clientname = zerlegt[1];
  71. }
  72. }
  73. if ((uri.length() > 0) && (topic.length() > 0))
  74. {
  75. MQTTInit(uri, clientname, user, password);
  76. }
  77. return true;
  78. }
  79. bool ClassFlowMQTT::doFlow(string zwtime)
  80. {
  81. std::string result;
  82. std::string resulterror = "";
  83. string zw = "";
  84. if (flowpostprocessing)
  85. {
  86. result = flowpostprocessing->getReadoutParam(false, true);
  87. resulterror = flowpostprocessing->getReadoutError();
  88. }
  89. else
  90. {
  91. for (int i = 0; i < ListFlowControll->size(); ++i)
  92. {
  93. zw = (*ListFlowControll)[i]->getReadout();
  94. if (zw.length() > 0)
  95. {
  96. if (result.length() == 0)
  97. result = zw;
  98. else
  99. result = result + "\t" + zw;
  100. }
  101. }
  102. }
  103. MQTTPublish(topic, result);
  104. if (topicError.length() > 0) {
  105. MQTTPublish(topicError, resulterror);
  106. }
  107. OldValue = result;
  108. return true;
  109. }