ClassFlowMQTT.cpp 2.4 KB

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