DeviceUpdateLogic.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 "restclient-cpp/restclient.h"
  9. #include "base/http_client.h"
  10. #include "net/tcp_client.h"
  11. #include "os/UpgradeMonitor.h"
  12. #include "core/utilities.h"
  13. #include "core/jhws.h"
  14. #include "service/BusinessConfig.h"
  15. #include "manager/LanguageManager.h"
  16. namespace {
  17. base::UpdateAssistant assistant;
  18. base::VersionInfo info;
  19. enum Timer {
  20. kTimerProgress,
  21. };
  22. int loading_index = 0;
  23. }
  24. Json::Value linuxVersionList;
  25. string updateUrl;
  26. static void getLinuxVersionList() {
  27. std::string url = getHttpGateway() + "/util/get_linux_version_list";
  28. LOGD("请求Linux设备初始化版本列表. url = %s", url.c_str());
  29. //发起HTTP GET请求
  30. RestClient::Response r = RestClient::get(url);
  31. if (r.code != 200) {
  32. LOGD("请求Linux设备初始化版本列表-> 错误代码: %d", r.code);
  33. return;
  34. }
  35. LOGD("获得Linux设备初始化版本列表. result = %s", r.body.c_str());
  36. //解析json
  37. Json::Reader reader;
  38. Json::Value root;
  39. if(reader.parse(r.body, root, false)) {
  40. linuxVersionList = root;
  41. mDeviceTypeListViewPtr->refreshListView();
  42. }
  43. }
  44. void NavibarSetProgressWindowVisible(bool visible) {
  45. mWindowProgressPtr->setVisible(visible);
  46. }
  47. void NavibarSetDialog1WindowVisible(bool visible) {
  48. mWindowDialog1Ptr->setVisible(visible);
  49. }
  50. void NavibarSetProgressMessage(const std::string& msg) {
  51. mTextViewProgressMessagePtr->setText(msg);
  52. }
  53. void NavibarSetDialog1Message(const std::string& msg) {
  54. mTextViewDialog1Ptr->setText(msg);
  55. }
  56. void NavibarProgressAnimation(bool visible) {
  57. mTextViewProgressLoadingPtr->setVisible(visible);
  58. if (visible) {
  59. loading_index = 0;
  60. mActivityPtr->registerUserTimer(kTimerProgress, 100);
  61. return;
  62. }
  63. mActivityPtr->unregisterUserTimer(kTimerProgress);
  64. }
  65. void NavibarDialog1ClickOk() {
  66. while (PopupService::instance()->busy_) {
  67. PopupService::instance()->busy_ = false;
  68. }
  69. //EASYUICONTEXT->hideNaviBar();
  70. }
  71. void updateDevice(){
  72. PopupService::Show([](PopupService* srv){
  73. base::HttpClient client;
  74. client.SetConnectionTimeout(5000);
  75. client.SetTimeout(120 * 1000);
  76. std::string url = getHttpGateway() + "/" + info.url.c_str();
  77. LOGD("请求更新文件的url: %s", url.c_str());
  78. base::HttpRequest req("GET", url, ""); // 去获取文件
  79. const char* tmp_file = "/tmp/update.img";
  80. base::HttpResponse response =
  81. client.Do(req, tmp_file, [srv, info](int64_t dltotal,
  82. int64_t dlnow, int64_t ultotal, int64_t ulnow){
  83. LOGD("downloading %lld/%lld", dlnow, dltotal);
  84. string msg = LANGUAGEMANAGER->getValue("Downloading") + " %.0f%%";
  85. srv->SetMessage(base::format(msg.c_str(),
  86. dltotal == 0 ? 0 : (dlnow * 1.0/dltotal * 100)));
  87. return 0;
  88. });
  89. if (response.StatusCode() != 200) { // 下载失败
  90. string msg = LANGUAGEMANAGER->getValue("DownloadFailed") + "%d";
  91. srv->SetMessage(base::format(msg.c_str(), response.ErrorCode()));
  92. return -1;
  93. }
  94. // TODO: 应该去判断一下,下载的版本是否真的大于当前版本号
  95. UpgradeMonitor::getInstance()->checkUpgradeFile("/mnt/extsd");
  96. LOGD("img版本-> %s", "");
  97. const char* msg = "-1";
  98. TcpClient::instance()->sendMsg(msg);
  99. system("touch /tmp/zkautoupgrade");
  100. UPGRADEMONITOR->checkUpgradeFile("/tmp");
  101. return 0;
  102. });
  103. }
  104. /**
  105. * 注册定时器
  106. * 填充数组用于注册定时器
  107. * 注意:id不能重复
  108. */
  109. static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
  110. //{0, 6000}, //定时器id=0, 时间间隔6秒
  111. //{1, 1000},
  112. };
  113. /**
  114. * 当界面构造时触发
  115. */
  116. static void onUI_init(){
  117. // mVersionPtr->setText(StoragePreferences::getString(WDKL_VERSION, GetVersion()));
  118. // int currentVersionNo = StoragePreferences::getInt(JHWS_VERSION_NUMBER, GetVersionNo());
  119. std::string currentVersion = getVersion();
  120. int currentVersionNo = getVersionNo();
  121. mVersionPtr->setText(currentVersion); // 写入界面的版本
  122. mVersionNoPtr->setText(to_string(currentVersionNo)); // 写入界面的版本号
  123. mWindowProgressPtr->setVisible(false);
  124. mWindowDialog1Ptr->setVisible(false);
  125. }
  126. /**
  127. * 当切换到该界面时触发
  128. */
  129. static void onUI_intent(const Intent *intentPtr) {
  130. if (intentPtr != NULL) {
  131. std::string _appUpdate = intentPtr->getExtra(appUpdate);
  132. if (_appUpdate == "true") {
  133. int ret = assistant.GetLatest("bed", &info);
  134. // int currentVersionNo = StoragePreferences::getInt(JHWS_VERSION_NUMBER, GetVersionNo());
  135. int currentVersionNo = getVersionNo();
  136. LOGD("from server : %s %d, from device: %d", info.version.c_str(), info.versionNo, currentVersionNo);
  137. if (currentVersionNo < info.versionNo) {
  138. updateDevice();
  139. }
  140. }
  141. }
  142. }
  143. /*
  144. * 当界面显示时触发
  145. */
  146. static void onUI_show() {
  147. }
  148. /*
  149. * 当界面隐藏时触发
  150. */
  151. static void onUI_hide() {
  152. }
  153. /*
  154. * 当界面完全退出时触发
  155. */
  156. static void onUI_quit() {
  157. }
  158. /**
  159. * 串口数据回调接口
  160. */
  161. static void onProtocolDataUpdate(const SProtocolData &data) {
  162. }
  163. /**
  164. * 定时器触发函数
  165. * 不建议在此函数中写耗时操作,否则将影响UI刷新
  166. * 参数: id
  167. * 当前所触发定时器的id,与注册时的id相同
  168. * 返回值: true
  169. * 继续运行当前定时器
  170. * false
  171. * 停止运行当前定时器
  172. */
  173. static bool onUI_Timer(int id){
  174. switch (id) {
  175. case kTimerProgress: {
  176. loading_index = (loading_index + 1) % 8;
  177. mTextViewProgressLoadingPtr->setBackgroundPic(
  178. base::format("loading/%d.png", loading_index).c_str());
  179. }
  180. break;
  181. default:
  182. break;
  183. }
  184. return true;
  185. }
  186. /**
  187. * 有新的触摸事件时触发
  188. * 参数:ev
  189. * 新的触摸事件
  190. * 返回值:true
  191. * 表示该触摸事件在此被拦截,系统不再将此触摸事件传递到控件上
  192. * false
  193. * 触摸事件将继续传递到控件上
  194. */
  195. static bool onDeviceUpdateActivityTouchEvent(const MotionEvent &ev) {
  196. switch (ev.mActionStatus) {
  197. case MotionEvent::E_ACTION_DOWN://触摸按下
  198. //LOGD("时刻 = %ld 坐标 x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
  199. break;
  200. case MotionEvent::E_ACTION_MOVE://触摸滑动
  201. break;
  202. case MotionEvent::E_ACTION_UP: //触摸抬起
  203. break;
  204. default:
  205. break;
  206. }
  207. return false;
  208. }
  209. static bool onButtonClick_sys_back(ZKButton *pButton) {
  210. LOGD(" ButtonClick sys_back !!!\n");
  211. return false;
  212. }
  213. static bool onButtonClick_ButtonUpdate(ZKButton *pButton) {
  214. LOGD(" ButtonClick ButtonUpdate !!!\n");
  215. mWindow1Ptr->hideWnd();
  216. PopupService::Show([](PopupService* srv){
  217. string msg = LANGUAGEMANAGER->getValue("Searching");
  218. srv->SetMessage(msg);
  219. if (!NETMANAGER->getEthernetManager()->isConnected()) {
  220. std::string msg = LANGUAGEMANAGER->getValue("EthernetDisconnect");
  221. srv->SetMessage(msg);
  222. mWindow1Ptr->showWnd();
  223. return -1;
  224. }
  225. int ret = assistant.GetLatest("bed", &info);
  226. if (ret != 0) {
  227. LOGE("get latest failed ret %d", ret);
  228. msg = LANGUAGEMANAGER->getValue("GetVersionFailed") + "%d";
  229. srv->SetMessage(base::format(msg.c_str(), ret));
  230. return ret;
  231. }
  232. // int currentVersionNo = StoragePreferences::getInt(JHWS_VERSION_NUMBER, GetVersionNo());
  233. int currentVersionNo = getVersionNo();
  234. // LOGD("from server : %s %d, from device: %d", info.version.c_str(), info.versionNo, currentVersionNo);
  235. LOGD("更新版本,服务器版本-> %s, 版本号-> %d, 本机版本号-> %d", info.version.c_str(), info.versionNo, currentVersionNo);
  236. if (currentVersionNo >= info.versionNo) { // 判断本身的版本大于新的版本时,不进行升级
  237. msg = LANGUAGEMANAGER->getValue("IsTheLastVersion"); // 已经是最新版本
  238. srv->SetMessage(msg);
  239. mWindow1Ptr->showWnd();
  240. return -1;
  241. }
  242. mVersionNewPtr->setText(info.version);
  243. mVersionNoNewPtr->setText(to_string(info.versionNo));
  244. mWindowFindPtr->showWnd();
  245. return 0;
  246. });
  247. return false;
  248. }
  249. static bool onButtonClick_ButtonInstantly(ZKButton *pButton) {
  250. LOGD(" ButtonClick ButtonInstantly !!!\n");
  251. updateDevice();
  252. return false;
  253. }
  254. static bool onButtonClick_ButtonDialog1(ZKButton *pButton) {
  255. LOGD(" ButtonClick ButtonDialog1 !!!\n");
  256. return false;
  257. }
  258. static bool onButtonClick_ButtonInit(ZKButton *pButton) {
  259. LOGD(" ButtonClick ButtonInit !!!\n");
  260. // getLinuxVersionList();
  261. // mWindowInitPtr->showWnd();
  262. mWindowPwdPtr->showWnd();
  263. return false;
  264. }
  265. static int getListItemCount_DeviceTypeListView(const ZKListView *pListView) {
  266. //LOGD("getListItemCount_DeviceTypeListView !\n");
  267. if (linuxVersionList.size() > 5) {
  268. return linuxVersionList.size();
  269. }
  270. return 5;
  271. }
  272. static void obtainListItemData_DeviceTypeListView(ZKListView *pListView,ZKListView::ZKListItem *pListItem, int index) {
  273. //LOGD(" obtainListItemData_ DeviceTypeListView !!!\n");
  274. ZKListView::ZKListSubItem* down = pListItem->findSubItemByID(ID_DEVICEUPDATE_DownSubItem);
  275. string version = linuxVersionList[index]["type_name"].asString() + " " + linuxVersionList[index]["version_code"].asString();
  276. if (version != " ") {
  277. pListItem->setText(version);
  278. down->setText(LANGUAGEMANAGER->getValue("SoftDown"));
  279. }
  280. else {
  281. pListItem->setText("");
  282. down->setText("");
  283. }
  284. }
  285. static void onListItemClick_DeviceTypeListView(ZKListView *pListView, int index, int id) {
  286. //LOGD(" onListItemClick_ DeviceTypeListView !!!\n");
  287. // 小于的时候,代表在linuxVersionList里面
  288. if (linuxVersionList[index]["app_path"].asString() != "") {
  289. mWindowProgressPtr->showWnd();
  290. updateUrl = linuxVersionList[index]["app_path"].asString();
  291. PopupService::Show([](PopupService* srv){
  292. base::HttpClient client;
  293. client.SetConnectionTimeout(5000);
  294. client.SetTimeout(120 * 1000);
  295. std::string url = getHttpGateway() + "/" + updateUrl;
  296. LOGD("请求更新文件的url: %s", url.c_str());
  297. base::HttpRequest req("GET", url, ""); // 去获取文件
  298. const char* tmp_file = "/tmp/update.img";
  299. base::HttpResponse response =
  300. client.Do(req, tmp_file, [srv, info](int64_t dltotal,
  301. int64_t dlnow, int64_t ultotal, int64_t ulnow){
  302. LOGD("downloading %lld/%lld", dlnow, dltotal);
  303. string msg = LANGUAGEMANAGER->getValue("Downloading") + " %.0f%%";
  304. srv->SetMessage(base::format(msg.c_str(),
  305. dltotal == 0 ? 0 : (dlnow * 1.0/dltotal * 100)));
  306. return 0;
  307. });
  308. if (response.StatusCode() != 200) { // 下载失败
  309. string msg = LANGUAGEMANAGER->getValue("DownloadFailed") + "%d";
  310. srv->SetMessage(base::format(msg.c_str(), response.ErrorCode()));
  311. mWindowProgressPtr->hideWnd();
  312. return -1;
  313. }
  314. // TODO: 应该去判断一下,下载的版本是否真的大于当前版本号
  315. UpgradeMonitor::getInstance()->checkUpgradeFile("/mnt/extsd");
  316. LOGD("img版本-> %s", "");
  317. const char* msg = "-1";
  318. TcpClient::instance()->sendMsg(msg);
  319. system("touch /tmp/zkautoupgrade");
  320. UPGRADEMONITOR->checkUpgradeFile("/tmp");
  321. return 0;
  322. });
  323. }
  324. }
  325. static bool onButtonClick_BackButton(ZKButton *pButton) {
  326. LOGD(" ButtonClick BackButton !!!\n");
  327. mWindowInitPtr->hideWnd();
  328. return false;
  329. }
  330. static bool onButtonClick_FindBackButton(ZKButton *pButton) {
  331. LOGD(" ButtonClick FindBackButton !!!\n");
  332. EASYUICONTEXT->goBack();
  333. return false;
  334. }
  335. static void onEditTextChanged_EditTextPwd(const std::string &text) {
  336. //LOGD(" onEditTextChanged_ EditTextPwd %s !!!\n", text.c_str());
  337. }
  338. static bool onButtonClick_BtnPwdConfirm(ZKButton *pButton) {
  339. LOGD(" ButtonClick BtnPwdConfirm !!!\n");
  340. string pwd = mEditTextPwdPtr->getText();
  341. // 密码为666
  342. string cpwd = "666";
  343. if (pwd == cpwd){
  344. getLinuxVersionList();
  345. mWindowPwdPtr->hideWnd();
  346. mWindowInitPtr->showWnd();
  347. return false;
  348. } else {
  349. mTextPwdInfoPtr->setTextTr("PasswordWrong");
  350. }
  351. return false;
  352. }