ClassFlowMQTT.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. keepAlive = 600; // TODO This must be greater than the Flow Interval!
  32. }
  33. ClassFlowMQTT::ClassFlowMQTT()
  34. {
  35. SetInitialParameter();
  36. }
  37. ClassFlowMQTT::ClassFlowMQTT(std::vector<ClassFlow*>* lfc)
  38. {
  39. SetInitialParameter();
  40. ListFlowControll = lfc;
  41. for (int i = 0; i < ListFlowControll->size(); ++i)
  42. {
  43. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  44. {
  45. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  46. }
  47. }
  48. }
  49. ClassFlowMQTT::ClassFlowMQTT(std::vector<ClassFlow*>* lfc, ClassFlow *_prev)
  50. {
  51. SetInitialParameter();
  52. previousElement = _prev;
  53. ListFlowControll = lfc;
  54. for (int i = 0; i < ListFlowControll->size(); ++i)
  55. {
  56. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  57. {
  58. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  59. }
  60. }
  61. }
  62. bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph)
  63. {
  64. std::vector<string> zerlegt;
  65. aktparamgraph = trim(aktparamgraph);
  66. if (aktparamgraph.size() == 0)
  67. if (!this->GetNextParagraph(pfile, aktparamgraph))
  68. return false;
  69. if (toUpper(aktparamgraph).compare("[MQTT]") != 0) // Paragraph passt nich zu MakeImage
  70. return false;
  71. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  72. {
  73. zerlegt = this->ZerlegeZeile(aktparamgraph);
  74. if ((toUpper(zerlegt[0]) == "USER") && (zerlegt.size() > 1))
  75. {
  76. this->user = zerlegt[1];
  77. }
  78. if ((toUpper(zerlegt[0]) == "PASSWORD") && (zerlegt.size() > 1))
  79. {
  80. this->password = zerlegt[1];
  81. }
  82. if ((toUpper(zerlegt[0]) == "URI") && (zerlegt.size() > 1))
  83. {
  84. this->uri = zerlegt[1];
  85. }
  86. if ((toUpper(zerlegt[0]) == "SETRETAINFLAG") && (zerlegt.size() > 1))
  87. {
  88. if (toUpper(zerlegt[1]) == "TRUE")
  89. SetRetainFlag = 1;
  90. }
  91. if ((toUpper(zerlegt[0]) == "CLIENTID") && (zerlegt.size() > 1))
  92. {
  93. this->clientname = zerlegt[1];
  94. }
  95. if (((toUpper(zerlegt[0]) == "TOPIC") || (toUpper(zerlegt[0]) == "MAINTOPIC")) && (zerlegt.size() > 1))
  96. {
  97. maintopic = zerlegt[1];
  98. }
  99. }
  100. 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());
  101. if (!MQTTisConnected() && (uri.length() > 0) && (maintopic.length() > 0))
  102. {
  103. printf("InitMQTTInit\n");
  104. mainerrortopic = maintopic + "/connection";
  105. 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());
  106. if (!MQTTInit(uri, clientname, user, password, mainerrortopic, keepAlive))
  107. { // Failed
  108. MQTTenable = false;
  109. return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
  110. }
  111. }
  112. // Try sending mainerrortopic. If it fails, re-run init
  113. if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
  114. { // Failed
  115. LogFile.WriteToFile("MQTT - Re-running init...!");
  116. if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->mainerrortopic, keepAlive))
  117. { // Failed
  118. MQTTenable = false;
  119. return false;
  120. }
  121. }
  122. // Try again and quit if it fails
  123. if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
  124. { // Failed
  125. MQTTenable = false;
  126. return false;
  127. }
  128. /* if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
  129. { // Failed
  130. LogFile.WriteToFile("MQTT - Could not publish connection status!");
  131. MQTTenable = false;
  132. return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
  133. }*/
  134. /* if(!MQTTPublish(_LWTContext, "", 1))
  135. {
  136. LogFile.WriteToFile("MQTT - Could not publish LWT!");
  137. MQTTenable = false;
  138. return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
  139. }*/
  140. MQTTenable = true;
  141. return true;
  142. }
  143. string ClassFlowMQTT::GetMQTTMainTopic()
  144. {
  145. return maintopic;
  146. }
  147. bool ClassFlowMQTT::doFlow(string zwtime)
  148. {
  149. // if (!MQTTenable) {
  150. // LogFile.WriteToFile("MQTT not enabled!");
  151. //
  152. // // Try again to init it
  153. // if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->mainerrortopic, keepAlive))
  154. // { // Failed
  155. // MQTTenable = false;
  156. // return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
  157. // }
  158. //
  159. // if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
  160. // { // Failed
  161. // MQTTenable = false;
  162. // return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
  163. // }
  164. //
  165. // LogFile.WriteToFile("MQTT is now enabled");
  166. // MQTTenable = true;
  167. // }
  168. // Try sending mainerrortopic. If it fails, re-run init
  169. if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
  170. { // Failed
  171. LogFile.WriteToFile("MQTT - Re-running init...!");
  172. if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->mainerrortopic, keepAlive))
  173. { // Failed
  174. MQTTenable = false;
  175. return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
  176. }
  177. }
  178. // Try again and quit if it fails
  179. if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
  180. { // Failed
  181. MQTTenable = false;
  182. return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
  183. }
  184. std::string result;
  185. std::string resulterror = "";
  186. std::string resultraw = "";
  187. std::string resultrate = "";
  188. std::string resulttimestamp = "";
  189. std::string resultchangabs = "";
  190. string zw = "";
  191. string namenumber = "";
  192. // if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
  193. //{ // Failed, skip other topics
  194. // return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
  195. //}
  196. zw = maintopic + "/" + "uptime";
  197. char uptimeStr[11];
  198. sprintf(uptimeStr, "%ld", (long)getUpTime());
  199. MQTTPublish(zw, uptimeStr, SetRetainFlag);
  200. zw = maintopic + "/" + "freeMem";
  201. char freeheapmem[11];
  202. sprintf(freeheapmem, "%zu", esp_get_free_heap_size());
  203. if (!MQTTPublish(zw, freeheapmem, SetRetainFlag))
  204. { // Failed, skip other topics
  205. return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
  206. }
  207. zw = maintopic + "/" + "wifiRSSI";
  208. char rssi[11];
  209. sprintf(rssi, "%d", get_WIFI_RSSI());
  210. MQTTPublish(zw, rssi, SetRetainFlag);
  211. zw = maintopic + "/" + "CPUtemp";
  212. std::string cputemp = std::to_string(temperatureRead());
  213. MQTTPublish(zw, cputemp, SetRetainFlag);
  214. if (flowpostprocessing)
  215. {
  216. std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers();
  217. for (int i = 0; i < (*NUMBERS).size(); ++i)
  218. {
  219. result = (*NUMBERS)[i]->ReturnValue;
  220. resultraw = (*NUMBERS)[i]->ReturnRawValue;
  221. resulterror = (*NUMBERS)[i]->ErrorMessageText;
  222. resultrate = (*NUMBERS)[i]->ReturnRateValue;
  223. resultchangabs = (*NUMBERS)[i]->ReturnChangeAbsolute;
  224. resulttimestamp = (*NUMBERS)[i]->timeStamp;
  225. namenumber = (*NUMBERS)[i]->name;
  226. if (namenumber == "default")
  227. namenumber = maintopic + "/";
  228. else
  229. namenumber = maintopic + "/" + namenumber + "/";
  230. zw = namenumber + "value";
  231. if (result.length() > 0)
  232. MQTTPublish(zw, result, SetRetainFlag);
  233. zw = namenumber + "error";
  234. if (resulterror.length() > 0)
  235. MQTTPublish(zw, resulterror, SetRetainFlag);
  236. zw = namenumber + "rate";
  237. if (resultrate.length() > 0)
  238. MQTTPublish(zw, resultrate, SetRetainFlag);
  239. zw = namenumber + "changeabsolut";
  240. if (resultchangabs.length() > 0)
  241. MQTTPublish(zw, resultchangabs, SetRetainFlag);
  242. zw = namenumber + "raw";
  243. if (resultraw.length() > 0)
  244. MQTTPublish(zw, resultraw, SetRetainFlag);
  245. zw = namenumber + "timestamp";
  246. if (resulttimestamp.length() > 0)
  247. MQTTPublish(zw, resulttimestamp, SetRetainFlag);
  248. std::string json = "";
  249. if (result.length() > 0)
  250. json += "{\"value\":"+result;
  251. else
  252. json += "{\"value\":\"\"";
  253. json += ",\"raw\":\""+resultraw;
  254. json += "\",\"error\":\""+resulterror;
  255. if (resultrate.length() > 0)
  256. json += "\",\"rate\":"+resultrate;
  257. else
  258. json += "\",\"rate\":\"\"";
  259. json += ",\"timestamp\":\""+resulttimestamp+"\"}";
  260. zw = namenumber + "json";
  261. MQTTPublish(zw, json, SetRetainFlag);
  262. }
  263. }
  264. else
  265. {
  266. for (int i = 0; i < ListFlowControll->size(); ++i)
  267. {
  268. zw = (*ListFlowControll)[i]->getReadout();
  269. if (zw.length() > 0)
  270. {
  271. if (result.length() == 0)
  272. result = zw;
  273. else
  274. result = result + "\t" + zw;
  275. }
  276. }
  277. MQTTPublish(topic, result, SetRetainFlag);
  278. }
  279. OldValue = result;
  280. return true;
  281. }