ClassFlowMQTT.cpp 2.8 KB

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