From 2b5fd8d3229595277f893083e7476afb23ffd33d Mon Sep 17 00:00:00 2001 From: Manuel Pietschmann Date: Tue, 26 Apr 2016 16:41:01 +0200 Subject: [PATCH] Use correct datatype and value range for sensor battery parameter --- de_web_plugin.cpp | 39 +++++++++++++++++++++++++++ rest_sensors.cpp | 67 +++++++++-------------------------------------- sensor.cpp | 22 +++++++++++----- sensor.h | 6 ++--- 4 files changed, 71 insertions(+), 63 deletions(-) diff --git a/de_web_plugin.cpp b/de_web_plugin.cpp index 9e9db18..c6151c5 100644 --- a/de_web_plugin.cpp +++ b/de_web_plugin.cpp @@ -2138,6 +2138,38 @@ void DeRestPluginPrivate::updateSensorNode(const deCONZ::NodeEvent &event) continue; } + if (event.event() == deCONZ::NodeEvent::UpdatedPowerDescriptor) + { + if (event.node()->powerDescriptor().isValid()) + { + SensorConfig config = i->config(); + + if (event.node()->powerDescriptor().currentPowerSource() == deCONZ::PowerSourceRechargeable || + event.node()->powerDescriptor().currentPowerSource() == deCONZ::PowerSourceDisposable) + { + switch (event.node()->powerDescriptor().currentPowerLevel()) + { + case deCONZ::PowerLevel100: config.setBattery(100); break; + case deCONZ::PowerLevel66: config.setBattery(66); break; + case deCONZ::PowerLevel33: config.setBattery(33); break; + case deCONZ::PowerLevelCritical: config.setBattery(0); break; + default: + config.setBattery(255); // invalid + break; + } + } + else + { + config.setBattery(255); // invalid + } + + i->setConfig(config); + updateEtag(i->etag); + updateEtag(gwConfigEtag); + } + return; + } + // filter for relevant clusters if (event.profileId() == HA_PROFILE_ID || event.profileId() == ZLL_PROFILE_ID) { @@ -2222,6 +2254,7 @@ void DeRestPluginPrivate::updateSensorNode(const deCONZ::NodeEvent &event) if (i->modelId().startsWith("FLS-NB")) { + // TODO check firmware version } else if (lux > 0 && lux < 0xffff) { @@ -4315,6 +4348,12 @@ void DeRestPluginPrivate::nodeEvent(const deCONZ::NodeEvent &event) } break; + case deCONZ::NodeEvent::UpdatedPowerDescriptor: + { + updateSensorNode(event); + } + break; + case deCONZ::NodeEvent::UpdatedClusterData: case deCONZ::NodeEvent::UpdatedClusterDataZclRead: case deCONZ::NodeEvent::UpdatedClusterDataZclReport: diff --git a/rest_sensors.cpp b/rest_sensors.cpp index 6531916..d6e2aa8 100644 --- a/rest_sensors.cpp +++ b/rest_sensors.cpp @@ -175,31 +175,9 @@ int DeRestPluginPrivate::getAllSensors(const ApiRequest &req, ApiResponse &rsp) config["reachable"] = i->config().reachable(); } - if (i->node() && i->node()->powerDescriptor().isValid()) + if (i->config().battery() <= 100) // valid value? { - switch (i->node()->powerDescriptor().currentPowerLevel()) - { - case deCONZ::PowerLevel100: - config["battery"] = "66 - 100%"; // 67 - 100 % - break; - - case deCONZ::PowerLevel66: - config["battery"] = "33 - 66%"; // 34 - 66 % - break; - - case deCONZ::PowerLevel33: - config["battery"] = "1 - 33%"; // 1 - 33 % - break; - - case deCONZ::PowerLevelCritical: - default: - config["battery"] = "< 1%"; // critical - break; - } - } - else - { - config["battery"] = ""; + config["battery"] = (double)i->config().battery(); } if (i->config().url() != "" ) @@ -339,31 +317,9 @@ int DeRestPluginPrivate::getSensor(const ApiRequest &req, ApiResponse &rsp) config["reachable"] = sensor->config().reachable(); } - if (sensor->node() && sensor->node()->powerDescriptor().isValid()) + if (sensor->config().battery() <= 100) // valid value? { - switch (sensor->node()->powerDescriptor().currentPowerLevel()) - { - case deCONZ::PowerLevel100: - config["battery"] = "66 - 100%"; // 67 - 100 % - break; - - case deCONZ::PowerLevel66: - config["battery"] = "33 - 66%"; // 34 - 66 % - break; - - case deCONZ::PowerLevel33: - config["battery"] = "1 - 33%"; // 1 - 33 % - break; - - case deCONZ::PowerLevelCritical: - default: - config["battery"] = "< 1%"; // critical - break; - } - } - else - { - config["battery"] = ""; + config["battery"] = (double)sensor->config().battery(); } if (sensor->config().url() != "" ) @@ -683,13 +639,14 @@ int DeRestPluginPrivate::createSensor(const ApiRequest &req, ApiResponse &rsp) } if (!config["battery"].isNull()) { - if (((config["battery"].toInt() < 0) || (config["battery"].toInt() > 255)) || (config["battery"].type() == QVariant::String) || (config["battery"].type() == QVariant::Bool)) + int battery = config["battery"].toInt(&ok); + if (!ok || (battery < 0) || (battery > 100)) { rsp.list.append(errorToMap(ERR_INVALID_VALUE, QString("/sensors/config"), QString("invalid value, %1, for parameter battery").arg(config["battery"].toString()))); rsp.httpStatus = HttpStatusBadRequest; return REQ_READY_SEND; } - newConfig.setBattery(config["battery"].toString()); + newConfig.setBattery(battery); } sensor.setConfig(newConfig); @@ -966,7 +923,9 @@ int DeRestPluginPrivate::changeSensorConfig(const ApiRequest &req, ApiResponse & } if (map.contains("battery")) { - if (((map["battery"].toInt() < 0) || (map["battery"].toInt() > 255)) || (map["battery"].type() == QVariant::String) || (map["battery"].type() == QVariant::Bool)) + int battery = map["battery"].toInt(&ok); + + if (!ok || (battery < 0) || (battery > 100)) { rsp.list.append(errorToMap(ERR_INVALID_VALUE, QString("/sensors/%1/config").arg(id), QString("invalid value, %1, for parameter battery").arg(map["battery"].toString()))); rsp.httpStatus = HttpStatusBadRequest; @@ -974,7 +933,7 @@ int DeRestPluginPrivate::changeSensorConfig(const ApiRequest &req, ApiResponse & } rspItemState[QString("/sensors/%1/config/battery").arg(id)] = map["battery"]; rspItem["success"] = rspItemState; - config.setBattery(map["battery"].toString()); + config.setBattery(battery); } if (map.contains("long")) { @@ -1470,9 +1429,9 @@ bool DeRestPluginPrivate::sensorToMap(const Sensor *sensor, QVariantMap &map) config["reachable"] = sensor->config().reachable(); } - if (sensor->config().battery() != "" ) + if (sensor->config().battery() <= 100) { - config["battery"] = sensor->config().battery().toUInt(); + config["battery"] = (double)sensor->config().battery(); } if (sensor->config().url() != "" ) { diff --git a/sensor.cpp b/sensor.cpp index a5eadd1..5d6c7af 100644 --- a/sensor.cpp +++ b/sensor.cpp @@ -327,13 +327,23 @@ SensorState Sensor::jsonToState(const QString &json) SensorConfig Sensor::jsonToConfig(const QString &json) { bool ok; - QVariant var = (Json::parse(json, ok)); - QVariantMap map = var.toMap(); SensorConfig config; + QVariant var = Json::parse(json, ok); + + if (!ok) + { + return config; + } + QVariantMap map = var.toMap(); config.setOn(map["on"].toBool()); config.setReachable(map["reachable"].toBool()); - config.setBattery(map["battery"].toString()); + + uint battery = map["battery"].toUInt(&ok); + if (ok) + { + config.setBattery(battery); + } config.setUrl(map["url"].toString()); config.setLongitude(map["long"].toString()); config.setLat(map["lat"].toString()); @@ -556,7 +566,7 @@ SensorConfig::SensorConfig() : m_on(true), m_reachable(false), m_duration(-1), - m_battery(""), + m_battery(255), // invalid m_url(""), m_long(""), m_lat(""), @@ -623,7 +633,7 @@ void SensorConfig::setDuration(double duration) /*! Returns the sensor config battery attribute. Sensortypes: all CLIP, Generic, General sensors */ -const QString &SensorConfig::battery() const +quint8 SensorConfig::battery() const { return m_battery; } @@ -632,7 +642,7 @@ const QString &SensorConfig::battery() const Sensortypes: all CLIP, Generic, General sensors \param battery the sensor config battery */ -void SensorConfig::setBattery(const QString &battery) +void SensorConfig::setBattery(quint8 battery) { m_battery = battery; } diff --git a/sensor.h b/sensor.h index f64a6c5..6f0ec82 100644 --- a/sensor.h +++ b/sensor.h @@ -72,8 +72,8 @@ class SensorConfig void setReachable(bool reachable); double duration() const; void setDuration(double duration); - const QString &battery() const; - void setBattery(const QString &battery); + quint8 battery() const; + void setBattery(quint8 battery); const QString &url() const; void setUrl(const QString &url); const QString &longitude() const; @@ -89,7 +89,7 @@ class SensorConfig bool m_on; bool m_reachable; double m_duration; - QString m_battery; //uint8 + quint8 m_battery; QString m_url; QString m_long; QString m_lat;