small fixes in plantcare and orderbutton

This commit is contained in:
Simon Stürz 2016-04-12 15:00:10 +02:00 committed by Michael Zanetti
parent 4f36f7c543
commit 2ddf19b6da
5 changed files with 31 additions and 24 deletions

View File

@ -174,9 +174,9 @@ DeviceManager::DeviceError DevicePluginOrderButton::executeAction(Device *device
QUrl url;
url.setScheme("coap");
url.setHost(device->paramValue("host").toString());
url.setPath("/a/reset");
url.setPath("/s/count");
CoapReply *reply = m_coap->post(CoapRequest(url));
CoapReply *reply = m_coap->post(CoapRequest(url), QByteArray("count=0"));
if (reply->isFinished() && reply->error() != CoapReply::NoError) {
qCWarning(dcOrderButton) << "CoAP reply finished with error" << reply->errorString();
setReachable(device, false);

View File

@ -14,7 +14,7 @@
{
"name": "guh",
"idName": "guh",
"id": "76df1ad2-c179-4842-94ea-2da6e4796339",
"id": "2062d64d-3232-433c-88bc-0d33c0ba2ba6",
"deviceClasses": [
{
"deviceClassId": "af4df281-2b3b-490f-8352-2b99ff23fc15",

View File

@ -189,15 +189,15 @@ DeviceManager::DeviceError DevicePluginPlantCare::executeAction(Device *device,
m_asyncActions.insert(action.id(), device);
return DeviceManager::DeviceErrorAsync;
} else if(action.actionTypeId() == brightnessActionTypeId) {
int light = qRound(action.param("brightness").value().toInt() * 255.0 / 100.0);
} else if(action.actionTypeId() == ledPowerActionTypeId) {
int power = action.param("led power").value().toInt();
QUrl url;
url.setScheme("coap");
url.setHost(device->paramValue("host").toString());
url.setPath("/a/light");
QByteArray payload = QString("pwm=%1").arg(QString::number(light)).toUtf8();
QByteArray payload = QString("pwm=%1").arg(QString::number(power)).toUtf8();
qCDebug(dcPlantCare()) << "Sending" << payload << url.path();
CoapReply *reply = m_coap->post(CoapRequest(url), payload);
if (reply->isFinished() && reply->error() != CoapReply::NoError) {
@ -207,7 +207,7 @@ DeviceManager::DeviceError DevicePluginPlantCare::executeAction(Device *device,
return DeviceManager::DeviceErrorHardwareFailure;
}
m_setBrightness.insert(reply, action);
m_setLedPower.insert(reply, action);
m_asyncActions.insert(action.id(), device);
return DeviceManager::DeviceErrorAsync;
@ -451,8 +451,13 @@ void DevicePluginPlantCare::coapReplyFinished(CoapReply *reply)
qCDebug(dcPlantCare()) << "Updated pump value:" << reply->payload();
device->setStateValue(waterPumpStateTypeId, QVariant(reply->payload().toInt()).toBool());
} else if (urlPath == "/a/light") {
qCDebug(dcPlantCare()) << "Updated brightness value:" << reply->payload();
device->setStateValue(brightnessStateTypeId, qRound(reply->payload().toInt() * 100.0 / 255.0));
qCDebug(dcPlantCare()) << "Updated led power value:" << reply->payload();
int powerValue = reply->payload().toInt();
if (powerValue > 0) {
device->setStateValue(ledPowerStateTypeId, false);
} else {
device->setStateValue(ledPowerStateTypeId, true);
}
}
} else if (m_toggleLightRequests.contains(reply)) {
@ -478,13 +483,13 @@ void DevicePluginPlantCare::coapReplyFinished(CoapReply *reply)
// Tell the user about the action execution result
emit actionExecutionFinished(action.id(), DeviceManager::DeviceErrorNoError);
} else if (m_setBrightness.contains(reply)) {
Action action = m_setBrightness.take(reply);
} else if (m_setLedPower.contains(reply)) {
Action action = m_setLedPower.take(reply);
Device *device = m_asyncActions.take(action.id());
// check CoAP reply error
if (reply->error() != CoapReply::NoError) {
qCWarning(dcPlantCare) << "CoAP set brightness reply finished with error" << reply->errorString();
qCWarning(dcPlantCare) << "CoAP set led power reply finished with error" << reply->errorString();
setReachable(device, false);
reply->deleteLater();
emit actionExecutionFinished(action.id(), DeviceManager::DeviceErrorHardwareFailure);
@ -493,14 +498,14 @@ void DevicePluginPlantCare::coapReplyFinished(CoapReply *reply)
// Check CoAP status code
if (reply->statusCode() != CoapPdu::Content) {
qCWarning(dcPlantCare) << "Set brightness status code error:" << reply;
qCWarning(dcPlantCare) << "Set led power status code error:" << reply;
reply->deleteLater();
emit actionExecutionFinished(action.id(), DeviceManager::DeviceErrorHardwareFailure);
return;
}
// Update the state here, so we don't have to wait for the notification
device->setStateValue(brightnessStateTypeId, action.param("brightness").value().toInt());
device->setStateValue(ledPowerStateTypeId, action.param("led power").value().toBool());
// Tell the user about the action execution result
emit actionExecutionFinished(action.id(), DeviceManager::DeviceErrorNoError);
@ -574,6 +579,11 @@ void DevicePluginPlantCare::onNotificationReceived(const CoapObserveResource &re
} else if (resource.url().path() == "/a/pump") {
device->setStateValue(waterPumpStateTypeId, QVariant(payload.toInt()).toBool());
} else if (resource.url().path() == "/a/light") {
device->setStateValue(brightnessStateTypeId, qRound(payload.toInt() * 100.0 / 255.0));
int powerValue = QVariant(payload).toInt();
if (powerValue > 0) {
device->setStateValue(ledPowerStateTypeId, false);
} else {
device->setStateValue(ledPowerStateTypeId, true);
}
}
}

View File

@ -61,7 +61,7 @@ private:
QHash<ActionId, Device *> m_asyncActions;
QHash<CoapReply *, Action> m_toggleLightRequests;
QHash<CoapReply *, Action> m_setBrightness;
QHash<CoapReply *, Action> m_setLedPower;
QHash<CoapReply *, Action> m_setPumpPower;
void pingDevice(Device *device);

View File

@ -57,14 +57,11 @@
"defaultValue": false
},
{
"id": "21b51fc9-1bfd-4970-a50d-e697a58df1bf",
"idName": "brightness",
"name": "brightness",
"unit": "Percentage",
"type": "int",
"minValue": 0,
"maxValue": 100,
"defaultValue": 0,
"id": "819aca98-25e5-4733-81e0-5921478b3e89",
"idName": "ledPower",
"name": "led power",
"type": "bool",
"defaultValue": false,
"writable": true
},
{