ClassFlowMQTT.cpp 10 KB

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