123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- /*
- * WeatherManager.cpp
- *
- * Created on: 2021年10月30日
- * Author: Administrator
- */
- #include "WeatherManager.h"
- #include "utils/log.h"
- #include "json/json.h"
- #include "net/NetManager.h"
- #include "storage/storagePreferences.h"
- #include "base/http_client.h"
- #include <unistd.h>
- #define TABLESIZE(ptr) sizeof(ptr)/sizeof(ptr[0])
- WeatherManager::WeatherManager() {
- // TODO 自动生成的构造函数存根
- //m_city = "深圳";
- m_city = StoragePreferences::getString("city", "深圳");
- updateUiCb = NULL;
- bforceSync = 1;
- //启动线程每两小时同步天气一次
- run();
- }
- WeatherManager::~WeatherManager() {
- // TODO 自动生成的析构函数存根
- }
- WeatherManager* WeatherManager::getInstance(void)
- {
- static WeatherManager WM;
- return &WM;
- }
- void WeatherManager::setCityName(const std::string city)
- {
- m_city = city;
- }
- const std::string WeatherManager::getCityName(void)
- {
- return m_city;
- }
- void WeatherManager::setTemperature(const std::string temperature)
- {
- m_temperature = temperature;
- }
- const std::string WeatherManager::getTemperature(void)
- {
- return m_temperature;
- }
- void WeatherManager::setWeather(const std::string weather)
- {
- m_weather = weather;
- }
- const std::string WeatherManager::getWeather(void)
- {
- return m_weather;
- }
- void WeatherManager::setWeaIcon(const std::string weaIcon)
- {
- m_weaIcon = weaIcon;
- }
- const std::string WeatherManager::getWeaIcon(void)
- {
- return m_weaIcon;
- }
- const int WeatherManager::syncWeather(void)
- {
- LOGD("--%d-- --%s-- 准备同步天气信息!!", __LINE__, __FILE__);
- sleep(500);
- if(true == m_city.empty())
- {
- LOGD("--%d-- --%s--\n 城市名为空!!", __LINE__, __FILE__);
- return 1;
- }
- if(!NETMANAGER->getEthernetManager()->isConnected()){
- return 1;
- }
- //发起HTTP GET请求
- char url[1024]={0};
- snprintf(url,TABLESIZE(url),"http://apis.juhe.cn/simpleWeather/query?city=%s&key=447439c56372ed79fb27581ebeba95dd", m_city.c_str());
- LOGD("sync weather:%s \n",url);
- base::HttpClient client;
- client.SetTimeout(5000);
- base::HttpRequest request("GET", url, "");
- base::HttpResponse r = client.Do(request);
- //将内容以文本形式显示出来
- if(200 == r.StatusCode())
- {
- LOGD("--%d-- --%s--\n r.code=%d 查询成功!!", __LINE__, __FILE__, r.StatusCode());
- if(StoragePreferences::getString("city", "深圳") != m_city){
- StoragePreferences::putString("city", m_city);
- }
- }
- else
- {
- LOGD("--%d-- --%s--\n r.code=%d 查询失败!!", __LINE__, __FILE__, r.StatusCode());
- return 1;
- }
- try{
- //解析json
- Json::Reader reader;
- Json::Value root2;
- if (reader.parse(r.Body(), root2, false))
- {
- //天气信息包含在result中
- if (root2.isMember("result"))
- {
- Json::Value sub1 = root2["result"];
- if (sub1.isObject()) {
- if (sub1.isMember("city")) {
- m_city = sub1["city"].asString();
- }
- if (sub1.isMember("realtime")) {
- Json::Value sub2 = sub1["realtime"];
- if (sub2.isObject()) {
- if (sub2.isMember("temperature")) {
- m_temperature = sub2["temperature"].asString();
- }
- if (sub2.isMember("info")) {
- m_weather = sub2["info"].asString();
- }
- if (sub2.isMember("wid")) {
- m_weaIcon = sub2["wid"].asString();
- }
- }
- }
- }
- }
- LOGD("--%d-- --%s--\n 城市:%s 温度:%s 天气:%s 天气图标:%s", __LINE__, __FILE__,
- m_city.c_str(), m_temperature.c_str(), m_weather.c_str(), m_weaIcon.c_str());
- }
- else
- {
- LOGD("--%d-- --%s--\n 解析失败!!", __LINE__, __FILE__);
- return 1;
- }
- }catch(int e){
- LOGD("get weather get error \n");
- return 0;
- }
- LOGD("--%d-- --%s-- 准备更新UI!!", __LINE__, __FILE__);
- //同步UI
- if(NULL != updateUiCb)
- {
- updateUiCb(NULL);
- }
- LOGD("--%d-- --%s-- 同步天气信息完毕!!", __LINE__, __FILE__);
- return 0;
- }
- bool WeatherManager::threadLoop() {
- //同步天气,城市名称为空则返回
- if (true == getCityName().empty()) {
- sleep(100);
- return true;
- }
- if (!((NETMANAGER->getWifiManager() && NETMANAGER->getWifiManager()->isConnected()) ||
- (NETMANAGER->getEthernetManager() && NETMANAGER->getEthernetManager()->isConnected()))) {
- sleep(100);
- return true;
- }
- sleep(100);
- syncWeather();
- bforceSync = 0;
- //每两个小时同步一次
- for (int level1 = 0; level1 < 2 * 60 * 60 * 10; ++level1) {
- //检查是否有退出线程的请求,如果有,则返回false,立即退出线程
- if (exitPending()) {
- return false;
- }
- sleep(100);
- if (bforceSync) {
- break;
- }
- }
- return true;
- }
|