DeviceUpdateLogic.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #pragma once
  2. #include "uart/ProtocolSender.h"
  3. #include "manager/ConfigManager.h"
  4. #include "net/NetManager.h"
  5. #include "core/update_assistant.h"
  6. #include "edge/popup_service.h"
  7. #include "base/strings.hpp"
  8. #include "base/http_client.h"
  9. #include "os/UpgradeMonitor.h"
  10. #include "core/utilities.h"
  11. #include "core/jhws.h"
  12. #include "service/BusinessConfig.h"
  13. #include "manager/LanguageManager.h"
  14. namespace {
  15. base::UpdateAssistant assistant;
  16. base::VersionInfo info;
  17. enum Timer {
  18. kTimerProgress,
  19. };
  20. int loading_index = 0;
  21. }
  22. void NavibarSetProgressWindowVisible(bool visible) {
  23. mWindowProgressPtr->setVisible(visible);
  24. }
  25. void NavibarSetDialog1WindowVisible(bool visible) {
  26. mWindowDialog1Ptr->setVisible(visible);
  27. }
  28. void NavibarSetProgressMessage(const std::string& msg) {
  29. mTextViewProgressMessagePtr->setText(msg);
  30. }
  31. void NavibarSetDialog1Message(const std::string& msg) {
  32. mTextViewDialog1Ptr->setText(msg);
  33. }
  34. void NavibarProgressAnimation(bool visible) {
  35. mTextViewProgressLoadingPtr->setVisible(visible);
  36. if (visible) {
  37. loading_index = 0;
  38. mActivityPtr->registerUserTimer(kTimerProgress, 100);
  39. return;
  40. }
  41. mActivityPtr->unregisterUserTimer(kTimerProgress);
  42. }
  43. void NavibarDialog1ClickOk() {
  44. while (PopupService::instance()->busy_) {
  45. PopupService::instance()->busy_ = false;
  46. }
  47. //EASYUICONTEXT->hideNaviBar();
  48. }
  49. void updateDevice(){
  50. PopupService::Show([](PopupService* srv){
  51. base::HttpClient client;
  52. client.SetConnectionTimeout(5000);
  53. client.SetTimeout(120 * 1000);
  54. std::string url = getHttpGateway() + "/" + info.url.c_str();
  55. LOGD("请求更新文件的url: %s", url.c_str());
  56. base::HttpRequest req("GET", url, ""); // 去获取文件
  57. const char* tmp_file = "/tmp/update.img";
  58. base::HttpResponse response =
  59. client.Do(req, tmp_file, [srv, info](int64_t dltotal,
  60. int64_t dlnow, int64_t ultotal, int64_t ulnow){
  61. LOGD("downloading %lld/%lld", dlnow, dltotal);
  62. string msg = LANGUAGEMANAGER->getValue("Downloading") + " %.0f%%";
  63. srv->SetMessage(base::format(msg.c_str(),
  64. dltotal == 0 ? 0 : (dlnow * 1.0/dltotal * 100)));
  65. return 0;
  66. });
  67. if (response.StatusCode() != 200) { // 下载失败
  68. string msg = LANGUAGEMANAGER->getValue("DownloadFailed") + "%d";
  69. srv->SetMessage(base::format(msg.c_str(), response.ErrorCode()));
  70. return -1;
  71. }
  72. // TODO: 应该去判断一下,下载的版本是否真的大于当前版本号
  73. UpgradeMonitor::getInstance()->checkUpgradeFile("/mnt/extsd");
  74. LOGD("img版本-> %s", "");
  75. // 更新设备的版本
  76. // StoragePreferences::putString(WDKL_VERSION, info.version);
  77. // StoragePreferences::putInt(JHWS_VERSION_NUMBER, info.versionNo);
  78. system("touch /tmp/zkautoupgrade");
  79. UPGRADEMONITOR->checkUpgradeFile("/tmp");
  80. return 0;
  81. });
  82. }
  83. /**
  84. * 注册定时器
  85. * 填充数组用于注册定时器
  86. * 注意:id不能重复
  87. */
  88. static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
  89. //{0, 6000}, //定时器id=0, 时间间隔6秒
  90. //{1, 1000},
  91. };
  92. /**
  93. * 当界面构造时触发
  94. */
  95. static void onUI_init(){
  96. // mVersionPtr->setText(StoragePreferences::getString(WDKL_VERSION, GetVersion()));
  97. // int currentVersionNo = StoragePreferences::getInt(JHWS_VERSION_NUMBER, GetVersionNo());
  98. std::string currentVersion = getVersion();
  99. int currentVersionNo = getVersionNo();
  100. mVersionPtr->setText(currentVersion); // 写入界面的版本
  101. mVersionNoPtr->setText(to_string(currentVersionNo)); // 写入界面的版本号
  102. mWindowProgressPtr->setVisible(false);
  103. mWindowDialog1Ptr->setVisible(false);
  104. }
  105. /**
  106. * 当切换到该界面时触发
  107. */
  108. static void onUI_intent(const Intent *intentPtr) {
  109. if (intentPtr != NULL) {
  110. std::string _appUpdate = intentPtr->getExtra(appUpdate);
  111. if (_appUpdate == "true") {
  112. int ret = assistant.GetLatest("bed", &info);
  113. // int currentVersionNo = StoragePreferences::getInt(JHWS_VERSION_NUMBER, GetVersionNo());
  114. int currentVersionNo = getVersionNo();
  115. LOGD("from server : %s %d, from device: %d", info.version.c_str(), info.versionNo, currentVersionNo);
  116. if (currentVersionNo < info.versionNo) {
  117. updateDevice();
  118. }
  119. }
  120. }
  121. }
  122. /*
  123. * 当界面显示时触发
  124. */
  125. static void onUI_show() {
  126. }
  127. /*
  128. * 当界面隐藏时触发
  129. */
  130. static void onUI_hide() {
  131. }
  132. /*
  133. * 当界面完全退出时触发
  134. */
  135. static void onUI_quit() {
  136. }
  137. /**
  138. * 串口数据回调接口
  139. */
  140. static void onProtocolDataUpdate(const SProtocolData &data) {
  141. }
  142. /**
  143. * 定时器触发函数
  144. * 不建议在此函数中写耗时操作,否则将影响UI刷新
  145. * 参数: id
  146. * 当前所触发定时器的id,与注册时的id相同
  147. * 返回值: true
  148. * 继续运行当前定时器
  149. * false
  150. * 停止运行当前定时器
  151. */
  152. static bool onUI_Timer(int id){
  153. switch (id) {
  154. case kTimerProgress: {
  155. loading_index = (loading_index + 1) % 8;
  156. mTextViewProgressLoadingPtr->setBackgroundPic(
  157. base::format("loading/%d.png", loading_index).c_str());
  158. }
  159. break;
  160. default:
  161. break;
  162. }
  163. return true;
  164. }
  165. /**
  166. * 有新的触摸事件时触发
  167. * 参数:ev
  168. * 新的触摸事件
  169. * 返回值:true
  170. * 表示该触摸事件在此被拦截,系统不再将此触摸事件传递到控件上
  171. * false
  172. * 触摸事件将继续传递到控件上
  173. */
  174. static bool onDeviceUpdateActivityTouchEvent(const MotionEvent &ev) {
  175. switch (ev.mActionStatus) {
  176. case MotionEvent::E_ACTION_DOWN://触摸按下
  177. //LOGD("时刻 = %ld 坐标 x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
  178. break;
  179. case MotionEvent::E_ACTION_MOVE://触摸滑动
  180. break;
  181. case MotionEvent::E_ACTION_UP: //触摸抬起
  182. break;
  183. default:
  184. break;
  185. }
  186. return false;
  187. }
  188. static bool onButtonClick_sys_back(ZKButton *pButton) {
  189. LOGD(" ButtonClick sys_back !!!\n");
  190. return false;
  191. }
  192. static bool onButtonClick_ButtonUpdate(ZKButton *pButton) {
  193. LOGD(" ButtonClick ButtonUpdate !!!\n");
  194. mWindow1Ptr->hideWnd();
  195. PopupService::Show([](PopupService* srv){
  196. string msg = LANGUAGEMANAGER->getValue("Searching");
  197. srv->SetMessage(msg);
  198. if (!NETMANAGER->getEthernetManager()->isConnected()) {
  199. std::string msg = LANGUAGEMANAGER->getValue("EthernetDisconnect");
  200. srv->SetMessage(msg);
  201. return -1;
  202. }
  203. int ret = assistant.GetLatest("bed", &info);
  204. if (ret != 0) {
  205. LOGE("get latest failed ret %d", ret);
  206. msg = LANGUAGEMANAGER->getValue("GetVersionFailed") + "%d";
  207. srv->SetMessage(base::format(msg.c_str(), ret));
  208. return ret;
  209. }
  210. // int currentVersionNo = StoragePreferences::getInt(JHWS_VERSION_NUMBER, GetVersionNo());
  211. int currentVersionNo = getVersionNo();
  212. // LOGD("from server : %s %d, from device: %d", info.version.c_str(), info.versionNo, currentVersionNo);
  213. LOGD("更新版本,服务器版本-> %s, 版本号-> %d, 本机版本号-> %d", info.version.c_str(), info.versionNo, currentVersionNo);
  214. if (currentVersionNo >= info.versionNo) { // 判断本身的版本大于新的版本时,不进行升级
  215. msg = LANGUAGEMANAGER->getValue("IsTheLastVersion"); // 已经是最新版本
  216. srv->SetMessage(msg);
  217. mWindow1Ptr->showWnd();
  218. return -1;
  219. }
  220. mVersionNewPtr->setText(info.version);
  221. mVersionNoNewPtr->setText(to_string(info.versionNo));
  222. mWindowFindPtr->showWnd();
  223. return 0;
  224. });
  225. return false;
  226. }
  227. static bool onButtonClick_ButtonInstantly(ZKButton *pButton) {
  228. LOGD(" ButtonClick ButtonInstantly !!!\n");
  229. updateDevice();
  230. return false;
  231. }
  232. static bool onButtonClick_ButtonDialog1(ZKButton *pButton) {
  233. LOGD(" ButtonClick ButtonDialog1 !!!\n");
  234. return false;
  235. }