startLogic.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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. setHelpButton("/button/button_help3.png", false);
  148. setReinforce(false); // 设置增援状态为true
  149. //3秒后还原白色
  150. if (!isHelpTimerRegistered){
  151. mActivityPtr->registerUserTimer(HELP_TIMER_HANDLE, 3000);
  152. }
  153. }
  154. } else if (tcpModel.type == TcpType::DEVICE){
  155. if (tcpModel.action == DeviceAction::APP_UPDATE){ //软件升级
  156. if (isSleep) {
  157. isSleep = false;
  158. BRIGHTNESSHELPER->screenOn();
  159. }
  160. Intent* intent = new Intent();
  161. intent->putExtra(appUpdate, "true");
  162. EASYUICONTEXT->openActivity("DeviceUpdateActivity", intent);
  163. }
  164. else if (tcpModel.action == DeviceAction::RESTART) { // 重启设备
  165. sync();
  166. reboot(RB_AUTOBOOT);
  167. }
  168. else if (tcpModel.action == DeviceAction::DEVICE_REFRESH) { // 设备刷新
  169. dataRefresh();
  170. }
  171. else if (tcpModel.action == DeviceAction::SYSTEM_SETTING) { // 修改科室设置
  172. getPartSetting();
  173. }
  174. } else if (tcpModel.type == TcpType::DATA){
  175. if (tcpModel.action == DataAction::REFRESH){ // 刷新数据
  176. dataRefresh();
  177. }
  178. } else if (tcpModel.type == TcpType::VOICE){
  179. if (tcpModel.action == VoiceAction::FAILED){
  180. callActivityFinish(CallFinishType::FAILED);
  181. } else if (tcpModel.action == VoiceAction::SUCCESS){
  182. CallingStatus::instance()->setTcpModel(tcpModel); // 只有呼叫成功才闪红灯
  183. std::string heartStr = "DOORLED,200F"; // 红灯闪烁
  184. const char* sendMsg = heartStr.c_str();
  185. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  186. } else if (tcpModel.action == VoiceAction::ACCEPT){
  187. if (CallingStatus::instance()->busy()){
  188. CallingStatus::instance()->setTcpModel(tcpModel);
  189. std::string toSipId = tcpModel.json["toSipId"].asString();
  190. callActivityFinish(CallFinishType::ACCEPT);
  191. //接听
  192. buildSIP(toSipId);
  193. }
  194. // 还原成无灯光的状态
  195. std::string heartStr = "DOORLED,000F";
  196. const char* sendMsg = heartStr.c_str();
  197. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  198. } else if (tcpModel.action == VoiceAction::REJECT){
  199. if (CallingStatus::instance()->busy()){
  200. callActivityFinish(CallFinishType::REJECT);
  201. // 还原成无灯光的状态
  202. std::string heartStr = "DOORLED,000F";
  203. const char* sendMsg = heartStr.c_str();
  204. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  205. }
  206. } else if (tcpModel.action == VoiceAction::CANCEL){
  207. //CallingStatus::instance()->setTcpModel(tcpModel);
  208. if (CallingStatus::instance()->busy()){
  209. callActivityFinish(CallFinishType::CANCEL);
  210. }
  211. } else if (tcpModel.action == VoiceAction::CALLING){
  212. //CallingStatus::instance()->setTcpModel(tcpModel);
  213. //对方忙线
  214. callActivityFinish(CallFinishType::BUSY);
  215. } else if (tcpModel.action == VoiceAction::CALL){
  216. //我方忙线判断
  217. if (CallingStatus::instance()->busy()){
  218. sendVoiceTcp(VoiceAction::CALLING, tcpModel, tcpModel.from_id);
  219. } else {
  220. CallingStatus::instance()->setTcpModel(tcpModel);
  221. //来电话了
  222. Intent* intent = new Intent();
  223. intent->putExtra(isOutgoing, "false");
  224. intent->putExtra(audioOnly, "true");
  225. EASYUICONTEXT->openActivity("callActivity", intent);
  226. }
  227. } else if (tcpModel.action == VoiceAction::HANDOFF){
  228. //判断 是否同一个对话
  229. long iId = CallingStatus::instance()->getInteractionId();
  230. if (iId > 0 && CallingStatus::instance()->busy()){
  231. long inId = tcpModel.json["id"].asInt();
  232. if (iId == inId && CallingStatus::instance()->busy()){
  233. callActivityFinish(CallFinishType::HANDOFF);
  234. }
  235. }
  236. }
  237. }
  238. else if (tcpModel.type == TcpType::SOS) {
  239. if (tcpModel.action == SosAction::CANCEL) {
  240. // SOS还原成无灯光的状态
  241. std::string heartStr = "ULED,0F";
  242. const char* sendMsg = heartStr.c_str();
  243. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  244. // 门灯还原成无灯光的状态
  245. std::string heartStr2 = "DOORLED,000F";
  246. const char* sendMsg2 = heartStr2.c_str();
  247. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg2), strlen(sendMsg2));
  248. mActivityPtr->unregisterUserTimer(SOS_LAMP_TIME_HANDLE);
  249. }
  250. }
  251. else if (tcpModel.type == TcpType::SIDE) {
  252. if (tcpModel.action == SideAction::NURSING) {
  253. // 将tcpModel缓存起来
  254. setNursingTcpModel(tcpModel);
  255. // 收到nursing,需要把通话给挂断了
  256. voip::CallInfo info = GetTelephone()->GetCallInfo();
  257. LOGD("info state = %d",info.state);
  258. if (info.state == voip::STATE_CALL_CALLING
  259. || info.state == voip::STATE_CALL_CALLING
  260. || info.state == voip::STATE_CALL_CONFIRMED){
  261. TcpModel storeModel = CallingStatus::instance()->getTcpModel();
  262. //只回传iId
  263. storeModel.data = storeModel.json["id"].asString();
  264. sendVoiceTcp(VoiceAction::HANDOFF, storeModel, storeModel.from_id);
  265. GetTelephone()->Hangup(info.id, voip::SIP_STATUS_CODE_DECLINE);
  266. GetTelephone()->Hangup();
  267. EASYUICONTEXT->goBack();
  268. std::string heartStr = "DOORLED,000F";
  269. const char* sendMsg = heartStr.c_str();
  270. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  271. }
  272. // 删除这个定时器
  273. mActivityPtr->unregisterUserTimer(NURSING_TIME_HANDLE);
  274. LOGD("收到服务器返回的消息,关闭Nursing的定时器");
  275. }
  276. else if (tcpModel.action == SideAction::NURSING_END) {
  277. // 收到nursing_end
  278. }
  279. else if (tcpModel.action == SideAction::CALL) {
  280. // 收到CALL,需要亮红灯
  281. std::string heartStr = "DOORLED,200F"; // 红灯闪烁
  282. const char* sendMsg = heartStr.c_str();
  283. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  284. }
  285. else if (tcpModel.action == SideAction::ACCEPT) {
  286. // 收到ACCEPT,需要灭灯
  287. // 还原成无灯光的状态
  288. std::string heartStr = "DOORLED,000F";
  289. const char* sendMsg = heartStr.c_str();
  290. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  291. }
  292. else if (tcpModel.action == SideAction::CANCEL) {
  293. // 收到CANCEL,需要灭灯
  294. // 还原成无灯光的状态
  295. std::string heartStr = "DOORLED,000F";
  296. const char* sendMsg = heartStr.c_str();
  297. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  298. }
  299. else if (tcpModel.action == SideAction::SOS_CALL) {
  300. // 收到SOS_CALL,需要亮红灯
  301. // SOS亮红灯
  302. string heartStr = "ULED,1F";
  303. // 门灯亮红灯
  304. std::string heartStr2 = "DOORLED,200F";
  305. const char* sendMsg2 = heartStr2.c_str();
  306. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg2), strlen(sendMsg2));
  307. sosTimerRegistered = false;
  308. mActivityPtr->registerUserTimer(SOS_CLICK_TIME_HANDLE, 10000); // 10秒后才能触发
  309. mActivityPtr->registerUserTimer(SOS_LAMP_TIME_HANDLE, 120000); // 2分钟后才能触发
  310. }
  311. else if (tcpModel.action == SideAction::SOS_CANCEL) {
  312. // 收到SOS_CANCEL,需要灭灯
  313. // 还原成无灯光的状态
  314. std::string heartStr = "DOORLED,000F";
  315. const char* sendMsg = heartStr.c_str();
  316. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  317. }
  318. }
  319. else if (tcpModel.type == TcpType::CALLBACK) {
  320. if (tcpModel.action == CallbackAction::ACK) {
  321. LOGD("CALLBACK ACK !!!!");
  322. //回调
  323. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  324. if (callback.tid != "0"){
  325. callback.onSuccess(tcpModel.json);
  326. }
  327. }
  328. else if (tcpModel.action == CallbackAction::SUCCESS) {
  329. //回调
  330. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  331. if (callback.tid != "0"){
  332. callback.onSuccess(tcpModel.json);
  333. }
  334. }
  335. else if (tcpModel.action == CallbackAction::FAILED) {
  336. //回调
  337. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  338. if (callback.tid != "0"){
  339. callback.onFalied(tcpModel.json);
  340. }
  341. callActivityFinish(CallFinishType::FAILED);
  342. }
  343. }
  344. }
  345. bool getSleepTimerRegistered() {
  346. return isSleepTimerRegistered;
  347. }
  348. // 触发定时任务
  349. void setSleepTimerRegistered(bool result) {
  350. isSleepTimerRegistered = result;
  351. // if (isSleepTimerRegistered) {
  352. // int sleepTime = StoragePreferences::getInt(STORE_SLEEP_TIME, 3);
  353. // mActivityPtr->registerUserTimer(SLEEP_STRAT_TIME_HANDLE, sleepTime * 60 * 1000);
  354. // } else { // 如果是false的话,就需要关闭定时器
  355. // mActivityPtr->unregisterUserTimer(SLEEP_STRAT_TIME_HANDLE); // 关闭定时器
  356. // }
  357. }
  358. void scrrenOn() {
  359. isSleep = false;
  360. BRIGHTNESSHELPER->screenOn();
  361. }
  362. //================================= IO 口操作
  363. //紧急按钮灯,明/灭
  364. void setSOS_A5(bool light){
  365. int result = -1;
  366. int slight = light?0:1;
  367. result = GpioHelper::output("A5", slight);
  368. if (result==0){
  369. LOGD("set A5 SOS light %d. success",light);
  370. } else {
  371. LOGD("set A5 SOS light %d. failed",light);
  372. }
  373. }
  374. //A6,A7,A8 RGB灯控制
  375. void setRGB_A678(bool R, bool G, bool B){
  376. int rR = -1, rG = -1, rB = -1;
  377. int sR = R?0:1, sG = G?0:1, sB = B?0:1;
  378. rR = GpioHelper::output("A6", sR);
  379. rG = GpioHelper::output("A7", sG);
  380. rB = GpioHelper::output("A8", sB);
  381. const char *strLog = "set %s light %d. %d";
  382. LOGD(strLog,"A6 R",R, rR);
  383. LOGD(strLog,"A7 G",G, rG);
  384. LOGD(strLog,"A8 B",B, rB);
  385. }
  386. //手柄按钮
  387. class A1GpioListener : public IGpioListener{
  388. public:
  389. bool onGpioEdge(const char *pPin) override {
  390. int state = GpioHelper::input("A1");
  391. LOGD("IGpioListener GPIO IS A1=%d", state);
  392. if (state==0){
  393. voip::CallInfo info = GetTelephone()->GetCallInfo();
  394. //拨打 或 挂断
  395. if (CallingStatus::instance()->busy()){
  396. callActivityFinish(CallFinishType::A1CLICK);
  397. } else {
  398. Intent* intent = new Intent();
  399. intent->putExtra(isOutgoing, "true");
  400. intent->putExtra(audioOnly, "true");
  401. EASYUICONTEXT->openActivity("callActivity", intent);
  402. }
  403. }
  404. return true;
  405. }
  406. void onGpioError(const char *pPin, int error) override {
  407. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  408. }
  409. };
  410. //7寸面板,呼出
  411. class A2GpioListener : public IGpioListener{
  412. public:
  413. bool onGpioEdge(const char *pPin) override {
  414. int state = GpioHelper::input("A2");
  415. LOGD("IGpioListener GPIO IS A2=%d", state);
  416. if (state==0){
  417. //拨打 或 接听
  418. }
  419. return true;
  420. }
  421. void onGpioError(const char *pPin, int error) override {
  422. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  423. }
  424. };
  425. //7寸面板,挂断
  426. class A3GpioListener : public IGpioListener{
  427. public:
  428. bool onGpioEdge(const char *pPin) override {
  429. int state = GpioHelper::input("A3");
  430. LOGD("IGpioListener GPIO IS A3=%d", state);
  431. if (state==0){
  432. //挂断
  433. }
  434. return true;
  435. }
  436. void onGpioError(const char *pPin, int error) override {
  437. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  438. }
  439. };
  440. //紧急按钮
  441. class A4GpioListener : public IGpioListener{
  442. public:
  443. bool onGpioEdge(const char *pPin) override {
  444. int state = GpioHelper::input("A4");
  445. LOGD("IGpioListener GPIO IS A4=%d", state);
  446. if (state==0){
  447. //发出
  448. setSOS_A5(true);
  449. }
  450. return true;
  451. }
  452. void onGpioError(const char *pPin, int error) override {
  453. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  454. }
  455. };
  456. /**
  457. * 注册定时器
  458. * 填充数组用于注册定时器
  459. * 注意:id不能重复
  460. */
  461. static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
  462. {1, 5000},
  463. {2, 60*1000}
  464. };
  465. /**
  466. * 当界面构造时触发
  467. */
  468. static void onUI_init(){
  469. //IO监测
  470. IGpioListener *iGpioListenerA1 = new A1GpioListener();
  471. GpioHelper::registerGpioListener("A1", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  472. IGpioListener *iGpioListenerA2 = new A2GpioListener();
  473. GpioHelper::registerGpioListener("A2", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  474. IGpioListener *iGpioListenerA3 = new A3GpioListener();
  475. GpioHelper::registerGpioListener("A3", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  476. IGpioListener *iGpioListenerA4 = new A4GpioListener();
  477. GpioHelper::registerGpioListener("A4", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  478. //TCP启动
  479. TcpClient::instance()->startTcp();
  480. //监听SIP信令
  481. GetTelephone()->AddCallStateListener(OnCallStateChanged);
  482. //请求版本号
  483. // if(UartContext::Uart3IsOpen()) {
  484. // string heartStr = "ASK,VNF-0";
  485. // const char* sendMsg = heartStr.c_str();
  486. // sendProtocolTo(UART_TTYS3, (byte*)(sendMsg), strlen(sendMsg));
  487. // }
  488. // if (isSleepTimerRegistered) { // 开启夜间息屏
  489. int sleepTime = StoragePreferences::getInt(STORE_SLEEP_TIME, 3);
  490. mActivityPtr->registerUserTimer(SLEEP_STRAT_TIME_HANDLE, sleepTime * 60 * 1000);
  491. // }
  492. #if 0
  493. std::thread backend([](){
  494. while (true) {
  495. if (feed_dogs > 0) {
  496. --feed_dogs;
  497. LOGD("feed");
  498. int ret = GpioHelper::output("GPIO_2", 1); //拉高
  499. if (ret != 0) {
  500. LOGE("GPIO操作失败");
  501. }
  502. usleep(1000 * 20);
  503. ret = GpioHelper::output("GPIO_2", 0); //拉低
  504. if (ret != 0) {
  505. LOGE("GPIO操作失败");
  506. }
  507. }
  508. usleep(1000 * 1000);
  509. }
  510. });
  511. backend.detach();
  512. #endif
  513. }
  514. /**
  515. * 当切换到该界面时触发
  516. */
  517. static void onUI_intent(const Intent *intentPtr) {
  518. if (intentPtr != NULL) {
  519. //TODO
  520. }
  521. }
  522. /*
  523. * 当界面显示时触发
  524. */
  525. static void onUI_show() {
  526. //进入主界面
  527. EASYUICONTEXT->openActivity("mainActivity");
  528. }
  529. /*
  530. * 当界面隐藏时触发
  531. */
  532. static void onUI_hide() {
  533. }
  534. /*
  535. * 当界面完全退出时触发
  536. */
  537. static void onUI_quit() {
  538. }
  539. /**
  540. * 串口数据回调接口
  541. */
  542. static void onProtocolDataUpdate(const SProtocolData &data) {
  543. LOGD("cmd = %s", data.cmd.c_str());
  544. if (data.state!=""){
  545. LOGD("state = %s",data.state.c_str());
  546. }
  547. // if (data.msg != ""){
  548. // LOGD("msg = %s", data.msg.c_str());
  549. // }
  550. if(UartContext::Uart3IsOpen()) { // 如果是true,表示串口打开了
  551. string heartStr;
  552. //按下
  553. if (data.state=="0"){ // 0表示正被按下,1表示短按松开,2表示长按松开。
  554. if (data.cmd == "KEY5") { // key5是面板拨号按钮
  555. if (isSleep) {
  556. scrrenOn();
  557. }
  558. else {
  559. voip::CallInfo info = GetTelephone()->GetCallInfo();
  560. //拨打 或 挂断
  561. if (CallingStatus::instance()->busy()){
  562. callActivityFinish(CallFinishType::A1CLICK);
  563. } else {
  564. Intent* intent = new Intent();
  565. intent->putExtra(isOutgoing, "true");
  566. intent->putExtra(audioOnly, "true");
  567. EASYUICONTEXT->openActivity("callActivity", intent);
  568. }
  569. }
  570. }
  571. else if (data.cmd == "KEY6"){ // key6是手柄触发,手柄只有呼叫和挂断功能,没有取消功能,防止用户不停地按
  572. if (isSleep) {
  573. scrrenOn();
  574. }
  575. else {
  576. voip::CallInfo info = GetTelephone()->GetCallInfo();
  577. //拨打 或 挂断
  578. if (CallingStatus::instance()->busy()){
  579. callActivityFinish(CallFinishType::KEY6_CLICK);
  580. } else {
  581. Intent* intent = new Intent();
  582. intent->putExtra(isOutgoing, "true");
  583. intent->putExtra(audioOnly, "true");
  584. EASYUICONTEXT->openActivity("callActivity", intent);
  585. }
  586. }
  587. }
  588. else if (data.cmd == "KEY7"){ // key7是紧急按钮
  589. if (isSleep) {
  590. scrrenOn();
  591. }
  592. if (sosTimerRegistered) {
  593. LOGD("TCP -> SOS_CALL");
  594. TcpModel tcpModel;
  595. tcpModel.type = TcpType::SOS;
  596. tcpModel.action = SosAction::CALL;
  597. tcpModel.from_id = StoragePreferences::getInt(STORE_DEVICE_ID, 0);
  598. std::string req = getTcpModelString(tcpModel);
  599. LOGD("TCP SOS_CALL : %s",req.c_str());
  600. TcpClient::instance()->sendMsg(req.c_str());
  601. // SOS亮红灯
  602. heartStr = "ULED,1F";
  603. // 门灯亮红灯
  604. std::string heartStr2 = "DOORLED,200F";
  605. const char* sendMsg2 = heartStr2.c_str();
  606. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg2), strlen(sendMsg2));
  607. sosTimerRegistered = false;
  608. mActivityPtr->registerUserTimer(SOS_CLICK_TIME_HANDLE, 10000); // 10秒后才能触发
  609. mActivityPtr->registerUserTimer(SOS_LAMP_TIME_HANDLE, 120000); // 2分钟后才能触发
  610. }
  611. }
  612. else if (data.cmd == "KEY<") { //下左3,红
  613. LOGD("KEY<");
  614. heartStr = "DOORLED,100F";
  615. }
  616. else if (data.cmd == "KEY>") { // key>是面板挂断按钮
  617. if (isSleep) {
  618. scrrenOn();
  619. }
  620. else {
  621. voip::CallInfo info = GetTelephone()->GetCallInfo();
  622. //拨打 或 挂断
  623. if (CallingStatus::instance()->busy()){
  624. callActivityFinish(CallFinishType::A1CLICK);
  625. heartStr = "DOORLED,000F";
  626. const char* sendMsg = heartStr.c_str();
  627. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  628. }
  629. }
  630. }
  631. else if (data.cmd == "KEY;"){ //下左4,白
  632. heartStr = "DOORLED,111F";
  633. }
  634. else {
  635. heartStr = "DOORLED,000F";
  636. }
  637. // 发送给板子的指令
  638. const char* sendMsg = heartStr.c_str();
  639. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  640. }
  641. }
  642. }
  643. /**
  644. * 定时器触发函数
  645. * 不建议在此函数中写耗时操作,否则将影响UI刷新
  646. * 参数: id
  647. * 当前所触发定时器的id,与注册时的id相同
  648. * 返回值: true
  649. * 继续运行当前定时器
  650. * false
  651. * 停止运行当前定时器
  652. */
  653. static bool onUI_Timer(int id){
  654. switch (id) {
  655. case 1:
  656. {
  657. //定时器跑在UI线程,这里将喂狗标记加1
  658. //++feed_dogs;
  659. //不用心跳
  660. // if(UartContext::Uart3IsOpen()) {
  661. // string heartStr = "HEART,1E";
  662. // const char* sendMsg = heartStr.c_str();
  663. // sendProtocolTo(UART_TTYS3, (byte*)(sendMsg), strlen(sendMsg));
  664. // }
  665. }
  666. break;
  667. case 2: //TCP心跳
  668. {
  669. const char* req = "0";
  670. TcpClient::instance()->sendMsg(req);
  671. }
  672. break;
  673. case HELP_TIMER_HANDLE:
  674. {
  675. SetPainterInfo(0xFFFFFFFF,"");
  676. setHelpButton("/button/button_help2.png", true);
  677. setReinforce(false); // 设置增援状态为false
  678. // //取消计时
  679. // if (isHelpTimerRegistered) {
  680. // mActivityPtr->unregisterUserTimer(HELP_TIMER_HANDLE);
  681. // isHelpTimerRegistered = false;
  682. // }
  683. return false;
  684. }
  685. break;
  686. case SOS_CLICK_TIME_HANDLE: {
  687. sosTimerRegistered = true;
  688. return false;
  689. }
  690. break;
  691. case SOS_LAMP_TIME_HANDLE: {
  692. // SOS还原成无灯光的状态
  693. std::string heartStr = "ULED,0F";
  694. const char* sendMsg = heartStr.c_str();
  695. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  696. // 门灯还原成无灯光的状态
  697. std::string heartStr2 = "DOORLED,000F";
  698. const char* sendMsg2 = heartStr2.c_str();
  699. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg2), strlen(sendMsg2));
  700. return false;
  701. }
  702. break;
  703. case SLEEP_STRAT_TIME_HANDLE: { // 息屏
  704. if (isSleepTimerRegistered) {
  705. isSleep = true;
  706. BRIGHTNESSHELPER->screenOff();
  707. } else { // 如果是false的话,就需要关闭定时器
  708. isSleep = false;
  709. BRIGHTNESSHELPER->screenOn();
  710. }
  711. }
  712. break;
  713. }
  714. return true;
  715. }
  716. /**
  717. * 有新的触摸事件时触发
  718. * 参数:ev
  719. * 新的触摸事件
  720. * 返回值:true
  721. * 表示该触摸事件在此被拦截,系统不再将此触摸事件传递到控件上
  722. * false
  723. * 触摸事件将继续传递到控件上
  724. */
  725. static bool onstartActivityTouchEvent(const MotionEvent &ev) {
  726. switch (ev.mActionStatus) {
  727. case MotionEvent::E_ACTION_DOWN://触摸按下
  728. //LOGD("时刻 = %ld 坐标 x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
  729. break;
  730. case MotionEvent::E_ACTION_MOVE://触摸滑动
  731. break;
  732. case MotionEvent::E_ACTION_UP: //触摸抬起
  733. break;
  734. default:
  735. break;
  736. }
  737. return false;
  738. }