ClassFlowMQTT.cpp 3.5 KB

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