ClassFlowMQTT.cpp 7.7 KB

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