ClassFlowMQTT.cpp 7.4 KB

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