Update daylightsensor
This commit is contained in:
parent
fe7dd62fa8
commit
79479ff162
@ -44,53 +44,46 @@ DevicePluginDaylightSensor::~DevicePluginDaylightSensor()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Device::DeviceError DevicePluginDaylightSensor::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms)
|
void DevicePluginDaylightSensor::discoverDevices(DeviceDiscoveryInfo *info)
|
||||||
{
|
{
|
||||||
Q_UNUSED(deviceClassId)
|
|
||||||
Q_UNUSED(params)
|
|
||||||
|
|
||||||
QNetworkRequest request(QUrl("http://ip-api.com/json"));
|
QNetworkRequest request(QUrl("http://ip-api.com/json"));
|
||||||
QNetworkReply* reply = hardwareManager()->networkManager()->get(request);
|
QNetworkReply* reply = hardwareManager()->networkManager()->get(request);
|
||||||
connect(reply, &QNetworkReply::finished, this, [this, reply, deviceClassId]() {
|
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
|
||||||
reply->deleteLater();
|
connect(reply, &QNetworkReply::finished, info, [this, reply, info]() {
|
||||||
QList<DeviceDescriptor> results;
|
|
||||||
if (reply->error() != QNetworkReply::NoError) {
|
if (reply->error() != QNetworkReply::NoError) {
|
||||||
qCWarning(dcDaylightSensor()) << "Error fetching geolocation from ip-api:" << reply->error() << reply->errorString();
|
qCWarning(dcDaylightSensor()) << "Error fetching geolocation from ip-api:" << reply->error() << reply->errorString();
|
||||||
emit devicesDiscovered(deviceClassId, results);
|
info->finish(Device::DeviceErrorHardwareFailure, QT_TR_NOOP("Failed to fetch data from the internet."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QJsonParseError error;
|
QJsonParseError error;
|
||||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &error);
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &error);
|
||||||
if (error.error != QJsonParseError::NoError) {
|
if (error.error != QJsonParseError::NoError) {
|
||||||
qCWarning(dcDaylightSensor()) << "Failed to parse json from ip-api:" << error.error << error.errorString();
|
qCWarning(dcDaylightSensor()) << "Failed to parse json from ip-api:" << error.error << error.errorString();
|
||||||
emit devicesDiscovered(deviceClassId, results);
|
info->finish(Device::DeviceErrorHardwareFailure, QT_TR_NOOP("The server returned unexpected data."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!jsonDoc.toVariant().toMap().contains("lat") || !jsonDoc.toVariant().toMap().contains("lon")) {
|
if (!jsonDoc.toVariant().toMap().contains("lat") || !jsonDoc.toVariant().toMap().contains("lon")) {
|
||||||
qCWarning(dcDaylightSensor()) << "Reply missing geolocation info" << qUtf8Printable(jsonDoc.toJson(QJsonDocument::Indented));
|
qCWarning(dcDaylightSensor()) << "Reply missing geolocation info" << qUtf8Printable(jsonDoc.toJson(QJsonDocument::Indented));
|
||||||
emit devicesDiscovered(deviceClassId, results);
|
info->finish(Device::DeviceErrorHardwareFailure, QT_TR_NOOP("The server returned unexpected data."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
qreal lat = jsonDoc.toVariant().toMap().value("lat").toDouble();
|
qreal lat = jsonDoc.toVariant().toMap().value("lat").toDouble();
|
||||||
qreal lon = jsonDoc.toVariant().toMap().value("lon").toDouble();
|
qreal lon = jsonDoc.toVariant().toMap().value("lon").toDouble();
|
||||||
|
|
||||||
DeviceDescriptor descriptor(deviceClassId, tr("Daylight sensor"), jsonDoc.toVariant().toMap().value("city").toString());
|
DeviceDescriptor descriptor(info->deviceClassId(), tr("Daylight sensor"), jsonDoc.toVariant().toMap().value("city").toString());
|
||||||
ParamList params;
|
ParamList params;
|
||||||
params.append(Param(daylightSensorDeviceLatitudeParamTypeId, lat));
|
params.append(Param(daylightSensorDeviceLatitudeParamTypeId, lat));
|
||||||
params.append(Param(daylightSensorDeviceLongitudeParamTypeId, lon));
|
params.append(Param(daylightSensorDeviceLongitudeParamTypeId, lon));
|
||||||
descriptor.setParams(params);
|
descriptor.setParams(params);
|
||||||
results.append(descriptor);
|
info->addDeviceDescriptor(descriptor);
|
||||||
emit devicesDiscovered(deviceClassId, results);
|
info->finish(Device::DeviceErrorNoError);
|
||||||
});
|
});
|
||||||
|
|
||||||
return Device::DeviceErrorAsync;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Device::DeviceSetupStatus DevicePluginDaylightSensor::setupDevice(Device *device)
|
void DevicePluginDaylightSensor::setupDevice(DeviceSetupInfo *info)
|
||||||
{
|
{
|
||||||
updateDevice(device);
|
updateDevice(info->device());
|
||||||
|
info->finish(Device::DeviceErrorNoError);
|
||||||
return Device::DeviceSetupStatusSuccess;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DevicePluginDaylightSensor::deviceRemoved(Device *device)
|
void DevicePluginDaylightSensor::deviceRemoved(Device *device)
|
||||||
|
|||||||
@ -37,8 +37,8 @@ public:
|
|||||||
explicit DevicePluginDaylightSensor();
|
explicit DevicePluginDaylightSensor();
|
||||||
~DevicePluginDaylightSensor() override;
|
~DevicePluginDaylightSensor() override;
|
||||||
|
|
||||||
Device::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) override;
|
void discoverDevices(DeviceDiscoveryInfo *info) override;
|
||||||
Device::DeviceSetupStatus setupDevice(Device *device) override;
|
void setupDevice(DeviceSetupInfo *info) override;
|
||||||
void deviceRemoved(Device *device) override;
|
void deviceRemoved(Device *device) override;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user