From 039ef144380af57db692cd718290e71afd3eadd9 Mon Sep 17 00:00:00 2001 From: Krzysztof Lewandowski Date: Mon, 12 Dec 2022 14:54:41 +0100 Subject: [PATCH] Linux: add ThermHygroMeterParsed for temperature+humidity channel. See README.md file for example usage --- extras/examples/linux/README.md | 28 ++++++++ extras/porting/linux/CMakeLists.txt | 1 + extras/porting/linux/linux_yaml_config.cpp | 68 +++++++++++++++++++ extras/porting/linux/linux_yaml_config.h | 3 + .../supla/sensor/therm_hygro_meter_parsed.cpp | 66 ++++++++++++++++++ .../supla/sensor/therm_hygro_meter_parsed.h | 47 +++++++++++++ 6 files changed, 213 insertions(+) create mode 100644 extras/porting/linux/supla/sensor/therm_hygro_meter_parsed.cpp create mode 100644 extras/porting/linux/supla/sensor/therm_hygro_meter_parsed.h diff --git a/extras/examples/linux/README.md b/extras/examples/linux/README.md index 10e294e4..6b002022 100644 --- a/extras/examples/linux/README.md +++ b/extras/examples/linux/README.md @@ -224,6 +224,7 @@ Supported channel types: * `VirtualRelay` - related class `Supla::Control::VirtualRelay` * `Fronius` - related class `Supla::PV::Fronius` * `ThermometerParsed` - related class `Supla::Sensor::ThermometerParsed` +* `ThermHygroMeterParsed` - related class `Supla::Sensor::ThermHygroMeterParsed` * `ImpulseCounterParsed` - related class `Supla::Sensor::ImpulseCounterParsed` * `ElectricityMeterParsed` - related class `Supla::Sensor::ElectricityMeterParsed` * `BinaryParsed` - related class `Supla::Sensor::BinaryParsed` @@ -315,6 +316,22 @@ Example channels configuration (details are exaplained later): parser: use: parser_1 + - type: ThermHygroMeterParsed + name: th1 + source: + type: File + # use file "temp_humi.txt" from current folder + file: "temp_humi.txt" + parser: + type: Simple + refresh_time_ms: 200 + # temperature is read from first line of txt file + temperature: 0 + # humidity is read from second line of txt file + humidity: 1 + multiplier_temp: 1 + multiplier_humi: 1 + There are some new classes (compared to standard non-Linux supla-device) which names end with "Parsed" word. In general, those channels use `parser` and `source` functions to get some data from your computer and put it to that @@ -395,6 +412,17 @@ from `parser`. Optional parameter: `multiplier` - defines multiplier for fetched value (you can put any floating point number). +### `ThermHygroMeterParsed` + +Add channel with "thermometer + hygrometer" type. + +Mandatory parameters: `temperature` - defines key/index by which data is fetched +from `parser` for temperature value, `humidity` - defines key/index by which +data is fetched from `parser` for humidity value; +Optional parameter: `multiplier_temp` - defines multiplier for temperatur value +(you can put any floating point number), `multiplier_humi` - defines multiplier +for humidity value. + ### `ImpulseCounterParsed` Add channel with "impulse counter" type. You can define in Supla Cloud what diff --git a/extras/porting/linux/CMakeLists.txt b/extras/porting/linux/CMakeLists.txt index b41cd0c0..3d7a71b2 100644 --- a/extras/porting/linux/CMakeLists.txt +++ b/extras/porting/linux/CMakeLists.txt @@ -47,6 +47,7 @@ set(SUPLA_DEVICE_LINUX_SRCS supla/sensor/pressure_parsed.cpp supla/sensor/rain_parsed.cpp supla/sensor/wind_parsed.cpp + supla/sensor/therm_hygro_meter_parsed.cpp ../../../src/supla/pv/fronius.cpp ../../../src/supla/pv/afore.cpp diff --git a/extras/porting/linux/linux_yaml_config.cpp b/extras/porting/linux/linux_yaml_config.cpp index 65b1ab3b..4f58db08 100644 --- a/extras/porting/linux/linux_yaml_config.cpp +++ b/extras/porting/linux/linux_yaml_config.cpp @@ -46,9 +46,12 @@ #include #include "linux_yaml_config.h" +#include "supla/sensor/therm_hygro_meter_parsed.h" namespace Supla { const char Multiplier[] = "multiplier"; +const char MultiplierTemp[] = "multiplier_temp"; +const char MultiplierHumi[] = "multiplier_humi"; const char GuidAuthFileName[] = "/guid_auth.yaml"; const char GuidKey[] = "guid"; @@ -433,6 +436,12 @@ bool Supla::LinuxYamlConfig::parseChannel(const YAML::Node& ch, return false; } return addRainParsed(ch, channelNumber, parser); + } else if (type == "ThermHygroMeterParsed") { + if (!parser) { + SUPLA_LOG_ERROR("Channel[%d] config: missing parser", channelNumber); + return false; + } + return addThermHygroMeterParsed(ch, channelNumber, parser); } else { SUPLA_LOG_ERROR( "Channel[%d] config: unknown type \"%s\"", @@ -519,6 +528,10 @@ bool Supla::LinuxYamlConfig::addThermometerParsed( paramCount++; double multiplier = ch[Supla::Multiplier].as(); therm->setMultiplier(Supla::Parser::Temperature, multiplier); + } else if (ch[Supla::MultiplierTemp]) { + paramCount++; + double multiplier = ch[Supla::MultiplierTemp].as(); + therm->setMultiplier(Supla::Parser::Temperature, multiplier); } return true; @@ -834,6 +847,61 @@ bool Supla::LinuxYamlConfig::isConfigModeSupported() { return false; } +bool Supla::LinuxYamlConfig::addThermHygroMeterParsed(const YAML::Node& ch, + int channelNumber, + Supla::Parser::Parser* parser) { + SUPLA_LOG_INFO("Channel[%d] config: adding ThermHygroMeterParsed", + channelNumber); + auto thermHumi = new Supla::Sensor::ThermHygroMeterParsed(parser); + if (ch[Supla::Parser::Humidity]) { + paramCount++; + if (parser->isBasedOnIndex()) { + int index = ch[Supla::Parser::Humidity].as(); + thermHumi->setMapping(Supla::Parser::Humidity, index); + } else { + std::string key = ch[Supla::Parser::Humidity].as(); + thermHumi->setMapping(Supla::Parser::Humidity, key); + } + } else { + SUPLA_LOG_ERROR( + "Channel[%d] config: missing \"%s\" parameter", + channelNumber, + Supla::Parser::Humidity); + return false; + } + + if (ch[Supla::MultiplierHumi]) { + paramCount++; + double multiplier = ch[Supla::MultiplierHumi].as(); + thermHumi->setMultiplier(Supla::Parser::Humidity, multiplier); + } + + if (ch[Supla::Parser::Temperature]) { + paramCount++; + if (parser->isBasedOnIndex()) { + int index = ch[Supla::Parser::Temperature].as(); + thermHumi->setMapping(Supla::Parser::Temperature, index); + } else { + std::string key = ch[Supla::Parser::Temperature].as(); + thermHumi->setMapping(Supla::Parser::Temperature, key); + } + } else { + SUPLA_LOG_ERROR( + "Channel[%d] config: missing \"%s\" parameter", + channelNumber, + Supla::Parser::Temperature); + return false; + } + + if (ch[Supla::MultiplierTemp]) { + paramCount++; + double multiplier = ch[Supla::MultiplierTemp].as(); + thermHumi->setMultiplier(Supla::Parser::Temperature, multiplier); + } + + return true; +} + bool Supla::LinuxYamlConfig::addHumidityParsed( const YAML::Node& ch, int channelNumber, Supla::Parser::Parser* parser) { SUPLA_LOG_INFO("Channel[%d] config: adding HumidityParsed", channelNumber); diff --git a/extras/porting/linux/linux_yaml_config.h b/extras/porting/linux/linux_yaml_config.h index cd69a15d..afb6dbbd 100644 --- a/extras/porting/linux/linux_yaml_config.h +++ b/extras/porting/linux/linux_yaml_config.h @@ -150,6 +150,9 @@ class LinuxYamlConfig : public Config { bool addBinaryParsed(const YAML::Node& ch, int channelNumber, Supla::Parser::Parser* parser); + bool addThermHygroMeterParsed(const YAML::Node& ch, + int channelNumber, + Supla::Parser::Parser* parser); bool addHumidityParsed(const YAML::Node& ch, int channelNumber, Supla::Parser::Parser* parser); diff --git a/extras/porting/linux/supla/sensor/therm_hygro_meter_parsed.cpp b/extras/porting/linux/supla/sensor/therm_hygro_meter_parsed.cpp new file mode 100644 index 00000000..fab2f0da --- /dev/null +++ b/extras/porting/linux/supla/sensor/therm_hygro_meter_parsed.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (C) AC SOFTWARE SP. Z O.O + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include + +#include "humidity_parsed.h" +#include "therm_hygro_meter_parsed.h" + +Supla::Sensor::ThermHygroMeterParsed::ThermHygroMeterParsed( + Supla::Parser::Parser *parser) + : SensorParsed(parser) { +} + +double Supla::Sensor::ThermHygroMeterParsed::getTemp() { + double value = TEMPERATURE_NOT_AVAILABLE; + + if (isParameterConfigured(Supla::Parser::Temperature)) { + if (refreshParserSource()) { + value = getParameterValue(Supla::Parser::Temperature); + } + if (!parser->isValid()) { + if (!isDataErrorLogged) { + isDataErrorLogged = true; + SUPLA_LOG_WARNING("ThermHygroMeterParsed: source data error"); + } + return TEMPERATURE_NOT_AVAILABLE; + } + isDataErrorLogged = false; + } + return value; +} + +double Supla::Sensor::ThermHygroMeterParsed::getHumi() { + double value = HUMIDITY_NOT_AVAILABLE; + + if (isParameterConfigured(Supla::Parser::Humidity)) { + if (refreshParserSource()) { + value = getParameterValue(Supla::Parser::Humidity); + } + if (!parser->isValid()) { + if (!isDataErrorLogged) { + isDataErrorLogged = true; + SUPLA_LOG_WARNING("ThermHygroMeterParsed: source data error"); + } + return HUMIDITY_NOT_AVAILABLE; + } + isDataErrorLogged = false; + } + return value; +} + diff --git a/extras/porting/linux/supla/sensor/therm_hygro_meter_parsed.h b/extras/porting/linux/supla/sensor/therm_hygro_meter_parsed.h new file mode 100644 index 00000000..b990282b --- /dev/null +++ b/extras/porting/linux/supla/sensor/therm_hygro_meter_parsed.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) AC SOFTWARE SP. Z O.O + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef EXTRAS_PORTING_LINUX_SUPLA_SENSOR_THERM_HYGRO_METER_PARSED_H_ +#define EXTRAS_PORTING_LINUX_SUPLA_SENSOR_THERM_HYGRO_METER_PARSED_H_ + +#include +#include + +#include + +#include "sensor_parsed.h" +#include "thermometer_parsed.h" +#include "humidity_parsed.h" + +namespace Supla { +namespace Sensor { + +class ThermHygroMeterParsed : public ThermHygroMeter, public SensorParsed { + public: + explicit ThermHygroMeterParsed(Supla::Parser::Parser *); + double getTemp() override; + double getHumi() override; + + protected: + bool isDataErrorLogged = false; +}; +}; // namespace Sensor +}; // namespace Supla + + +#endif // EXTRAS_PORTING_LINUX_SUPLA_SENSOR_THERM_HYGRO_METER_PARSED_H_