ClassFlowMQTT.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "ClassFlowMQTT.h"
  2. #include "Helper.h"
  3. #include "interface_mqtt.h"
  4. #include "ClassFlowPostProcessing.h"
  5. #include <time.h>
  6. void ClassFlowMQTT::SetInitialParameter(void)
  7. {
  8. uri = "";
  9. topic = "";
  10. topicError = "";
  11. topicRate = "";
  12. topicTimeStamp = "";
  13. maintopic = "";
  14. mainerrortopic = "";
  15. clientname = "watermeter";
  16. OldValue = "";
  17. flowpostprocessing = NULL;
  18. user = "";
  19. password = "";
  20. previousElement = NULL;
  21. ListFlowControll = NULL;
  22. disabled = false;
  23. }
  24. ClassFlowMQTT::ClassFlowMQTT()
  25. {
  26. SetInitialParameter();
  27. }
  28. ClassFlowMQTT::ClassFlowMQTT(std::vector<ClassFlow*>* lfc)
  29. {
  30. SetInitialParameter();
  31. ListFlowControll = lfc;
  32. for (int i = 0; i < ListFlowControll->size(); ++i)
  33. {
  34. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  35. {
  36. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  37. }
  38. }
  39. }
  40. ClassFlowMQTT::ClassFlowMQTT(std::vector<ClassFlow*>* lfc, ClassFlow *_prev)
  41. {
  42. SetInitialParameter();
  43. previousElement = _prev;
  44. ListFlowControll = lfc;
  45. for (int i = 0; i < ListFlowControll->size(); ++i)
  46. {
  47. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  48. {
  49. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  50. }
  51. }
  52. }
  53. bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph)
  54. {
  55. std::vector<string> zerlegt;
  56. aktparamgraph = trim(aktparamgraph);
  57. if (aktparamgraph.size() == 0)
  58. if (!this->GetNextParagraph(pfile, aktparamgraph))
  59. return false;
  60. if (toUpper(aktparamgraph).compare("[MQTT]") != 0) // Paragraph passt nich zu MakeImage
  61. return false;
  62. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  63. {
  64. zerlegt = this->ZerlegeZeile(aktparamgraph);
  65. if ((toUpper(zerlegt[0]) == "USER") && (zerlegt.size() > 1))
  66. {
  67. this->user = zerlegt[1];
  68. }
  69. if ((toUpper(zerlegt[0]) == "PASSWORD") && (zerlegt.size() > 1))
  70. {
  71. this->password = zerlegt[1];
  72. }
  73. if ((toUpper(zerlegt[0]) == "URI") && (zerlegt.size() > 1))
  74. {
  75. this->uri = zerlegt[1];
  76. }
  77. if ((toUpper(zerlegt[0]) == "CLIENTID") && (zerlegt.size() > 1))
  78. {
  79. this->clientname = zerlegt[1];
  80. }
  81. if (((toUpper(zerlegt[0]) == "TOPIC") || (toUpper(zerlegt[0]) == "MAINTOPIC")) && (zerlegt.size() > 1))
  82. {
  83. maintopic = zerlegt[1];
  84. }
  85. }
  86. if ((uri.length() > 0) && (maintopic.length() > 0))
  87. {
  88. mainerrortopic = maintopic + "/connection";
  89. MQTTInit(uri, clientname, user, password, mainerrortopic, 60);
  90. MQTTPublish(mainerrortopic, "connected");
  91. }
  92. return true;
  93. }
  94. bool ClassFlowMQTT::doFlow(string zwtime)
  95. {
  96. std::string result;
  97. std::string resulterror = "";
  98. std::string resultrate = "";
  99. std::string resulttimestamp = "";
  100. string zw = "";
  101. string namenumber = "";
  102. MQTTPublish(mainerrortopic, "connected");
  103. if (flowpostprocessing)
  104. {
  105. std::vector<NumberPost*> NUMBERS = flowpostprocessing->GetNumbers();
  106. for (int i = 0; i < NUMBERS.size(); ++i)
  107. {
  108. result = NUMBERS[i]->ReturnValueNoError;
  109. resulterror = NUMBERS[i]->ErrorMessageText;
  110. resultrate = std::to_string(NUMBERS[i]->FlowRateAct);
  111. resulttimestamp = NUMBERS[i]->timeStamp;
  112. namenumber = NUMBERS[i]->name;
  113. if (namenumber == "default")
  114. namenumber = maintopic + "/";
  115. else
  116. namenumber = maintopic + "/" + namenumber + "/";
  117. zw = namenumber + "value";
  118. MQTTPublish(zw, result);
  119. zw = namenumber + "error";
  120. MQTTPublish(zw, resulterror, 1);
  121. zw = namenumber + "rate";
  122. MQTTPublish(zw, resultrate);
  123. zw = namenumber + "timestamp";
  124. MQTTPublish(zw, resulttimestamp);
  125. }
  126. }
  127. else
  128. {
  129. for (int i = 0; i < ListFlowControll->size(); ++i)
  130. {
  131. zw = (*ListFlowControll)[i]->getReadout();
  132. if (zw.length() > 0)
  133. {
  134. if (result.length() == 0)
  135. result = zw;
  136. else
  137. result = result + "\t" + zw;
  138. }
  139. }
  140. MQTTPublish(topic, result);
  141. }
  142. OldValue = result;
  143. return true;
  144. }