ClassFlowMQTT.cpp 6.6 KB

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