From 74b1106b71f438150746474ca632f6c522b8c22f Mon Sep 17 00:00:00 2001 From: Nicolas Aguirre Date: Thu, 14 Apr 2022 22:24:01 +0200 Subject: [PATCH] Correctly convert values when coeff_a and coeef_b is defined --- src/bin/calaos_server/IO/Mqtt/MqttCtrl.cpp | 46 ++++++++++++++----- src/bin/calaos_server/IO/Mqtt/MqttCtrl.h | 9 ++-- .../calaos_server/IO/Mqtt/MqttInputAnalog.cpp | 2 +- .../calaos_server/IO/Mqtt/MqttInputString.cpp | 2 +- .../calaos_server/IO/Mqtt/MqttInputSwitch.cpp | 2 +- .../calaos_server/IO/Mqtt/MqttInputTemp.cpp | 2 +- .../calaos_server/IO/Mqtt/MqttOutputLight.cpp | 2 +- 7 files changed, 46 insertions(+), 19 deletions(-) diff --git a/src/bin/calaos_server/IO/Mqtt/MqttCtrl.cpp b/src/bin/calaos_server/IO/Mqtt/MqttCtrl.cpp index 6377d488..9ab2b5fd 100644 --- a/src/bin/calaos_server/IO/Mqtt/MqttCtrl.cpp +++ b/src/bin/calaos_server/IO/Mqtt/MqttCtrl.cpp @@ -4,6 +4,7 @@ #include "IOFactory.h" #include "MqttCtrl.h" #include "Prefix.h" +#include "Params.h" using namespace Calaos; @@ -101,7 +102,6 @@ void MqttCtrl::publishTopic(const string topic, const string payload) { string message; - json_t *jroot = json_object(); json_object_set_new(jroot, "topic", json_string(topic.c_str())); json_object_set_new(jroot, "payload", json_string(payload.c_str())); @@ -110,8 +110,7 @@ void MqttCtrl::publishTopic(const string topic, const string payload) json_decref(jroot); } - -string MqttCtrl::getValueJson(string path, string payload) +string MqttCtrl::getValueJson(const Params ¶ms, string path, string payload) { string value; @@ -132,7 +131,7 @@ string MqttCtrl::getValueJson(string path, string payload) if (!tokens.empty()) { Json parent = root; - for (auto it = tokens.begin();it != tokens.end();it++) + for (auto it = tokens.begin(); it != tokens.end(); it++) { string val = *it; @@ -175,9 +174,25 @@ string MqttCtrl::getValueJson(string path, string payload) if (parent.is_null()) value = "null"; else if (parent.is_boolean()) - value = parent.get()?"true":"false"; + value = parent.get() ? "true" : "false"; else if (parent.is_number()) - value = Utils::to_string(parent.get()); + { + double v; + double coeff_a, coeff_b; + if (params.Exists("coeff_a")) + Utils::from_string(params["coeff_a"], coeff_a); + else + coeff_a = 1.0; + + if (params.Exists("coeff_b")) + Utils::from_string(params["coeff_b"], coeff_b); + else + coeff_b = 0.0; + + v = (parent.get() - coeff_b) / coeff_a; + + value = Utils::to_string(v); + } else if (parent.is_string()) value = parent.get(); else if (parent.is_object()) @@ -199,7 +214,6 @@ string MqttCtrl::getValueJson(string path, string payload) return value; } - string MqttCtrl::getValue(const Params ¶ms, bool &err) { string type = params["type"]; @@ -220,7 +234,7 @@ string MqttCtrl::getValue(const Params ¶ms, bool &err) return ""; } err = false; - return getValueJson(params["path"], payload); + return getValueJson(params, params["path"], payload); } double MqttCtrl::getValueDouble(const Params ¶ms, bool &err) @@ -259,7 +273,6 @@ void MqttCtrl::setValueString(const Params ¶ms, string val) cDebugDom("mqtt") << "Publish " << data << " on topic" << topic; publishTopic(topic, data); - } void MqttCtrl::setValue(const Params ¶ms, bool val) @@ -291,7 +304,6 @@ void MqttCtrl::setValue(const Params ¶ms, bool val) publishTopic(topic, data); } - void MqttCtrl::setValueInt(const Params ¶ms, int val) { string topic = params["topic_pub"]; @@ -300,7 +312,19 @@ void MqttCtrl::setValueInt(const Params ¶ms, int val) if (params.Exists("data")) { data = params["data"]; - replace_str(data, "__##VALUE##__", to_string((int)(val * 2.55))); + double coeff_a, coeff_b; + if (params.Exists("coeff_a")) + Utils::from_string(params["coeff_a"], coeff_a); + else + coeff_a = 1.0; + + if (params.Exists("coeff_b")) + Utils::from_string(params["coeff_b"], coeff_b); + else + coeff_b = 0.0; + + + replace_str(data, "__##VALUE##__", to_string((int)(val * coeff_a + coeff_b))); } else { diff --git a/src/bin/calaos_server/IO/Mqtt/MqttCtrl.h b/src/bin/calaos_server/IO/Mqtt/MqttCtrl.h index d657b98a..dcc0dcfa 100644 --- a/src/bin/calaos_server/IO/Mqtt/MqttCtrl.h +++ b/src/bin/calaos_server/IO/Mqtt/MqttCtrl.h @@ -16,24 +16,27 @@ class MqttCtrl : public sigc::trackable MqttCtrl(const Params &p); ~MqttCtrl(); - void subscribeTopic(const string topic, sigc::slot callback); + void subscribeTopic(const string topic, sigc::slot callback); void publishTopic(const string topic, const string payload); - string getValueJson(string path, string payload); + string getValueJson(const Params ¶ms,string path, string payload); string getValue(const Params ¶ms, bool &err); double getValueDouble(const Params ¶ms, bool &err); static void commonDoc(IODoc *ioDoc); void setValue(const Params ¶ms, bool val); void setValueString(const Params ¶ms, string val); void setValueInt(const Params ¶ms, int val); + bool topicMatchesSubscription(string subscription, string topic); private: ExternProcServer *process; string exe; - unordered_map>> subscribeCb; + unordered_map>> subscribeCb; unordered_map messages; bool connected = false; + + }; diff --git a/src/bin/calaos_server/IO/Mqtt/MqttInputAnalog.cpp b/src/bin/calaos_server/IO/Mqtt/MqttInputAnalog.cpp index 67b8de6f..5cd1e4f4 100644 --- a/src/bin/calaos_server/IO/Mqtt/MqttInputAnalog.cpp +++ b/src/bin/calaos_server/IO/Mqtt/MqttInputAnalog.cpp @@ -44,7 +44,7 @@ MqttInputAnalog::MqttInputAnalog(Params &p): Calaos::StartReadRules::Instance().addIO(); ctrl = MqttBrokersList::Instance().get_ctrl(get_params()); - ctrl->subscribeTopic(get_param("topic_sub"), [=](string) + ctrl->subscribeTopic(get_param("topic_sub"), [=](string, string) { readValue(); }); diff --git a/src/bin/calaos_server/IO/Mqtt/MqttInputString.cpp b/src/bin/calaos_server/IO/Mqtt/MqttInputString.cpp index d171791e..b5661bb9 100644 --- a/src/bin/calaos_server/IO/Mqtt/MqttInputString.cpp +++ b/src/bin/calaos_server/IO/Mqtt/MqttInputString.cpp @@ -44,7 +44,7 @@ MqttInputString::MqttInputString(Params &p): Calaos::StartReadRules::Instance().addIO(); ctrl = MqttBrokersList::Instance().get_ctrl(get_params()); - ctrl->subscribeTopic(get_param("topic_sub"), [=](string) + ctrl->subscribeTopic(get_param("topic_sub"), [=](string, string) { cDebugDom("mqtt") << "Read Value"; readValue(); diff --git a/src/bin/calaos_server/IO/Mqtt/MqttInputSwitch.cpp b/src/bin/calaos_server/IO/Mqtt/MqttInputSwitch.cpp index 2c29a4c1..d4669b43 100644 --- a/src/bin/calaos_server/IO/Mqtt/MqttInputSwitch.cpp +++ b/src/bin/calaos_server/IO/Mqtt/MqttInputSwitch.cpp @@ -49,7 +49,7 @@ MqttInputSwitch::MqttInputSwitch(Params &p): ctrl = MqttBrokersList::Instance().get_ctrl(get_params()); cDebugDom("mqtt") << "regoister on topic : " << get_param("topic_sub"); - ctrl->subscribeTopic(get_param("topic_sub"), [=](string) + ctrl->subscribeTopic(get_param("topic_sub"), [=](string, string) { cDebugDom("mqtt") << "New value on topic " << get_param("topic_sub"); hasChanged(); diff --git a/src/bin/calaos_server/IO/Mqtt/MqttInputTemp.cpp b/src/bin/calaos_server/IO/Mqtt/MqttInputTemp.cpp index 0d3ebccb..fa06bdf3 100644 --- a/src/bin/calaos_server/IO/Mqtt/MqttInputTemp.cpp +++ b/src/bin/calaos_server/IO/Mqtt/MqttInputTemp.cpp @@ -45,7 +45,7 @@ MqttInputTemp::MqttInputTemp(Params &p): Calaos::StartReadRules::Instance().addIO(); ctrl = MqttBrokersList::Instance().get_ctrl(get_params()); - ctrl->subscribeTopic(get_param("topic_sub"), [=](string) + ctrl->subscribeTopic(get_param("topic_sub"), [=](string, string) { readValue(); }); diff --git a/src/bin/calaos_server/IO/Mqtt/MqttOutputLight.cpp b/src/bin/calaos_server/IO/Mqtt/MqttOutputLight.cpp index 4fc123b2..eee31bf3 100644 --- a/src/bin/calaos_server/IO/Mqtt/MqttOutputLight.cpp +++ b/src/bin/calaos_server/IO/Mqtt/MqttOutputLight.cpp @@ -41,7 +41,7 @@ MqttOutputLight::MqttOutputLight(Params &p): IODoc::TYPE_STRING, true); ctrl = MqttBrokersList::Instance().get_ctrl(get_params()); - ctrl->subscribeTopic(get_param("topic_sub"), [=](string) + ctrl->subscribeTopic(get_param("topic_sub"), [=](string, string) { readValue(); });