ClassFlowMQTT.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 "ClassLogFile.h"
  9. #include <time.h>
  10. void ClassFlowMQTT::SetInitialParameter(void)
  11. {
  12. uri = "";
  13. topic = "";
  14. topicError = "";
  15. topicRate = "";
  16. topicTimeStamp = "";
  17. maintopic = "";
  18. mainerrortopic = "";
  19. topicUptime = "";
  20. topicFreeMem = "";
  21. clientname = "watermeter";
  22. OldValue = "";
  23. flowpostprocessing = NULL;
  24. user = "";
  25. password = "";
  26. SetRetainFlag = 0;
  27. previousElement = NULL;
  28. ListFlowControll = NULL;
  29. disabled = false;
  30. MQTTenable = false;
  31. }
  32. ClassFlowMQTT::ClassFlowMQTT()
  33. {
  34. SetInitialParameter();
  35. }
  36. ClassFlowMQTT::ClassFlowMQTT(std::vector<ClassFlow*>* lfc)
  37. {
  38. SetInitialParameter();
  39. ListFlowControll = lfc;
  40. for (int i = 0; i < ListFlowControll->size(); ++i)
  41. {
  42. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  43. {
  44. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  45. }
  46. }
  47. }
  48. ClassFlowMQTT::ClassFlowMQTT(std::vector<ClassFlow*>* lfc, ClassFlow *_prev)
  49. {
  50. SetInitialParameter();
  51. previousElement = _prev;
  52. ListFlowControll = lfc;
  53. for (int i = 0; i < ListFlowControll->size(); ++i)
  54. {
  55. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  56. {
  57. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  58. }
  59. }
  60. }
  61. bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph)
  62. {
  63. std::vector<string> zerlegt;
  64. aktparamgraph = trim(aktparamgraph);
  65. if (aktparamgraph.size() == 0)
  66. if (!this->GetNextParagraph(pfile, aktparamgraph))
  67. return false;
  68. if (toUpper(aktparamgraph).compare("[MQTT]") != 0) // Paragraph passt nich zu MakeImage
  69. return false;
  70. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  71. {
  72. zerlegt = this->ZerlegeZeile(aktparamgraph);
  73. if ((toUpper(zerlegt[0]) == "USER") && (zerlegt.size() > 1))
  74. {
  75. this->user = zerlegt[1];
  76. }
  77. if ((toUpper(zerlegt[0]) == "PASSWORD") && (zerlegt.size() > 1))
  78. {
  79. this->password = zerlegt[1];
  80. }
  81. if ((toUpper(zerlegt[0]) == "URI") && (zerlegt.size() > 1))
  82. {
  83. this->uri = zerlegt[1];
  84. }
  85. if ((toUpper(zerlegt[0]) == "SETRETAINFLAG") && (zerlegt.size() > 1))
  86. {
  87. if (toUpper(zerlegt[1]) == "TRUE")
  88. SetRetainFlag = 1;
  89. }
  90. if ((toUpper(zerlegt[0]) == "CLIENTID") && (zerlegt.size() > 1))
  91. {
  92. this->clientname = zerlegt[1];
  93. }
  94. if (((toUpper(zerlegt[0]) == "TOPIC") || (toUpper(zerlegt[0]) == "MAINTOPIC")) && (zerlegt.size() > 1))
  95. {
  96. maintopic = zerlegt[1];
  97. }
  98. }
  99. 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());
  100. if (!MQTTisConnected() && (uri.length() > 0) && (maintopic.length() > 0))
  101. {
  102. printf("InitMQTTInit\n");
  103. mainerrortopic = maintopic + "/connection";
  104. 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());
  105. MQTTInit(uri, clientname, user, password, mainerrortopic, 60);
  106. if (MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) {
  107. MQTTenable = true;
  108. }
  109. else {
  110. MQTTenable = true;
  111. }
  112. }
  113. return true;
  114. }
  115. string ClassFlowMQTT::GetMQTTMainTopic()
  116. {
  117. return maintopic;
  118. }
  119. bool ClassFlowMQTT::doFlow(string zwtime)
  120. {
  121. if (!MQTTenable) {
  122. LogFile.WriteToFile("MQTT not enabled!");
  123. // Try again to init it
  124. MQTTInit(this->uri, this->clientname, this->user, this->password, this->mainerrortopic, 60);
  125. if (MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) {
  126. MQTTenable = true;
  127. }
  128. else { // Failed
  129. return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
  130. }
  131. }
  132. LogFile.WriteToFile("MQTT enabled");
  133. std::string result;
  134. std::string resulterror = "";
  135. std::string resultraw = "";
  136. std::string resultrate = "";
  137. std::string resulttimestamp = "";
  138. std::string resultchangabs = "";
  139. string zw = "";
  140. string namenumber = "";
  141. if (MQTTPublish(mainerrortopic, "connected")) {
  142. MQTTenable = true;
  143. }
  144. else { // Failed, skip other topics
  145. return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
  146. }
  147. zw = maintopic + "/" + "uptime";
  148. char uptimeStr[11];
  149. sprintf(uptimeStr, "%ld", (long)getUpTime());
  150. MQTTPublish(zw, uptimeStr, SetRetainFlag);
  151. zw = maintopic + "/" + "freeMem";
  152. char freeheapmem[11];
  153. sprintf(freeheapmem, "%zu", esp_get_free_heap_size());
  154. MQTTPublish(zw, freeheapmem, SetRetainFlag);
  155. zw = maintopic + "/" + "wifiRSSI";
  156. char rssi[11];
  157. sprintf(rssi, "%d", get_WIFI_RSSI());
  158. MQTTPublish(zw, rssi, SetRetainFlag);
  159. zw = maintopic + "/" + "CPUtemp";
  160. std::string cputemp = std::to_string(temperatureRead());
  161. MQTTPublish(zw, cputemp, SetRetainFlag);
  162. if (flowpostprocessing)
  163. {
  164. std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers();
  165. for (int i = 0; i < (*NUMBERS).size(); ++i)
  166. {
  167. result = (*NUMBERS)[i]->ReturnValue;
  168. resultraw = (*NUMBERS)[i]->ReturnRawValue;
  169. resulterror = (*NUMBERS)[i]->ErrorMessageText;
  170. resultrate = (*NUMBERS)[i]->ReturnRateValue;
  171. resultchangabs = (*NUMBERS)[i]->ReturnChangeAbsolute;
  172. resulttimestamp = (*NUMBERS)[i]->timeStamp;
  173. namenumber = (*NUMBERS)[i]->name;
  174. if (namenumber == "default")
  175. namenumber = maintopic + "/";
  176. else
  177. namenumber = maintopic + "/" + namenumber + "/";
  178. zw = namenumber + "value";
  179. if (result.length() > 0)
  180. MQTTPublish(zw, result, SetRetainFlag);
  181. zw = namenumber + "error";
  182. if (resulterror.length() > 0)
  183. MQTTPublish(zw, resulterror, SetRetainFlag);
  184. zw = namenumber + "rate";
  185. if (resultrate.length() > 0)
  186. MQTTPublish(zw, resultrate, SetRetainFlag);
  187. zw = namenumber + "changeabsolut";
  188. if (resultchangabs.length() > 0)
  189. MQTTPublish(zw, resultchangabs, SetRetainFlag);
  190. zw = namenumber + "raw";
  191. if (resultraw.length() > 0)
  192. MQTTPublish(zw, resultraw, SetRetainFlag);
  193. zw = namenumber + "timestamp";
  194. if (resulttimestamp.length() > 0)
  195. MQTTPublish(zw, resulttimestamp, SetRetainFlag);
  196. std::string json = "";
  197. if (result.length() > 0)
  198. json += "{\"value\":"+result;
  199. else
  200. json += "{\"value\":\"\"";
  201. json += ",\"raw\":\""+resultraw;
  202. json += "\",\"error\":\""+resulterror;
  203. if (resultrate.length() > 0)
  204. json += "\",\"rate\":"+resultrate;
  205. else
  206. json += "\",\"rate\":\"\"";
  207. json += ",\"timestamp\":\""+resulttimestamp+"\"}";
  208. zw = namenumber + "json";
  209. MQTTPublish(zw, json, SetRetainFlag);
  210. }
  211. }
  212. else
  213. {
  214. for (int i = 0; i < ListFlowControll->size(); ++i)
  215. {
  216. zw = (*ListFlowControll)[i]->getReadout();
  217. if (zw.length() > 0)
  218. {
  219. if (result.length() == 0)
  220. result = zw;
  221. else
  222. result = result + "\t" + zw;
  223. }
  224. }
  225. MQTTPublish(topic, result, SetRetainFlag);
  226. }
  227. OldValue = result;
  228. return true;
  229. }