added http put device and fixed bugs in the post device

master
Bernhard Trinnes 2017-10-17 17:21:44 +02:00 committed by Michael Zanetti
parent 17f5ebc6e0
commit 06046b21b0
2 changed files with 111 additions and 21 deletions

View File

@ -34,13 +34,15 @@ DeviceManager::HardwareResources DevicePluginHttpCommander::requiredHardware() c
DeviceManager::DeviceSetupStatus DevicePluginHttpCommander::setupDevice(Device *device) DeviceManager::DeviceSetupStatus DevicePluginHttpCommander::setupDevice(Device *device)
{ {
if (device->deviceClassId() == httpGetDeviceClassId) { if ((device->deviceClassId() == httpGetDeviceClassId) || (device->deviceClassId() == httpPostDeviceClassId) || (device->deviceClassId() == httpPutDeviceClassId)) {
QUrl url = device->paramValue(urlParamTypeId).toUrl();
if (!url.isValid()) {
qDebug(dcHttpCommander()) << "Given URL is not valid";
}
return DeviceManager::DeviceSetupStatusSuccess; return DeviceManager::DeviceSetupStatusSuccess;
} }
if (device->deviceClassId() == httpPostDeviceClassId) {
return DeviceManager::DeviceSetupStatusSuccess;
}
return DeviceManager::DeviceSetupStatusFailure; return DeviceManager::DeviceSetupStatusFailure;
} }
@ -56,6 +58,10 @@ void DevicePluginHttpCommander::postSetupDevice(Device *device)
QNetworkReply *reply = networkManagerGet(request);; QNetworkReply *reply = networkManagerGet(request);;
m_httpRequests.insert(reply, device); m_httpRequests.insert(reply, device);
} }
if ((device->deviceClassId() == httpPostDeviceClassId) || (device->deviceClassId() == httpPutDeviceClassId)) {
//TODO find a way to check it the URL is reachable
device->setStateValue(reachableStateTypeId, true);
}
} }
@ -77,6 +83,20 @@ DeviceManager::DeviceError DevicePluginHttpCommander::executeAction(Device *devi
return DeviceManager::DeviceErrorActionTypeNotFound; return DeviceManager::DeviceErrorActionTypeNotFound;
} }
if (device->deviceClassId() == httpPutDeviceClassId) {
// check if this is the "press" action
if (action.actionTypeId() == putActionTypeId) {
QUrl url = device->paramValue(urlParamTypeId).toUrl();
url.setPort(device->paramValue(portParamTypeId).toInt());
QByteArray payload = action.param(putDataParamTypeId).value().toByteArray();
QNetworkReply *reply = networkManagerPut(QNetworkRequest(url), payload);
m_httpRequests.insert(reply, device);
return DeviceManager::DeviceErrorNoError;
}
}
return DeviceManager::DeviceErrorDeviceClassNotFound; return DeviceManager::DeviceErrorDeviceClassNotFound;
} }
@ -116,8 +136,7 @@ void DevicePluginHttpCommander::networkManagerReplyReady(QNetworkReply *reply)
Device *device = m_httpRequests.take(reply); Device *device = m_httpRequests.take(reply);
if (device->deviceClassId() == httpGetDeviceClassId) { if (device->deviceClassId() == httpGetDeviceClassId) {
device->setStateValue(httpResponseStateTypeId, data); device->setStateValue(getDataStateTypeId, data);
//device->setStateValue(statusStateTypeId, reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString());
// check HTTP status code // check HTTP status code
if (status != 200 || reply->error() != QNetworkReply::NoError) { if (status != 200 || reply->error() != QNetworkReply::NoError) {
qCWarning(dcHttpCommander()) << "Request error:" << status << reply->errorString(); qCWarning(dcHttpCommander()) << "Request error:" << status << reply->errorString();
@ -126,8 +145,8 @@ void DevicePluginHttpCommander::networkManagerReplyReady(QNetworkReply *reply)
return; return;
} }
device->setStateValue(reachableStateTypeId, true); device->setStateValue(reachableStateTypeId, true);
} else if (device->deviceClassId() == httpPostDeviceClassId) { } else if ((device->deviceClassId() == httpPostDeviceClassId) || (device->deviceClassId() == httpPutDeviceClassId) ) {
device->setStateValue(httpResponseStateTypeId, data);
// check HTTP status code // check HTTP status code
if (status != 200 || reply->error() != QNetworkReply::NoError) { if (status != 200 || reply->error() != QNetworkReply::NoError) {
qCWarning(dcHttpCommander()) << "Request error:" << status << reply->errorString(); qCWarning(dcHttpCommander()) << "Request error:" << status << reply->errorString();

View File

@ -17,7 +17,7 @@
"basicTags": [ "basicTags": [
"Service" "Service"
], ],
"primaryStateTypeId": "137ec5fc-22f5-4201-8921-2896deda0fe6", "criticalStateTypeId": "8daac0e7-4c2f-4cdf-b528-02cfe04c6b39",
"paramTypes": [ "paramTypes": [
{ {
"id": "1a3fcb23-931b-4ba1-b134-c49b656c76f7", "id": "1a3fcb23-931b-4ba1-b134-c49b656c76f7",
@ -42,7 +42,15 @@
"name": "reachable", "name": "reachable",
"type": "bool", "type": "bool",
"defaultValue": false, "defaultValue": false,
"eventTypeName": "reachable status changed" "eventTypeName": "reachability changed"
},
{
"id": "69f32ec8-114d-43f4-9241-1f6a57261f32",
"idName": "httpResponse",
"name": "response",
"type": "QString",
"defaultValue": "",
"eventTypeName": "response received"
} }
], ],
"actionTypes": [ "actionTypes": [
@ -63,6 +71,69 @@
} }
] ]
}, },
{
"id": "05bf65f5-ff13-43e3-b6ae-77019e79d8a1",
"idName": "httpPut",
"name": "http put",
"deviceIcon": "Network",
"createMethods": ["user"],
"basicTags": [
"Service"
],
"criticalStateTypeId": "8daac0e7-4c2f-4cdf-b528-02cfe04c6b39",
"paramTypes": [
{
"id": "1a3fcb23-931b-4ba1-b134-c49b656c76f7",
"idName": "url",
"name": "url or ipv4 address",
"type": "QString",
"inputType": "None",
"defaultValue": "http://nymea.io"
},
{
"id": "bee8b151-815a-4159-9d8a-42b76e99b42c",
"idName": "port",
"name": "port",
"type": "int",
"defaultValue": "80"
}
],
"stateTypes": [
{
"id": "8daac0e7-4c2f-4cdf-b528-02cfe04c6b39",
"idName": "reachable",
"name": "reachable",
"type": "bool",
"defaultValue": false,
"eventTypeName": "reachability changed"
},
{
"id": "69f32ec8-114d-43f4-9241-1f6a57261f32",
"idName": "httpResponse",
"name": "response",
"type": "QString",
"defaultValue": "",
"eventTypeName": "response received"
}
],
"actionTypes": [
{
"id": "a9f165dc-cdf1-48f0-b4b6-7c24373cb77c",
"idName": "put",
"name": "put",
"paramTypes": [
{
"id": "7742d445-8fc1-4b20-87f2-1bb35929fce1",
"idName": "putData",
"name": "put data",
"type": "QString",
"defaultValue": "",
"eventTypeName": "post data sent"
}
]
}
]
},
{ {
"id": "8f3f6dde-9db3-4237-800b-bb7f804098c9", "id": "8f3f6dde-9db3-4237-800b-bb7f804098c9",
"idName": "httpGet", "idName": "httpGet",
@ -72,7 +143,7 @@
"basicTags": [ "basicTags": [
"Service" "Service"
], ],
"primaryStateTypeId": "137ec5fc-22f5-4201-8921-2896deda0fe6", "criticalStateTypeId": "8daac0e7-4c2f-4cdf-b528-02cfe04c6b39",
"paramTypes": [ "paramTypes": [
{ {
"id": "1a3fcb23-931b-4ba1-b134-c49b656c76f7", "id": "1a3fcb23-931b-4ba1-b134-c49b656c76f7",
@ -92,20 +163,20 @@
], ],
"stateTypes":[ "stateTypes":[
{ {
"id": "137ec5fc-22f5-4201-8921-2896deda0fe6", "id": "8daac0e7-4c2f-4cdf-b528-02cfe04c6b39",
"idName": "status", "idName": "reachable",
"name": "status", "name": "reachable",
"type": "QString", "type": "bool",
"defaultValue": "Undefined", "defaultValue": false,
"eventTypeName": "connection status changed" "eventTypeName": "reachability changed"
}, },
{ {
"id": "5a97ca56-b334-411b-adba-116496ffe83d", "id": "d81f0644-b94e-48ed-ae48-1b8ff6cebc0c",
"idName": "httpResponse", "idName": "getData",
"name": "response", "name": "data",
"type": "QString", "type": "QString",
"defaultValue": "", "defaultValue": "",
"eventTypeName": "response received" "eventTypeName": "get data received"
} }
] ]
} }