startLogic.cc 24 KB

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