Update dweet.io plugin
This commit is contained in:
parent
2b099ecdd7
commit
0283001acb
@ -49,22 +49,20 @@ void DevicePluginDweetio::init()
|
|||||||
connect(m_pluginTimer, &PluginTimer::timeout, this, &DevicePluginDweetio::onPluginTimer);
|
connect(m_pluginTimer, &PluginTimer::timeout, this, &DevicePluginDweetio::onPluginTimer);
|
||||||
}
|
}
|
||||||
|
|
||||||
Device::DeviceSetupStatus DevicePluginDweetio::setupDevice(Device *device)
|
void DevicePluginDweetio::setupDevice(DeviceSetupInfo *info)
|
||||||
{
|
{
|
||||||
|
if (info->device()->deviceClassId() == postDeviceClassId) {
|
||||||
if (device->deviceClassId() == postDeviceClassId) {
|
QString thing = info->device()->paramValue(postDeviceThingParamTypeId).toString();
|
||||||
|
|
||||||
QString thing = device->paramValue(postDeviceThingParamTypeId).toString();
|
|
||||||
if (thing.isEmpty()){
|
if (thing.isEmpty()){
|
||||||
qDebug(dcDweetio) << "No thing name given, creating one";
|
qDebug(dcDweetio) << "No thing name given, creating one";
|
||||||
thing = QUuid::createUuid().toString();
|
thing = QUuid::createUuid().toString();
|
||||||
}
|
}
|
||||||
return Device::DeviceSetupStatusSuccess;
|
return info->finish(Device::DeviceErrorNoError);
|
||||||
} else if (device->deviceClassId() == getDeviceClassId) {
|
} else if (info->device()->deviceClassId() == getDeviceClassId) {
|
||||||
|
return info->finish(Device::DeviceErrorNoError);
|
||||||
return Device::DeviceSetupStatusSuccess;
|
|
||||||
}
|
}
|
||||||
return Device::DeviceSetupStatusFailure;
|
|
||||||
|
info->finish(Device::DeviceErrorDeviceClassNotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DevicePluginDweetio::postSetupDevice(Device *device)
|
void DevicePluginDweetio::postSetupDevice(Device *device)
|
||||||
@ -93,8 +91,10 @@ void DevicePluginDweetio::deviceRemoved(Device *device)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Device::DeviceError DevicePluginDweetio::executeAction(Device *device, const Action &action)
|
void DevicePluginDweetio::executeAction(DeviceActionInfo *info)
|
||||||
{
|
{
|
||||||
|
Device *device = info->device();
|
||||||
|
Action action = info->action();
|
||||||
qCDebug(dcDweetio) << "Execute action" << device->id() << action.id() << action.params();
|
qCDebug(dcDweetio) << "Execute action" << device->id() << action.id() << action.params();
|
||||||
|
|
||||||
if (device->deviceClassId() == postDeviceClassId) {
|
if (device->deviceClassId() == postDeviceClassId) {
|
||||||
@ -102,16 +102,15 @@ Device::DeviceError DevicePluginDweetio::executeAction(Device *device, const Act
|
|||||||
if (action.actionTypeId() == postContentDataActionTypeId) {
|
if (action.actionTypeId() == postContentDataActionTypeId) {
|
||||||
|
|
||||||
QString content = action.param(postContentDataActionContentDataAreaParamTypeId).value().toString();
|
QString content = action.param(postContentDataActionContentDataAreaParamTypeId).value().toString();
|
||||||
postContent(content, device, action);
|
postContent(content, device, info);
|
||||||
|
return;
|
||||||
return Device::DeviceErrorAsync;
|
|
||||||
}
|
}
|
||||||
return Device::DeviceErrorActionTypeNotFound;
|
return info->finish(Device::DeviceErrorActionTypeNotFound);
|
||||||
}
|
}
|
||||||
return Device::DeviceErrorDeviceClassNotFound;
|
return info->finish(Device::DeviceErrorDeviceClassNotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DevicePluginDweetio::postContent(const QString &content, Device *device, const Action &action)
|
void DevicePluginDweetio::postContent(const QString &content, Device *device, DeviceActionInfo *info)
|
||||||
{
|
{
|
||||||
QUrl url = QString("https://dweet.io:443/dweet/for/") + device->paramValue(postDeviceThingParamTypeId).toString();
|
QUrl url = QString("https://dweet.io:443/dweet/for/") + device->paramValue(postDeviceThingParamTypeId).toString();
|
||||||
QNetworkRequest request(url);
|
QNetworkRequest request(url);
|
||||||
@ -133,7 +132,9 @@ void DevicePluginDweetio::postContent(const QString &content, Device *device, co
|
|||||||
qDebug(dcDweetio) << "Dweet: " << data << "Url: " << url;
|
qDebug(dcDweetio) << "Dweet: " << data << "Url: " << url;
|
||||||
|
|
||||||
QNetworkReply *reply = hardwareManager()->networkManager()->post(request, data);
|
QNetworkReply *reply = hardwareManager()->networkManager()->post(request, data);
|
||||||
m_asyncActions.insert(reply, action.id());
|
m_asyncActions.insert(reply, info);
|
||||||
|
// In case the action is cancelled before the reply returns
|
||||||
|
connect(info, &DeviceActionInfo::destroyed, this, [this, reply](){ m_asyncActions.remove(reply); });
|
||||||
connect(reply, &QNetworkReply::finished, this, &DevicePluginDweetio::onNetworkReplyFinished);
|
connect(reply, &QNetworkReply::finished, this, &DevicePluginDweetio::onNetworkReplyFinished);
|
||||||
m_postReplies.insert(reply, device);
|
m_postReplies.insert(reply, device);
|
||||||
|
|
||||||
@ -169,18 +170,18 @@ void DevicePluginDweetio::onNetworkReplyFinished()
|
|||||||
if (m_postReplies.contains(reply)) {
|
if (m_postReplies.contains(reply)) {
|
||||||
Device *device = m_postReplies.value(reply);
|
Device *device = m_postReplies.value(reply);
|
||||||
m_postReplies.remove(reply);
|
m_postReplies.remove(reply);
|
||||||
ActionId actionId = m_asyncActions.value(reply);
|
DeviceActionInfo *info = m_asyncActions.take(reply);
|
||||||
|
|
||||||
// check HTTP status code
|
// check HTTP status code
|
||||||
if ((status != 200) && (status != 204)) {
|
if ((status != 200) && (status != 204)) {
|
||||||
qCWarning(dcDweetio) << "Update reply HTTP error:" << status << reply->errorString() << reply->readAll();
|
qCWarning(dcDweetio) << "Update reply HTTP error:" << status << reply->errorString() << reply->readAll();
|
||||||
emit actionExecutionFinished(actionId, Device::DeviceErrorMissingParameter);
|
info->finish(Device::DeviceErrorMissingParameter);
|
||||||
setConnectionStatus(false, device);
|
setConnectionStatus(false, device);
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
emit actionExecutionFinished(actionId, Device::DeviceErrorNoError);
|
info->finish(Device::DeviceErrorNoError);
|
||||||
setConnectionStatus(true, device);
|
setConnectionStatus(true, device);
|
||||||
if (status == 204){
|
if (status == 204){
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
|
|||||||
@ -45,9 +45,9 @@ public:
|
|||||||
~DevicePluginDweetio();
|
~DevicePluginDweetio();
|
||||||
|
|
||||||
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;
|
||||||
Device::DeviceError executeAction(Device *device, const Action &action) override;
|
void executeAction(DeviceActionInfo *info) override;
|
||||||
void postSetupDevice(Device *device) override;
|
void postSetupDevice(Device *device) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -55,9 +55,9 @@ private:
|
|||||||
QHash<QNetworkReply *, Device *> m_postReplies;
|
QHash<QNetworkReply *, Device *> m_postReplies;
|
||||||
QHash<QNetworkReply *, Device *> m_getReplies;
|
QHash<QNetworkReply *, Device *> m_getReplies;
|
||||||
|
|
||||||
QHash<QNetworkReply *, ActionId> m_asyncActions;
|
QHash<QNetworkReply *, DeviceActionInfo*> m_asyncActions;
|
||||||
|
|
||||||
void postContent(const QString &content, Device *device, const Action &action);
|
void postContent(const QString &content, Device *device, DeviceActionInfo *info);
|
||||||
void processPostReply(const QVariantMap &data, Device *device);
|
void processPostReply(const QVariantMap &data, Device *device);
|
||||||
void processGetReply(const QVariantMap &data, Device *device);
|
void processGetReply(const QVariantMap &data, Device *device);
|
||||||
void setConnectionStatus(bool status, Device *device);
|
void setConnectionStatus(bool status, Device *device);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user