Updated pushbullet plugin
This commit is contained in:
parent
6bcdc50d13
commit
bf71132faa
@ -37,18 +37,46 @@ DevicePluginPushbullet::~DevicePluginPushbullet()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Device::DeviceSetupStatus DevicePluginPushbullet::setupDevice(Device *device)
|
void DevicePluginPushbullet::setupDevice(DeviceSetupInfo *info)
|
||||||
{
|
{
|
||||||
|
Device *device = info->device();
|
||||||
|
|
||||||
qCDebug(dcPushbullet()) << "Setting up Pushbullet device" << device->name() << device->id().toString();
|
qCDebug(dcPushbullet()) << "Setting up Pushbullet device" << device->name() << device->id().toString();
|
||||||
|
|
||||||
|
QString token;
|
||||||
|
|
||||||
|
// TODO: Activate this and drop the token param from json when maveo is ready for it.
|
||||||
|
// pluginStorage()->beginGroup(info->device()->id().toString());
|
||||||
|
// token = pluginStorage()->value("token").toString();
|
||||||
|
// pluginStorage()->endGroup();
|
||||||
|
|
||||||
|
if (token.isEmpty()) {
|
||||||
|
// Check for legacy token in device params...
|
||||||
|
ParamTypeId pushNotificationDeviceTokenParamTypeId = ParamTypeId("{1f37ce22-50b8-4183-8b74-557fdb2d3076}");
|
||||||
|
token = device->paramValue(pushNotificationDeviceTokenParamTypeId).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (token.isEmpty()) {
|
||||||
|
//: Error setting up device
|
||||||
|
return info->finish(Device::DeviceErrorAuthenticationFailure, QT_TR_NOOP("The provided token must not be empty."));
|
||||||
|
}
|
||||||
|
|
||||||
QNetworkRequest request(QUrl("https://api.pushbullet.com/v2/users/me"));
|
QNetworkRequest request(QUrl("https://api.pushbullet.com/v2/users/me"));
|
||||||
request.setRawHeader("Access-Token", device->paramValue(pushNotificationDeviceTokenParamTypeId).toByteArray().trimmed());
|
request.setRawHeader("Access-Token", token.toUtf8().trimmed());
|
||||||
|
|
||||||
QNetworkReply *reply = hardwareManager()->networkManager()->get(request);
|
QNetworkReply *reply = hardwareManager()->networkManager()->get(request);
|
||||||
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
|
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
|
||||||
connect(reply, &QNetworkReply::finished, device, [this, reply, device]() {
|
|
||||||
|
connect(reply, &QNetworkReply::finished, info, [reply, info]() {
|
||||||
|
if (reply->error() == QNetworkReply::AuthenticationRequiredError) {
|
||||||
|
//: Error setting up device
|
||||||
|
info->finish(Device::DeviceErrorAuthenticationFailure, QT_TR_NOOP("The provided token is not valid."));
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (reply->error() != QNetworkReply::NoError) {
|
if (reply->error() != QNetworkReply::NoError) {
|
||||||
qCWarning(dcPushbullet()) << "Error fetching user profile:" << reply->errorString() << reply->error();
|
qCWarning(dcPushbullet()) << "Error fetching user profile:" << reply->errorString() << reply->error();
|
||||||
emit deviceSetupFinished(device, Device::DeviceSetupStatusFailure);
|
//: Error setting up device
|
||||||
|
info->finish(Device::DeviceErrorHardwareFailure, QT_TR_NOOP("Error connecting to Pushbullet."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,31 +87,51 @@ Device::DeviceSetupStatus DevicePluginPushbullet::setupDevice(Device *device)
|
|||||||
if (error.error != QJsonParseError::NoError) {
|
if (error.error != QJsonParseError::NoError) {
|
||||||
qCWarning(dcPushbullet()) << "Error parsing reply from Pushbullet:" << error.errorString();
|
qCWarning(dcPushbullet()) << "Error parsing reply from Pushbullet:" << error.errorString();
|
||||||
qCWarning(dcPushbullet()) << qUtf8Printable(data);
|
qCWarning(dcPushbullet()) << qUtf8Printable(data);
|
||||||
emit deviceSetupFinished(device, Device::DeviceSetupStatusFailure);
|
//: Error setting up device
|
||||||
|
info->finish(Device::DeviceErrorHardwareFailure, QT_TR_NOOP("Received unexpected data from Pushbullet."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariantMap replyMap = jsonDoc.toVariant().toMap();
|
QVariantMap replyMap = jsonDoc.toVariant().toMap();
|
||||||
if (!replyMap.value("active").toBool()) {
|
if (!replyMap.value("active").toBool()) {
|
||||||
qCWarning(dcPushbullet()) << "Pushbullet account seems to be deactivated.";
|
qCWarning(dcPushbullet()) << "Pushbullet account seems to be deactivated.";
|
||||||
emit deviceSetupFinished(device, Device::DeviceSetupStatusFailure);
|
//: Error setting up device
|
||||||
|
info->finish(Device::DeviceErrorAuthenticationFailure, QT_TR_NOOP("The Pushbullet account seems to be disabled."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
qCDebug(dcPushbullet()) << "Pushbullet device" << device->name() << device->id().toString() << "setup complete";
|
qCDebug(dcPushbullet()) << "Pushbullet device" << info->device()->name() << info->device()->id().toString() << "setup complete";
|
||||||
emit deviceSetupFinished(device, Device::DeviceSetupStatusSuccess);
|
info->finish(Device::DeviceErrorNoError);
|
||||||
});
|
});
|
||||||
|
|
||||||
return Device::DeviceSetupStatusAsync;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Device::DeviceError DevicePluginPushbullet::executeAction(Device *device, const Action &action)
|
void DevicePluginPushbullet::executeAction(DeviceActionInfo *info)
|
||||||
{
|
{
|
||||||
|
Device *device = info->device();
|
||||||
|
Action action = info->action();
|
||||||
|
|
||||||
qCDebug(dcPushbullet()) << "Executing action" << action.actionTypeId() << "for device" << device->name() << device->id().toString();
|
qCDebug(dcPushbullet()) << "Executing action" << action.actionTypeId() << "for device" << device->name() << device->id().toString();
|
||||||
|
|
||||||
|
QString token;
|
||||||
|
|
||||||
|
// TODO: Activate this and drop the token param from json when maveo is ready for it.
|
||||||
|
// pluginStorage()->beginGroup(info->device()->id().toString());
|
||||||
|
// token = pluginStorage()->value("token").toString();
|
||||||
|
// pluginStorage()->endGroup();
|
||||||
|
|
||||||
|
if (token.isEmpty()) {
|
||||||
|
// Check for legacy token in device params...
|
||||||
|
ParamTypeId pushNotificationDeviceTokenParamTypeId = ParamTypeId("{1f37ce22-50b8-4183-8b74-557fdb2d3076}");
|
||||||
|
token = device->paramValue(pushNotificationDeviceTokenParamTypeId).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (token.isEmpty()) {
|
||||||
|
return info->finish(Device::DeviceErrorAuthenticationFailure, QT_TR_NOOP("Not authenticated to Pushbullet."));
|
||||||
|
}
|
||||||
|
|
||||||
QNetworkRequest request(QUrl("https://api.pushbullet.com/v2/pushes"));
|
QNetworkRequest request(QUrl("https://api.pushbullet.com/v2/pushes"));
|
||||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
||||||
request.setRawHeader("Access-Token", device->paramValue(pushNotificationDeviceTokenParamTypeId).toByteArray().trimmed());
|
request.setRawHeader("Access-Token", token.toUtf8().trimmed());
|
||||||
|
|
||||||
QVariantMap params;
|
QVariantMap params;
|
||||||
params.insert("type", "note");
|
params.insert("type", "note");
|
||||||
@ -91,10 +139,10 @@ Device::DeviceError DevicePluginPushbullet::executeAction(Device *device, const
|
|||||||
params.insert("body", action.param(pushNotificationNotifyActionBodyParamTypeId).value().toString());
|
params.insert("body", action.param(pushNotificationNotifyActionBodyParamTypeId).value().toString());
|
||||||
QNetworkReply *reply = hardwareManager()->networkManager()->post(request, QJsonDocument::fromVariant(params).toJson(QJsonDocument::Compact));
|
QNetworkReply *reply = hardwareManager()->networkManager()->post(request, QJsonDocument::fromVariant(params).toJson(QJsonDocument::Compact));
|
||||||
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
|
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
|
||||||
connect(reply, &QNetworkReply::finished, device, [this, reply, device, action]{
|
connect(reply, &QNetworkReply::finished, info, [reply, info]{
|
||||||
if (reply->error() != QNetworkReply::NoError) {
|
if (reply->error() != QNetworkReply::NoError) {
|
||||||
qCWarning(dcPushbullet()) << "Push message sending failed for" << device->name() << device->id() << reply->errorString() << reply->error();
|
qCWarning(dcPushbullet()) << "Push message sending failed for" << info->device()->name() << info->device()->id() << reply->errorString() << reply->error();
|
||||||
emit actionExecutionFinished(action.id(), Device::DeviceErrorHardwareNotAvailable);
|
emit info->finish(Device::DeviceErrorHardwareNotAvailable);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,29 +151,27 @@ Device::DeviceError DevicePluginPushbullet::executeAction(Device *device, const
|
|||||||
QJsonParseError error;
|
QJsonParseError error;
|
||||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
|
||||||
if (error.error != QJsonParseError::NoError) {
|
if (error.error != QJsonParseError::NoError) {
|
||||||
qCWarning(dcPushbullet()) << "Error reading reply from Pushbullet for" << device->name() << device->id().toString() << error.errorString();
|
qCWarning(dcPushbullet()) << "Error reading reply from Pushbullet for" << info->device()->name() << info->device()->id().toString() << error.errorString();
|
||||||
qCWarning(dcPushbullet()) << qUtf8Printable(data);
|
qCWarning(dcPushbullet()) << qUtf8Printable(data);
|
||||||
emit actionExecutionFinished(action.id(), Device::DeviceErrorHardwareFailure);
|
info->finish(Device::DeviceErrorHardwareFailure);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariantMap replyMap = jsonDoc.toVariant().toMap();
|
QVariantMap replyMap = jsonDoc.toVariant().toMap();
|
||||||
if (!replyMap.value("active").toBool()) {
|
if (!replyMap.value("active").toBool()) {
|
||||||
qCWarning(dcPushbullet()) << "Error sending message. The account seems to be deactivated" << device->name() << device->id().toString();
|
qCWarning(dcPushbullet()) << "Error sending message. The account seems to be deactivated" << info->device()->name() << info->device()->id().toString();
|
||||||
emit actionExecutionFinished(action.id(), Device::DeviceErrorHardwareFailure);
|
info->finish(Device::DeviceErrorAuthenticationFailure, QT_TR_NOOP("The Pushbullet account seems to be disabled."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (replyMap.value("dismissed", true).toBool()) {
|
if (replyMap.value("dismissed", true).toBool()) {
|
||||||
qCWarning(dcPushbullet()) << "Error sending message. The message has been dismissed by the server" << device->name() << device->id().toString();
|
qCWarning(dcPushbullet()) << "Error sending message. The message has been dismissed by the server" << info->device()->name() << info->device()->id().toString();
|
||||||
emit actionExecutionFinished(action.id(), Device::DeviceErrorHardwareFailure);
|
info->finish(Device::DeviceErrorHardwareFailure);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
qCDebug(dcPushbullet()) << "Message sent successfully";
|
qCDebug(dcPushbullet()) << "Message sent successfully";
|
||||||
emit actionExecutionFinished(action.id(), Device::DeviceErrorNoError);
|
info->finish(Device::DeviceErrorNoError);
|
||||||
});
|
});
|
||||||
|
|
||||||
return Device::DeviceErrorAsync;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -36,8 +36,8 @@ public:
|
|||||||
explicit DevicePluginPushbullet(QObject *parent = nullptr);
|
explicit DevicePluginPushbullet(QObject *parent = nullptr);
|
||||||
~DevicePluginPushbullet() override;
|
~DevicePluginPushbullet() override;
|
||||||
|
|
||||||
Device::DeviceSetupStatus setupDevice(Device *device) override;
|
void setupDevice(DeviceSetupInfo *info) override;
|
||||||
Device::DeviceError executeAction(Device *device, const Action &action) override;
|
void executeAction(DeviceActionInfo *info) override;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user