ClassFlowMQTT.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include <sstream>
  2. #include "ClassFlowMQTT.h"
  3. #include "Helper.h"
  4. #include "time_sntp.h"
  5. #include "interface_mqtt.h"
  6. #include "ClassFlowPostProcessing.h"
  7. #include <time.h>
  8. void ClassFlowMQTT::SetInitialParameter(void)
  9. {
  10. uri = "";
  11. topic = "";
  12. topicError = "";
  13. topicRate = "";
  14. topicTimeStamp = "";
  15. topicUptime = "";
  16. topicFreeMem = "";
  17. clientname = "watermeter";
  18. OldValue = "";
  19. flowpostprocessing = NULL;
  20. user = "";
  21. password = "";
  22. previousElement = NULL;
  23. ListFlowControll = NULL;
  24. disabled = false;
  25. }
  26. ClassFlowMQTT::ClassFlowMQTT()
  27. {
  28. SetInitialParameter();
  29. }
  30. ClassFlowMQTT::ClassFlowMQTT(std::vector<ClassFlow*>* lfc)
  31. {
  32. SetInitialParameter();
  33. ListFlowControll = lfc;
  34. for (int i = 0; i < ListFlowControll->size(); ++i)
  35. {
  36. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  37. {
  38. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  39. }
  40. }
  41. }
  42. ClassFlowMQTT::ClassFlowMQTT(std::vector<ClassFlow*>* lfc, ClassFlow *_prev)
  43. {
  44. SetInitialParameter();
  45. previousElement = _prev;
  46. ListFlowControll = lfc;
  47. for (int i = 0; i < ListFlowControll->size(); ++i)
  48. {
  49. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  50. {
  51. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  52. }
  53. }
  54. }
  55. bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph)
  56. {
  57. std::vector<string> zerlegt;
  58. aktparamgraph = trim(aktparamgraph);
  59. if (aktparamgraph.size() == 0)
  60. if (!this->GetNextParagraph(pfile, aktparamgraph))
  61. return false;
  62. if (toUpper(aktparamgraph).compare("[MQTT]") != 0) // Paragraph passt nich zu MakeImage
  63. return false;
  64. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  65. {
  66. zerlegt = this->ZerlegeZeile(aktparamgraph);
  67. if ((toUpper(zerlegt[0]) == "USER") && (zerlegt.size() > 1))
  68. {
  69. this->user = zerlegt[1];
  70. }
  71. if ((toUpper(zerlegt[0]) == "PASSWORD") && (zerlegt.size() > 1))
  72. {
  73. this->password = zerlegt[1];
  74. }
  75. if ((toUpper(zerlegt[0]) == "URI") && (zerlegt.size() > 1))
  76. {
  77. this->uri = zerlegt[1];
  78. }
  79. if ((toUpper(zerlegt[0]) == "TOPIC") && (zerlegt.size() > 1))
  80. {
  81. this->topic = zerlegt[1];
  82. }
  83. if ((toUpper(zerlegt[0]) == "TOPICERROR") && (zerlegt.size() > 1))
  84. {
  85. this->topicError = zerlegt[1];
  86. }
  87. if ((toUpper(zerlegt[0]) == "TOPICRATE") && (zerlegt.size() > 1))
  88. {
  89. this->topicRate = zerlegt[1];
  90. }
  91. if ((toUpper(zerlegt[0]) == "TOPICTIMESTAMP") && (zerlegt.size() > 1))
  92. {
  93. this->topicTimeStamp = zerlegt[1];
  94. }
  95. if ((toUpper(zerlegt[0]) == "TOPICUPTIME") && (zerlegt.size() > 1))
  96. {
  97. this->topicUptime = zerlegt[1];
  98. }
  99. if ((toUpper(zerlegt[0]) == "TOPICFREEMEM") && (zerlegt.size() > 1))
  100. {
  101. this->topicFreeMem = zerlegt[1];
  102. }
  103. if ((toUpper(zerlegt[0]) == "CLIENTID") && (zerlegt.size() > 1))
  104. {
  105. this->clientname = zerlegt[1];
  106. }
  107. }
  108. if (!MQTTisConnected() && (uri.length() > 0) && (topic.length() > 0))
  109. {
  110. MQTTInit(uri, clientname, user, password, topicError, 60);
  111. }
  112. return true;
  113. }
  114. bool ClassFlowMQTT::doFlow(string zwtime)
  115. {
  116. std::string result;
  117. std::string resulterror = "";
  118. std::string resultrate = "";
  119. std::string resulttimestamp = "";
  120. string zw = "";
  121. if (flowpostprocessing)
  122. {
  123. result = flowpostprocessing->getReadoutParam(false, true);
  124. resulterror = flowpostprocessing->getReadoutError();
  125. resultrate = flowpostprocessing->getReadoutRate();
  126. resulttimestamp = flowpostprocessing->getReadoutTimeStamp();
  127. }
  128. else
  129. {
  130. for (int i = 0; i < ListFlowControll->size(); ++i)
  131. {
  132. zw = (*ListFlowControll)[i]->getReadout();
  133. if (zw.length() > 0)
  134. {
  135. if (result.length() == 0)
  136. result = zw;
  137. else
  138. result = result + "\t" + zw;
  139. }
  140. }
  141. }
  142. MQTTPublish(topic, result);
  143. if (topicError.length() > 0) {
  144. if (resulterror.length() == 0)
  145. {
  146. resulterror = " ";
  147. }
  148. MQTTPublish(topicError, resulterror, 1);
  149. }
  150. if (topicRate.length() > 0) {
  151. MQTTPublish(topicRate, resultrate);
  152. }
  153. if (topicTimeStamp.length() > 0) {
  154. MQTTPublish(topicTimeStamp, resulttimestamp);
  155. }
  156. if (topicUptime.length() > 0) {
  157. char uptimeStr[11];
  158. sprintf(uptimeStr, "%ld", (long)getUpTime());
  159. MQTTPublish(topicUptime, uptimeStr);
  160. }
  161. if (topicFreeMem.length() > 0) {
  162. char freeheapmem[11];
  163. sprintf(freeheapmem, "%zu", esp_get_free_heap_size());
  164. MQTTPublish(topicFreeMem, freeheapmem);
  165. }
  166. OldValue = result;
  167. return true;
  168. }