Update awattar plugin to new api
parent
f18a7e44db
commit
3d50fabe6d
|
|
@ -39,26 +39,51 @@ DevicePluginAwattar::~DevicePluginAwattar()
|
|||
{
|
||||
}
|
||||
|
||||
Device::DeviceSetupStatus DevicePluginAwattar::setupDevice(Device *device)
|
||||
void DevicePluginAwattar::startPairing(DevicePairingInfo *info)
|
||||
{
|
||||
qCDebug(dcAwattar) << "Setup device" << device->name() << device->params();
|
||||
info->finish(Device::DeviceErrorNoError, QT_TR_NOOP("Please enter your token for awattar.com"));
|
||||
}
|
||||
|
||||
QString userUuid = device->paramValue(awattarDeviceUserUuidParamTypeId).toString();
|
||||
QString token = device->paramValue(awattarDeviceTokenParamTypeId).toString();
|
||||
void DevicePluginAwattar::confirmPairing(DevicePairingInfo *info, const QString &username, const QString &secret)
|
||||
{
|
||||
Q_UNUSED(username)
|
||||
|
||||
QByteArray data = QString(secret + ":").toUtf8().toBase64();
|
||||
QString header = "Basic " + data;
|
||||
QNetworkRequest request(QUrl("https://api.awattar.com/v1/marketdata"));
|
||||
request.setRawHeader("Authorization", header.toLocal8Bit());
|
||||
request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
|
||||
QNetworkReply *reply = hardwareManager()->networkManager()->get(request);
|
||||
connect(reply, &QNetworkReply::finished, info, [this, reply, info, secret](){
|
||||
reply->deleteLater();
|
||||
|
||||
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
|
||||
// check HTTP status code
|
||||
if (status != 200) {
|
||||
//: Error setting up device with invalid token
|
||||
info->finish(Device::DeviceErrorAuthenticationFailure, QT_TR_NOOP("This token is not valid."));
|
||||
return;
|
||||
}
|
||||
|
||||
pluginStorage()->beginGroup(info->deviceId().toString());
|
||||
pluginStorage()->setValue("token", secret);
|
||||
pluginStorage()->endGroup();
|
||||
info->finish(Device::DeviceErrorNoError);
|
||||
});
|
||||
}
|
||||
|
||||
void DevicePluginAwattar::setupDevice(DeviceSetupInfo *info)
|
||||
{
|
||||
qCDebug(dcAwattar) << "Setup device" << info->device()->name() << info->device()->params();
|
||||
|
||||
if (token.isEmpty() || userUuid.isEmpty()) {
|
||||
qCWarning(dcAwattar) << "Missing token oder user uuid.";
|
||||
return Device::DeviceSetupStatusFailure;
|
||||
}
|
||||
|
||||
if (!m_pluginTimer) {
|
||||
m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(60 * 60);
|
||||
connect(m_pluginTimer, &PluginTimer::timeout, this, &DevicePluginAwattar::onPluginTimer);
|
||||
}
|
||||
|
||||
requestPriceData(device, true);
|
||||
|
||||
return Device::DeviceSetupStatusAsync;
|
||||
requestPriceData(info->device(), info);
|
||||
}
|
||||
|
||||
void DevicePluginAwattar::deviceRemoved(Device *device)
|
||||
|
|
@ -76,24 +101,27 @@ void DevicePluginAwattar::onPluginTimer()
|
|||
}
|
||||
}
|
||||
|
||||
void DevicePluginAwattar::requestPriceData(Device* device, bool setupInProgress)
|
||||
void DevicePluginAwattar::requestPriceData(Device* device, DeviceSetupInfo *setup)
|
||||
{
|
||||
QString token = device->paramValue(awattarDeviceTokenParamTypeId).toString();
|
||||
pluginStorage()->beginGroup(device->id().toString());
|
||||
QString token = pluginStorage()->value("token").toString();
|
||||
pluginStorage()->endGroup();
|
||||
|
||||
QByteArray data = QString(token + ":").toUtf8().toBase64();
|
||||
QString header = "Basic " + data;
|
||||
QNetworkRequest request(QUrl("https://api.awattar.com/v1/marketdata"));
|
||||
request.setRawHeader("Authorization", header.toLocal8Bit());
|
||||
request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
|
||||
QNetworkReply *reply = hardwareManager()->networkManager()->get(request);
|
||||
connect(reply, &QNetworkReply::finished, device, [this, reply, device, setupInProgress](){
|
||||
connect(reply, &QNetworkReply::finished, device, [this, reply, device, setup](){
|
||||
reply->deleteLater();
|
||||
|
||||
// check HTTP status code
|
||||
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
if (status != 200) {
|
||||
qCWarning(dcAwattar) << "Update reply HTTP error:" << status << reply->errorString();
|
||||
if (setupInProgress) {
|
||||
emit deviceSetupFinished(device, Device::DeviceSetupStatusFailure);
|
||||
if (setup) {
|
||||
setup->finish(Device::DeviceErrorHardwareFailure, QT_TR_NOOP("Error getting data from server."));
|
||||
} else {
|
||||
device->setStateValue(awattarConnectedStateTypeId, false);
|
||||
}
|
||||
|
|
@ -104,15 +132,18 @@ void DevicePluginAwattar::requestPriceData(Device* device, bool setupInProgress)
|
|||
QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &error);
|
||||
if (error.error != QJsonParseError::NoError) {
|
||||
qCWarning(dcAwattar) << "Update reply JSON error:" << error.errorString();
|
||||
if (setupInProgress) {
|
||||
emit deviceSetupFinished(device, Device::DeviceSetupStatusFailure);
|
||||
if (setup) {
|
||||
setup->finish(Device::DeviceErrorHardwareFailure, QT_TR_NOOP("The server returned unexpected data."));
|
||||
} else {
|
||||
device->setStateValue(awattarConnectedStateTypeId, false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
emit deviceSetupFinished(device, Device::DeviceSetupStatusSuccess);
|
||||
if (setup) {
|
||||
setup->finish(Device::DeviceErrorNoError);
|
||||
}
|
||||
|
||||
device->setStateValue(awattarConnectedStateTypeId, true);
|
||||
|
||||
processPriceData(device, jsonDoc.toVariant().toMap());
|
||||
|
|
|
|||
|
|
@ -44,7 +44,9 @@ public:
|
|||
explicit DevicePluginAwattar();
|
||||
~DevicePluginAwattar();
|
||||
|
||||
Device::DeviceSetupStatus setupDevice(Device *device) override;
|
||||
void startPairing(DevicePairingInfo *info) override;
|
||||
void confirmPairing(DevicePairingInfo *info, const QString &username, const QString &secret) override;
|
||||
void setupDevice(DeviceSetupInfo *info) override;
|
||||
void deviceRemoved(Device *device) override;
|
||||
|
||||
private:
|
||||
|
|
@ -52,7 +54,7 @@ private:
|
|||
|
||||
private slots:
|
||||
void onPluginTimer();
|
||||
void requestPriceData(Device* device, bool setupInProgress = false);
|
||||
void requestPriceData(Device* device, DeviceSetupInfo *setup = nullptr);
|
||||
void processPriceData(Device *device, const QVariantMap &data);
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -23,23 +23,8 @@
|
|||
"displayName": "aWATTar",
|
||||
"name": "awattar",
|
||||
"createMethods": ["user"],
|
||||
"setupMethod": "displaypin",
|
||||
"interfaces": ["connectable"],
|
||||
"paramTypes": [
|
||||
{
|
||||
"id": "cbbf6f0a-6a91-4844-83f5-60eadb46099b",
|
||||
"name": "userUuid",
|
||||
"displayName": "user uuid",
|
||||
"type": "QString",
|
||||
"inputType": "TextLine"
|
||||
},
|
||||
{
|
||||
"id": "7ab323d0-f3b5-4baa-9ff9-4843b4dd9811",
|
||||
"name": "token",
|
||||
"displayName": "token",
|
||||
"type": "QString",
|
||||
"inputType": "Password"
|
||||
}
|
||||
],
|
||||
"stateTypes": [
|
||||
{
|
||||
"id": "470b9b88-17f3-42e3-9250-cc181984eafe",
|
||||
|
|
|
|||
Loading…
Reference in New Issue