ClassFlowMQTT.cpp 5.3 KB

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