ClassFlowMQTT.cpp 4.3 KB

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