openweathermap states get updated
This commit is contained in:
parent
9ab9f2314f
commit
98dc4f3808
@ -145,17 +145,34 @@
|
|||||||
#include "devicemanager.h"
|
#include "devicemanager.h"
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QStringList>
|
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
|
#include <QVariantMap>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
|
||||||
|
|
||||||
VendorId openweathermapVendorId = VendorId("bf1e96f0-9650-4e7c-a56c-916d54d18e7a");
|
VendorId openweathermapVendorId = VendorId("bf1e96f0-9650-4e7c-a56c-916d54d18e7a");
|
||||||
DeviceClassId deviceClassId = DeviceClassId("985195aa-17ad-4530-88a4-cdd753d747d7");
|
DeviceClassId openweathermapDeviceClassId = DeviceClassId("985195aa-17ad-4530-88a4-cdd753d747d7");
|
||||||
|
|
||||||
|
ActionTypeId updateWeatherActionTypeId = ActionTypeId("cfbc6504-d86f-4856-8dfa-97b6fbb385e4");
|
||||||
|
|
||||||
|
StateTypeId updateTimeStateTypeId = StateTypeId("36b2f09b-7d77-4fbc-a68f-23d735dda0b1");
|
||||||
|
StateTypeId temperatureStateTypeId = StateTypeId("6013402f-b5b1-46b3-8490-f0c20d62fe61");
|
||||||
|
StateTypeId temperatureMinStateTypeId = StateTypeId("14ec2781-cb04-4bbf-b097-7d01ef982630");
|
||||||
|
StateTypeId temperatureMaxStateTypeId = StateTypeId("fefe5563-452f-4833-b5cf-49c3cc67c772");
|
||||||
|
StateTypeId humidityStateTypeId = StateTypeId("6f32ec73-3240-4630-ada9-1c10b8e98123");
|
||||||
|
StateTypeId pressureStateTypeId = StateTypeId("4a42eea9-00eb-440b-915e-dbe42180f83b");
|
||||||
|
StateTypeId windSpeedStateTypeId = StateTypeId("2bf63430-e9e2-4fbf-88e6-6f1b4770f287");
|
||||||
|
StateTypeId windDirectionStateTypeId = StateTypeId("589e2ea5-65b2-4afd-9b72-e3708a589a12");
|
||||||
|
StateTypeId cloudinessStateTypeId = StateTypeId("798553bc-45c7-42eb-9105-430bddb5d9b7");
|
||||||
|
StateTypeId weatherDescriptionStateTypeId = StateTypeId("f9539108-0e0e-4736-a306-6408f8e20a26");
|
||||||
|
StateTypeId sunriseStateTypeId = StateTypeId("af155e94-9492-44e1-8608-7d0ee8b5d50d");
|
||||||
|
StateTypeId sunsetStateTypeId = StateTypeId("a1dddc3d-549f-4f20-b78b-be850548f286");
|
||||||
|
|
||||||
|
|
||||||
DevicePluginOpenweathermap::DevicePluginOpenweathermap()
|
DevicePluginOpenweathermap::DevicePluginOpenweathermap()
|
||||||
{
|
{
|
||||||
m_openweaher = new OpenWeatherMap(this);
|
m_openweaher = new OpenWeatherMap(this);
|
||||||
connect(m_openweaher, SIGNAL(searchResultReady(QList<QVariantMap>)), this, SLOT(searchResultsReady(QList<QVariantMap>)));
|
connect(m_openweaher, SIGNAL(searchResultReady(QList<QVariantMap>)), this, SLOT(searchResultsReady(QList<QVariantMap>)));
|
||||||
|
connect(m_openweaher, SIGNAL(weatherDataReady(QByteArray)), this, SLOT(weatherDataReady(QByteArray)));
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<Vendor> DevicePluginOpenweathermap::supportedVendors() const
|
QList<Vendor> DevicePluginOpenweathermap::supportedVendors() const
|
||||||
@ -170,8 +187,8 @@ QList<DeviceClass> DevicePluginOpenweathermap::supportedDevices() const
|
|||||||
{
|
{
|
||||||
QList<DeviceClass> ret;
|
QList<DeviceClass> ret;
|
||||||
|
|
||||||
DeviceClass deviceClassOpenweathermap(pluginId(), openweathermapVendorId, DeviceClassId("985195aa-17ad-4530-88a4-cdd753d747d7"));
|
DeviceClass deviceClassOpenweathermap(pluginId(), openweathermapVendorId, openweathermapDeviceClassId);
|
||||||
deviceClassOpenweathermap.setName("Weather from openweathermap");
|
deviceClassOpenweathermap.setName("Weather from openweathermap.org");
|
||||||
deviceClassOpenweathermap.setCreateMethod(DeviceClass::CreateMethodDiscovery);
|
deviceClassOpenweathermap.setCreateMethod(DeviceClass::CreateMethodDiscovery);
|
||||||
|
|
||||||
// Params
|
// Params
|
||||||
@ -195,105 +212,84 @@ QList<DeviceClass> DevicePluginOpenweathermap::supportedDevices() const
|
|||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
QList<ActionType> weatherActions;
|
QList<ActionType> weatherActions;
|
||||||
ActionType updateWeather(ActionTypeId("cfbc6504-d86f-4856-8dfa-97b6fbb385e4"));
|
ActionType updateWeather(updateWeatherActionTypeId);
|
||||||
updateWeather.setName("refresh");
|
updateWeather.setName("refresh");
|
||||||
weatherActions.append(updateWeather);
|
weatherActions.append(updateWeather);
|
||||||
|
|
||||||
// States
|
// States
|
||||||
QList<StateType> weatherStates;
|
QList<StateType> weatherStates;
|
||||||
StateType cityNameState(StateTypeId("fd9e7b7f-cf1f-4093-8f6d-fff5b223471f"));
|
StateType updateTimeState(updateTimeStateTypeId);
|
||||||
cityNameState.setName("city name");
|
|
||||||
cityNameState.setType(QVariant::String);
|
|
||||||
cityNameState.setDefaultValue("");
|
|
||||||
weatherStates.append(cityNameState);
|
|
||||||
|
|
||||||
StateType cityIdState(StateTypeId("c6ef1c07-e817-4251-b83d-115bbf6f0ae9"));
|
|
||||||
cityIdState.setName("city id");
|
|
||||||
cityIdState.setType(QVariant::String);
|
|
||||||
cityIdState.setDefaultValue("");
|
|
||||||
weatherStates.append(cityIdState);
|
|
||||||
|
|
||||||
StateType countryNameState(StateTypeId("0e607a5f-1938-4e77-a146-15e9ad15bfad"));
|
|
||||||
countryNameState.setName("country name");
|
|
||||||
countryNameState.setType(QVariant::String);
|
|
||||||
countryNameState.setDefaultValue("");
|
|
||||||
weatherStates.append(countryNameState);
|
|
||||||
|
|
||||||
StateType updateTimeState(StateTypeId("98e48095-87da-47a4-b812-28c6c17a3e76"));
|
|
||||||
updateTimeState.setName("last update [unixtime]");
|
updateTimeState.setName("last update [unixtime]");
|
||||||
updateTimeState.setType(QVariant::UInt);
|
updateTimeState.setType(QVariant::UInt);
|
||||||
updateTimeState.setDefaultValue(0);
|
updateTimeState.setDefaultValue(0);
|
||||||
weatherStates.append(updateTimeState);
|
weatherStates.append(updateTimeState);
|
||||||
|
|
||||||
StateType temperatureState(StateTypeId("2f949fa3-ff21-4721-87ec-0a5c9d0a5b8a"));
|
StateType temperatureState(temperatureStateTypeId);
|
||||||
temperatureState.setName("temperature [°C]");
|
temperatureState.setName("temperature [Celsius]");
|
||||||
temperatureState.setType(QVariant::Double);
|
temperatureState.setType(QVariant::Double);
|
||||||
temperatureState.setDefaultValue(-999.9);
|
temperatureState.setDefaultValue(-999.9);
|
||||||
weatherStates.append(temperatureState);
|
weatherStates.append(temperatureState);
|
||||||
|
|
||||||
StateType temperatureMinState(StateTypeId("701338b3-80de-4c95-8abf-26f44529d620"));
|
StateType temperatureMinState(temperatureMinStateTypeId);
|
||||||
temperatureMinState.setName("temperature minimum [Celsius]");
|
temperatureMinState.setName("temperature minimum [Celsius]");
|
||||||
temperatureMinState.setType(QVariant::Double);
|
temperatureMinState.setType(QVariant::Double);
|
||||||
temperatureMinState.setDefaultValue(-999.9);
|
temperatureMinState.setDefaultValue(-999.9);
|
||||||
weatherStates.append(temperatureMinState);
|
weatherStates.append(temperatureMinState);
|
||||||
|
|
||||||
StateType temperatureMaxState(StateTypeId("f69bedd2-c997-4a7d-9242-76bf2aab3d3d"));
|
StateType temperatureMaxState(temperatureMaxStateTypeId);
|
||||||
temperatureMaxState.setName("temperature maximum [Celsius]");
|
temperatureMaxState.setName("temperature maximum [Celsius]");
|
||||||
temperatureMaxState.setType(QVariant::Double);
|
temperatureMaxState.setType(QVariant::Double);
|
||||||
temperatureMaxState.setDefaultValue(999.9);
|
temperatureMaxState.setDefaultValue(999.9);
|
||||||
weatherStates.append(temperatureMaxState);
|
weatherStates.append(temperatureMaxState);
|
||||||
|
|
||||||
StateType humidityState(StateTypeId("3f01c9f0-206b-4477-afa2-80d6e5e54fbb"));
|
StateType humidityState(humidityStateTypeId);
|
||||||
humidityState.setName("humidity [%]");
|
humidityState.setName("humidity [%]");
|
||||||
humidityState.setType(QVariant::Int);
|
humidityState.setType(QVariant::Int);
|
||||||
humidityState.setDefaultValue(-1);
|
humidityState.setDefaultValue(-1);
|
||||||
weatherStates.append(humidityState);
|
weatherStates.append(humidityState);
|
||||||
|
|
||||||
StateType pressureState(StateTypeId("6a57b6e9-7010-4a89-982c-ce0bc2a71f11"));
|
StateType pressureState(pressureStateTypeId);
|
||||||
pressureState.setName("pressure [hPa]");
|
pressureState.setName("pressure [hPa]");
|
||||||
pressureState.setType(QVariant::Double);
|
pressureState.setType(QVariant::Double);
|
||||||
pressureState.setDefaultValue(-1);
|
pressureState.setDefaultValue(-1);
|
||||||
weatherStates.append(pressureState);
|
weatherStates.append(pressureState);
|
||||||
|
|
||||||
StateType windSpeedState(StateTypeId("12dc85a9-825d-4375-bef4-abd66e9e301b"));
|
StateType windSpeedState(windSpeedStateTypeId);
|
||||||
windSpeedState.setName("wind speed [m/s]");
|
windSpeedState.setName("wind speed [m/s]");
|
||||||
windSpeedState.setType(QVariant::Double);
|
windSpeedState.setType(QVariant::Double);
|
||||||
windSpeedState.setDefaultValue(-1);
|
windSpeedState.setDefaultValue(-1);
|
||||||
weatherStates.append(windSpeedState);
|
weatherStates.append(windSpeedState);
|
||||||
|
|
||||||
StateType windDirectionState(StateTypeId("a8b0383c-d615-41fe-82b8-9b797f045cc9"));
|
StateType windDirectionState(windDirectionStateTypeId);
|
||||||
windDirectionState.setName("wind direction [°]");
|
windDirectionState.setName("wind direction [degree]");
|
||||||
windDirectionState.setType(QVariant::Int);
|
windDirectionState.setType(QVariant::Int);
|
||||||
windDirectionState.setDefaultValue(-1);
|
windDirectionState.setDefaultValue(-1);
|
||||||
weatherStates.append(windDirectionState);
|
weatherStates.append(windDirectionState);
|
||||||
|
|
||||||
StateType cloudinessState(StateTypeId("0c1dc881-560e-40ac-a4a1-9ab69138cfe3"));
|
StateType cloudinessState(cloudinessStateTypeId);
|
||||||
cloudinessState.setName("cloudiness [%]");
|
cloudinessState.setName("cloudiness [%]");
|
||||||
cloudinessState.setType(QVariant::Int);
|
cloudinessState.setType(QVariant::Int);
|
||||||
cloudinessState.setDefaultValue(-1);
|
cloudinessState.setDefaultValue(-1);
|
||||||
weatherStates.append(cloudinessState);
|
weatherStates.append(cloudinessState);
|
||||||
|
|
||||||
StateType weatherDescriptionState(StateTypeId("e71d98e3-ebd8-4abf-ad25-9ecc2d05276a"));
|
StateType weatherDescriptionState(weatherDescriptionStateTypeId);
|
||||||
weatherDescriptionState.setName("weather description");
|
weatherDescriptionState.setName("weather description");
|
||||||
weatherDescriptionState.setType(QVariant::String);
|
weatherDescriptionState.setType(QVariant::String);
|
||||||
weatherDescriptionState.setDefaultValue("");
|
weatherDescriptionState.setDefaultValue("");
|
||||||
weatherStates.append(weatherDescriptionState);
|
weatherStates.append(weatherDescriptionState);
|
||||||
|
|
||||||
StateType sunsetState(StateTypeId("5dd6f5a3-25d6-4e60-82ca-e934ad76a4b6"));
|
StateType sunsetState(sunsetStateTypeId);
|
||||||
sunsetState.setName("sunset [unixtime]");
|
sunsetState.setName("sunset [unixtime]");
|
||||||
sunsetState.setType(QVariant::UInt);
|
sunsetState.setType(QVariant::UInt);
|
||||||
sunsetState.setDefaultValue(0);
|
sunsetState.setDefaultValue(0);
|
||||||
weatherStates.append(sunsetState);
|
weatherStates.append(sunsetState);
|
||||||
|
|
||||||
StateType sunriseState(StateTypeId("413b3fc6-bd1c-46fb-8c86-03096254f94f"));
|
StateType sunriseState(sunriseStateTypeId);
|
||||||
sunriseState.setName("sunrise [unixtime]");
|
sunriseState.setName("sunrise [unixtime]");
|
||||||
sunriseState.setType(QVariant::UInt);
|
sunriseState.setType(QVariant::UInt);
|
||||||
sunriseState.setDefaultValue(0);
|
sunriseState.setDefaultValue(0);
|
||||||
weatherStates.append(sunriseState);
|
weatherStates.append(sunriseState);
|
||||||
|
|
||||||
//Events
|
|
||||||
|
|
||||||
|
|
||||||
deviceClassOpenweathermap.setActions(weatherActions);
|
deviceClassOpenweathermap.setActions(weatherActions);
|
||||||
deviceClassOpenweathermap.setStates(weatherStates);
|
deviceClassOpenweathermap.setStates(weatherStates);
|
||||||
|
|
||||||
@ -303,7 +299,7 @@ QList<DeviceClass> DevicePluginOpenweathermap::supportedDevices() const
|
|||||||
|
|
||||||
DeviceManager::DeviceError DevicePluginOpenweathermap::discoverDevices(const DeviceClassId &deviceClassId, const QVariantMap ¶ms) const
|
DeviceManager::DeviceError DevicePluginOpenweathermap::discoverDevices(const DeviceClassId &deviceClassId, const QVariantMap ¶ms) const
|
||||||
{
|
{
|
||||||
qDebug() << "should discover divces for" << deviceClassId << params;
|
qDebug() << "should discover divces for" << deviceClassId << params.value("location").toString();
|
||||||
if(params.value("location").toString() == ""){
|
if(params.value("location").toString() == ""){
|
||||||
m_openweaher->searchAutodetect();
|
m_openweaher->searchAutodetect();
|
||||||
return DeviceManager::DeviceErrorNoError;
|
return DeviceManager::DeviceErrorNoError;
|
||||||
@ -312,6 +308,12 @@ DeviceManager::DeviceError DevicePluginOpenweathermap::discoverDevices(const Dev
|
|||||||
return DeviceManager::DeviceErrorNoError;
|
return DeviceManager::DeviceErrorNoError;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool DevicePluginOpenweathermap::deviceCreated(Device *device)
|
||||||
|
{
|
||||||
|
m_openweaher->update(device->params().value("id").toString());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
DeviceManager::HardwareResources DevicePluginOpenweathermap::requiredHardware() const
|
DeviceManager::HardwareResources DevicePluginOpenweathermap::requiredHardware() const
|
||||||
{
|
{
|
||||||
return DeviceManager::HardwareResourceTimer;
|
return DeviceManager::HardwareResourceTimer;
|
||||||
@ -329,14 +331,16 @@ PluginId DevicePluginOpenweathermap::pluginId() const
|
|||||||
|
|
||||||
void DevicePluginOpenweathermap::guhTimer()
|
void DevicePluginOpenweathermap::guhTimer()
|
||||||
{
|
{
|
||||||
m_openweaher->update();
|
foreach (Device *device, deviceManager()->findConfiguredDevices(openweathermapDeviceClassId)) {
|
||||||
|
m_openweaher->update(device->params().value("id").toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DevicePluginOpenweathermap::searchResultsReady(const QList<QVariantMap> &cityList)
|
void DevicePluginOpenweathermap::searchResultsReady(const QList<QVariantMap> &cityList)
|
||||||
{
|
{
|
||||||
QList<DeviceDescriptor> retList;
|
QList<DeviceDescriptor> retList;
|
||||||
foreach (QVariantMap elemant, cityList) {
|
foreach (QVariantMap elemant, cityList) {
|
||||||
DeviceDescriptor descriptor(deviceClassId, elemant.value("name").toString(),elemant.value("country").toString());
|
DeviceDescriptor descriptor(openweathermapDeviceClassId, elemant.value("name").toString(),elemant.value("country").toString());
|
||||||
QVariantMap params;
|
QVariantMap params;
|
||||||
params.insert("location", elemant.value("name"));
|
params.insert("location", elemant.value("name"));
|
||||||
params.insert("country", elemant.value("country"));
|
params.insert("country", elemant.value("country"));
|
||||||
@ -344,7 +348,73 @@ void DevicePluginOpenweathermap::searchResultsReady(const QList<QVariantMap> &ci
|
|||||||
descriptor.setParams(params);
|
descriptor.setParams(params);
|
||||||
retList.append(descriptor);
|
retList.append(descriptor);
|
||||||
}
|
}
|
||||||
emit devicesDiscovered(deviceClassId,retList);
|
emit devicesDiscovered(openweathermapDeviceClassId,retList);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DevicePluginOpenweathermap::weatherDataReady(const QByteArray &data)
|
||||||
|
{
|
||||||
|
QJsonParseError error;
|
||||||
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
|
||||||
|
|
||||||
|
if(error.error != QJsonParseError::NoError) {
|
||||||
|
qWarning() << "failed to parse data" << data << ":" << error.errorString();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariantMap dataMap = jsonDoc.toVariant().toMap();
|
||||||
|
qDebug() << "##############################";
|
||||||
|
|
||||||
|
foreach (Device *device, deviceManager()->findConfiguredDevices(openweathermapDeviceClassId)) {
|
||||||
|
qDebug() << device->params().value("id").toString();
|
||||||
|
qDebug() << dataMap.value("id").toString();
|
||||||
|
qDebug() << "##############################";
|
||||||
|
if(device->params().value("id").toString() == dataMap.value("id").toString()){
|
||||||
|
|
||||||
|
if(dataMap.contains("clouds")){
|
||||||
|
int cloudiness = dataMap.value("clouds").toMap().value("all").toInt();
|
||||||
|
device->setStateValue(cloudinessStateTypeId,cloudiness);
|
||||||
|
}
|
||||||
|
if(dataMap.contains("dt")){
|
||||||
|
uint lastUpdate = dataMap.value("dt").toUInt();
|
||||||
|
device->setStateValue(updateTimeStateTypeId,lastUpdate);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(dataMap.contains("main")){
|
||||||
|
double temperatur = dataMap.value("main").toMap().value("temp").toDouble();
|
||||||
|
double temperaturMax = dataMap.value("main").toMap().value("temp_max").toDouble();
|
||||||
|
double temperaturMin = dataMap.value("main").toMap().value("temp_min").toDouble();
|
||||||
|
double pressure = dataMap.value("main").toMap().value("pressure").toDouble();
|
||||||
|
int humidity = dataMap.value("main").toMap().value("humidity").toInt();
|
||||||
|
|
||||||
|
device->setStateValue(temperatureStateTypeId,temperatur);
|
||||||
|
device->setStateValue(temperatureMinStateTypeId,temperaturMin);
|
||||||
|
device->setStateValue(temperatureMaxStateTypeId,temperaturMax);
|
||||||
|
device->setStateValue(pressureStateTypeId,pressure);
|
||||||
|
device->setStateValue(humidityStateTypeId,humidity);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(dataMap.contains("sys")){
|
||||||
|
uint sunrise = dataMap.value("sys").toMap().value("sunrise").toUInt();
|
||||||
|
uint sunset = dataMap.value("sys").toMap().value("sunset").toUInt();
|
||||||
|
|
||||||
|
device->setStateValue(sunriseStateTypeId,sunrise);
|
||||||
|
device->setStateValue(sunsetStateTypeId,sunset);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(dataMap.contains("weather")){
|
||||||
|
QString description = dataMap.value("weather").toMap().value("description").toString();
|
||||||
|
device->setStateValue(weatherDescriptionStateTypeId,description);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(dataMap.contains("wind")){
|
||||||
|
int windDirection = dataMap.value("wind").toMap().value("deg").toInt();
|
||||||
|
double windSpeed = dataMap.value("wind").toMap().value("speed").toDouble();
|
||||||
|
|
||||||
|
device->setStateValue(windDirectionStateTypeId,windDirection);
|
||||||
|
device->setStateValue(windSpeedStateTypeId,windSpeed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -39,7 +39,7 @@ public:
|
|||||||
QList<DeviceClass> supportedDevices() const override;
|
QList<DeviceClass> supportedDevices() const override;
|
||||||
|
|
||||||
DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QVariantMap ¶ms) const override;
|
DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QVariantMap ¶ms) const override;
|
||||||
|
bool deviceCreated(Device *device) override;
|
||||||
DeviceManager::HardwareResources requiredHardware() const override;
|
DeviceManager::HardwareResources requiredHardware() const override;
|
||||||
|
|
||||||
QString pluginName() const override;
|
QString pluginName() const override;
|
||||||
@ -49,7 +49,7 @@ public:
|
|||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void searchResultsReady(const QList<QVariantMap> &cityList);
|
void searchResultsReady(const QList<QVariantMap> &cityList);
|
||||||
|
void weatherDataReady(const QByteArray &data);
|
||||||
public slots:
|
public slots:
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -27,7 +27,17 @@ OpenWeatherMap::OpenWeatherMap(QObject *parent) :
|
|||||||
connect(m_manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));
|
connect(m_manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenWeatherMap::updateLocationData()
|
void OpenWeatherMap::update(QString id)
|
||||||
|
{
|
||||||
|
m_cityId = id;
|
||||||
|
QString urlString = "http://api.openweathermap.org/data/2.5/weather?id="+ m_cityId + "&mode=json&units=metric";
|
||||||
|
QNetworkRequest weatherRequest;
|
||||||
|
weatherRequest.setUrl(QUrl(urlString));
|
||||||
|
|
||||||
|
m_weatherReply = m_manager->get(weatherRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenWeatherMap::searchAutodetect()
|
||||||
{
|
{
|
||||||
QString urlString = "http://ip-api.com/json";
|
QString urlString = "http://ip-api.com/json";
|
||||||
QNetworkRequest locationRequest;
|
QNetworkRequest locationRequest;
|
||||||
@ -36,22 +46,13 @@ void OpenWeatherMap::updateLocationData()
|
|||||||
m_locationReply = m_manager->get(locationRequest);
|
m_locationReply = m_manager->get(locationRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenWeatherMap::updateSearchData()
|
void OpenWeatherMap::search(QString searchString)
|
||||||
{
|
{
|
||||||
QString urlString = "http://api.openweathermap.org/data/2.5/find?q=" + m_cityName;
|
QString urlString = "http://api.openweathermap.org/data/2.5/find?q=" + searchString + "&type=like&units=metric&mode=json";
|
||||||
QNetworkRequest searchRequest;
|
QNetworkRequest searchRequest;
|
||||||
searchRequest.setUrl(QUrl(urlString));
|
searchRequest.setUrl(QUrl(urlString));
|
||||||
|
|
||||||
m_searchLocationReply = m_manager->get(searchRequest);
|
m_searchReply = m_manager->get(searchRequest);
|
||||||
}
|
|
||||||
|
|
||||||
void OpenWeatherMap::updateWeatherData()
|
|
||||||
{
|
|
||||||
QString urlString = "http://api.openweathermap.org/data/2.5/weather?id="+ m_cityId + "&mode=json&units=metric";
|
|
||||||
QNetworkRequest weatherRequest;
|
|
||||||
weatherRequest.setUrl(QUrl(urlString));
|
|
||||||
|
|
||||||
m_weatherReply = m_manager->get(weatherRequest);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenWeatherMap::processLocationResponse(QByteArray data)
|
void OpenWeatherMap::processLocationResponse(QByteArray data)
|
||||||
@ -72,6 +73,10 @@ void OpenWeatherMap::processLocationResponse(QByteArray data)
|
|||||||
|
|
||||||
void OpenWeatherMap::processSearchResponse(QByteArray data)
|
void OpenWeatherMap::processSearchResponse(QByteArray data)
|
||||||
{
|
{
|
||||||
|
emit weatherDataReady(data);
|
||||||
|
|
||||||
|
// TODO: return here...remove the rest from here...
|
||||||
|
|
||||||
QJsonParseError error;
|
QJsonParseError error;
|
||||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
|
||||||
|
|
||||||
@ -148,88 +153,61 @@ void OpenWeatherMap::processWeatherResponse(QByteArray data)
|
|||||||
QVariantMap dataMap = jsonDoc.toVariant().toMap();
|
QVariantMap dataMap = jsonDoc.toVariant().toMap();
|
||||||
if(dataMap.contains("clouds")){
|
if(dataMap.contains("clouds")){
|
||||||
int cloudiness = dataMap.value("clouds").toMap().value("all").toInt();
|
int cloudiness = dataMap.value("clouds").toMap().value("all").toInt();
|
||||||
if(m_cloudiness != cloudiness){
|
m_cloudiness = cloudiness;
|
||||||
m_cloudiness = cloudiness;
|
|
||||||
//emit cloudiness changed
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if(dataMap.contains("dt")){
|
if(dataMap.contains("dt")){
|
||||||
uint lastUpdate = dataMap.value("dt").toUInt();
|
uint lastUpdate = dataMap.value("dt").toUInt();
|
||||||
if(m_lastUpdate != lastUpdate){
|
m_lastUpdate = lastUpdate;
|
||||||
m_lastUpdate = lastUpdate;
|
}
|
||||||
// emit
|
if(dataMap.contains("name")){
|
||||||
}
|
QString description = dataMap.value("name").toString();
|
||||||
|
m_cityName = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(dataMap.contains("sys")){
|
||||||
|
QString description = dataMap.value("sys").toMap().value("country").toString();
|
||||||
|
m_country = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(dataMap.contains("main")){
|
if(dataMap.contains("main")){
|
||||||
double temperatur = dataMap.value("main").toMap().value("temp").toDouble();
|
double temperatur = dataMap.value("main").toMap().value("temp").toDouble();
|
||||||
if(m_temperatur != temperatur){
|
m_temperatur = temperatur;
|
||||||
m_temperatur = temperatur;
|
|
||||||
// emit
|
|
||||||
}
|
|
||||||
|
|
||||||
double temperaturMax = dataMap.value("main").toMap().value("temp_max").toDouble();
|
double temperaturMax = dataMap.value("main").toMap().value("temp_max").toDouble();
|
||||||
if(m_temperaturMax != temperaturMax){
|
m_temperaturMax = temperaturMax;
|
||||||
m_temperaturMax = temperaturMax;
|
|
||||||
// emit
|
|
||||||
}
|
|
||||||
|
|
||||||
double temperaturMin = dataMap.value("main").toMap().value("temp_min").toDouble();
|
|
||||||
if(m_temperaturMin != temperaturMin){
|
|
||||||
m_temperaturMin = temperaturMin;
|
|
||||||
// emit
|
|
||||||
}
|
|
||||||
|
|
||||||
double pressure = dataMap.value("main").toMap().value("pressure").toDouble();
|
|
||||||
if(m_pressure != pressure){
|
|
||||||
m_pressure = pressure;
|
|
||||||
//emit
|
|
||||||
}
|
|
||||||
|
|
||||||
int humidity = dataMap.value("main").toMap().value("humidity").toInt();
|
|
||||||
if(m_humidity != humidity){
|
|
||||||
m_humidity = humidity;
|
|
||||||
//emit
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
double temperaturMin = dataMap.value("main").toMap().value("temp_min").toDouble();
|
||||||
|
m_temperaturMin = temperaturMin;
|
||||||
|
|
||||||
|
double pressure = dataMap.value("main").toMap().value("pressure").toDouble();
|
||||||
|
m_pressure = pressure;
|
||||||
|
|
||||||
|
int humidity = dataMap.value("main").toMap().value("humidity").toInt();
|
||||||
|
m_humidity = humidity;
|
||||||
|
|
||||||
if(dataMap.contains("sys")){
|
if(dataMap.contains("sys")){
|
||||||
uint sunrise = dataMap.value("sys").toMap().value("sunrise").toUInt();
|
uint sunrise = dataMap.value("sys").toMap().value("sunrise").toUInt();
|
||||||
if(m_sunrise != sunrise){
|
m_sunrise = sunrise;
|
||||||
m_sunrise = sunrise;
|
|
||||||
// emit
|
|
||||||
}
|
|
||||||
|
|
||||||
uint sunset = dataMap.value("sys").toMap().value("sunset").toUInt();
|
uint sunset = dataMap.value("sys").toMap().value("sunset").toUInt();
|
||||||
if(m_sunset != sunset){
|
m_sunset = sunset;
|
||||||
m_sunset = sunset;
|
|
||||||
// emit
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(dataMap.contains("weather")){
|
if(dataMap.contains("weather")){
|
||||||
QString description = dataMap.value("weather").toMap().value("description").toString();
|
QString description = dataMap.value("weather").toMap().value("description").toString();
|
||||||
if(m_weatherDescription != description){
|
m_weatherDescription = description;
|
||||||
m_weatherDescription = description;
|
|
||||||
// emit
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(dataMap.contains("wind")){
|
if(dataMap.contains("wind")){
|
||||||
int windDirection = dataMap.value("wind").toMap().value("deg").toInt();
|
int windDirection = dataMap.value("wind").toMap().value("deg").toInt();
|
||||||
if(m_windDirection != windDirection){
|
m_windDirection = windDirection;
|
||||||
m_windDirection = windDirection;
|
|
||||||
//emit
|
|
||||||
}
|
|
||||||
|
|
||||||
double windSpeed = dataMap.value("wind").toMap().value("speed").toDouble();
|
double windSpeed = dataMap.value("wind").toMap().value("speed").toDouble();
|
||||||
if(m_windSpeed != windSpeed){
|
m_windSpeed = windSpeed;
|
||||||
m_windSpeed = windSpeed;
|
|
||||||
// emit
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
qDebug() << "#########################################################";
|
qDebug() << "#########################################################";
|
||||||
qDebug() << m_cityName << m_cityId;
|
qDebug() << m_cityName << m_country << m_cityId;
|
||||||
qDebug() << "#########################################################";
|
qDebug() << "#########################################################";
|
||||||
qDebug() << "temp" << m_temperatur;
|
qDebug() << "temp" << m_temperatur;
|
||||||
qDebug() << "temp min" << m_temperaturMin;
|
qDebug() << "temp min" << m_temperaturMin;
|
||||||
@ -244,25 +222,6 @@ void OpenWeatherMap::processWeatherResponse(QByteArray data)
|
|||||||
qDebug() << "last update" << QDateTime::fromTime_t(m_lastUpdate);
|
qDebug() << "last update" << QDateTime::fromTime_t(m_lastUpdate);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenWeatherMap::update()
|
|
||||||
{
|
|
||||||
updateWeatherData();
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenWeatherMap::searchAutodetect()
|
|
||||||
{
|
|
||||||
updateLocationData();
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenWeatherMap::search(const QString &searchString)
|
|
||||||
{
|
|
||||||
QString urlString = "http://api.openweathermap.org/data/2.5/find?q=" + searchString + "&type=like&units=metric&mode=json";
|
|
||||||
QNetworkRequest searchRequest;
|
|
||||||
searchRequest.setUrl(QUrl(urlString));
|
|
||||||
|
|
||||||
m_searchReply = m_manager->get(searchRequest);
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenWeatherMap::replyFinished(QNetworkReply *reply)
|
void OpenWeatherMap::replyFinished(QNetworkReply *reply)
|
||||||
{
|
{
|
||||||
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
|
|||||||
@ -32,6 +32,9 @@ class OpenWeatherMap : public QObject
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit OpenWeatherMap(QObject *parent = 0);
|
explicit OpenWeatherMap(QObject *parent = 0);
|
||||||
|
void update(QString id);
|
||||||
|
void searchAutodetect();
|
||||||
|
void search(QString searchString);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QNetworkAccessManager *m_manager;
|
QNetworkAccessManager *m_manager;
|
||||||
@ -58,10 +61,6 @@ private:
|
|||||||
int m_humidity;
|
int m_humidity;
|
||||||
int m_cloudiness;
|
int m_cloudiness;
|
||||||
|
|
||||||
void updateLocationData();
|
|
||||||
void updateSearchData();
|
|
||||||
void updateWeatherData();
|
|
||||||
|
|
||||||
void processLocationResponse(QByteArray data);
|
void processLocationResponse(QByteArray data);
|
||||||
void processSearchResponse(QByteArray data);
|
void processSearchResponse(QByteArray data);
|
||||||
void processSearchLocationResponse(QByteArray data);
|
void processSearchLocationResponse(QByteArray data);
|
||||||
@ -69,11 +68,10 @@ private:
|
|||||||
|
|
||||||
signals:
|
signals:
|
||||||
void searchResultReady(const QList<QVariantMap> &cityList);
|
void searchResultReady(const QList<QVariantMap> &cityList);
|
||||||
|
void weatherDataReady(const QByteArray &data);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void update();
|
|
||||||
void searchAutodetect();
|
|
||||||
void search(const QString &searchString);
|
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void replyFinished(QNetworkReply *reply);
|
void replyFinished(QNetworkReply *reply);
|
||||||
|
|||||||
Reference in New Issue
Block a user