ClassFlowMQTT.cpp 6.9 KB

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