ClassFlowMQTT.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. string ClassFlowMQTT::GetMQTTMainTopic()
  101. {
  102. return maintopic;
  103. }
  104. bool ClassFlowMQTT::doFlow(string zwtime)
  105. {
  106. if (!MQTTenable)
  107. return true;
  108. std::string result;
  109. std::string resulterror = "";
  110. std::string resultrate = "";
  111. std::string resulttimestamp = "";
  112. string zw = "";
  113. string namenumber = "";
  114. MQTTPublish(mainerrortopic, "connected");
  115. zw = maintopic + "/" + "uptime";
  116. char uptimeStr[11];
  117. sprintf(uptimeStr, "%ld", (long)getUpTime());
  118. MQTTPublish(zw, uptimeStr);
  119. zw = maintopic + "/" + "freeMem";
  120. char freeheapmem[11];
  121. sprintf(freeheapmem, "%zu", esp_get_free_heap_size());
  122. MQTTPublish(zw, freeheapmem);
  123. if (flowpostprocessing)
  124. {
  125. std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers();
  126. for (int i = 0; i < (*NUMBERS).size(); ++i)
  127. {
  128. result = (*NUMBERS)[i]->ReturnValueNoError;
  129. resulterror = (*NUMBERS)[i]->ErrorMessageText;
  130. resultrate = std::to_string((*NUMBERS)[i]->FlowRateAct);
  131. resulttimestamp = (*NUMBERS)[i]->timeStamp;
  132. namenumber = (*NUMBERS)[i]->name;
  133. if (namenumber == "default")
  134. namenumber = maintopic + "/";
  135. else
  136. namenumber = maintopic + "/" + namenumber + "/";
  137. zw = namenumber + "value";
  138. MQTTPublish(zw, result);
  139. zw = namenumber + "error";
  140. MQTTPublish(zw, resulterror, 1);
  141. zw = namenumber + "rate";
  142. MQTTPublish(zw, resultrate);
  143. zw = namenumber + "timestamp";
  144. MQTTPublish(zw, resulttimestamp);
  145. std::string json="{\"value\":"+result;
  146. json += ",\"error\":\""+resulterror;
  147. json += "\",\"rate\":"+resultrate;
  148. json += ",\"timestamp\":\""+resulttimestamp+"\"}";
  149. zw = namenumber + "json";
  150. MQTTPublish(zw, json);
  151. }
  152. }
  153. else
  154. {
  155. for (int i = 0; i < ListFlowControll->size(); ++i)
  156. {
  157. zw = (*ListFlowControll)[i]->getReadout();
  158. if (zw.length() > 0)
  159. {
  160. if (result.length() == 0)
  161. result = zw;
  162. else
  163. result = result + "\t" + zw;
  164. }
  165. }
  166. MQTTPublish(topic, result);
  167. }
  168. OldValue = result;
  169. return true;
  170. }