WeatherManager.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * WeatherManager.cpp
  3. *
  4. * Created on: 2021年10月30日
  5. * Author: Administrator
  6. */
  7. #include "WeatherManager.h"
  8. #include "utils/log.h"
  9. #include "json/json.h"
  10. #include "net/NetManager.h"
  11. #include "storage/storagePreferences.h"
  12. #include "base/http_client.h"
  13. #include <unistd.h>
  14. #define TABLESIZE(ptr) sizeof(ptr)/sizeof(ptr[0])
  15. WeatherManager::WeatherManager() {
  16. // TODO 自动生成的构造函数存根
  17. //m_city = "深圳";
  18. m_city = StoragePreferences::getString("city", "深圳");
  19. updateUiCb = NULL;
  20. bforceSync = 1;
  21. //启动线程每两小时同步天气一次
  22. run();
  23. }
  24. WeatherManager::~WeatherManager() {
  25. // TODO 自动生成的析构函数存根
  26. }
  27. WeatherManager* WeatherManager::getInstance(void)
  28. {
  29. static WeatherManager WM;
  30. return &WM;
  31. }
  32. void WeatherManager::setCityName(const std::string city)
  33. {
  34. m_city = city;
  35. }
  36. const std::string WeatherManager::getCityName(void)
  37. {
  38. return m_city;
  39. }
  40. void WeatherManager::setTemperature(const std::string temperature)
  41. {
  42. m_temperature = temperature;
  43. }
  44. const std::string WeatherManager::getTemperature(void)
  45. {
  46. return m_temperature;
  47. }
  48. void WeatherManager::setWeather(const std::string weather)
  49. {
  50. m_weather = weather;
  51. }
  52. const std::string WeatherManager::getWeather(void)
  53. {
  54. return m_weather;
  55. }
  56. void WeatherManager::setWeaIcon(const std::string weaIcon)
  57. {
  58. m_weaIcon = weaIcon;
  59. }
  60. const std::string WeatherManager::getWeaIcon(void)
  61. {
  62. return m_weaIcon;
  63. }
  64. const int WeatherManager::syncWeather(void)
  65. {
  66. LOGD("--%d-- --%s-- 准备同步天气信息!!", __LINE__, __FILE__);
  67. sleep(500);
  68. if(true == m_city.empty())
  69. {
  70. LOGD("--%d-- --%s--\n 城市名为空!!", __LINE__, __FILE__);
  71. return 1;
  72. }
  73. if(!NETMANAGER->getEthernetManager()->isConnected()){
  74. return 1;
  75. }
  76. //发起HTTP GET请求
  77. char url[1024]={0};
  78. snprintf(url,TABLESIZE(url),"http://apis.juhe.cn/simpleWeather/query?city=%s&key=447439c56372ed79fb27581ebeba95dd", m_city.c_str());
  79. LOGD("sync weather:%s \n",url);
  80. base::HttpClient client;
  81. client.SetTimeout(5000);
  82. base::HttpRequest request("GET", url, "");
  83. base::HttpResponse r = client.Do(request);
  84. //将内容以文本形式显示出来
  85. if(200 == r.StatusCode())
  86. {
  87. LOGD("--%d-- --%s--\n r.code=%d 查询成功!!", __LINE__, __FILE__, r.StatusCode());
  88. if(StoragePreferences::getString("city", "深圳") != m_city){
  89. StoragePreferences::putString("city", m_city);
  90. }
  91. }
  92. else
  93. {
  94. LOGD("--%d-- --%s--\n r.code=%d 查询失败!!", __LINE__, __FILE__, r.StatusCode());
  95. return 1;
  96. }
  97. try{
  98. //解析json
  99. Json::Reader reader;
  100. Json::Value root2;
  101. if (reader.parse(r.Body(), root2, false))
  102. {
  103. //天气信息包含在result中
  104. if (root2.isMember("result"))
  105. {
  106. Json::Value sub1 = root2["result"];
  107. if (sub1.isObject()) {
  108. if (sub1.isMember("city")) {
  109. m_city = sub1["city"].asString();
  110. }
  111. if (sub1.isMember("realtime")) {
  112. Json::Value sub2 = sub1["realtime"];
  113. if (sub2.isObject()) {
  114. if (sub2.isMember("temperature")) {
  115. m_temperature = sub2["temperature"].asString();
  116. }
  117. if (sub2.isMember("info")) {
  118. m_weather = sub2["info"].asString();
  119. }
  120. if (sub2.isMember("wid")) {
  121. m_weaIcon = sub2["wid"].asString();
  122. }
  123. }
  124. }
  125. }
  126. }
  127. LOGD("--%d-- --%s--\n 城市:%s 温度:%s 天气:%s 天气图标:%s", __LINE__, __FILE__,
  128. m_city.c_str(), m_temperature.c_str(), m_weather.c_str(), m_weaIcon.c_str());
  129. }
  130. else
  131. {
  132. LOGD("--%d-- --%s--\n 解析失败!!", __LINE__, __FILE__);
  133. return 1;
  134. }
  135. }catch(int e){
  136. LOGD("get weather get error \n");
  137. return 0;
  138. }
  139. LOGD("--%d-- --%s-- 准备更新UI!!", __LINE__, __FILE__);
  140. //同步UI
  141. if(NULL != updateUiCb)
  142. {
  143. updateUiCb(NULL);
  144. }
  145. LOGD("--%d-- --%s-- 同步天气信息完毕!!", __LINE__, __FILE__);
  146. return 0;
  147. }
  148. bool WeatherManager::threadLoop() {
  149. //同步天气,城市名称为空则返回
  150. if (true == getCityName().empty()) {
  151. sleep(100);
  152. return true;
  153. }
  154. if (!((NETMANAGER->getWifiManager() && NETMANAGER->getWifiManager()->isConnected()) ||
  155. (NETMANAGER->getEthernetManager() && NETMANAGER->getEthernetManager()->isConnected()))) {
  156. sleep(100);
  157. return true;
  158. }
  159. sleep(100);
  160. syncWeather();
  161. bforceSync = 0;
  162. //每两个小时同步一次
  163. for (int level1 = 0; level1 < 2 * 60 * 60 * 10; ++level1) {
  164. //检查是否有退出线程的请求,如果有,则返回false,立即退出线程
  165. if (exitPending()) {
  166. return false;
  167. }
  168. sleep(100);
  169. if (bforceSync) {
  170. break;
  171. }
  172. }
  173. return true;
  174. }