DeviceUpdateLogic.cc 7.4 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 "net/tcp_client.h"
  10. #include "os/UpgradeMonitor.h"
  11. #include "core/utilities.h"
  12. #include "core/jhws.h"
  13. #include "service/BusinessConfig.h"
  14. #include "manager/LanguageManager.h"
  15. namespace {
  16. base::UpdateAssistant assistant;
  17. base::VersionInfo info;
  18. enum Timer {
  19. kTimerProgress,
  20. };
  21. int loading_index = 0;
  22. }
  23. void NavibarSetProgressWindowVisible(bool visible) {
  24. mWindowProgressPtr->setVisible(visible);
  25. }
  26. void NavibarSetDialog1WindowVisible(bool visible) {
  27. mWindowDialog1Ptr->setVisible(visible);
  28. }
  29. void NavibarSetProgressMessage(const std::string& msg) {
  30. mTextViewProgressMessagePtr->setText(msg);
  31. }
  32. void NavibarSetDialog1Message(const std::string& msg) {
  33. mTextViewDialog1Ptr->setText(msg);
  34. }
  35. void NavibarProgressAnimation(bool visible) {
  36. mTextViewProgressLoadingPtr->setVisible(visible);
  37. if (visible) {
  38. loading_index = 0;
  39. mActivityPtr->registerUserTimer(kTimerProgress, 100);
  40. return;
  41. }
  42. mActivityPtr->unregisterUserTimer(kTimerProgress);
  43. }
  44. void NavibarDialog1ClickOk() {
  45. while (PopupService::instance()->busy_) {
  46. PopupService::instance()->busy_ = false;
  47. }
  48. //EASYUICONTEXT->hideNaviBar();
  49. }
  50. void updateDevice(){
  51. PopupService::Show([](PopupService* srv){
  52. base::HttpClient client;
  53. client.SetConnectionTimeout(5000);
  54. client.SetTimeout(120 * 1000);
  55. std::string url = getHttpGateway() + "/" + info.url.c_str();
  56. LOGD("请求更新文件的url: %s", url.c_str());
  57. base::HttpRequest req("GET", url, ""); // 去获取文件
  58. const char* tmp_file = "/tmp/update.img";
  59. base::HttpResponse response =
  60. client.Do(req, tmp_file, [srv, info](int64_t dltotal,
  61. int64_t dlnow, int64_t ultotal, int64_t ulnow){
  62. LOGD("downloading %lld/%lld", dlnow, dltotal);
  63. string msg = LANGUAGEMANAGER->getValue("Downloading") + " %.0f%%";
  64. srv->SetMessage(base::format(msg.c_str(),
  65. dltotal == 0 ? 0 : (dlnow * 1.0/dltotal * 100)));
  66. return 0;
  67. });
  68. if (response.StatusCode() != 200) { // 下载失败
  69. string msg = LANGUAGEMANAGER->getValue("DownloadFailed") + "%d";
  70. srv->SetMessage(base::format(msg.c_str(), response.ErrorCode()));
  71. return -1;
  72. }
  73. // TODO: 应该去判断一下,下载的版本是否真的大于当前版本号
  74. UpgradeMonitor::getInstance()->checkUpgradeFile("/mnt/extsd");
  75. LOGD("img版本-> %s", "");
  76. const char* msg = "-1";
  77. TcpClient::instance()->sendMsg(msg);
  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. }