startLogic.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. #pragma once
  2. #include "uart/ProtocolSender.h"
  3. #include "uart/UartContext.h"
  4. #include "core/utilities.h"
  5. #include "edge/call_log.h"
  6. #include "net/tcp_client.h"
  7. #include "net/tcp_model.h"
  8. #include "utils/GpioHelper.h"
  9. #include "utils/TimeHelper.h"
  10. #include "utils/BrightnessHelper.h"
  11. #include <time.h>
  12. #include <thread>
  13. #include <string>
  14. #include <unistd.h>
  15. #include <sys/reboot.h>
  16. #include "service/BusinessConfig.h"
  17. static bool isHelpTimerRegistered = false;
  18. static bool sosTimerRegistered = true;
  19. static bool isSleepTimerRegistered = false; // 是否开启息屏定时任务
  20. static bool isSleep = false; // 是否在息屏
  21. #define HELP_TIMER_HANDLE 3 // 增援的定时器id
  22. #define SOS_CLICK_TIME_HANDLE 4 // sos的定时器id
  23. #define SOS_LAMP_TIME_HANDLE 5 // sos的门灯定时器id
  24. #define NURSING_TIME_HANDLE 6 // 护理的定时器id
  25. #define OXYGEN_TIME_HANDLE 7 // 吸氧倒计时
  26. #define EVENT_TIME_HANDLE 8 // 事件定时器
  27. #define SLEEP_STRAT_TIME_HANDLE 9 // 息屏
  28. namespace {
  29. std::string uilogic[] = {
  30. "testActivity",
  31. "ui3Activity"
  32. };
  33. void CloseUi();
  34. int feed_dogs = 0;
  35. void PrintCallLog() {
  36. CallLogEntries entries;
  37. int ret = GetCallLog(&entries);
  38. LOGD("GetCallRecord %d", ret);
  39. for (auto r : entries) {
  40. LOGD("id=%d, uri=%s, contact=%s,duration=%d,created_at=%d",
  41. r.id, r.uri.c_str(), r.contact.c_str(), r.duration, r.created_at);
  42. }
  43. }
  44. void OnCallStateChanged(voip::Telephone* telephone, int call_id, voip::State state) {
  45. LOGD("call state = %d", state);
  46. if (state == voip::STATE_CALL_INCOMING
  47. || state == voip::STATE_CALL_CALLING
  48. || state == voip::STATE_CALL_CONFIRMED) {
  49. //EASYUICONTEXT->goHome();
  50. //EASYUICONTEXT->openActivity("callActivity");
  51. //CloseUi();
  52. if (state == voip::STATE_CALL_INCOMING) {
  53. if (telephone->GetCallCount() > 1) {
  54. LOGD("call count > 1, return");
  55. telephone->Hangup(call_id, voip::SIP_STATUS_CODE_BUSY_HERE);
  56. return;
  57. }
  58. //提前显示视频
  59. //telephone->Answer(call_id, voip::SIP_STATUS_CODE_PROGRESS);
  60. // telephone->Answer();
  61. acceptSIP();
  62. }
  63. }
  64. if (state == voip::STATE_CALL_DISCONNECTED) {
  65. if (telephone->GetCallCount() > 1) {
  66. LOGD("call count > 1, return");
  67. return;
  68. }
  69. //EASYUICONTEXT->goBack();
  70. //EASYUICONTEXT->goHome();
  71. //EASYUICONTEXT->openActivity("mainActivity");
  72. //保存通话记录
  73. voip::CallInfo info = telephone->GetCallInfo(call_id);
  74. CallLogEntry entry;
  75. entry.uri = info.remote_uri;
  76. entry.contact = info.remote_contact;
  77. entry.duration = info.duration/1000;
  78. if (0 != PutCallLog(entry)) {
  79. LOGE("保存通话记录失败");
  80. }
  81. int n = GetCallLogCount();
  82. if (n > CALL_LOG_COUNT_MAX) {
  83. //超过最大通话记录条数,删除旧记录
  84. if (0 != DeleteOldCallLog(n - CALL_LOG_COUNT_MAX)) {
  85. LOGE("删除失败");
  86. }
  87. }
  88. //PrintCallLog();
  89. }
  90. }
  91. void CloseUi() {
  92. for(int i = 0; i < 2; i++){
  93. EASYUICONTEXT->closeActivity(uilogic[i].c_str());
  94. }
  95. }
  96. void setBrightness(){
  97. int dayLight = StoragePreferences::getInt(STORE_DAY_LIGHT,100);
  98. int nightLight = StoragePreferences::getInt(STORE_NIGHT_LIGHT,10);
  99. if (checkIsDay()){
  100. BRIGHTNESSHELPER->setBrightness(dayLight);
  101. } else {
  102. BRIGHTNESSHELPER->setBrightness(nightLight);
  103. }
  104. }
  105. }
  106. // 接收tcp消息
  107. void handleMsg(byte* inBytes){
  108. LOGD("TCP received: %s", inBytes);
  109. const char* cstr = reinterpret_cast<const char*>(inBytes);
  110. string str = cstr;
  111. if (str == "1"){
  112. LOGD("get a heart beat");
  113. return;
  114. }
  115. TcpModel tcpModel;
  116. tcpModel = getTcpModel(inBytes);
  117. LOGD("tcp model: %s, %s", tcpModel.type.c_str(), tcpModel.action.c_str());
  118. if (tcpModel.type == TcpType::OTHER) {
  119. LOGD("trans tcp json failed");
  120. } else if (tcpModel.type == TcpType::TIME) {
  121. if (tcpModel.action == TimeAction::SYNC) {
  122. string serverTime = "";
  123. if (tcpModel.data != ""){
  124. serverTime = tcpModel.data;
  125. } else {
  126. serverTime = tcpModel.json["time"].asString();
  127. }
  128. LOGD("sync time : %s", serverTime.c_str());
  129. time_t timet = stoi(serverTime);
  130. struct tm *t = gmtime(&timet);
  131. char pDate[25];
  132. sprintf(pDate,"%d-%02d-%02d %02d:%02d:%02d",
  133. 1900 + t->tm_year, 1+ t->tm_mon, t->tm_mday,
  134. t->tm_hour + 8,t->tm_min,t->tm_sec);
  135. LOGD("transfered time : %s", pDate);
  136. TimeHelper::setDateTime(pDate);
  137. }
  138. } else if (tcpModel.type == TcpType::REINFORCE){
  139. if (tcpModel.action == ReinforceAction::RESPONSED){
  140. //回调
  141. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  142. if (callback.tid != "0"){
  143. callback.onSuccess(tcpModel.json);
  144. }
  145. cancelAutoBtnHelpTimer();
  146. SetPainterInfo(0xFF37C127, LANGUAGEMANAGER->getValue("HelpResponse"));
  147. setReinforce(false); // 设置增援状态为true
  148. //3秒后还原白色
  149. if (!isHelpTimerRegistered){
  150. mActivityPtr->registerUserTimer(HELP_TIMER_HANDLE, 3000);
  151. }
  152. }
  153. } else if (tcpModel.type == TcpType::DEVICE){
  154. if (tcpModel.action == DeviceAction::APP_UPDATE){ //软件升级
  155. if (isSleep) {
  156. isSleep = false;
  157. BRIGHTNESSHELPER->screenOn();
  158. }
  159. Intent* intent = new Intent();
  160. intent->putExtra(appUpdate, "true");
  161. EASYUICONTEXT->openActivity("DeviceUpdateActivity", intent);
  162. }
  163. else if (tcpModel.action == DeviceAction::RESTART) { // 重启设备
  164. sync();
  165. reboot(RB_AUTOBOOT);
  166. }
  167. else if (tcpModel.action == DeviceAction::DEVICE_REFRESH) { // 设备刷新
  168. dataRefresh();
  169. }
  170. else if (tcpModel.action == DeviceAction::SYSTEM_SETTING) { // 修改科室设置
  171. getPartSetting();
  172. }
  173. } else if (tcpModel.type == TcpType::DATA){
  174. if (tcpModel.action == DataAction::REFRESH){ // 刷新数据
  175. dataRefresh();
  176. }
  177. } else if (tcpModel.type == TcpType::VOICE){
  178. if (tcpModel.action == VoiceAction::FAILED){
  179. callActivityFinish(CallFinishType::FAILED);
  180. } else if (tcpModel.action == VoiceAction::SUCCESS){
  181. CallingStatus::instance()->setTcpModel(tcpModel); // 只有呼叫成功才闪红灯
  182. std::string heartStr = "DOORLED,200F"; // 红灯闪烁
  183. const char* sendMsg = heartStr.c_str();
  184. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  185. } else if (tcpModel.action == VoiceAction::ACCEPT){
  186. if (CallingStatus::instance()->busy()){
  187. CallingStatus::instance()->setTcpModel(tcpModel);
  188. std::string toSipId = tcpModel.json["toSipId"].asString();
  189. callActivityFinish(CallFinishType::ACCEPT);
  190. //接听
  191. buildSIP(toSipId);
  192. }
  193. // 还原成无灯光的状态
  194. std::string heartStr = "DOORLED,000F";
  195. const char* sendMsg = heartStr.c_str();
  196. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  197. } else if (tcpModel.action == VoiceAction::REJECT){
  198. if (CallingStatus::instance()->busy()){
  199. callActivityFinish(CallFinishType::REJECT);
  200. // 还原成无灯光的状态
  201. std::string heartStr = "DOORLED,000F";
  202. const char* sendMsg = heartStr.c_str();
  203. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  204. }
  205. } else if (tcpModel.action == VoiceAction::CANCEL){
  206. //CallingStatus::instance()->setTcpModel(tcpModel);
  207. if (CallingStatus::instance()->busy()){
  208. callActivityFinish(CallFinishType::CANCEL);
  209. }
  210. } else if (tcpModel.action == VoiceAction::CALLING){
  211. //CallingStatus::instance()->setTcpModel(tcpModel);
  212. //对方忙线
  213. callActivityFinish(CallFinishType::BUSY);
  214. } else if (tcpModel.action == VoiceAction::CALL){
  215. //我方忙线判断
  216. if (CallingStatus::instance()->busy()){
  217. sendVoiceTcp(VoiceAction::CALLING, tcpModel, tcpModel.from_id);
  218. } else {
  219. CallingStatus::instance()->setTcpModel(tcpModel);
  220. //来电话了
  221. Intent* intent = new Intent();
  222. intent->putExtra(isOutgoing, "false");
  223. intent->putExtra(audioOnly, "true");
  224. EASYUICONTEXT->openActivity("callActivity", intent);
  225. }
  226. } else if (tcpModel.action == VoiceAction::HANDOFF){
  227. //判断 是否同一个对话
  228. long iId = CallingStatus::instance()->getInteractionId();
  229. if (iId > 0 && CallingStatus::instance()->busy()){
  230. long inId = tcpModel.json["id"].asInt();
  231. if (iId == inId && CallingStatus::instance()->busy()){
  232. callActivityFinish(CallFinishType::HANDOFF);
  233. }
  234. }
  235. }
  236. }
  237. else if (tcpModel.type == TcpType::SOS) {
  238. if (tcpModel.action == SosAction::CANCEL) {
  239. // SOS还原成无灯光的状态
  240. std::string heartStr = "ULED,0F";
  241. const char* sendMsg = heartStr.c_str();
  242. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  243. // 门灯还原成无灯光的状态
  244. std::string heartStr2 = "DOORLED,000F";
  245. const char* sendMsg2 = heartStr2.c_str();
  246. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg2), strlen(sendMsg2));
  247. mActivityPtr->unregisterUserTimer(SOS_LAMP_TIME_HANDLE);
  248. }
  249. }
  250. else if (tcpModel.type == TcpType::SIDE) {
  251. if (tcpModel.action == SideAction::NURSING) {
  252. // 将tcpModel缓存起来
  253. setNursingTcpModel(tcpModel);
  254. // 收到nursing,需要把通话给挂断了
  255. voip::CallInfo info = GetTelephone()->GetCallInfo();
  256. LOGD("info state = %d",info.state);
  257. if (info.state == voip::STATE_CALL_CALLING
  258. || info.state == voip::STATE_CALL_CALLING
  259. || info.state == voip::STATE_CALL_CONFIRMED){
  260. TcpModel storeModel = CallingStatus::instance()->getTcpModel();
  261. //只回传iId
  262. storeModel.data = storeModel.json["id"].asString();
  263. sendVoiceTcp(VoiceAction::HANDOFF, storeModel, storeModel.from_id);
  264. GetTelephone()->Hangup(info.id, voip::SIP_STATUS_CODE_DECLINE);
  265. GetTelephone()->Hangup();
  266. EASYUICONTEXT->goBack();
  267. std::string heartStr = "DOORLED,000F";
  268. const char* sendMsg = heartStr.c_str();
  269. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  270. }
  271. // 删除这个定时器
  272. mActivityPtr->unregisterUserTimer(NURSING_TIME_HANDLE);
  273. LOGD("收到服务器返回的消息,关闭Nursing的定时器");
  274. }
  275. if (tcpModel.action == SideAction::NURSING_END) {
  276. // 收到nursing_end
  277. }
  278. }
  279. else if (tcpModel.type == TcpType::CALLBACK) {
  280. if (tcpModel.action == CallbackAction::ACK) {
  281. LOGD("CALLBACK ACK !!!!");
  282. //回调
  283. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  284. if (callback.tid != "0"){
  285. callback.onSuccess(tcpModel.json);
  286. }
  287. }
  288. else if (tcpModel.action == CallbackAction::SUCCESS) {
  289. //回调
  290. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  291. if (callback.tid != "0"){
  292. callback.onSuccess(tcpModel.json);
  293. }
  294. }
  295. else if (tcpModel.action == CallbackAction::FAILED) {
  296. //回调
  297. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  298. if (callback.tid != "0"){
  299. callback.onFalied(tcpModel.json);
  300. }
  301. }
  302. }
  303. }
  304. bool getSleepTimerRegistered() {
  305. return isSleepTimerRegistered;
  306. }
  307. // 触发定时任务
  308. void setSleepTimerRegistered(bool result) {
  309. isSleepTimerRegistered = result;
  310. LOGD("isSleepTimerRegistered = %d", isSleepTimerRegistered);
  311. // if (isSleepTimerRegistered) {
  312. // int sleepTime = StoragePreferences::getInt(STORE_SLEEP_TIME, 3);
  313. // mActivityPtr->registerUserTimer(SLEEP_STRAT_TIME_HANDLE, sleepTime * 60 * 1000);
  314. // } else { // 如果是false的话,就需要关闭定时器
  315. // mActivityPtr->unregisterUserTimer(SLEEP_STRAT_TIME_HANDLE); // 关闭定时器
  316. // }
  317. }
  318. void scrrenOn() {
  319. isSleep = false;
  320. BRIGHTNESSHELPER->screenOn();
  321. }
  322. //================================= IO 口操作
  323. //紧急按钮灯,明/灭
  324. void setSOS_A5(bool light){
  325. int result = -1;
  326. int slight = light?0:1;
  327. result = GpioHelper::output("A5", slight);
  328. if (result==0){
  329. LOGD("set A5 SOS light %d. success",light);
  330. } else {
  331. LOGD("set A5 SOS light %d. failed",light);
  332. }
  333. }
  334. //A6,A7,A8 RGB灯控制
  335. void setRGB_A678(bool R, bool G, bool B){
  336. int rR = -1, rG = -1, rB = -1;
  337. int sR = R?0:1, sG = G?0:1, sB = B?0:1;
  338. rR = GpioHelper::output("A6", sR);
  339. rG = GpioHelper::output("A7", sG);
  340. rB = GpioHelper::output("A8", sB);
  341. const char *strLog = "set %s light %d. %d";
  342. LOGD(strLog,"A6 R",R, rR);
  343. LOGD(strLog,"A7 G",G, rG);
  344. LOGD(strLog,"A8 B",B, rB);
  345. }
  346. //手柄按钮
  347. class A1GpioListener : public IGpioListener{
  348. public:
  349. bool onGpioEdge(const char *pPin) override {
  350. int state = GpioHelper::input("A1");
  351. LOGD("IGpioListener GPIO IS A1=%d", state);
  352. if (state==0){
  353. voip::CallInfo info = GetTelephone()->GetCallInfo();
  354. //拨打 或 挂断
  355. if (CallingStatus::instance()->busy()){
  356. callActivityFinish(CallFinishType::A1CLICK);
  357. } else {
  358. Intent* intent = new Intent();
  359. intent->putExtra(isOutgoing, "true");
  360. intent->putExtra(audioOnly, "true");
  361. EASYUICONTEXT->openActivity("callActivity", intent);
  362. }
  363. }
  364. return true;
  365. }
  366. void onGpioError(const char *pPin, int error) override {
  367. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  368. }
  369. };
  370. //7寸面板,呼出
  371. class A2GpioListener : public IGpioListener{
  372. public:
  373. bool onGpioEdge(const char *pPin) override {
  374. int state = GpioHelper::input("A2");
  375. LOGD("IGpioListener GPIO IS A2=%d", state);
  376. if (state==0){
  377. //拨打 或 接听
  378. }
  379. return true;
  380. }
  381. void onGpioError(const char *pPin, int error) override {
  382. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  383. }
  384. };
  385. //7寸面板,挂断
  386. class A3GpioListener : public IGpioListener{
  387. public:
  388. bool onGpioEdge(const char *pPin) override {
  389. int state = GpioHelper::input("A3");
  390. LOGD("IGpioListener GPIO IS A3=%d", state);
  391. if (state==0){
  392. //挂断
  393. }
  394. return true;
  395. }
  396. void onGpioError(const char *pPin, int error) override {
  397. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  398. }
  399. };
  400. //紧急按钮
  401. class A4GpioListener : public IGpioListener{
  402. public:
  403. bool onGpioEdge(const char *pPin) override {
  404. int state = GpioHelper::input("A4");
  405. LOGD("IGpioListener GPIO IS A4=%d", state);
  406. if (state==0){
  407. //发出
  408. setSOS_A5(true);
  409. }
  410. return true;
  411. }
  412. void onGpioError(const char *pPin, int error) override {
  413. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  414. }
  415. };
  416. /**
  417. * 注册定时器
  418. * 填充数组用于注册定时器
  419. * 注意:id不能重复
  420. */
  421. static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
  422. {1, 5000},
  423. {2, 60*1000}
  424. };
  425. /**
  426. * 当界面构造时触发
  427. */
  428. static void onUI_init(){
  429. //IO监测
  430. IGpioListener *iGpioListenerA1 = new A1GpioListener();
  431. GpioHelper::registerGpioListener("A1", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  432. IGpioListener *iGpioListenerA2 = new A2GpioListener();
  433. GpioHelper::registerGpioListener("A2", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  434. IGpioListener *iGpioListenerA3 = new A3GpioListener();
  435. GpioHelper::registerGpioListener("A3", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  436. IGpioListener *iGpioListenerA4 = new A4GpioListener();
  437. GpioHelper::registerGpioListener("A4", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  438. //TCP启动
  439. TcpClient::instance()->startTcp();
  440. //监听SIP信令
  441. GetTelephone()->AddCallStateListener(OnCallStateChanged);
  442. //请求版本号
  443. // if(UartContext::Uart3IsOpen()) {
  444. // string heartStr = "ASK,VNF-0";
  445. // const char* sendMsg = heartStr.c_str();
  446. // sendProtocolTo(UART_TTYS3, (byte*)(sendMsg), strlen(sendMsg));
  447. // }
  448. // if (isSleepTimerRegistered) { // 开启夜间息屏
  449. int sleepTime = StoragePreferences::getInt(STORE_SLEEP_TIME, 3);
  450. mActivityPtr->registerUserTimer(SLEEP_STRAT_TIME_HANDLE, sleepTime * 60 * 1000);
  451. // }
  452. #if 0
  453. std::thread backend([](){
  454. while (true) {
  455. if (feed_dogs > 0) {
  456. --feed_dogs;
  457. LOGD("feed");
  458. int ret = GpioHelper::output("GPIO_2", 1); //拉高
  459. if (ret != 0) {
  460. LOGE("GPIO操作失败");
  461. }
  462. usleep(1000 * 20);
  463. ret = GpioHelper::output("GPIO_2", 0); //拉低
  464. if (ret != 0) {
  465. LOGE("GPIO操作失败");
  466. }
  467. }
  468. usleep(1000 * 1000);
  469. }
  470. });
  471. backend.detach();
  472. #endif
  473. }
  474. /**
  475. * 当切换到该界面时触发
  476. */
  477. static void onUI_intent(const Intent *intentPtr) {
  478. if (intentPtr != NULL) {
  479. //TODO
  480. }
  481. }
  482. /*
  483. * 当界面显示时触发
  484. */
  485. static void onUI_show() {
  486. //进入主界面
  487. EASYUICONTEXT->openActivity("mainActivity");
  488. }
  489. /*
  490. * 当界面隐藏时触发
  491. */
  492. static void onUI_hide() {
  493. }
  494. /*
  495. * 当界面完全退出时触发
  496. */
  497. static void onUI_quit() {
  498. }
  499. /**
  500. * 串口数据回调接口
  501. */
  502. static void onProtocolDataUpdate(const SProtocolData &data) {
  503. LOGD("cmd = %s", data.cmd.c_str());
  504. if (data.state!=""){
  505. LOGD("state = %s",data.state.c_str());
  506. }
  507. // if (data.msg != ""){
  508. // LOGD("msg = %s", data.msg.c_str());
  509. // }
  510. if(UartContext::Uart3IsOpen()) { // 如果是true,表示串口打开了
  511. string heartStr;
  512. //按下
  513. if (data.state=="0"){ // 0表示正被按下,1表示短按松开,2表示长按松开。
  514. if (data.cmd == "KEY5") { // key5是面板拨号按钮
  515. if (isSleep) {
  516. scrrenOn();
  517. }
  518. else {
  519. voip::CallInfo info = GetTelephone()->GetCallInfo();
  520. //拨打 或 挂断
  521. if (CallingStatus::instance()->busy()){
  522. callActivityFinish(CallFinishType::A1CLICK);
  523. } else {
  524. Intent* intent = new Intent();
  525. intent->putExtra(isOutgoing, "true");
  526. intent->putExtra(audioOnly, "true");
  527. EASYUICONTEXT->openActivity("callActivity", intent);
  528. }
  529. }
  530. }
  531. else if (data.cmd == "KEY6"){ // key6是手柄触发,手柄只有呼叫和挂断功能,没有取消功能,防止用户不停地按
  532. if (isSleep) {
  533. scrrenOn();
  534. }
  535. else {
  536. voip::CallInfo info = GetTelephone()->GetCallInfo();
  537. //拨打 或 挂断
  538. if (CallingStatus::instance()->busy()){
  539. callActivityFinish(CallFinishType::KEY6_CLICK);
  540. } else {
  541. Intent* intent = new Intent();
  542. intent->putExtra(isOutgoing, "true");
  543. intent->putExtra(audioOnly, "true");
  544. EASYUICONTEXT->openActivity("callActivity", intent);
  545. }
  546. }
  547. }
  548. else if (data.cmd == "KEY7"){ // key7是紧急按钮
  549. if (isSleep) {
  550. scrrenOn();
  551. }
  552. if (sosTimerRegistered) {
  553. LOGD("TCP -> SOS_CALL");
  554. TcpModel tcpModel;
  555. tcpModel.type = TcpType::SOS;
  556. tcpModel.action = SosAction::CALL;
  557. tcpModel.from_id = StoragePreferences::getInt(STORE_DEVICE_ID, 0);
  558. std::string req = getTcpModelString(tcpModel);
  559. LOGD("TCP SOS_CALL : %s",req.c_str());
  560. TcpClient::instance()->sendMsg(req.c_str());
  561. // SOS亮红灯
  562. heartStr = "ULED,1F";
  563. // 门灯亮红灯
  564. std::string heartStr2 = "DOORLED,200F";
  565. const char* sendMsg2 = heartStr2.c_str();
  566. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg2), strlen(sendMsg2));
  567. sosTimerRegistered = false;
  568. mActivityPtr->registerUserTimer(SOS_CLICK_TIME_HANDLE, 10000); // 10秒后才能触发
  569. mActivityPtr->registerUserTimer(SOS_LAMP_TIME_HANDLE, 120000); // 2分钟后才能触发
  570. }
  571. }
  572. else if (data.cmd == "KEY<") { //下左3,红
  573. heartStr = "DOORLED,100F";
  574. }
  575. else if (data.cmd == "KEY>") { // key>是面板挂断按钮
  576. if (isSleep) {
  577. scrrenOn();
  578. }
  579. else {
  580. voip::CallInfo info = GetTelephone()->GetCallInfo();
  581. //拨打 或 挂断
  582. if (CallingStatus::instance()->busy()){
  583. callActivityFinish(CallFinishType::A1CLICK);
  584. heartStr = "DOORLED,000F";
  585. const char* sendMsg = heartStr.c_str();
  586. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  587. }
  588. }
  589. }
  590. else if (data.cmd == "KEY;"){ //下左4,白
  591. heartStr = "DOORLED,111F";
  592. }
  593. else {
  594. heartStr = "DOORLED,000F";
  595. }
  596. // 发送给板子的指令
  597. const char* sendMsg = heartStr.c_str();
  598. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  599. }
  600. }
  601. }
  602. /**
  603. * 定时器触发函数
  604. * 不建议在此函数中写耗时操作,否则将影响UI刷新
  605. * 参数: id
  606. * 当前所触发定时器的id,与注册时的id相同
  607. * 返回值: true
  608. * 继续运行当前定时器
  609. * false
  610. * 停止运行当前定时器
  611. */
  612. static bool onUI_Timer(int id){
  613. switch (id) {
  614. case 1:
  615. {
  616. //定时器跑在UI线程,这里将喂狗标记加1
  617. //++feed_dogs;
  618. //不用心跳
  619. // if(UartContext::Uart3IsOpen()) {
  620. // string heartStr = "HEART,1E";
  621. // const char* sendMsg = heartStr.c_str();
  622. // sendProtocolTo(UART_TTYS3, (byte*)(sendMsg), strlen(sendMsg));
  623. // }
  624. }
  625. break;
  626. case 2: //TCP心跳
  627. {
  628. const char* req = "0";
  629. TcpClient::instance()->sendMsg(req);
  630. }
  631. break;
  632. case HELP_TIMER_HANDLE:
  633. {
  634. SetPainterInfo(0xFFFFFFFF,"");
  635. setBtnHelpVisible(true);
  636. // //取消计时
  637. // if (isHelpTimerRegistered) {
  638. // mActivityPtr->unregisterUserTimer(HELP_TIMER_HANDLE);
  639. // isHelpTimerRegistered = false;
  640. // }
  641. return false;
  642. }
  643. break;
  644. case SOS_CLICK_TIME_HANDLE: {
  645. sosTimerRegistered = true;
  646. return false;
  647. }
  648. break;
  649. case SOS_LAMP_TIME_HANDLE: {
  650. // SOS还原成无灯光的状态
  651. std::string heartStr = "ULED,0F";
  652. const char* sendMsg = heartStr.c_str();
  653. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  654. // 门灯还原成无灯光的状态
  655. std::string heartStr2 = "DOORLED,000F";
  656. const char* sendMsg2 = heartStr2.c_str();
  657. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg2), strlen(sendMsg2));
  658. return false;
  659. }
  660. break;
  661. case SLEEP_STRAT_TIME_HANDLE: { // 息屏
  662. if (isSleepTimerRegistered) {
  663. isSleep = true;
  664. BRIGHTNESSHELPER->screenOff();
  665. } else { // 如果是false的话,就需要关闭定时器
  666. isSleep = false;
  667. BRIGHTNESSHELPER->screenOn();
  668. }
  669. }
  670. break;
  671. }
  672. return true;
  673. }
  674. /**
  675. * 有新的触摸事件时触发
  676. * 参数:ev
  677. * 新的触摸事件
  678. * 返回值:true
  679. * 表示该触摸事件在此被拦截,系统不再将此触摸事件传递到控件上
  680. * false
  681. * 触摸事件将继续传递到控件上
  682. */
  683. static bool onstartActivityTouchEvent(const MotionEvent &ev) {
  684. switch (ev.mActionStatus) {
  685. case MotionEvent::E_ACTION_DOWN://触摸按下
  686. //LOGD("时刻 = %ld 坐标 x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
  687. break;
  688. case MotionEvent::E_ACTION_MOVE://触摸滑动
  689. break;
  690. case MotionEvent::E_ACTION_UP: //触摸抬起
  691. break;
  692. default:
  693. break;
  694. }
  695. return false;
  696. }