ClassFlowMQTT.cpp 3.6 KB

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