Update osdomotics plugin

This commit is contained in:
Michael Zanetti 2019-09-19 13:13:32 +02:00
parent 313a38da5a
commit bc3d2f3b4a
2 changed files with 63 additions and 60 deletions

View File

@ -67,15 +67,18 @@ void DevicePluginOsdomotics::init()
connect(m_pluginTimer, &PluginTimer::timeout, this, &DevicePluginOsdomotics::onPluginTimer); connect(m_pluginTimer, &PluginTimer::timeout, this, &DevicePluginOsdomotics::onPluginTimer);
} }
Device::DeviceSetupStatus DevicePluginOsdomotics::setupDevice(Device *device) void DevicePluginOsdomotics::setupDevice(DeviceSetupInfo *info)
{ {
Device *device = info->device();
if (device->deviceClassId() == rplRouterDeviceClassId) { if (device->deviceClassId() == rplRouterDeviceClassId) {
qCDebug(dcOsdomotics) << "Setup RPL router" << device->paramValue(rplRouterDeviceRplHostParamTypeId).toString(); qCDebug(dcOsdomotics) << "Setup RPL router" << device->paramValue(rplRouterDeviceRplHostParamTypeId).toString();
QHostAddress address(device->paramValue(rplRouterDeviceRplHostParamTypeId).toString()); QHostAddress address(device->paramValue(rplRouterDeviceRplHostParamTypeId).toString());
if (address.isNull()) { if (address.isNull()) {
qCWarning(dcOsdomotics) << "Got invalid address" << device->paramValue(rplRouterDeviceRplHostParamTypeId).toString(); qCWarning(dcOsdomotics) << "Got invalid address" << device->paramValue(rplRouterDeviceRplHostParamTypeId).toString();
return Device::DeviceSetupStatusFailure; //: Error setting up device
return info->finish(Device::DeviceErrorInvalidParameter, QT_TR_NOOP("The given RPL address is not valid."));
} }
QUrl url; QUrl url;
@ -83,16 +86,32 @@ Device::DeviceSetupStatus DevicePluginOsdomotics::setupDevice(Device *device)
url.setHost(address.toString()); url.setHost(address.toString());
QNetworkReply *reply = hardwareManager()->networkManager()->get(QNetworkRequest(url)); QNetworkReply *reply = hardwareManager()->networkManager()->get(QNetworkRequest(url));
connect(reply, &QNetworkReply::finished, this, &DevicePluginOsdomotics::onNetworkReplyFinished); connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
m_asyncSetup.insert(reply, device);
return Device::DeviceSetupStatusAsync; connect(reply, &QNetworkReply::finished, info, [this, info, reply](){
} else if (device->deviceClassId() == merkurNodeDeviceClassId) { // check HTTP status code
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (status != 200) {
qCWarning(dcOsdomotics) << "Setup reply HTTP error:" << reply->errorString();
//: Error setting up device
info->finish(Device::DeviceErrorHardwareFailure, QT_TR_NOOP("Error communicating with RPL device."));
return;
}
QByteArray data = reply->readAll();
parseNodes(info->device(), data);
info->finish(Device::DeviceErrorNoError);
});
return;
}
if (device->deviceClassId() == merkurNodeDeviceClassId) {
qCDebug(dcOsdomotics) << "Setup Merkur node" << device->paramValue(merkurNodeDeviceHostParamTypeId).toString(); qCDebug(dcOsdomotics) << "Setup Merkur node" << device->paramValue(merkurNodeDeviceHostParamTypeId).toString();
device->setParentId(DeviceId(device->paramValue(merkurNodeDeviceRouterParamTypeId).toString())); device->setParentId(DeviceId(device->paramValue(merkurNodeDeviceRouterParamTypeId).toString()));
return Device::DeviceSetupStatusSuccess; return info->finish(Device::DeviceErrorNoError);
} }
return Device::DeviceSetupStatusFailure;
} }
void DevicePluginOsdomotics::deviceRemoved(Device *device) void DevicePluginOsdomotics::deviceRemoved(Device *device)
@ -105,34 +124,46 @@ void DevicePluginOsdomotics::postSetupDevice(Device *device)
updateNode(device); updateNode(device);
} }
Device::DeviceError DevicePluginOsdomotics::executeAction(Device *device, const Action &action) void DevicePluginOsdomotics::executeAction(DeviceActionInfo *info)
{ {
if (device->deviceClassId() == merkurNodeDeviceClassId) { Device *device = info->device();
if (action.actionTypeId() == merkurNodeToggleLedActionTypeId) { Action action = info->action();
QUrl url;
url.setScheme("coap");
url.setHost(device->paramValue(merkurNodeDeviceHostParamTypeId).toString());
url.setPath("/actuators/toggle");
qCDebug(dcOsdomotics) << "Toggle light"; if (action.actionTypeId() == merkurNodeToggleLedActionTypeId) {
QUrl url;
url.setScheme("coap");
url.setHost(device->paramValue(merkurNodeDeviceHostParamTypeId).toString());
url.setPath("/actuators/toggle");
CoapReply *reply = m_coap->post(CoapRequest(url)); qCDebug(dcOsdomotics) << "Toggle light";
if (reply->isFinished()) { CoapReply *reply = m_coap->post(CoapRequest(url));
if (reply->error() != CoapReply::NoError) {
qCWarning(dcOsdomotics) << "CoAP reply finished with error" << reply->errorString(); if (reply->isFinished()) {
reply->deleteLater(); if (reply->error() != CoapReply::NoError) {
return Device::DeviceErrorHardwareNotAvailable; qCWarning(dcOsdomotics) << "CoAP reply finished with error" << reply->errorString();
} reply->deleteLater();
return info->finish(Device::DeviceErrorHardwareNotAvailable);
}
return info->finish(Device::DeviceErrorNoError);
}
connect(reply, &CoapReply::finished, reply, &CoapReply::deleteLater);
connect(reply, &CoapReply::finished, info, [info, reply](){
if (reply->error() != CoapReply::NoError) {
qCWarning(dcOsdomotics) << "CoAP toggle reply finished with error" << reply->errorString();
info->finish(Device::DeviceErrorHardwareFailure);
return;
} }
m_toggleLightRequests.insert(reply, action); info->finish(Device::DeviceErrorNoError);
});
return Device::DeviceErrorAsync; return;
}
return Device::DeviceErrorActionTypeNotFound;
} }
return Device::DeviceErrorDeviceClassNotFound;
qCWarning(dcOsdomotics()) << "Unhandled executeAction in plugin!";
} }
void DevicePluginOsdomotics::scanNodes(Device *device) void DevicePluginOsdomotics::scanNodes(Device *device)
@ -236,29 +267,12 @@ void DevicePluginOsdomotics::onNetworkReplyFinished()
QNetworkReply *reply = static_cast<QNetworkReply *>(sender()); QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
// create user finished if (m_asyncNodeRescans.contains(reply)) {
if (m_asyncSetup.contains(reply)) {
Device *device = m_asyncSetup.take(reply); Device *device = m_asyncSetup.take(reply);
// check HTTP status code // check HTTP status code
if (status != 200) { if (status != 200) {
qCWarning(dcOsdomotics) << "Setup reply HTTP error:" << reply->errorString(); qCWarning(dcOsdomotics) << "Setup reply HTTP error:" << reply->errorString();
emit deviceSetupFinished(device, Device::DeviceSetupStatusFailure);
reply->deleteLater();
return;
}
QByteArray data = reply->readAll();
parseNodes(device, data);
emit deviceSetupFinished(device, Device::DeviceSetupStatusSuccess);
} else if (m_asyncNodeRescans.contains(reply)) {
Device *device = m_asyncSetup.take(reply);
// check HTTP status code
if (status != 200) {
qCWarning(dcOsdomotics) << "Setup reply HTTP error:" << reply->errorString();
emit deviceSetupFinished(device, Device::DeviceSetupStatusFailure);
reply->deleteLater(); reply->deleteLater();
return; return;
} }
@ -289,18 +303,7 @@ void DevicePluginOsdomotics::coapReplyFinished(CoapReply *reply)
params.append(Param(merkurNodeDeviceHostParamTypeId, reply->request().url().host())); params.append(Param(merkurNodeDeviceHostParamTypeId, reply->request().url().host()));
params.append(Param(merkurNodeDeviceRouterParamTypeId, device->id())); params.append(Param(merkurNodeDeviceRouterParamTypeId, device->id()));
descriptor.setParams(params); descriptor.setParams(params);
emit autoDevicesAppeared(merkurNodeDeviceClassId, QList<DeviceDescriptor>() << descriptor); emit autoDevicesAppeared({descriptor});
} else if (m_toggleLightRequests.contains(reply)) {
Action action = m_toggleLightRequests.take(reply);
if (reply->error() != CoapReply::NoError) {
qCWarning(dcOsdomotics) << "CoAP toggle reply finished with error" << reply->errorString();
reply->deleteLater();
emit actionExecutionFinished(action.id(), Device::DeviceErrorHardwareFailure);
return;
}
emit actionExecutionFinished(action.id(), Device::DeviceErrorNoError);
} else if (m_updateRequests.contains(reply)) { } else if (m_updateRequests.contains(reply)) {
Device *device = m_updateRequests.take(reply); Device *device = m_updateRequests.take(reply);
if (reply->error() != CoapReply::NoError) { if (reply->error() != CoapReply::NoError) {

View File

@ -43,10 +43,10 @@ public:
~DevicePluginOsdomotics(); ~DevicePluginOsdomotics();
void init() override; void init() override;
Device::DeviceSetupStatus setupDevice(Device *device) override; void setupDevice(DeviceSetupInfo *info) override;
void deviceRemoved(Device *device) override; void deviceRemoved(Device *device) override;
void postSetupDevice(Device *device) override; void postSetupDevice(Device *device) override;
Device::DeviceError executeAction(Device *device, const Action &action) override; void executeAction(DeviceActionInfo *info) override;
private: private:
PluginTimer *m_pluginTimer = nullptr; PluginTimer *m_pluginTimer = nullptr;