From: Eric Ruei Date: Fri, 27 Oct 2017 15:54:22 +0000 (-0400) Subject: fix crash problems and update the openweathermap URLs X-Git-Url: https://git.ti.com/gitweb?p=apps%2Fthermostat-demo.git;a=commitdiff_plain;h=b03821290a5a8ddf191ccfd400d1a1726e13f9ec 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 --- 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 data) // Slot that updates all displayed values. Useful for unit changes, and invokes scale contents afterwards void ForecastDataWidget::updateData() { - for(int a = 0;asetText(formatTemperatureString(m_data[a]->highTemp(), m_globalSettings->temperatureFormat())); m_forecastDataWidgetGroup[a].lowTemp->setText(formatTemperatureString(m_data[a]->lowTemp(), m_globalSettings->temperatureFormat())); @@ -101,7 +101,7 @@ void ForecastDataWidget::scaleContents() //the total height of the widget (giving each row that proportion of the total space) int scaleFactor =(1/(FORECAST_DAYS*1.1))*this->height(); - for(int a = 0;asetStyleSheet("font-size:"+QString::number((int)(0.50*scaleFactor))+"px;"); 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 @@ MainWindow::MainWindow(QWidget *parent) : QWidget(parent) { + + QString http_proxy = QString::fromLatin1(qgetenv("http_proxy")); + // the MainWindow provides the container for the ThermostatWidget, WeatherWidget, and OptionsWidget // it is also provides much of the interface between WebData and the widgets that require web updates // 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) : //get a handle to the global settings singleton m_globalSettings = GlobalSettings::getInstance(); + // Initialize the proxy setting from http_proxy if defined + if(!http_proxy.isEmpty()) + { + QUrl url(http_proxy); + m_globalSettings->setProxyInfo(url.host(), url.port()); + } + // sets initial weather background setStyleSheet("mainwindow {border-image: url(:/Images/background-sunny-very-few-clouds.JPG)}"); @@ -304,13 +314,14 @@ void MainWindow::webDataFailed() setBackground(weatherData->icon(), weatherData->localTime().time()); //now iterate through the forecast days and do the same thing - QList::iterator i; - for(i=weatherData->forecastData().begin();i!=weatherData->forecastData().end();i++) + int i; + for(i = 0; i < weatherData->forecastData().size(); i++) { + ForecastData* forecast = weatherData->forecastData().at(i); - (*i)->setLowTemp(75+(qrand()%10*qrand()%2*(-1))); - (*i)->setHighTemp((*i)->lowTemp()+qrand()%10); - (*i)->setIcon(icons[qrand()%icons.size()]); + forecast->setLowTemp(75+(qrand()%10*qrand()%2*(-1))); + forecast->setHighTemp(forecast->lowTemp()+qrand()%10); + forecast->setIcon(icons[qrand()%icons.size()]); } //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() m_weatherReceived = false; //for openweather map we first must find the city - QString cityUrl = "http://openweathermap.org/data/2.1/find/name?q="+m_preparedCity; + QString cityUrl = "http://api.openweathermap.org/data/2.5/find?q="+m_preparedCity+"&appid=62da841a6b62ce48d249c6e2d00a2102"; // receive document and parse it QNetworkRequest request; @@ -84,7 +84,7 @@ void OpenWeatherMapDataEngine::responseReceived() } else { - qDebug() << "Network Error: " << m_reply->errorString(); + qDebug() << "responseReceived(): Network Error: (" << m_reply->error() << ")" << m_reply->errorString(); loadLocalData(); emit networkTimeout(); } @@ -96,7 +96,7 @@ void OpenWeatherMapDataEngine::dispatchWeatherDataRequests() m_weatherData->setCurrentCity(m_fullCity); //first send request for current weather - QString currentWeatherURL = "http://openweathermap.org/data/2.1/weather/city/"+QString::number(m_cityId); + QString currentWeatherURL = "http://api.openweathermap.org/data/2.5/weather?id="+QString::number(m_cityId)+"&appid=62da841a6b62ce48d249c6e2d00a2102"; QNetworkRequest request; request.setUrl(QUrl(currentWeatherURL)); @@ -110,7 +110,7 @@ void OpenWeatherMapDataEngine::dispatchWeatherDataRequests() connect(m_reply,SIGNAL(finished()),this,SLOT(currentWeatherResponseReceived())); //next send request for current weather - QString forecastWeatherURL = "http://openweathermap.org/data/2.1/forecast/city/"+QString::number(m_cityId); + QString forecastWeatherURL = "http://api.openweathermap.org/data/2.5/forecast?id="+QString::number(m_cityId)+"&appid=62da841a6b62ce48d249c6e2d00a2102"; request.setUrl(QUrl(forecastWeatherURL)); @@ -128,7 +128,8 @@ void OpenWeatherMapDataEngine::currentWeatherResponseReceived() if(m_reply->error() != QNetworkReply::NoError) { - qDebug() << "Network Error: " << m_reply->errorString(); + qDebug() << "OpenWeatherMapDataEngine::currentWeatherResponseReceived()"; + qDebug() << "Network Error: (" << m_reply->error() << ")" << m_reply->errorString(); emit networkTimeout(); m_reply->deleteLater(); return; @@ -148,7 +149,8 @@ void OpenWeatherMapDataEngine::forecastResponseReceived() { if(m_forecastReply->error() != QNetworkReply::NoError) { - qDebug() << "Network Error: " << m_reply->errorString(); + qDebug() << "OpenWeatherMapDataEngine::forecastResponseReceived()"; + qDebug() << "Network Error: (" << m_reply->error() << ")" << m_reply->errorString(); emit networkTimeout(); m_forecastReply->deleteLater(); return; @@ -291,7 +293,7 @@ qlonglong OpenWeatherMapDataEngine::parseCityInformation(QString jsonData) //must have an object set equal to the class data received from the web or qt throws parse error QScriptValue result = engine.evaluate("weatherObject="+jsonData); - if(result.property("message").toString() != "") + if(result.property("message").toString() != "accurate") return -1; else return result.property("list").property("0").property("id").toInteger(); @@ -437,5 +439,5 @@ bool OpenWeatherMapDataEngine::readFromCache(QString alternateCacheFile) QString OpenWeatherMapDataEngine::licenseString() { - return QString("Weather data from openweathermap.org under CC-BY-SA"); + return QString("Weather data from openweathermap.org under CC-BY-SA 4.0"); }