Skip to content

Commit

Permalink
Use correct datatype and value range for sensor battery parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
manup committed Apr 26, 2016
1 parent fcfbab1 commit 2b5fd8d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 63 deletions.
39 changes: 39 additions & 0 deletions de_web_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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:
Expand Down
67 changes: 13 additions & 54 deletions rest_sensors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() != "" )
Expand Down Expand Up @@ -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() != "" )
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -966,15 +923,17 @@ 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;
return REQ_READY_SEND;
}
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"))
{
Expand Down Expand Up @@ -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() != "" )
{
Expand Down
22 changes: 16 additions & 6 deletions sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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(""),
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 2b5fd8d

Please sign in to comment.