]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - apps/thermostat-demo.git/blob - webdata.cpp
ThermostatDemo.pro.user: .pro.user files are specific to a machine and should
[apps/thermostat-demo.git] / webdata.cpp
1 #include "webdata.h"
2 #include "globalsettings.h"
4 #include "weatherdata.h"
5 #include "webdataengine/openweathermapdataengine.h"
6 #include "webdataengine/wundergrounddataengine.h"
8 #include "forecastdata.h"
10 #include <QtScript/QScriptEngine>
11 #include <QtScript/QScriptValueIterator>
13 #include <QtDebug>
14 #include <QTime>
16 /***********************************************************************************************************
17 * WebData
18 *   Class that wraps a webdataengine used for retrieving data from an online API. Handles network connection
19 *   manager and also configures the proxy, first by trying none and then a known one, then failing.
20 ************************************************************************************************************/
22 WebData::WebData(QObject *parent) :
23     QObject(parent)
24 {
25     m_globalSettings = GlobalSettings::getInstance();
27     // create network access manager
28     manager = new QNetworkAccessManager(this);
30     webDataEngine = new OpenWeatherMapDataEngine(manager);
31     connect(webDataEngine, SIGNAL(dataAvailable(WeatherData*)), this, SLOT(processDataAvailable(WeatherData*)));
32     connect(webDataEngine, SIGNAL(networkTimeout()), this, SLOT(processNetworkTimeout()));
34     //set proxy by default to none so we can try it
35     m_proxyState = WebData::InternalTI;
37     configureProxy();
39 }
41 //FUNCTION: configureProxy()
42 //
43 //  configures the correct proxy based on the ProxyState enum defined
45 void WebData::configureProxy()
46 {
47     //qDebug() << m_proxyState << manager->proxy().hostName() << manager->proxy().port();
49     switch(m_proxyState)
50     {
51     case WebData::ExternalTI:
52         configureNoProxy();
53         break;
54     case WebData::InternalTI:
55         configureTIProxy();
56         break;
57     case WebData::Configured:
58         configureSetProxy();
59         break;
60     default:
61         break;
62     }
63 }
65 //FUNCTION: configureNoProxy()
66 //
67 //  Sets the network access manager to use no proxy at all
69 void WebData::configureNoProxy()
70 {
71     manager->setProxy(QNetworkProxy(QNetworkProxy::NoProxy));
72     m_proxyState = WebData::ExternalTI;
73 }
75 //FUNCTION: configureTIProxy()
76 //
77 //  Sets the network access manager to use the internal TI proxy
79 void WebData::configureTIProxy()
80 {
81     manager->setProxy(QNetworkProxy(QNetworkProxy::HttpProxy, "wwwgate.ti.com", 80));
82     m_proxyState = WebData::InternalTI;
83 }
85 //FUNCTION: configureSetProxy()
86 //
87 //  Sets the network access manager to use proxy that is configured in the settings
89 void WebData::configureSetProxy()
90 {
91     QNetworkProxy proxy;
92     proxy.setType(QNetworkProxy::HttpProxy);
93     proxy.setHostName(m_globalSettings->proxyHost());
94     proxy.setPort(m_globalSettings->proxyPort());
95     manager->setProxy(proxy);
96     m_proxyState = WebData::Configured;
97 }
99 //FUNCTION: changeCity()
100 //
101 //  Sets the webDataEngine's city variable and then dispatches a request
103 void WebData::changeCity(QString city)
105     webDataEngine->setCity(city);
106     webDataEngine->dispatchRequest();
109 //FUNCTION: loadLocalData()
110 //
111 //  invokes the webdataengine's loadLocalData function to pull locally cached data
113 void WebData::loadLocalData()
115     webDataEngine->loadLocalData();
118 //FUNCTION: licenseString()
119 //
120 //  Returns the defined license string in the webdataengine
122 QString WebData::licenseString()
124     return webDataEngine->licenseString();
127 //FUNCTION: processDataAvailable()
128 //
129 //  Catches the dataAvailable signal from the webdataengine and sets the proxy if it worked
130 //  Otherwise emits signal with either web data or cache data
132 void WebData::processDataAvailable(WeatherData *weatherData)
134     if(!weatherData->cachedData())
135     {
136         m_globalSettings->setProxyInfo(manager->proxy().hostName(), manager->proxy().port());
137         m_proxyState = WebData::Configured;
138     }
140     emit dataAvailable(weatherData);
143 //FUNCTION: processNetworkTimeout()
144 //
145 //  Catches the networkTimeout signal form the webdataengine and tries a new proxy if
146 //  still in the configuration phase. Other wise emits a normal networkTimeout signal
148 void WebData::processNetworkTimeout()
150     //if we fail with a particular proxy, move on to the next in the list
151     //heirarchy is external (none) then TI then one that is set in the configuration
152     //once we get to configure call it quits on retrying
154     if(m_proxyState == WebData::ExternalTI)
155         configureTIProxy();
156     else if(m_proxyState == WebData::InternalTI)
157         configureSetProxy();
159     configureProxy();
161     if(m_proxyState != WebData::Configured)
162         webDataEngine->dispatchRequest();
163     else
164         emit networkTimeout();