updateLogic.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. #pragma once
  2. #include "uart/ProtocolSender.h"
  3. #include <sys/sysinfo.h>
  4. #include "manager/ConfigManager.h"
  5. #include "net/NetManager.h"
  6. #include "core/update_assistant.h"
  7. #include "edge/popup_service.h"
  8. #include "base/strings.hpp"
  9. #include "restclient-cpp/restclient.h"
  10. #include "base/http_client.h"
  11. #include "net/tcp_client.h"
  12. #include "os/UpgradeMonitor.h"
  13. #include "core/utilities.h"
  14. #include "core/jhws.h"
  15. #include "service/BusinessConfig.h"
  16. #include "manager/LanguageManager.h"
  17. /*
  18. *此文件由GUI工具生成
  19. *文件功能:用于处理用户的逻辑相应代码
  20. *功能说明:
  21. *========================onButtonClick_XXXX
  22. 当页面中的按键按下后系统会调用对应的函数,XXX代表GUI工具里面的[ID值]名称,
  23. 如Button1,当返回值为false的时候系统将不再处理这个按键,返回true的时候系统将会继续处理此按键。比如SYS_BACK.
  24. *========================onSlideWindowItemClick_XXXX(int index)
  25. 当页面中存在滑动窗口并且用户点击了滑动窗口的图标后系统会调用此函数,XXX代表GUI工具里面的[ID值]名称,
  26. 如slideWindow1;index 代表按下图标的偏移值
  27. *========================onSeekBarChange_XXXX(int progress)
  28. 当页面中存在滑动条并且用户改变了进度后系统会调用此函数,XXX代表GUI工具里面的[ID值]名称,
  29. 如SeekBar1;progress 代表当前的进度值
  30. *========================ogetListItemCount_XXXX()
  31. 当页面中存在滑动列表的时候,更新的时候系统会调用此接口获取列表的总数目,XXX代表GUI工具里面的[ID值]名称,
  32. 如List1;返回值为当前列表的总条数
  33. *========================oobtainListItemData_XXXX(ZKListView::ZKListItem *pListItem, int index)
  34. 当页面中存在滑动列表的时候,更新的时候系统会调用此接口获取列表当前条目下的内容信息,XXX代表GUI工具里面的[ID值]名称,
  35. 如List1;pListItem 是贴图中的单条目对象,index是列表总目的偏移量。具体见函数说明
  36. *========================常用接口===============
  37. *LOGD(...) 打印调试信息的接口
  38. *mTextXXXPtr->setText("****") 在控件TextXXX上显示文字****
  39. *mButton1Ptr->setSelected(true); 将控件mButton1设置为选中模式,图片会切换成选中图片,按钮文字会切换为选中后的颜色
  40. *mSeekBarPtr->setProgress(12) 在控件mSeekBar上将进度调整到12
  41. *mListView1Ptr->refreshListView() 让mListView1 重新刷新,当列表数据变化后调用
  42. *mDashbroadView1Ptr->setTargetAngle(120) 在控件mDashbroadView1上指针显示角度调整到120度
  43. *
  44. * 在Eclipse编辑器中 使用 “alt + /” 快捷键可以打开智能提示
  45. */
  46. namespace {
  47. base::UpdateAssistant assistant;
  48. base::VersionInfo info;
  49. enum Timer {
  50. kTimerProgress,
  51. };
  52. int loading_index = 0;
  53. }
  54. Json::Value linuxVersionList;
  55. string updateUrl;
  56. static void getLinuxVersionList() {
  57. std::string url = getHttpGateway() + "/util/get_linux_version_list";
  58. LOGD("请求Linux设备初始化版本列表. url = %s", url.c_str());
  59. //发起HTTP GET请求
  60. RestClient::Response r = RestClient::get(url);
  61. if (r.code != 200) {
  62. LOGD("请求Linux设备初始化版本列表-> 错误代码: %d", r.code);
  63. return;
  64. }
  65. LOGD("获得Linux设备初始化版本列表. result = %s", r.body.c_str());
  66. //解析json
  67. Json::Reader reader;
  68. Json::Value root;
  69. if(reader.parse(r.body, root, false)) {
  70. linuxVersionList = root;
  71. mDeviceTypeListViewPtr->refreshListView();
  72. }
  73. }
  74. void NavibarSetProgressWindowVisible(bool visible) {
  75. mWindowProgressPtr->setVisible(visible);
  76. }
  77. void NavibarSetDialog1WindowVisible(bool visible) {
  78. mWindowDialog1Ptr->setVisible(visible);
  79. }
  80. void NavibarSetProgressMessage(const std::string& msg) {
  81. mTextViewProgressMessagePtr->setText(msg);
  82. }
  83. void NavibarSetDialog1Message(const std::string& msg) {
  84. mTextViewDialog1Ptr->setText(msg);
  85. }
  86. void NavibarProgressAnimation(bool visible) {
  87. mTextViewProgressLoadingPtr->setVisible(visible);
  88. if (visible) {
  89. loading_index = 0;
  90. mActivityPtr->registerUserTimer(kTimerProgress, 100);
  91. return;
  92. }
  93. mActivityPtr->unregisterUserTimer(kTimerProgress);
  94. }
  95. void NavibarDialog1ClickOk() {
  96. while (PopupService::instance()->busy_) {
  97. PopupService::instance()->busy_ = false;
  98. }
  99. //EASYUICONTEXT->hideNaviBar();
  100. }
  101. struct sysinfo info2;
  102. static bool isUpdate = false;
  103. bool getIsUpdate() {
  104. return isUpdate;
  105. }
  106. void updateDevice(){
  107. sysinfo(&info2);
  108. LOGD("关闭页面前,totalram == %ld", info2.totalram);
  109. LOGD("关闭页面前,freeram == %ld", info2.freeram);
  110. EASYUICONTEXT->closeActivity("mianActivity");
  111. EASYUICONTEXT->closeActivity("callActivity");
  112. EASYUICONTEXT->closeActivity("settingActivity");
  113. EASYUICONTEXT->closeActivity("sipTestActivity");
  114. EASYUICONTEXT->closeActivity("ui3Activity");
  115. EASYUICONTEXT->closeActivity("warnActivity");
  116. sysinfo(&info2);
  117. LOGD("关闭页面后,存在延迟,totalram == %ld", info2.totalram);
  118. LOGD("关闭页面后,存在延迟,freeram == %ld", info2.freeram);
  119. isUpdate = true;
  120. PopupService::Show([](PopupService* srv){
  121. base::HttpClient client;
  122. client.SetConnectionTimeout(5000);
  123. client.SetTimeout(120 * 1000);
  124. std::string url = getHttpGateway() + "/" + info.url.c_str();
  125. LOGD("请求更新文件的url: %s", url.c_str());
  126. sysinfo(&info2);
  127. LOGD("进行下载前,totalram == %ld", info2.totalram);
  128. LOGD("进行下载前,freeram == %ld", info2.freeram);
  129. base::HttpRequest req("GET", url, ""); // 去获取文件
  130. const char* tmp_file = "/tmp/update.img";
  131. base::HttpResponse response =
  132. client.Do(req, tmp_file, [srv, info](int64_t dltotal,
  133. int64_t dlnow, int64_t ultotal, int64_t ulnow){
  134. LOGD("downloading %lld/%lld", dlnow, dltotal);
  135. string msg = LANGUAGEMANAGER->getValue("Downloading") + " %.0f%%";
  136. srv->SetMessage(base::format(msg.c_str(),
  137. dltotal == 0 ? 0 : (dlnow * 1.0/dltotal * 100)));
  138. return 0;
  139. });
  140. if (response.StatusCode() != 200) { // 下载失败
  141. string msg = LANGUAGEMANAGER->getValue("DownloadFailed") + "%d";
  142. srv->SetMessage(base::format(msg.c_str(), response.ErrorCode()));
  143. isUpdate = false;
  144. return -1;
  145. }
  146. // TODO: 应该去判断一下,下载的版本是否真的大于当前版本号
  147. // UpgradeMonitor::getInstance()->checkUpgradeFile("/mnt/extsd");
  148. // LOGD("img版本-> %s", "");
  149. const char* msg = "-1";
  150. TcpClient::instance()->sendMsg(msg);
  151. TcpClient::instance()->closeTcp();
  152. // ReleaseTelephone();
  153. pingThreadExitPending();
  154. system("touch /tmp/zkautoupgrade");
  155. UPGRADEMONITOR->checkUpgradeFile("/tmp");
  156. return 0;
  157. });
  158. }
  159. /**
  160. * 注册定时器
  161. * 填充数组用于注册定时器
  162. * 注意:id不能重复
  163. */
  164. static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
  165. //{0, 6000}, //定时器id=0, 时间间隔6秒
  166. //{1, 1000},
  167. };
  168. /**
  169. * 当界面构造时触发
  170. */
  171. static void onUI_init(){
  172. //Tips :添加 UI初始化的显示代码到这里,如:mText1Ptr->setText("123");
  173. std::string currentVersion = getVersionCode();
  174. int currentVersionNo = getVersionNo();
  175. mVersionPtr->setText(currentVersion); // 写入界面的版本
  176. mVersionNoPtr->setText(to_string(currentVersionNo)); // 写入界面的版本号
  177. mWindowProgressPtr->setVisible(false);
  178. mWindowDialog1Ptr->setVisible(false);
  179. }
  180. /**
  181. * 当切换到该界面时触发
  182. */
  183. static void onUI_intent(const Intent *intentPtr) {
  184. sysinfo(&info2);
  185. LOGD("跳转到升级页面,totalram == %ld", info2.totalram);
  186. LOGD("跳转到升级页面,freeram == %ld", info2.freeram);
  187. isUpdate = false;
  188. if (intentPtr != NULL) {
  189. std::string _appUpdate = intentPtr->getExtra(appUpdate);
  190. if (_appUpdate == "true") {
  191. int ret = assistant.GetLatest("bed", &info);
  192. // int currentVersionNo = StoragePreferences::getInt(JHWS_VERSION_NUMBER, GetVersionNo());
  193. int currentVersionNo = getVersionNo();
  194. LOGD("from server : %s %d, from device: %d", info.version.c_str(), info.versionNo, currentVersionNo);
  195. if (currentVersionNo < info.versionNo) {
  196. updateDevice();
  197. }
  198. else {
  199. EASYUICONTEXT->closeActivity("updateActivity");
  200. }
  201. }
  202. }
  203. }
  204. /*
  205. * 当界面显示时触发
  206. */
  207. static void onUI_show() {
  208. }
  209. /*
  210. * 当界面隐藏时触发
  211. */
  212. static void onUI_hide() {
  213. }
  214. /*
  215. * 当界面完全退出时触发
  216. */
  217. static void onUI_quit() {
  218. }
  219. /**
  220. * 串口数据回调接口
  221. */
  222. static void onProtocolDataUpdate(const SProtocolData &data) {
  223. }
  224. /**
  225. * 定时器触发函数
  226. * 不建议在此函数中写耗时操作,否则将影响UI刷新
  227. * 参数: id
  228. * 当前所触发定时器的id,与注册时的id相同
  229. * 返回值: true
  230. * 继续运行当前定时器
  231. * false
  232. * 停止运行当前定时器
  233. */
  234. static bool onUI_Timer(int id){
  235. switch (id) {
  236. case kTimerProgress: {
  237. loading_index = (loading_index + 1) % 8;
  238. mTextViewProgressLoadingPtr->setBackgroundPic(
  239. base::format("loading/%d.png", loading_index).c_str());
  240. }
  241. break;
  242. default:
  243. break;
  244. }
  245. return true;
  246. }
  247. /**
  248. * 有新的触摸事件时触发
  249. * 参数:ev
  250. * 新的触摸事件
  251. * 返回值:true
  252. * 表示该触摸事件在此被拦截,系统不再将此触摸事件传递到控件上
  253. * false
  254. * 触摸事件将继续传递到控件上
  255. */
  256. static bool onupdateActivityTouchEvent(const MotionEvent &ev) {
  257. switch (ev.mActionStatus) {
  258. case MotionEvent::E_ACTION_DOWN://触摸按下
  259. //LOGD("时刻 = %ld 坐标 x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
  260. break;
  261. case MotionEvent::E_ACTION_MOVE://触摸滑动
  262. break;
  263. case MotionEvent::E_ACTION_UP: //触摸抬起
  264. break;
  265. default:
  266. break;
  267. }
  268. return false;
  269. }
  270. static bool onButtonClick_sys_back(ZKButton *pButton) {
  271. LOGD(" ButtonClick sys_back !!!\n");
  272. return false;
  273. }
  274. static bool onButtonClick_ButtonUpdate(ZKButton *pButton) {
  275. LOGD(" ButtonClick ButtonUpdate !!!\n");
  276. mWindow1Ptr->hideWnd();
  277. PopupService::Show([](PopupService* srv){
  278. string msg = LANGUAGEMANAGER->getValue("Searching");
  279. srv->SetMessage(msg);
  280. if (!NETMANAGER->getEthernetManager()->isConnected()) {
  281. std::string msg = LANGUAGEMANAGER->getValue("EthernetDisconnect");
  282. srv->SetMessage(msg);
  283. mWindow1Ptr->showWnd();
  284. return -1;
  285. }
  286. int ret = assistant.GetLatest("bed", &info);
  287. if (ret != 0) {
  288. LOGE("get latest failed ret %d", ret);
  289. msg = LANGUAGEMANAGER->getValue("GetVersionFailed") + "%d";
  290. srv->SetMessage(base::format(msg.c_str(), ret));
  291. return ret;
  292. }
  293. // int currentVersionNo = StoragePreferences::getInt(JHWS_VERSION_NUMBER, GetVersionNo());
  294. int currentVersionNo = getVersionNo();
  295. // LOGD("from server : %s %d, from device: %d", info.version.c_str(), info.versionNo, currentVersionNo);
  296. LOGD("更新版本,服务器版本-> %s, 版本号-> %d, 本机版本号-> %d", info.version.c_str(), info.versionNo, currentVersionNo);
  297. if (currentVersionNo >= info.versionNo) { // 判断本身的版本大于新的版本时,不进行升级
  298. msg = LANGUAGEMANAGER->getValue("IsTheLastVersion"); // 已经是最新版本
  299. srv->SetMessage(msg);
  300. mWindow1Ptr->showWnd();
  301. return -1;
  302. }
  303. mVersionNewPtr->setText(info.version);
  304. mVersionNoNewPtr->setText(to_string(info.versionNo));
  305. mWindowFindPtr->showWnd();
  306. return 0;
  307. });
  308. return false;
  309. }
  310. static bool onButtonClick_ButtonInstantly(ZKButton *pButton) {
  311. LOGD(" ButtonClick ButtonInstantly !!!\n");
  312. updateDevice();
  313. return false;
  314. }
  315. static bool onButtonClick_ButtonDialog1(ZKButton *pButton) {
  316. LOGD(" ButtonClick ButtonDialog1 !!!\n");
  317. return false;
  318. }
  319. static bool onButtonClick_ButtonInit(ZKButton *pButton) {
  320. LOGD(" ButtonClick ButtonInit !!!\n");
  321. // getLinuxVersionList();
  322. // mWindowInitPtr->showWnd();
  323. mWindowPwdPtr->showWnd();
  324. return false;
  325. }
  326. static int getListItemCount_DeviceTypeListView(const ZKListView *pListView) {
  327. //LOGD("getListItemCount_DeviceTypeListView !\n");
  328. if (linuxVersionList.size() > 5) {
  329. return linuxVersionList.size();
  330. }
  331. return 5;
  332. }
  333. static void obtainListItemData_DeviceTypeListView(ZKListView *pListView,ZKListView::ZKListItem *pListItem, int index) {
  334. //LOGD(" obtainListItemData_ DeviceTypeListView !!!\n");
  335. ZKListView::ZKListSubItem* down = pListItem->findSubItemByID(ID_UPDATE_DownSubItem);
  336. string version = linuxVersionList[index]["type_name"].asString() + " " + linuxVersionList[index]["version_code"].asString();
  337. if (version != " ") {
  338. pListItem->setText(version);
  339. down->setText(LANGUAGEMANAGER->getValue("SoftDown"));
  340. }
  341. else {
  342. pListItem->setText("");
  343. down->setText("");
  344. }
  345. }
  346. static void onListItemClick_DeviceTypeListView(ZKListView *pListView, int index, int id) {
  347. //LOGD(" onListItemClick_ DeviceTypeListView !!!\n");
  348. // 小于的时候,代表在linuxVersionList里面
  349. if (linuxVersionList[index]["app_path"].asString() != "") {
  350. mWindowProgressPtr->showWnd();
  351. updateUrl = linuxVersionList[index]["app_path"].asString();
  352. PopupService::Show([](PopupService* srv){
  353. base::HttpClient client;
  354. client.SetConnectionTimeout(5000);
  355. client.SetTimeout(120 * 1000);
  356. std::string url = getHttpGateway() + "/" + updateUrl;
  357. LOGD("请求更新文件的url: %s", url.c_str());
  358. base::HttpRequest req("GET", url, ""); // 去获取文件
  359. const char* tmp_file = "/tmp/update.img";
  360. base::HttpResponse response =
  361. client.Do(req, tmp_file, [srv, info](int64_t dltotal,
  362. int64_t dlnow, int64_t ultotal, int64_t ulnow){
  363. LOGD("downloading %lld/%lld", dlnow, dltotal);
  364. string msg = LANGUAGEMANAGER->getValue("Downloading") + " %.0f%%";
  365. srv->SetMessage(base::format(msg.c_str(),
  366. dltotal == 0 ? 0 : (dlnow * 1.0/dltotal * 100)));
  367. return 0;
  368. });
  369. if (response.StatusCode() != 200) { // 下载失败
  370. string msg = LANGUAGEMANAGER->getValue("DownloadFailed") + "%d";
  371. srv->SetMessage(base::format(msg.c_str(), response.ErrorCode()));
  372. mWindowProgressPtr->hideWnd();
  373. return -1;
  374. }
  375. // TODO: 应该去判断一下,下载的版本是否真的大于当前版本号
  376. UpgradeMonitor::getInstance()->checkUpgradeFile("/mnt/extsd");
  377. LOGD("img版本-> %s", "");
  378. // const char* msg = "-1";
  379. // TcpClient::instance()->sendMsg(msg);
  380. system("touch /tmp/zkautoupgrade");
  381. UPGRADEMONITOR->checkUpgradeFile("/tmp");
  382. return 0;
  383. });
  384. }
  385. }
  386. static bool onButtonClick_BackButton(ZKButton *pButton) {
  387. LOGD(" ButtonClick BackButton !!!\n");
  388. mWindowInitPtr->hideWnd();
  389. return false;
  390. }
  391. static bool onButtonClick_FindBackButton(ZKButton *pButton) {
  392. LOGD(" ButtonClick FindBackButton !!!\n");
  393. EASYUICONTEXT->goBack();
  394. return false;
  395. }
  396. static void onEditTextChanged_EditTextPwd(const std::string &text) {
  397. //LOGD(" onEditTextChanged_ EditTextPwd %s !!!\n", text.c_str());
  398. }
  399. static bool onButtonClick_BtnPwdConfirm(ZKButton *pButton) {
  400. LOGD(" ButtonClick BtnPwdConfirm !!!\n");
  401. string pwd = mEditTextPwdPtr->getText();
  402. // 密码为666
  403. string cpwd = "666";
  404. if (pwd == cpwd){
  405. getLinuxVersionList();
  406. mWindowPwdPtr->hideWnd();
  407. mWindowInitPtr->showWnd();
  408. return false;
  409. } else {
  410. mTextPwdInfoPtr->setTextTr("PasswordWrong");
  411. }
  412. return false;
  413. }