-
Notifications
You must be signed in to change notification settings - Fork 667
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from jomjol/rolling
Rolling
- Loading branch information
Showing
27 changed files
with
410 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#include "ClassFlowMQTT.h" | ||
#include "Helper.h" | ||
|
||
#include "interface_mqtt.h" | ||
#include "ClassFlowPostProcessing.h" | ||
|
||
#include <time.h> | ||
|
||
static const char* TAG2 = "example"; | ||
|
||
ClassFlowMQTT::ClassFlowMQTT() | ||
{ | ||
uri = ""; | ||
topic = ""; | ||
clientname = "watermeter"; | ||
OldValue = ""; | ||
flowpostprocessing = NULL; | ||
user = ""; | ||
password = ""; | ||
} | ||
|
||
ClassFlowMQTT::ClassFlowMQTT(std::vector<ClassFlow*>* lfc) | ||
{ | ||
uri = ""; | ||
topic = ""; | ||
clientname = "watermeter"; | ||
OldValue = ""; | ||
flowpostprocessing = NULL; | ||
user = ""; | ||
password = ""; | ||
|
||
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]) == "TOPIC") && (zerlegt.size() > 1)) | ||
{ | ||
this->topic = zerlegt[1]; | ||
} | ||
if ((toUpper(zerlegt[0]) == "CLIENTID") && (zerlegt.size() > 1)) | ||
{ | ||
this->clientname = zerlegt[1]; | ||
} | ||
|
||
} | ||
|
||
if ((uri.length() > 0) && (topic.length() > 0)) | ||
{ | ||
MQTTInit(uri, clientname, user, password); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
|
||
bool ClassFlowMQTT::doFlow(string zwtime) | ||
{ | ||
std::string result; | ||
string zw = ""; | ||
|
||
if (flowpostprocessing) | ||
{ | ||
result = flowpostprocessing->getReadoutParam(false, true); | ||
} | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
#include "ClassFlow.h" | ||
|
||
#include "ClassFlowPostProcessing.h" | ||
|
||
#include <string> | ||
|
||
class ClassFlowMQTT : | ||
public ClassFlow | ||
{ | ||
protected: | ||
std::string uri, topic, clientname; | ||
std::string OldValue; | ||
ClassFlowPostProcessing* flowpostprocessing; | ||
std::string user, password; | ||
|
||
|
||
public: | ||
ClassFlowMQTT(); | ||
ClassFlowMQTT(std::vector<ClassFlow*>* lfc); | ||
bool ReadParameter(FILE* pfile, string& aktparamgraph); | ||
bool doFlow(string time); | ||
string name(){return "ClassFlowMQTT";}; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include "interface_mqtt.h" | ||
|
||
|
||
#include "esp_log.h" | ||
#include "mqtt_client.h" | ||
#include "ClassLogFile.h" | ||
|
||
static const char *TAG = "interface_mqtt"; | ||
|
||
bool debugdetail = true; | ||
|
||
// #define CONFIG_BROKER_URL "mqtt://192.168.178.43:1883" | ||
|
||
esp_mqtt_event_id_t esp_mmqtt_ID = MQTT_EVENT_ANY; | ||
|
||
bool mqtt_connected = false; | ||
esp_mqtt_client_handle_t client = NULL; | ||
|
||
void MQTTPublish(std::string _key, std::string _content){ | ||
if (client && mqtt_connected) { | ||
int msg_id; | ||
std::string zw; | ||
msg_id = esp_mqtt_client_publish(client, _key.c_str(), _content.c_str(), 0, 1, 0); | ||
zw = "sent publish successful in MQTTPublish, msg_id=" + std::to_string(msg_id) + ", " + _key + ", " + _content; | ||
if (debugdetail) LogFile.WriteToFile(zw); | ||
ESP_LOGI(TAG, "sent publish successful in MQTTPublish, msg_id=%d, %s, %s", msg_id, _key.c_str(), _content.c_str()); | ||
} | ||
else { | ||
ESP_LOGI(TAG, "Problem with Publish, client=%d, mqtt_connected %d", (int) client, (int) mqtt_connected); | ||
} | ||
} | ||
|
||
|
||
static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) | ||
{ | ||
switch (event->event_id) { | ||
case MQTT_EVENT_CONNECTED: | ||
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); | ||
mqtt_connected = true; | ||
break; | ||
case MQTT_EVENT_DISCONNECTED: | ||
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); | ||
break; | ||
case MQTT_EVENT_PUBLISHED: | ||
ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); | ||
break; | ||
case MQTT_EVENT_DATA: | ||
ESP_LOGI(TAG, "MQTT_EVENT_DATA"); | ||
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); | ||
printf("DATA=%.*s\r\n", event->data_len, event->data); | ||
break; | ||
case MQTT_EVENT_ERROR: | ||
ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); | ||
break; | ||
default: | ||
ESP_LOGI(TAG, "Other event id:%d", event->event_id); | ||
break; | ||
} | ||
return ESP_OK; | ||
} | ||
|
||
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { | ||
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); | ||
mqtt_event_handler_cb((esp_mqtt_event_handle_t) event_data); | ||
} | ||
|
||
void MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password){ | ||
esp_mqtt_client_config_t mqtt_cfg = { | ||
.uri = _mqttURI.c_str(), | ||
.client_id = _clientid.c_str(), | ||
}; | ||
|
||
if (_user.length() && _password.length()){ | ||
mqtt_cfg.username = _user.c_str(); | ||
mqtt_cfg.password = _password.c_str(); | ||
printf("Connect to MQTT: %s, %s", mqtt_cfg.username, mqtt_cfg.password); | ||
}; | ||
|
||
client = esp_mqtt_client_init(&mqtt_cfg); | ||
esp_mqtt_client_register_event(client, esp_mmqtt_ID, mqtt_event_handler, client); | ||
esp_mqtt_client_start(client); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#include <string> | ||
|
||
void MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user = "", std::string _password = ""); | ||
void MQTTPublish(std::string _key, std::string _content); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.