ClassFlowMQTT.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. mainerrortopic = "";
  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. }
  102. #ifdef __HIDE_PASSWORD
  103. 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(), mainerrortopic.c_str());
  104. #else
  105. 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(), mainerrortopic.c_str());
  106. #endif
  107. if (!MQTTisConnected() && (uri.length() > 0) && (maintopic.length() > 0))
  108. {
  109. ESP_LOGD(TAG, "InitMQTTInit");
  110. mainerrortopic = maintopic + "/connection";
  111. #ifdef __HIDE_PASSWORD
  112. 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(), mainerrortopic.c_str());
  113. #else
  114. 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(), mainerrortopic.c_str());
  115. #endif
  116. if (!MQTTInit(uri, clientname, user, password, mainerrortopic, keepAlive))
  117. { // Failed
  118. MQTTenable = false;
  119. return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
  120. }
  121. }
  122. // Try sending mainerrortopic. If it fails, re-run init
  123. if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
  124. { // Failed
  125. LogFile.WriteToFile(ESP_LOG_WARN, "MQTT - Re-running init...!");
  126. if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->mainerrortopic, keepAlive))
  127. { // Failed
  128. MQTTenable = false;
  129. return false;
  130. }
  131. }
  132. // Try again and quit if it fails
  133. if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
  134. { // Failed
  135. MQTTenable = false;
  136. return false;
  137. }
  138. /* if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
  139. { // Failed
  140. LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Could not publish connection status!");
  141. MQTTenable = false;
  142. return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
  143. }*/
  144. /* if(!MQTTPublish(_LWTContext, "", 1))
  145. {
  146. LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Could not publish LWT!");
  147. MQTTenable = false;
  148. return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
  149. }*/
  150. MQTTenable = true;
  151. return true;
  152. }
  153. string ClassFlowMQTT::GetMQTTMainTopic()
  154. {
  155. return maintopic;
  156. }
  157. bool ClassFlowMQTT::doFlow(string zwtime)
  158. {
  159. // Try sending mainerrortopic. If it fails, re-run init
  160. if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
  161. { // Failed
  162. LogFile.WriteToFile(ESP_LOG_WARN, "MQTT - Re-running init...!");
  163. if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->mainerrortopic, keepAlive))
  164. { // Failed
  165. MQTTenable = false;
  166. return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
  167. }
  168. }
  169. // Try again and quit if it fails
  170. if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
  171. { // Failed
  172. MQTTenable = false;
  173. return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
  174. }
  175. std::string result;
  176. std::string resulterror = "";
  177. std::string resultraw = "";
  178. std::string resultrate = "";
  179. std::string resulttimestamp = "";
  180. std::string resultchangabs = "";
  181. string zw = "";
  182. string namenumber = "";
  183. // if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
  184. //{ // Failed, skip other topics
  185. // return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
  186. //}
  187. zw = maintopic + "/" + "uptime";
  188. char uptimeStr[11];
  189. sprintf(uptimeStr, "%ld", (long)getUpTime());
  190. MQTTPublish(zw, uptimeStr, SetRetainFlag);
  191. zw = maintopic + "/" + "freeMem";
  192. char freeheapmem[11];
  193. sprintf(freeheapmem, "%zu", esp_get_free_heap_size());
  194. if (!MQTTPublish(zw, freeheapmem, SetRetainFlag))
  195. { // Failed, skip other topics
  196. return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
  197. }
  198. zw = maintopic + "/" + "wifiRSSI";
  199. char rssi[11];
  200. sprintf(rssi, "%d", get_WIFI_RSSI());
  201. MQTTPublish(zw, rssi, SetRetainFlag);
  202. zw = maintopic + "/" + "CPUtemp";
  203. std::string cputemp = std::to_string(temperatureRead());
  204. MQTTPublish(zw, cputemp, SetRetainFlag);
  205. if (flowpostprocessing)
  206. {
  207. std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers();
  208. for (int i = 0; i < (*NUMBERS).size(); ++i)
  209. {
  210. result = (*NUMBERS)[i]->ReturnValue;
  211. resultraw = (*NUMBERS)[i]->ReturnRawValue;
  212. resulterror = (*NUMBERS)[i]->ErrorMessageText;
  213. resultrate = (*NUMBERS)[i]->ReturnRateValue;
  214. resultchangabs = (*NUMBERS)[i]->ReturnChangeAbsolute;
  215. resulttimestamp = (*NUMBERS)[i]->timeStamp;
  216. namenumber = (*NUMBERS)[i]->name;
  217. if (namenumber == "default")
  218. namenumber = maintopic + "/";
  219. else
  220. namenumber = maintopic + "/" + namenumber + "/";
  221. zw = namenumber + "value";
  222. if (result.length() > 0)
  223. MQTTPublish(zw, result, SetRetainFlag);
  224. zw = namenumber + "error";
  225. if (resulterror.length() > 0)
  226. MQTTPublish(zw, resulterror, SetRetainFlag);
  227. zw = namenumber + "rate";
  228. if (resultrate.length() > 0)
  229. MQTTPublish(zw, resultrate, SetRetainFlag);
  230. zw = namenumber + "changeabsolut";
  231. if (resultchangabs.length() > 0)
  232. MQTTPublish(zw, resultchangabs, SetRetainFlag);
  233. zw = namenumber + "raw";
  234. if (resultraw.length() > 0)
  235. MQTTPublish(zw, resultraw, SetRetainFlag);
  236. zw = namenumber + "timestamp";
  237. if (resulttimestamp.length() > 0)
  238. MQTTPublish(zw, resulttimestamp, SetRetainFlag);
  239. std::string json = "";
  240. if (result.length() > 0)
  241. json += "{\"value\":"+result;
  242. else
  243. json += "{\"value\":\"\"";
  244. json += ",\"raw\":\""+resultraw;
  245. json += "\",\"error\":\""+resulterror;
  246. if (resultrate.length() > 0)
  247. json += "\",\"rate\":"+resultrate;
  248. else
  249. json += "\",\"rate\":\"\"";
  250. json += ",\"timestamp\":\""+resulttimestamp+"\"}";
  251. zw = namenumber + "json";
  252. MQTTPublish(zw, json, SetRetainFlag);
  253. }
  254. }
  255. else
  256. {
  257. for (int i = 0; i < ListFlowControll->size(); ++i)
  258. {
  259. zw = (*ListFlowControll)[i]->getReadout();
  260. if (zw.length() > 0)
  261. {
  262. if (result.length() == 0)
  263. result = zw;
  264. else
  265. result = result + "\t" + zw;
  266. }
  267. }
  268. MQTTPublish(topic, result, SetRetainFlag);
  269. }
  270. OldValue = result;
  271. return true;
  272. }