ClassFlowMQTT.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "ClassFlowMQTT.h"
  2. #include "Helper.h"
  3. #include "interface_mqtt.h"
  4. #include "ClassFlowPostProcessing.h"
  5. #include <time.h>
  6. ClassFlowMQTT::ClassFlowMQTT()
  7. {
  8. uri = "";
  9. topic = "";
  10. clientname = "watermeter";
  11. OldValue = "";
  12. flowpostprocessing = NULL;
  13. user = "";
  14. password = "";
  15. }
  16. ClassFlowMQTT::ClassFlowMQTT(std::vector<ClassFlow*>* lfc)
  17. {
  18. uri = "";
  19. topic = "";
  20. clientname = "watermeter";
  21. OldValue = "";
  22. flowpostprocessing = NULL;
  23. user = "";
  24. password = "";
  25. ListFlowControll = lfc;
  26. for (int i = 0; i < ListFlowControll->size(); ++i)
  27. {
  28. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  29. {
  30. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  31. }
  32. }
  33. }
  34. bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph)
  35. {
  36. std::vector<string> zerlegt;
  37. aktparamgraph = trim(aktparamgraph);
  38. if (aktparamgraph.size() == 0)
  39. if (!this->GetNextParagraph(pfile, aktparamgraph))
  40. return false;
  41. if (toUpper(aktparamgraph).compare("[MQTT]") != 0) // Paragraph passt nich zu MakeImage
  42. return false;
  43. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  44. {
  45. zerlegt = this->ZerlegeZeile(aktparamgraph);
  46. if ((toUpper(zerlegt[0]) == "USER") && (zerlegt.size() > 1))
  47. {
  48. this->user = zerlegt[1];
  49. }
  50. if ((toUpper(zerlegt[0]) == "PASSWORD") && (zerlegt.size() > 1))
  51. {
  52. this->password = zerlegt[1];
  53. }
  54. if ((toUpper(zerlegt[0]) == "URI") && (zerlegt.size() > 1))
  55. {
  56. this->uri = zerlegt[1];
  57. }
  58. if ((toUpper(zerlegt[0]) == "TOPIC") && (zerlegt.size() > 1))
  59. {
  60. this->topic = zerlegt[1];
  61. }
  62. if ((toUpper(zerlegt[0]) == "CLIENTID") && (zerlegt.size() > 1))
  63. {
  64. this->clientname = zerlegt[1];
  65. }
  66. }
  67. if ((uri.length() > 0) && (topic.length() > 0))
  68. {
  69. MQTTInit(uri, clientname, user, password);
  70. }
  71. return true;
  72. }
  73. bool ClassFlowMQTT::doFlow(string zwtime)
  74. {
  75. std::string result;
  76. string zw = "";
  77. if (flowpostprocessing)
  78. {
  79. result = flowpostprocessing->getReadoutParam(false, true);
  80. }
  81. else
  82. {
  83. for (int i = 0; i < ListFlowControll->size(); ++i)
  84. {
  85. zw = (*ListFlowControll)[i]->getReadout();
  86. if (zw.length() > 0)
  87. {
  88. if (result.length() == 0)
  89. result = zw;
  90. else
  91. result = result + "\t" + zw;
  92. }
  93. }
  94. }
  95. MQTTPublish(topic, result);
  96. OldValue = result;
  97. return true;
  98. }