Skip to content

Commit

Permalink
Correctly convert values when coeff_a and coeef_b is defined
Browse files Browse the repository at this point in the history
  • Loading branch information
naguirre committed Apr 14, 2022
1 parent 59cf9a9 commit 74b1106
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 19 deletions.
46 changes: 35 additions & 11 deletions src/bin/calaos_server/IO/Mqtt/MqttCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "IOFactory.h"
#include "MqttCtrl.h"
#include "Prefix.h"
#include "Params.h"

using namespace Calaos;

Expand Down Expand Up @@ -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()));
Expand All @@ -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 &params, string path, string payload)
{
string value;

Expand All @@ -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;

Expand Down Expand Up @@ -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<bool>()?"true":"false";
value = parent.get<bool>() ? "true" : "false";
else if (parent.is_number())
value = Utils::to_string(parent.get<double>());
{
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<double>() - coeff_b) / coeff_a;

value = Utils::to_string(v);
}
else if (parent.is_string())
value = parent.get<string>();
else if (parent.is_object())
Expand All @@ -199,7 +214,6 @@ string MqttCtrl::getValueJson(string path, string payload)
return value;
}


string MqttCtrl::getValue(const Params &params, bool &err)
{
string type = params["type"];
Expand All @@ -220,7 +234,7 @@ string MqttCtrl::getValue(const Params &params, bool &err)
return "";
}
err = false;
return getValueJson(params["path"], payload);
return getValueJson(params, params["path"], payload);
}

double MqttCtrl::getValueDouble(const Params &params, bool &err)
Expand Down Expand Up @@ -259,7 +273,6 @@ void MqttCtrl::setValueString(const Params &params, string val)
cDebugDom("mqtt") << "Publish " << data << " on topic" << topic;

publishTopic(topic, data);

}

void MqttCtrl::setValue(const Params &params, bool val)
Expand Down Expand Up @@ -291,7 +304,6 @@ void MqttCtrl::setValue(const Params &params, bool val)
publishTopic(topic, data);
}


void MqttCtrl::setValueInt(const Params &params, int val)
{
string topic = params["topic_pub"];
Expand All @@ -300,7 +312,19 @@ void MqttCtrl::setValueInt(const Params &params, 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
{
Expand Down
9 changes: 6 additions & 3 deletions src/bin/calaos_server/IO/Mqtt/MqttCtrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,27 @@ class MqttCtrl : public sigc::trackable
MqttCtrl(const Params &p);
~MqttCtrl();

void subscribeTopic(const string topic, sigc::slot<void, string> callback);
void subscribeTopic(const string topic, sigc::slot<void, string, string> callback);
void publishTopic(const string topic, const string payload);

string getValueJson(string path, string payload);
string getValueJson(const Params &params,string path, string payload);
string getValue(const Params &params, bool &err);
double getValueDouble(const Params &params, bool &err);
static void commonDoc(IODoc *ioDoc);
void setValue(const Params &params, bool val);
void setValueString(const Params &params, string val);
void setValueInt(const Params &params, int val);
bool topicMatchesSubscription(string subscription, string topic);

private:
ExternProcServer *process;
string exe;

unordered_map<string, vector<sigc::slot<void, string>>> subscribeCb;
unordered_map<string, vector<sigc::slot<void, string, string>>> subscribeCb;
unordered_map<string, string> messages;
bool connected = false;


};


Expand Down
2 changes: 1 addition & 1 deletion src/bin/calaos_server/IO/Mqtt/MqttInputAnalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
2 changes: 1 addition & 1 deletion src/bin/calaos_server/IO/Mqtt/MqttInputString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/bin/calaos_server/IO/Mqtt/MqttInputSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/bin/calaos_server/IO/Mqtt/MqttInputTemp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
2 changes: 1 addition & 1 deletion src/bin/calaos_server/IO/Mqtt/MqttOutputLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down

0 comments on commit 74b1106

Please sign in to comment.