ClassFlowMQTT.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 "ClassFlowPostProcessing.h"
  9. #include "ClassFlowControll.h"
  10. #include <time.h>
  11. #define __HIDE_PASSWORD
  12. static const char *TAG = "class_flow_MQTT";
  13. void publishRuntimeData(std::string maintopic, int SetRetainFlag) {
  14. char tmp_char[50];
  15. sprintf(tmp_char, "%ld", (long)getUpTime());
  16. MQTTPublish(maintopic + "/" + "uptime", std::string(tmp_char), SetRetainFlag);
  17. sprintf(tmp_char, "%zu", esp_get_free_heap_size());
  18. MQTTPublish(maintopic + "/" + "freeMem", std::string(tmp_char), SetRetainFlag);
  19. sprintf(tmp_char, "%d", get_WIFI_RSSI());
  20. MQTTPublish(maintopic + "/" + "wifiRSSI", std::string(tmp_char), SetRetainFlag);
  21. sprintf(tmp_char, "%d", (int)temperatureRead());
  22. MQTTPublish(maintopic + "/" + "CPUtemp", std::string(tmp_char), SetRetainFlag);
  23. }
  24. void GotConnected(std::string maintopic, int SetRetainFlag) {
  25. MQTTPublish(maintopic + "/" + "mac", getMac(), SetRetainFlag);
  26. MQTTPublish(maintopic + "/" + "ip", *getIPAddress(), SetRetainFlag);
  27. MQTTPublish(maintopic + "/" + "hostname", hostname, SetRetainFlag);
  28. publishRuntimeData(maintopic, SetRetainFlag);
  29. }
  30. void ClassFlowMQTT::SetInitialParameter(void)
  31. {
  32. uri = "";
  33. topic = "";
  34. topicError = "";
  35. topicRate = "";
  36. topicTimeStamp = "";
  37. maintopic = hostname;
  38. topicUptime = "";
  39. topicFreeMem = "";
  40. clientname = "AIOTED-" + getMac();
  41. OldValue = "";
  42. flowpostprocessing = NULL;
  43. user = "";
  44. password = "";
  45. SetRetainFlag = 0;
  46. previousElement = NULL;
  47. ListFlowControll = NULL;
  48. disabled = false;
  49. keepAlive = 25*60;
  50. }
  51. ClassFlowMQTT::ClassFlowMQTT()
  52. {
  53. SetInitialParameter();
  54. }
  55. ClassFlowMQTT::ClassFlowMQTT(std::vector<ClassFlow*>* lfc)
  56. {
  57. SetInitialParameter();
  58. ListFlowControll = lfc;
  59. for (int i = 0; i < ListFlowControll->size(); ++i)
  60. {
  61. // ESP_LOGW(TAG, "LCF: %s", ((*ListFlowControll)[i])->name().c_str());
  62. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  63. {
  64. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  65. }
  66. // TODO this does not work since ClassFlowControll is not in the list!
  67. /* if (((*ListFlowControll)[i])->name().compare("ClassFlowControll") == 0)
  68. {
  69. ClassFlowControll *cfc = (ClassFlowControll*) (*ListFlowControll)[i];
  70. this->keepAlive = cfc->getAutoInterval()* 2.5; // Allow at least than 2 failed rounds before we are threated as disconnected
  71. ESP_LOGW(TAG, "KEEPALIVE: %d", this->keepAlive);
  72. }*/
  73. }
  74. }
  75. ClassFlowMQTT::ClassFlowMQTT(std::vector<ClassFlow*>* lfc, ClassFlow *_prev)
  76. {
  77. SetInitialParameter();
  78. previousElement = _prev;
  79. ListFlowControll = lfc;
  80. for (int i = 0; i < ListFlowControll->size(); ++i)
  81. {
  82. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  83. {
  84. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  85. }
  86. }
  87. }
  88. bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph)
  89. {
  90. std::vector<string> zerlegt;
  91. aktparamgraph = trim(aktparamgraph);
  92. if (aktparamgraph.size() == 0)
  93. if (!this->GetNextParagraph(pfile, aktparamgraph))
  94. return false;
  95. if (toUpper(aktparamgraph).compare("[MQTT]") != 0) // Paragraph passt nich zu MakeImage
  96. return false;
  97. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  98. {
  99. zerlegt = this->ZerlegeZeile(aktparamgraph);
  100. if ((toUpper(zerlegt[0]) == "USER") && (zerlegt.size() > 1))
  101. {
  102. this->user = zerlegt[1];
  103. }
  104. if ((toUpper(zerlegt[0]) == "PASSWORD") && (zerlegt.size() > 1))
  105. {
  106. this->password = zerlegt[1];
  107. }
  108. if ((toUpper(zerlegt[0]) == "URI") && (zerlegt.size() > 1))
  109. {
  110. this->uri = zerlegt[1];
  111. }
  112. if ((toUpper(zerlegt[0]) == "SETRETAINFLAG") && (zerlegt.size() > 1))
  113. {
  114. if (toUpper(zerlegt[1]) == "TRUE")
  115. SetRetainFlag = 1;
  116. }
  117. if ((toUpper(zerlegt[0]) == "CLIENTID") && (zerlegt.size() > 1))
  118. {
  119. this->clientname = zerlegt[1];
  120. }
  121. if (((toUpper(zerlegt[0]) == "TOPIC") || (toUpper(zerlegt[0]) == "MAINTOPIC")) && (zerlegt.size() > 1))
  122. {
  123. maintopic = zerlegt[1];
  124. }
  125. }
  126. MQTT_Configure(uri, clientname, user, password, maintopic, "connection", keepAlive, SetRetainFlag, (void *)&GotConnected);
  127. if (!MQTT_Init()) {
  128. if (!MQTT_Init()) { // Retry
  129. return false;
  130. }
  131. }
  132. return true;
  133. }
  134. string ClassFlowMQTT::GetMQTTMainTopic()
  135. {
  136. return maintopic;
  137. }
  138. bool ClassFlowMQTT::doFlow(string zwtime)
  139. {
  140. std::string result;
  141. std::string resulterror = "";
  142. std::string resultraw = "";
  143. std::string resultrate = "";
  144. std::string resulttimestamp = "";
  145. std::string resultchangabs = "";
  146. string zw = "";
  147. string namenumber = "";
  148. publishRuntimeData(maintopic, SetRetainFlag);
  149. if (flowpostprocessing)
  150. {
  151. std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers();
  152. for (int i = 0; i < (*NUMBERS).size(); ++i)
  153. {
  154. result = (*NUMBERS)[i]->ReturnValue;
  155. resultraw = (*NUMBERS)[i]->ReturnRawValue;
  156. resulterror = (*NUMBERS)[i]->ErrorMessageText;
  157. resultrate = (*NUMBERS)[i]->ReturnRateValue;
  158. resultchangabs = (*NUMBERS)[i]->ReturnChangeAbsolute;
  159. resulttimestamp = (*NUMBERS)[i]->timeStamp;
  160. namenumber = (*NUMBERS)[i]->name;
  161. if (namenumber == "default")
  162. namenumber = maintopic + "/";
  163. else
  164. namenumber = maintopic + "/" + namenumber + "/";
  165. if (result.length() > 0)
  166. MQTTPublish(namenumber + "value", result, SetRetainFlag);
  167. if (resulterror.length() > 0)
  168. MQTTPublish(namenumber + "error", resulterror, SetRetainFlag);
  169. if (resultrate.length() > 0)
  170. MQTTPublish(namenumber + "rate", resultrate, SetRetainFlag);
  171. if (resultchangabs.length() > 0)
  172. MQTTPublish(namenumber + "changeabsolut", resultchangabs, SetRetainFlag);
  173. if (resultraw.length() > 0)
  174. MQTTPublish(namenumber + "raw", resultraw, SetRetainFlag);
  175. if (resulttimestamp.length() > 0)
  176. MQTTPublish(namenumber + "timestamp", resulttimestamp, SetRetainFlag);
  177. std::string json = "";
  178. if (result.length() > 0)
  179. json += "{\"value\":"+result;
  180. else
  181. json += "{\"value\":\"\"";
  182. json += ",\"raw\":\""+resultraw;
  183. json += "\",\"error\":\""+resulterror;
  184. if (resultrate.length() > 0)
  185. json += "\",\"rate\":"+resultrate;
  186. else
  187. json += "\",\"rate\":\"\"";
  188. json += ",\"timestamp\":\""+resulttimestamp+"\"}";
  189. MQTTPublish(namenumber + "json", json, SetRetainFlag);
  190. }
  191. }
  192. else
  193. {
  194. for (int i = 0; i < ListFlowControll->size(); ++i)
  195. {
  196. zw = (*ListFlowControll)[i]->getReadout();
  197. if (zw.length() > 0)
  198. {
  199. if (result.length() == 0)
  200. result = zw;
  201. else
  202. result = result + "\t" + zw;
  203. }
  204. }
  205. MQTTPublish(topic, result, SetRetainFlag);
  206. }
  207. OldValue = result;
  208. return true;
  209. }