diff options
author | Eric Ruei | 2017-10-27 10:54:22 -0500 |
---|---|---|
committer | Eric Ruei | 2017-10-27 10:54:22 -0500 |
commit | b03821290a5a8ddf191ccfd400d1a1726e13f9ec (patch) | |
tree | aafd1a2ba80984732c47aa80a6ee04793f71f935 | |
parent | dab34cf4c7dc0f321dcf8f13e6ebfb8316b95227 (diff) | |
download | thermostat-demo-b03821290a5a8ddf191ccfd400d1a1726e13f9ec.tar.gz thermostat-demo-b03821290a5a8ddf191ccfd400d1a1726e13f9ec.tar.xz thermostat-demo-b03821290a5a8ddf191ccfd400d1a1726e13f9ec.zip |
fix crash problems and update the openweathermap URLs
mainwindow.cpp:
- add default proxy setting
- replace QList iterator with QList index
forecastdatawidget.cpp: add boundary check at ::updateData()
openweathermapdataengine.cpp: use the latset URLs from openweathermap.com
Signed-off-by: Eric Ruei <e-ruei1@ti.com>
-rw-r--r-- | forecastdatawidget.cpp | 4 | ||||
-rw-r--r-- | mainwindow.cpp | 21 | ||||
-rw-r--r-- | webdataengine/openweathermapdataengine.cpp | 18 |
3 files changed, 28 insertions, 15 deletions
diff --git a/forecastdatawidget.cpp b/forecastdatawidget.cpp index 63fcbf7..ebabc57 100644 --- a/forecastdatawidget.cpp +++ b/forecastdatawidget.cpp | |||
@@ -80,7 +80,7 @@ void ForecastDataWidget::setData(QList<ForecastData *> data) | |||
80 | // Slot that updates all displayed values. Useful for unit changes, and invokes scale contents afterwards | 80 | // Slot that updates all displayed values. Useful for unit changes, and invokes scale contents afterwards |
81 | void ForecastDataWidget::updateData() | 81 | void ForecastDataWidget::updateData() |
82 | { | 82 | { |
83 | for(int a = 0;a<FORECAST_DAYS;a++) | 83 | for(int a = 0;a<FORECAST_DAYS && a<m_data.size();a++) |
84 | { | 84 | { |
85 | m_forecastDataWidgetGroup[a].highTemp->setText(formatTemperatureString(m_data[a]->highTemp(), m_globalSettings->temperatureFormat())); | 85 | m_forecastDataWidgetGroup[a].highTemp->setText(formatTemperatureString(m_data[a]->highTemp(), m_globalSettings->temperatureFormat())); |
86 | m_forecastDataWidgetGroup[a].lowTemp->setText(formatTemperatureString(m_data[a]->lowTemp(), m_globalSettings->temperatureFormat())); | 86 | m_forecastDataWidgetGroup[a].lowTemp->setText(formatTemperatureString(m_data[a]->lowTemp(), m_globalSettings->temperatureFormat())); |
@@ -101,7 +101,7 @@ void ForecastDataWidget::scaleContents() | |||
101 | //the total height of the widget (giving each row that proportion of the total space) | 101 | //the total height of the widget (giving each row that proportion of the total space) |
102 | int scaleFactor =(1/(FORECAST_DAYS*1.1))*this->height(); | 102 | int scaleFactor =(1/(FORECAST_DAYS*1.1))*this->height(); |
103 | 103 | ||
104 | for(int a = 0;a<FORECAST_DAYS;a++) | 104 | for(int a = 0;a<FORECAST_DAYS && a<m_data.size();a++) |
105 | { | 105 | { |
106 | m_forecastDataWidgetGroup[a].highTemp->setStyleSheet("font-size:"+QString::number((int)(0.50*scaleFactor))+"px;"); | 106 | m_forecastDataWidgetGroup[a].highTemp->setStyleSheet("font-size:"+QString::number((int)(0.50*scaleFactor))+"px;"); |
107 | m_forecastDataWidgetGroup[a].lowTemp->setStyleSheet("font-size:"+QString::number((int)(0.50*scaleFactor))+"px;"); | 107 | m_forecastDataWidgetGroup[a].lowTemp->setStyleSheet("font-size:"+QString::number((int)(0.50*scaleFactor))+"px;"); |
diff --git a/mainwindow.cpp b/mainwindow.cpp index b3f8994..9b89742 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp | |||
@@ -22,6 +22,9 @@ | |||
22 | MainWindow::MainWindow(QWidget *parent) : | 22 | MainWindow::MainWindow(QWidget *parent) : |
23 | QWidget(parent) | 23 | QWidget(parent) |
24 | { | 24 | { |
25 | |||
26 | QString http_proxy = QString::fromLatin1(qgetenv("http_proxy")); | ||
27 | |||
25 | // the MainWindow provides the container for the ThermostatWidget, WeatherWidget, and OptionsWidget | 28 | // the MainWindow provides the container for the ThermostatWidget, WeatherWidget, and OptionsWidget |
26 | // it is also provides much of the interface between WebData and the widgets that require web updates | 29 | // it is also provides much of the interface between WebData and the widgets that require web updates |
27 | // it is set to initial hard-coded values first, and only updated from the web if internet options | 30 | // it is set to initial hard-coded values first, and only updated from the web if internet options |
@@ -31,6 +34,13 @@ MainWindow::MainWindow(QWidget *parent) : | |||
31 | //get a handle to the global settings singleton | 34 | //get a handle to the global settings singleton |
32 | m_globalSettings = GlobalSettings::getInstance(); | 35 | m_globalSettings = GlobalSettings::getInstance(); |
33 | 36 | ||
37 | // Initialize the proxy setting from http_proxy if defined | ||
38 | if(!http_proxy.isEmpty()) | ||
39 | { | ||
40 | QUrl url(http_proxy); | ||
41 | m_globalSettings->setProxyInfo(url.host(), url.port()); | ||
42 | } | ||
43 | |||
34 | // sets initial weather background | 44 | // sets initial weather background |
35 | setStyleSheet("mainwindow {border-image: url(:/Images/background-sunny-very-few-clouds.JPG)}"); | 45 | setStyleSheet("mainwindow {border-image: url(:/Images/background-sunny-very-few-clouds.JPG)}"); |
36 | 46 | ||
@@ -304,13 +314,14 @@ void MainWindow::webDataFailed() | |||
304 | setBackground(weatherData->icon(), weatherData->localTime().time()); | 314 | setBackground(weatherData->icon(), weatherData->localTime().time()); |
305 | 315 | ||
306 | //now iterate through the forecast days and do the same thing | 316 | //now iterate through the forecast days and do the same thing |
307 | QList<ForecastData* >::iterator i; | 317 | int i; |
308 | for(i=weatherData->forecastData().begin();i!=weatherData->forecastData().end();i++) | 318 | for(i = 0; i < weatherData->forecastData().size(); i++) |
309 | { | 319 | { |
320 | ForecastData* forecast = weatherData->forecastData().at(i); | ||
310 | 321 | ||
311 | (*i)->setLowTemp(75+(qrand()%10*qrand()%2*(-1))); | 322 | forecast->setLowTemp(75+(qrand()%10*qrand()%2*(-1))); |
312 | (*i)->setHighTemp((*i)->lowTemp()+qrand()%10); | 323 | forecast->setHighTemp(forecast->lowTemp()+qrand()%10); |
313 | (*i)->setIcon(icons[qrand()%icons.size()]); | 324 | forecast->setIcon(icons[qrand()%icons.size()]); |
314 | } | 325 | } |
315 | 326 | ||
316 | //set the data just to trigger an update of the widgets | 327 | //set the data just to trigger an update of the widgets |
diff --git a/webdataengine/openweathermapdataengine.cpp b/webdataengine/openweathermapdataengine.cpp index 09581b3..b5554dd 100644 --- a/webdataengine/openweathermapdataengine.cpp +++ b/webdataengine/openweathermapdataengine.cpp | |||
@@ -36,7 +36,7 @@ void OpenWeatherMapDataEngine::dispatchRequest() | |||
36 | m_weatherReceived = false; | 36 | m_weatherReceived = false; |
37 | 37 | ||
38 | //for openweather map we first must find the city | 38 | //for openweather map we first must find the city |
39 | QString cityUrl = "http://openweathermap.org/data/2.1/find/name?q="+m_preparedCity; | 39 | QString cityUrl = "http://api.openweathermap.org/data/2.5/find?q="+m_preparedCity+"&appid=62da841a6b62ce48d249c6e2d00a2102"; |
40 | 40 | ||
41 | // receive document and parse it | 41 | // receive document and parse it |
42 | QNetworkRequest request; | 42 | QNetworkRequest request; |
@@ -84,7 +84,7 @@ void OpenWeatherMapDataEngine::responseReceived() | |||
84 | } | 84 | } |
85 | else | 85 | else |
86 | { | 86 | { |
87 | qDebug() << "Network Error: " << m_reply->errorString(); | 87 | qDebug() << "responseReceived(): Network Error: (" << m_reply->error() << ")" << m_reply->errorString(); |
88 | loadLocalData(); | 88 | loadLocalData(); |
89 | emit networkTimeout(); | 89 | emit networkTimeout(); |
90 | } | 90 | } |
@@ -96,7 +96,7 @@ void OpenWeatherMapDataEngine::dispatchWeatherDataRequests() | |||
96 | m_weatherData->setCurrentCity(m_fullCity); | 96 | m_weatherData->setCurrentCity(m_fullCity); |
97 | 97 | ||
98 | //first send request for current weather | 98 | //first send request for current weather |
99 | QString currentWeatherURL = "http://openweathermap.org/data/2.1/weather/city/"+QString::number(m_cityId); | 99 | QString currentWeatherURL = "http://api.openweathermap.org/data/2.5/weather?id="+QString::number(m_cityId)+"&appid=62da841a6b62ce48d249c6e2d00a2102"; |
100 | 100 | ||
101 | QNetworkRequest request; | 101 | QNetworkRequest request; |
102 | request.setUrl(QUrl(currentWeatherURL)); | 102 | request.setUrl(QUrl(currentWeatherURL)); |
@@ -110,7 +110,7 @@ void OpenWeatherMapDataEngine::dispatchWeatherDataRequests() | |||
110 | connect(m_reply,SIGNAL(finished()),this,SLOT(currentWeatherResponseReceived())); | 110 | connect(m_reply,SIGNAL(finished()),this,SLOT(currentWeatherResponseReceived())); |
111 | 111 | ||
112 | //next send request for current weather | 112 | //next send request for current weather |
113 | QString forecastWeatherURL = "http://openweathermap.org/data/2.1/forecast/city/"+QString::number(m_cityId); | 113 | QString forecastWeatherURL = "http://api.openweathermap.org/data/2.5/forecast?id="+QString::number(m_cityId)+"&appid=62da841a6b62ce48d249c6e2d00a2102"; |
114 | 114 | ||
115 | request.setUrl(QUrl(forecastWeatherURL)); | 115 | request.setUrl(QUrl(forecastWeatherURL)); |
116 | 116 | ||
@@ -128,7 +128,8 @@ void OpenWeatherMapDataEngine::currentWeatherResponseReceived() | |||
128 | 128 | ||
129 | if(m_reply->error() != QNetworkReply::NoError) | 129 | if(m_reply->error() != QNetworkReply::NoError) |
130 | { | 130 | { |
131 | qDebug() << "Network Error: " << m_reply->errorString(); | 131 | qDebug() << "OpenWeatherMapDataEngine::currentWeatherResponseReceived()"; |
132 | qDebug() << "Network Error: (" << m_reply->error() << ")" << m_reply->errorString(); | ||
132 | emit networkTimeout(); | 133 | emit networkTimeout(); |
133 | m_reply->deleteLater(); | 134 | m_reply->deleteLater(); |
134 | return; | 135 | return; |
@@ -148,7 +149,8 @@ void OpenWeatherMapDataEngine::forecastResponseReceived() | |||
148 | { | 149 | { |
149 | if(m_forecastReply->error() != QNetworkReply::NoError) | 150 | if(m_forecastReply->error() != QNetworkReply::NoError) |
150 | { | 151 | { |
151 | qDebug() << "Network Error: " << m_reply->errorString(); | 152 | qDebug() << "OpenWeatherMapDataEngine::forecastResponseReceived()"; |
153 | qDebug() << "Network Error: (" << m_reply->error() << ")" << m_reply->errorString(); | ||
152 | emit networkTimeout(); | 154 | emit networkTimeout(); |
153 | m_forecastReply->deleteLater(); | 155 | m_forecastReply->deleteLater(); |
154 | return; | 156 | return; |
@@ -291,7 +293,7 @@ qlonglong OpenWeatherMapDataEngine::parseCityInformation(QString jsonData) | |||
291 | //must have an object set equal to the class data received from the web or qt throws parse error | 293 | //must have an object set equal to the class data received from the web or qt throws parse error |
292 | QScriptValue result = engine.evaluate("weatherObject="+jsonData); | 294 | QScriptValue result = engine.evaluate("weatherObject="+jsonData); |
293 | 295 | ||
294 | if(result.property("message").toString() != "") | 296 | if(result.property("message").toString() != "accurate") |
295 | return -1; | 297 | return -1; |
296 | else | 298 | else |
297 | return result.property("list").property("0").property("id").toInteger(); | 299 | return result.property("list").property("0").property("id").toInteger(); |
@@ -437,5 +439,5 @@ bool OpenWeatherMapDataEngine::readFromCache(QString alternateCacheFile) | |||
437 | 439 | ||
438 | QString OpenWeatherMapDataEngine::licenseString() | 440 | QString OpenWeatherMapDataEngine::licenseString() |
439 | { | 441 | { |
440 | return QString("Weather data from openweathermap.org under CC-BY-SA"); | 442 | return QString("Weather data from openweathermap.org under CC-BY-SA 4.0"); |
441 | } | 443 | } |