| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- #include <sstream>
- #include "ClassFlowMQTT.h"
- #include "Helper.h"
- #include "time_sntp.h"
- #include "interface_mqtt.h"
- #include "ClassFlowPostProcessing.h"
- #include <time.h>
- void ClassFlowMQTT::SetInitialParameter(void)
- {
- uri = "";
- topic = "";
- topicError = "";
- topicRate = "";
- topicTimeStamp = "";
- maintopic = "";
- mainerrortopic = "";
- topicUptime = "";
- topicFreeMem = "";
- clientname = "watermeter";
- OldValue = "";
- flowpostprocessing = NULL;
- user = "";
- password = "";
- previousElement = NULL;
- ListFlowControll = NULL;
- disabled = false;
- MQTTenable = false;
-
-
- }
- ClassFlowMQTT::ClassFlowMQTT()
- {
- SetInitialParameter();
- }
- ClassFlowMQTT::ClassFlowMQTT(std::vector<ClassFlow*>* lfc)
- {
- SetInitialParameter();
- ListFlowControll = lfc;
- for (int i = 0; i < ListFlowControll->size(); ++i)
- {
- if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
- {
- flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
- }
- }
- }
- ClassFlowMQTT::ClassFlowMQTT(std::vector<ClassFlow*>* lfc, ClassFlow *_prev)
- {
- SetInitialParameter();
- previousElement = _prev;
- ListFlowControll = lfc;
- for (int i = 0; i < ListFlowControll->size(); ++i)
- {
- if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
- {
- flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
- }
- }
- }
- bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph)
- {
- std::vector<string> zerlegt;
- aktparamgraph = trim(aktparamgraph);
- if (aktparamgraph.size() == 0)
- if (!this->GetNextParagraph(pfile, aktparamgraph))
- return false;
- if (toUpper(aktparamgraph).compare("[MQTT]") != 0) // Paragraph passt nich zu MakeImage
- return false;
- while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
- {
- zerlegt = this->ZerlegeZeile(aktparamgraph);
- if ((toUpper(zerlegt[0]) == "USER") && (zerlegt.size() > 1))
- {
- this->user = zerlegt[1];
- }
- if ((toUpper(zerlegt[0]) == "PASSWORD") && (zerlegt.size() > 1))
- {
- this->password = zerlegt[1];
- }
- if ((toUpper(zerlegt[0]) == "URI") && (zerlegt.size() > 1))
- {
- this->uri = zerlegt[1];
- }
- if ((toUpper(zerlegt[0]) == "CLIENTID") && (zerlegt.size() > 1))
- {
- this->clientname = zerlegt[1];
- }
- if (((toUpper(zerlegt[0]) == "TOPIC") || (toUpper(zerlegt[0]) == "MAINTOPIC")) && (zerlegt.size() > 1))
- {
- maintopic = zerlegt[1];
- }
- }
- if (!MQTTisConnected() && (uri.length() > 0) && (maintopic.length() > 0))
- {
- mainerrortopic = maintopic + "/connection";
- MQTTInit(uri, clientname, user, password, mainerrortopic, 60);
- MQTTPublish(mainerrortopic, "connected");
- MQTTenable = true;
- }
-
- return true;
- }
- string ClassFlowMQTT::GetMQTTMainTopic()
- {
- return maintopic;
- }
- bool ClassFlowMQTT::doFlow(string zwtime)
- {
- if (!MQTTenable)
- return true;
- std::string result;
- std::string resulterror = "";
- std::string resultrate = "";
- std::string resulttimestamp = "";
- string zw = "";
- string namenumber = "";
- MQTTPublish(mainerrortopic, "connected");
-
- zw = maintopic + "/" + "uptime";
- char uptimeStr[11];
- sprintf(uptimeStr, "%ld", (long)getUpTime());
- MQTTPublish(zw, uptimeStr);
- zw = maintopic + "/" + "freeMem";
- char freeheapmem[11];
- sprintf(freeheapmem, "%zu", esp_get_free_heap_size());
- MQTTPublish(zw, freeheapmem);
- if (flowpostprocessing)
- {
- std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers();
- for (int i = 0; i < (*NUMBERS).size(); ++i)
- {
- result = (*NUMBERS)[i]->ReturnValueNoError;
- resulterror = (*NUMBERS)[i]->ErrorMessageText;
- resultrate = std::to_string((*NUMBERS)[i]->FlowRateAct);
- resulttimestamp = (*NUMBERS)[i]->timeStamp;
- namenumber = (*NUMBERS)[i]->name;
- if (namenumber == "default")
- namenumber = maintopic + "/";
- else
- namenumber = maintopic + "/" + namenumber + "/";
- zw = namenumber + "value";
- MQTTPublish(zw, result);
- zw = namenumber + "error";
- MQTTPublish(zw, resulterror, 1);
- zw = namenumber + "rate";
- MQTTPublish(zw, resultrate);
- zw = namenumber + "timestamp";
- MQTTPublish(zw, resulttimestamp);
- std::string json="{\"value\":"+result;
- json += ",\"error\":\""+resulterror;
- json += "\",\"rate\":"+resultrate;
- json += ",\"timestamp\":\""+resulttimestamp+"\"}";
- zw = namenumber + "json";
- MQTTPublish(zw, json);
- }
- }
- else
- {
- for (int i = 0; i < ListFlowControll->size(); ++i)
- {
- zw = (*ListFlowControll)[i]->getReadout();
- if (zw.length() > 0)
- {
- if (result.length() == 0)
- result = zw;
- else
- result = result + "\t" + zw;
- }
- }
- MQTTPublish(topic, result);
- }
-
- OldValue = result;
-
- return true;
- }
|