ClassFlowMQTT.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "ClassFlowMQTT.h"
  2. #include "Helper.h"
  3. #include "interface_mqtt.h"
  4. #include "ClassFlowPostProcessing.h"
  5. #include <time.h>
  6. void ClassFlowMQTT::SetInitialParameter(void)
  7. {
  8. uri = "";
  9. topic = "";
  10. topicError = "";
  11. topicRate = "";
  12. topicTimeStamp = "";
  13. maintopic = "";
  14. mainerrortopic = "";
  15. clientname = "watermeter";
  16. OldValue = "";
  17. flowpostprocessing = NULL;
  18. user = "";
  19. password = "";
  20. previousElement = NULL;
  21. ListFlowControll = NULL;
  22. disabled = false;
  23. MQTTenable = false;
  24. }
  25. ClassFlowMQTT::ClassFlowMQTT()
  26. {
  27. SetInitialParameter();
  28. }
  29. ClassFlowMQTT::ClassFlowMQTT(std::vector<ClassFlow*>* lfc)
  30. {
  31. SetInitialParameter();
  32. ListFlowControll = lfc;
  33. for (int i = 0; i < ListFlowControll->size(); ++i)
  34. {
  35. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  36. {
  37. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  38. }
  39. }
  40. }
  41. ClassFlowMQTT::ClassFlowMQTT(std::vector<ClassFlow*>* lfc, ClassFlow *_prev)
  42. {
  43. SetInitialParameter();
  44. previousElement = _prev;
  45. ListFlowControll = lfc;
  46. for (int i = 0; i < ListFlowControll->size(); ++i)
  47. {
  48. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  49. {
  50. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  51. }
  52. }
  53. }
  54. bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph)
  55. {
  56. std::vector<string> zerlegt;
  57. aktparamgraph = trim(aktparamgraph);
  58. if (aktparamgraph.size() == 0)
  59. if (!this->GetNextParagraph(pfile, aktparamgraph))
  60. return false;
  61. if (toUpper(aktparamgraph).compare("[MQTT]") != 0) // Paragraph passt nich zu MakeImage
  62. return false;
  63. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  64. {
  65. zerlegt = this->ZerlegeZeile(aktparamgraph);
  66. if ((toUpper(zerlegt[0]) == "USER") && (zerlegt.size() > 1))
  67. {
  68. this->user = zerlegt[1];
  69. }
  70. if ((toUpper(zerlegt[0]) == "PASSWORD") && (zerlegt.size() > 1))
  71. {
  72. this->password = zerlegt[1];
  73. }
  74. if ((toUpper(zerlegt[0]) == "URI") && (zerlegt.size() > 1))
  75. {
  76. this->uri = zerlegt[1];
  77. }
  78. if ((toUpper(zerlegt[0]) == "CLIENTID") && (zerlegt.size() > 1))
  79. {
  80. this->clientname = zerlegt[1];
  81. }
  82. if (((toUpper(zerlegt[0]) == "TOPIC") || (toUpper(zerlegt[0]) == "MAINTOPIC")) && (zerlegt.size() > 1))
  83. {
  84. maintopic = zerlegt[1];
  85. }
  86. }
  87. if ((uri.length() > 0) && (maintopic.length() > 0))
  88. {
  89. mainerrortopic = maintopic + "/connection";
  90. MQTTInit(uri, clientname, user, password, mainerrortopic, 60);
  91. MQTTPublish(mainerrortopic, "connected");
  92. MQTTenable = true;
  93. }
  94. return true;
  95. }
  96. bool ClassFlowMQTT::doFlow(string zwtime)
  97. {
  98. if (!MQTTenable)
  99. return true;
  100. std::string result;
  101. std::string resulterror = "";
  102. std::string resultrate = "";
  103. std::string resulttimestamp = "";
  104. string zw = "";
  105. string namenumber = "";
  106. MQTTPublish(mainerrortopic, "connected");
  107. if (flowpostprocessing)
  108. {
  109. std::vector<NumberPost*> NUMBERS = flowpostprocessing->GetNumbers();
  110. for (int i = 0; i < NUMBERS.size(); ++i)
  111. {
  112. result = NUMBERS[i]->ReturnValueNoError;
  113. resulterror = NUMBERS[i]->ErrorMessageText;
  114. resultrate = std::to_string(NUMBERS[i]->FlowRateAct);
  115. resulttimestamp = NUMBERS[i]->timeStamp;
  116. namenumber = NUMBERS[i]->name;
  117. if (namenumber == "default")
  118. namenumber = maintopic + "/";
  119. else
  120. namenumber = maintopic + "/" + namenumber + "/";
  121. zw = namenumber + "value";
  122. MQTTPublish(zw, result);
  123. zw = namenumber + "error";
  124. MQTTPublish(zw, resulterror, 1);
  125. zw = namenumber + "rate";
  126. MQTTPublish(zw, resultrate);
  127. zw = namenumber + "timestamp";
  128. MQTTPublish(zw, resulttimestamp);
  129. }
  130. }
  131. else
  132. {
  133. for (int i = 0; i < ListFlowControll->size(); ++i)
  134. {
  135. zw = (*ListFlowControll)[i]->getReadout();
  136. if (zw.length() > 0)
  137. {
  138. if (result.length() == 0)
  139. result = zw;
  140. else
  141. result = result + "\t" + zw;
  142. }
  143. }
  144. MQTTPublish(topic, result);
  145. }
  146. OldValue = result;
  147. return true;
  148. }