DeviceUpdateLogic.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. //TCP启动
  76. TcpClient::instance()->closeTcp();
  77. system("touch /tmp/zkautoupgrade");
  78. UPGRADEMONITOR->checkUpgradeFile("/tmp");
  79. return 0;
  80. });
  81. }
  82. /**
  83. * 注册定时器
  84. * 填充数组用于注册定时器
  85. * 注意:id不能重复
  86. */
  87. static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
  88. //{0, 6000}, //定时器id=0, 时间间隔6秒
  89. //{1, 1000},
  90. };
  91. /**
  92. * 当界面构造时触发
  93. */
  94. static void onUI_init(){
  95. // mVersionPtr->setText(StoragePreferences::getString(WDKL_VERSION, GetVersion()));
  96. // int currentVersionNo = StoragePreferences::getInt(JHWS_VERSION_NUMBER, GetVersionNo());
  97. std::string currentVersion = getVersion();
  98. int currentVersionNo = getVersionNo();
  99. mVersionPtr->setText(currentVersion); // 写入界面的版本
  100. mVersionNoPtr->setText(to_string(currentVersionNo)); // 写入界面的版本号
  101. mWindowProgressPtr->setVisible(false);
  102. mWindowDialog1Ptr->setVisible(false);
  103. }
  104. /**
  105. * 当切换到该界面时触发
  106. */
  107. static void onUI_intent(const Intent *intentPtr) {
  108. if (intentPtr != NULL) {
  109. std::string _appUpdate = intentPtr->getExtra(appUpdate);
  110. if (_appUpdate == "true") {
  111. int ret = assistant.GetLatest("bed", &info);
  112. // int currentVersionNo = StoragePreferences::getInt(JHWS_VERSION_NUMBER, GetVersionNo());
  113. int currentVersionNo = getVersionNo();
  114. LOGD("from server : %s %d, from device: %d", info.version.c_str(), info.versionNo, currentVersionNo);
  115. if (currentVersionNo < info.versionNo) {
  116. updateDevice();
  117. }
  118. }
  119. }
  120. }
  121. /*
  122. * 当界面显示时触发
  123. */
  124. static void onUI_show() {
  125. }
  126. /*
  127. * 当界面隐藏时触发
  128. */
  129. static void onUI_hide() {
  130. }
  131. /*
  132. * 当界面完全退出时触发
  133. */
  134. static void onUI_quit() {
  135. }
  136. /**
  137. * 串口数据回调接口
  138. */
  139. static void onProtocolDataUpdate(const SProtocolData &data) {
  140. }
  141. /**
  142. * 定时器触发函数
  143. * 不建议在此函数中写耗时操作,否则将影响UI刷新
  144. * 参数: id
  145. * 当前所触发定时器的id,与注册时的id相同
  146. * 返回值: true
  147. * 继续运行当前定时器
  148. * false
  149. * 停止运行当前定时器
  150. */
  151. static bool onUI_Timer(int id){
  152. switch (id) {
  153. case kTimerProgress: {
  154. loading_index = (loading_index + 1) % 8;
  155. mTextViewProgressLoadingPtr->setBackgroundPic(
  156. base::format("loading/%d.png", loading_index).c_str());
  157. }
  158. break;
  159. default:
  160. break;
  161. }
  162. return true;
  163. }
  164. /**
  165. * 有新的触摸事件时触发
  166. * 参数:ev
  167. * 新的触摸事件
  168. * 返回值:true
  169. * 表示该触摸事件在此被拦截,系统不再将此触摸事件传递到控件上
  170. * false
  171. * 触摸事件将继续传递到控件上
  172. */
  173. static bool onDeviceUpdateActivityTouchEvent(const MotionEvent &ev) {
  174. switch (ev.mActionStatus) {
  175. case MotionEvent::E_ACTION_DOWN://触摸按下
  176. //LOGD("时刻 = %ld 坐标 x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
  177. break;
  178. case MotionEvent::E_ACTION_MOVE://触摸滑动
  179. break;
  180. case MotionEvent::E_ACTION_UP: //触摸抬起
  181. break;
  182. default:
  183. break;
  184. }
  185. return false;
  186. }
  187. static bool onButtonClick_sys_back(ZKButton *pButton) {
  188. LOGD(" ButtonClick sys_back !!!\n");
  189. return false;
  190. }
  191. static bool onButtonClick_ButtonUpdate(ZKButton *pButton) {
  192. LOGD(" ButtonClick ButtonUpdate !!!\n");
  193. mWindow1Ptr->hideWnd();
  194. PopupService::Show([](PopupService* srv){
  195. string msg = LANGUAGEMANAGER->getValue("Searching");
  196. srv->SetMessage(msg);
  197. if (!NETMANAGER->getEthernetManager()->isConnected()) {
  198. std::string msg = LANGUAGEMANAGER->getValue("EthernetDisconnect");
  199. srv->SetMessage(msg);
  200. return -1;
  201. }
  202. int ret = assistant.GetLatest("bed", &info);
  203. if (ret != 0) {
  204. LOGE("get latest failed ret %d", ret);
  205. msg = LANGUAGEMANAGER->getValue("GetVersionFailed") + "%d";
  206. srv->SetMessage(base::format(msg.c_str(), ret));
  207. return ret;
  208. }
  209. // int currentVersionNo = StoragePreferences::getInt(JHWS_VERSION_NUMBER, GetVersionNo());
  210. int currentVersionNo = getVersionNo();
  211. // LOGD("from server : %s %d, from device: %d", info.version.c_str(), info.versionNo, currentVersionNo);
  212. LOGD("更新版本,服务器版本-> %s, 版本号-> %d, 本机版本号-> %d", info.version.c_str(), info.versionNo, currentVersionNo);
  213. if (currentVersionNo >= info.versionNo) { // 判断本身的版本大于新的版本时,不进行升级
  214. msg = LANGUAGEMANAGER->getValue("IsTheLastVersion"); // 已经是最新版本
  215. srv->SetMessage(msg);
  216. mWindow1Ptr->showWnd();
  217. return -1;
  218. }
  219. mVersionNewPtr->setText(info.version);
  220. mVersionNoNewPtr->setText(to_string(info.versionNo));
  221. mWindowFindPtr->showWnd();
  222. return 0;
  223. });
  224. return false;
  225. }
  226. static bool onButtonClick_ButtonInstantly(ZKButton *pButton) {
  227. LOGD(" ButtonClick ButtonInstantly !!!\n");
  228. updateDevice();
  229. return false;
  230. }
  231. static bool onButtonClick_ButtonDialog1(ZKButton *pButton) {
  232. LOGD(" ButtonClick ButtonDialog1 !!!\n");
  233. return false;
  234. }