ClassFlowMQTT.cpp 9.4 KB

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