startLogic.cc 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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 "server/http_server.h"
  9. #include "base/strings.hpp"
  10. #include "utils/GpioHelper.h"
  11. #include "utils/TimeHelper.h"
  12. #include "utils/BrightnessHelper.h"
  13. #include <time.h>
  14. #include <thread>
  15. #include <string>
  16. #include <unistd.h>
  17. #include <sys/reboot.h>
  18. #include "service/BusinessConfig.h"
  19. static bool sosTimerRegistered = true;
  20. static bool isSleepTimerRegistered = false; // 是否开启息屏定时任务
  21. static bool isSleep = false; // 是否在息屏
  22. static bool isNursing = false; // 用于启动时判断是否处于护理状态
  23. static string callTid = "";
  24. #define FUNCTION_TIMER_HANDLE 3 // 增援的定时器id
  25. #define SOS_CLICK_TIME_HANDLE 4 // sos的定时器id
  26. #define SOS_LAMP_TIME_HANDLE 5 // sos的门灯定时器id
  27. #define EVENT_TIME_HANDLE 8 // 事件定时器
  28. #define SLEEP_STRAT_TIME_HANDLE 9 // 息屏
  29. #define SYS_RESTART 10 // 重启
  30. #define PROMPT_TIME_HANDLE 11 // 提示语
  31. #define HTTP_SERVER_PORT 80
  32. srv::HttpServer httpServer;
  33. namespace {
  34. std::string uilogic[] = {
  35. "testActivity",
  36. "ui3Activity"
  37. };
  38. void CloseUi();
  39. int feed_dogs = 0;
  40. void PrintCallLog() {
  41. CallLogEntries entries;
  42. int ret = GetCallLog(&entries);
  43. LOGD("GetCallRecord %d", ret);
  44. for (auto r : entries) {
  45. LOGD("id=%d, uri=%s, contact=%s,duration=%d,created_at=%d",
  46. r.id, r.uri.c_str(), r.contact.c_str(), r.duration, r.created_at);
  47. }
  48. }
  49. void OnCallStateChanged(voip::Telephone* telephone, int call_id, voip::State state) {
  50. LOGD("call state = %d", state);
  51. if (state == voip::STATE_CALL_INCOMING
  52. || state == voip::STATE_CALL_CALLING
  53. || state == voip::STATE_CALL_CONFIRMED) {
  54. voip::CallInfo info = GetTelephone()->GetCallInfo();
  55. LOGD("info.id == %d", info.id);
  56. LOGD("info.remote_uri == %s", info.remote_uri.c_str());
  57. LOGD("info.remote_contact == %s", info.remote_contact.c_str());
  58. string sipId = info.remote_uri.substr(info.remote_uri.find(":") + 1, info.remote_uri.find("@") - info.remote_uri.find(":") - 1);
  59. LOGD("sipId == %s", sipId.c_str());
  60. if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP"){
  61. EASYUICONTEXT->openActivity("callActivity");
  62. }
  63. else {
  64. if (sipId != "0000000000") {
  65. const char* currentAppName = EASYUICONTEXT->currentAppName();
  66. string _currentAppName = currentAppName;
  67. if (_currentAppName != "callActivity") {
  68. telephone->Hangup();
  69. LOGD("=================> 不在通话界面,直接挂断!");
  70. return;
  71. }
  72. }
  73. }
  74. if (state == voip::STATE_CALL_INCOMING) {
  75. if (telephone->GetCallCount() > 1) {
  76. LOGD("call count > 1, return");
  77. telephone->Hangup(call_id, voip::SIP_STATUS_CODE_BUSY_HERE);
  78. return;
  79. }
  80. //提前显示视频
  81. if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP"){
  82. telephone->Answer(call_id, voip::SIP_STATUS_CODE_PROGRESS);
  83. telephone->Answer();
  84. } else {
  85. acceptSIP();
  86. }
  87. }
  88. }
  89. if (state == voip::STATE_CALL_DISCONNECTED) {
  90. if (telephone->GetCallCount() > 1) {
  91. LOGD("call count > 1, return");
  92. return;
  93. }
  94. if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP"){
  95. //EASYUICONTEXT->goHome();
  96. EASYUICONTEXT->goBack();
  97. }
  98. //保存通话记录
  99. voip::CallInfo info = telephone->GetCallInfo(call_id);
  100. CallLogEntry entry;
  101. entry.uri = info.remote_uri;
  102. entry.contact = info.remote_contact;
  103. entry.duration = info.duration/1000;
  104. if (0 != PutCallLog(entry)) {
  105. LOGE("保存通话记录失败");
  106. }
  107. int n = GetCallLogCount();
  108. if (n > CALL_LOG_COUNT_MAX) {
  109. //超过最大通话记录条数,删除旧记录
  110. if (0 != DeleteOldCallLog(n - CALL_LOG_COUNT_MAX)) {
  111. LOGE("删除失败");
  112. }
  113. }
  114. //PrintCallLog();
  115. }
  116. }
  117. void CloseUi() {
  118. for(int i = 0; i < 2; i++){
  119. EASYUICONTEXT->closeActivity(uilogic[i].c_str());
  120. }
  121. }
  122. void setBrightness(){
  123. int dayLight = StoragePreferences::getInt(STORE_DAY_LIGHT,100);
  124. int nightLight = StoragePreferences::getInt(STORE_NIGHT_LIGHT,10);
  125. if (checkIsDay()){
  126. BRIGHTNESSHELPER->setBrightness(dayLight);
  127. } else {
  128. BRIGHTNESSHELPER->setBrightness(nightLight);
  129. }
  130. }
  131. }
  132. // 门灯还原
  133. void lightControl(std::string typeStr, std::string lightColorStr) {
  134. // 类型是门灯,并且还原成白色灯
  135. std::string heartStr;
  136. if (typeStr == "DOORLED" && lightColorStr == "000F") {
  137. //护理状态,需要还原成护理灯
  138. if (StoragePreferences::getBool(STORE_NURSING_TYPE, false)) {
  139. std::string color = StoragePreferences::getString(STORE_NURSING_COLOR_RGB, "010");
  140. if (color != "" && color.size() == 3) {
  141. heartStr = "DOORLED," + color + "F";
  142. } else {
  143. heartStr = "DOORLED,010F";
  144. }
  145. }
  146. else {
  147. // 还原成无灯光的状态
  148. heartStr = "DOORLED,000F";
  149. }
  150. } else {
  151. heartStr = typeStr + "," + lightColorStr;
  152. }
  153. LOGD("=====> the heartStr == %s", heartStr.c_str());
  154. const char* sendMsg = heartStr.c_str();
  155. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  156. }
  157. // 接收tcp消息
  158. void handleMsg(byte* inBytes){
  159. LOGD("TCP received: %s", inBytes);
  160. const char* cstr = reinterpret_cast<const char*>(inBytes);
  161. string str = cstr;
  162. if (str == "1"){
  163. LOGD("get a heart beat");
  164. return;
  165. }
  166. TcpModel tcpModel;
  167. tcpModel = getTcpModel(inBytes);
  168. LOGD("tcp model: %s, %s", tcpModel.type.c_str(), tcpModel.action.c_str());
  169. if (tcpModel.type == TcpType::OTHER) {
  170. LOGD("trans tcp json failed");
  171. } else if (tcpModel.type == TcpType::TIME) {
  172. if (tcpModel.action == TimeAction::SYNC) {
  173. string serverTime = "";
  174. if (tcpModel.data != ""){
  175. serverTime = tcpModel.data;
  176. } else {
  177. serverTime = tcpModel.json["time"].asString();
  178. }
  179. LOGD("sync time : %s", serverTime.c_str());
  180. time_t timet = stoi(serverTime);
  181. struct tm *t = gmtime(&timet);
  182. char pDate[25];
  183. sprintf(pDate,"%d-%02d-%02d %02d:%02d:%02d",
  184. 1900 + t->tm_year, 1+ t->tm_mon, t->tm_mday,
  185. t->tm_hour + 8,t->tm_min,t->tm_sec);
  186. LOGD("transfered time : %s", pDate);
  187. TimeHelper::setDateTime(pDate);
  188. dataRefresh();
  189. if (isNursing) {
  190. setNursingEnd();
  191. }
  192. }
  193. } else if (tcpModel.type == TcpType::REINFORCE){
  194. if (tcpModel.action == ReinforceAction::RESPONSED){
  195. //回调
  196. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  197. if (callback.tid != "0"){
  198. callback.onSuccess(tcpModel.json);
  199. }
  200. const char* currentAppName = EASYUICONTEXT->currentAppName();
  201. string _currentAppName = currentAppName;
  202. if (_currentAppName != "functionActivity") {
  203. Intent* intent = new Intent();
  204. intent->putExtra(functionWindows, "helpResonse");
  205. EASYUICONTEXT->openActivity("functionActivity", intent);
  206. } else {
  207. toFunctionActivity("");
  208. }
  209. }
  210. } else if (tcpModel.type == TcpType::DEVICE){
  211. if (tcpModel.action == DeviceAction::APP_UPDATE){ //软件升级
  212. if (isSleep) {
  213. isSleep = false;
  214. BRIGHTNESSHELPER->screenOn();
  215. }
  216. Intent* intent = new Intent();
  217. intent->putExtra(appUpdate, "true");
  218. EASYUICONTEXT->openActivity("updateActivity", intent);
  219. }
  220. else if (tcpModel.action == DeviceAction::RESTART) { // 重启设备
  221. const char* req = "-1";
  222. TcpClient::instance()->sendMsg(req);
  223. lightControl("CALLLED", "000F");
  224. sync();
  225. reboot(RB_AUTOBOOT);
  226. }
  227. else if (tcpModel.action == DeviceAction::REBOOT) {
  228. const char* req = "-1";
  229. TcpClient::instance()->sendMsg(req);
  230. lightControl("CALLLED", "000F");
  231. //重启
  232. sync();
  233. reboot(RB_AUTOBOOT);
  234. }
  235. else if (tcpModel.action == DeviceAction::DEVICE_REFRESH) { // 设备刷新
  236. dataRefresh();
  237. }
  238. else if (tcpModel.action == DeviceAction::SYSTEM_SETTING) { // 修改科室设置
  239. getPartSetting();
  240. }
  241. else if (tcpModel.action == DeviceAction::SERVER_CHANGE) {
  242. StoragePreferences::putString(STORE_GATEWAY, tcpModel.json["server_ip"].asCString());
  243. StoragePreferences::putString(STORE_HTTP_PORT, tcpModel.json["server_port"].asCString());
  244. const char* req = "-1";
  245. TcpClient::instance()->sendMsg(req);
  246. lightControl("CALLLED", "000F");
  247. //重启
  248. sync();
  249. reboot(RB_AUTOBOOT);
  250. }
  251. } else if (tcpModel.type == TcpType::DATA){
  252. if (tcpModel.action == DataAction::REFRESH){ // 刷新数据
  253. dataRefresh();
  254. }
  255. } else if (tcpModel.type == TcpType::VOICE){
  256. if (tcpModel.action == VoiceAction::FAILED){
  257. callActivityFinish(CallFinishType::FAILED);
  258. } else if (tcpModel.action == VoiceAction::SUCCESS){
  259. CallingStatus::instance()->setTcpModel(tcpModel); // 只有呼叫成功才闪红灯
  260. lightControl("DOORLED", "200F");
  261. } else if (tcpModel.action == VoiceAction::ACCEPT){
  262. if (CallingStatus::instance()->busy()){
  263. CallingStatus::instance()->setTcpModel(tcpModel);
  264. std::string toSipId = tcpModel.json["toSipId"].asString();
  265. callActivityFinish(CallFinishType::ACCEPT);
  266. //接听
  267. buildSIP(toSipId);
  268. }
  269. // 进行门灯还原
  270. lightControl("DOORLED", "000F");
  271. } else if (tcpModel.action == VoiceAction::REJECT){
  272. if (CallingStatus::instance()->busy()){
  273. callActivityFinish(CallFinishType::REJECT);
  274. // 进行门灯还原
  275. lightControl("DOORLED", "000F");
  276. }
  277. } else if (tcpModel.action == VoiceAction::CANCEL){
  278. if (CallingStatus::instance()->busy()){
  279. callActivityFinish(CallFinishType::CANCEL);
  280. }
  281. } else if (tcpModel.action == VoiceAction::CALLING){
  282. //对方忙线
  283. callActivityFinish(CallFinishType::BUSY);
  284. } else if (tcpModel.action == VoiceAction::CALL){
  285. //我方忙线判断
  286. if (CallingStatus::instance()->busy()){
  287. sendVoiceTcp(VoiceAction::CALLING, tcpModel, tcpModel.from_id);
  288. } else {
  289. CallingStatus::instance()->setTcpModel(tcpModel);
  290. //来电话了
  291. Intent* intent = new Intent();
  292. intent->putExtra(isOutgoing, "false");
  293. intent->putExtra(audioOnly, "true");
  294. EASYUICONTEXT->openActivity("callActivity", intent);
  295. }
  296. } else if (tcpModel.action == VoiceAction::HANDOFF){
  297. //判断 是否同一个对话
  298. long iId = CallingStatus::instance()->getInteractionId();
  299. if (iId > 0 && CallingStatus::instance()->busy()){
  300. long inId = tcpModel.json["id"].asInt();
  301. if (iId == inId && CallingStatus::instance()->busy()){
  302. callActivityFinish(CallFinishType::HANDOFF);
  303. }
  304. }
  305. }
  306. }
  307. else if (tcpModel.type == TcpType::SOS) {
  308. if (tcpModel.action == SosAction::CANCEL) {
  309. // SOS还原成无灯光的状态
  310. lightControl("ULED", "0F");
  311. // 进行门灯还原
  312. lightControl("DOORLED", "000F");
  313. mActivityPtr->unregisterUserTimer(SOS_LAMP_TIME_HANDLE);
  314. }
  315. }
  316. else if (tcpModel.type == TcpType::SIDE) {
  317. if (tcpModel.action == SideAction::NURSING) {
  318. StoragePreferences::putString(STORE_NURSING_INTERACTION_ID, tcpModel.json["id"].asString());
  319. // 收到nursing,需要把通话给挂断了
  320. voip::CallInfo info = GetTelephone()->GetCallInfo();
  321. LOGD("info state = %d",info.state);
  322. if (info.state == voip::STATE_CALL_CALLING
  323. || info.state == voip::STATE_CALL_CALLING
  324. || info.state == voip::STATE_CALL_CONFIRMED){
  325. TcpModel storeModel = CallingStatus::instance()->getTcpModel();
  326. //只回传iId
  327. storeModel.data = storeModel.json["id"].asString();
  328. sendVoiceTcp(VoiceAction::HANDOFF, storeModel, storeModel.from_id);
  329. GetTelephone()->Hangup(info.id, voip::SIP_STATUS_CODE_DECLINE);
  330. GetTelephone()->Hangup();
  331. EASYUICONTEXT->goBack();
  332. }
  333. std::string color = StoragePreferences::getString(STORE_NURSING_COLOR_RGB, "010");
  334. if (color != "" && color.size() == 3) {
  335. color = color + "F";
  336. } else {
  337. color = "010F";
  338. }
  339. lightControl("DOORLED", color);
  340. }
  341. else if (tcpModel.action == SideAction::NURSING_END) {
  342. // 收到nursing_end
  343. lightControl("DOORLED", "000F");
  344. }
  345. else if (tcpModel.action == SideAction::CALL) {
  346. // 收到CALL,需要亮红灯
  347. lightControl("DOORLED", "200F");
  348. }
  349. else if (tcpModel.action == SideAction::ACCEPT) {
  350. // 进行门灯还原
  351. lightControl("DOORLED", "000F");
  352. }
  353. else if (tcpModel.action == SideAction::CANCEL) {
  354. // 进行门灯还原
  355. lightControl("DOORLED", "000F");
  356. }
  357. else if (tcpModel.action == SideAction::SOS_CALL) {
  358. // 收到SOS_CALL,需要亮红灯
  359. // 门灯亮红灯
  360. lightControl("DOORLED", "200F");
  361. sosTimerRegistered = false;
  362. mActivityPtr->registerUserTimer(SOS_CLICK_TIME_HANDLE, 10000); // 10秒后才能触发
  363. mActivityPtr->registerUserTimer(SOS_LAMP_TIME_HANDLE, 120000); // 2分钟后才能触发
  364. }
  365. else if (tcpModel.action == SideAction::SOS_CANCEL) {
  366. // 进行门灯还原
  367. lightControl("DOORLED", "000F");
  368. }
  369. }
  370. else if (tcpModel.type == TcpType::CALLBACK) {
  371. if (tcpModel.action == CallbackAction::ACK) {
  372. LOGD("CALLBACK ACK !!!!");
  373. //回调
  374. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  375. if (callback.tid != "0"){
  376. callback.onSuccess(tcpModel.json);
  377. }
  378. }
  379. else if (tcpModel.action == CallbackAction::SUCCESS) {
  380. //回调
  381. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  382. if (callback.tid != "0"){
  383. callback.onSuccess(tcpModel.json);
  384. }
  385. }
  386. else if (tcpModel.action == CallbackAction::FAILED) {
  387. //回调
  388. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  389. if (callback.tid != "0"){
  390. callback.onFalied(tcpModel.json);
  391. }
  392. if (tcpModel.tid == callTid){
  393. callActivityFinish(CallFinishType::FAILED);
  394. }
  395. }
  396. }
  397. else if (tcpModel.type == TcpType::POSITION) {
  398. if (tcpModel.action == PositionAction::POSITION_START) {
  399. setPosistionItId(tcpModel.json["id"].asInt());
  400. }
  401. }
  402. else if (tcpModel.type == TcpType::ROOMCHECK) {
  403. if (tcpModel.action == RoomCheckAction::START) {
  404. setRoomCheckItId(tcpModel.json["id"].asInt());
  405. }
  406. }
  407. else if (tcpModel.type == TcpType::SCREEN_TIP) {
  408. if (tcpModel.action == ScreenTipAction::START) {
  409. setPromptTimeItId(tcpModel.json["id"].asInt());
  410. }
  411. }
  412. else if (tcpModel.type == TcpType::AUTH) {
  413. if (tcpModel.action == AuthAction::LOGIN) {
  414. // 需要判断fromId跟自己是不是一样的,一样的就代表是自己发送出去,不一样的就代表员工已经登录到其他设备去了
  415. if (tcpModel.from_id == StoragePreferences::getInt(STORE_DEVICE_ID, 0)) {
  416. LOGD("id ===> %d", tcpModel.json["id"].asInt());
  417. setAuthItId(tcpModel.json["id"].asInt());
  418. }
  419. else {
  420. logoutMedicalCare();
  421. const char* currentAppName = EASYUICONTEXT->currentAppName();
  422. string _currentAppName = currentAppName;
  423. if (_currentAppName == "medicalCareActivity") {
  424. goHome();
  425. }
  426. }
  427. }
  428. }
  429. }
  430. bool getSleepTimerRegistered() {
  431. return isSleepTimerRegistered;
  432. }
  433. // 触发定时任务
  434. void setSleepTimerRegistered(bool result) {
  435. LOGD("isSleepTimerRegistered = %d", isSleepTimerRegistered);
  436. LOGD("result = %d", result);
  437. if (mActivityPtr == NULL) {
  438. return;
  439. }
  440. if (result != isSleepTimerRegistered) { // 与定时任务不同时,才进行处理
  441. if (isSleepTimerRegistered) { // 与定时任务开关进行判断,如果定时任务开启,那就要关闭
  442. isSleepTimerRegistered = result;
  443. mActivityPtr->unregisterUserTimer(SLEEP_STRAT_TIME_HANDLE); // 关闭定时器
  444. if (isSleep) {
  445. isSleep = false;
  446. BRIGHTNESSHELPER->screenOn();
  447. }
  448. LOGD("关闭息屏");
  449. } else { // 如果定时任务关着,那就要打开
  450. isSleepTimerRegistered = result;
  451. int sleepTime = StoragePreferences::getInt(STORE_SLEEP_TIME, 3);
  452. if (sleepTime > 0) {
  453. mActivityPtr->registerUserTimer(SLEEP_STRAT_TIME_HANDLE, sleepTime * 60 * 1000);
  454. LOGD("开启息屏");
  455. }
  456. }
  457. }
  458. }
  459. void scrrenOn() {
  460. isSleep = false;
  461. BRIGHTNESSHELPER->screenOn();
  462. }
  463. void setCallTid(std::string tid) {
  464. callTid = tid;
  465. }
  466. //================================= IO 口操作
  467. //紧急按钮灯,明/灭
  468. void setSOS_A5(bool light){
  469. int result = -1;
  470. int slight = light?0:1;
  471. result = GpioHelper::output("A5", slight);
  472. if (result==0){
  473. LOGD("set A5 SOS light %d. success",light);
  474. } else {
  475. LOGD("set A5 SOS light %d. failed",light);
  476. }
  477. }
  478. //A6,A7,A8 RGB灯控制
  479. void setRGB_A678(bool R, bool G, bool B){
  480. int rR = -1, rG = -1, rB = -1;
  481. int sR = R?0:1, sG = G?0:1, sB = B?0:1;
  482. rR = GpioHelper::output("A6", sR);
  483. rG = GpioHelper::output("A7", sG);
  484. rB = GpioHelper::output("A8", sB);
  485. const char *strLog = "set %s light %d. %d";
  486. LOGD(strLog,"A6 R",R, rR);
  487. LOGD(strLog,"A7 G",G, rG);
  488. LOGD(strLog,"A8 B",B, rB);
  489. }
  490. //手柄按钮
  491. class A1GpioListener : public IGpioListener{
  492. public:
  493. bool onGpioEdge(const char *pPin) override {
  494. int state = GpioHelper::input("A1");
  495. LOGD("IGpioListener GPIO IS A1=%d", state);
  496. if (state==0){
  497. voip::CallInfo info = GetTelephone()->GetCallInfo();
  498. //拨打 或 挂断
  499. if (CallingStatus::instance()->busy()){
  500. callActivityFinish(CallFinishType::A1CLICK);
  501. } else {
  502. Intent* intent = new Intent();
  503. intent->putExtra(isOutgoing, "true");
  504. intent->putExtra(audioOnly, "true");
  505. EASYUICONTEXT->openActivity("callActivity", intent);
  506. }
  507. }
  508. return true;
  509. }
  510. void onGpioError(const char *pPin, int error) override {
  511. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  512. }
  513. };
  514. //7寸面板,呼出
  515. class A2GpioListener : public IGpioListener{
  516. public:
  517. bool onGpioEdge(const char *pPin) override {
  518. int state = GpioHelper::input("A2");
  519. LOGD("IGpioListener GPIO IS A2=%d", state);
  520. if (state==0){
  521. //拨打 或 接听
  522. }
  523. return true;
  524. }
  525. void onGpioError(const char *pPin, int error) override {
  526. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  527. }
  528. };
  529. //7寸面板,挂断
  530. class A3GpioListener : public IGpioListener{
  531. public:
  532. bool onGpioEdge(const char *pPin) override {
  533. int state = GpioHelper::input("A3");
  534. LOGD("IGpioListener GPIO IS A3=%d", state);
  535. if (state==0){
  536. //挂断
  537. }
  538. return true;
  539. }
  540. void onGpioError(const char *pPin, int error) override {
  541. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  542. }
  543. };
  544. //紧急按钮
  545. class A4GpioListener : public IGpioListener{
  546. public:
  547. bool onGpioEdge(const char *pPin) override {
  548. int state = GpioHelper::input("A4");
  549. LOGD("IGpioListener GPIO IS A4=%d", state);
  550. if (state==0){
  551. //发出
  552. setSOS_A5(true);
  553. }
  554. return true;
  555. }
  556. void onGpioError(const char *pPin, int error) override {
  557. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  558. }
  559. };
  560. /**
  561. * 注册定时器
  562. * 填充数组用于注册定时器
  563. * 注意:id不能重复
  564. */
  565. static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
  566. {1, 5000},
  567. {2, 30*1000},
  568. };
  569. /**
  570. * 当界面构造时触发
  571. */
  572. static void onUI_init(){
  573. //IO监测
  574. IGpioListener *iGpioListenerA1 = new A1GpioListener();
  575. GpioHelper::registerGpioListener("A1", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  576. IGpioListener *iGpioListenerA2 = new A2GpioListener();
  577. GpioHelper::registerGpioListener("A2", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  578. IGpioListener *iGpioListenerA3 = new A3GpioListener();
  579. GpioHelper::registerGpioListener("A3", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  580. IGpioListener *iGpioListenerA4 = new A4GpioListener();
  581. GpioHelper::registerGpioListener("A4", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  582. //TCP启动
  583. TcpClient::instance()->startTcp();
  584. //监听SIP信令
  585. GetTelephone()->AddCallStateListener(OnCallStateChanged);
  586. //开启网页
  587. httpServer.RunAsync(HTTP_SERVER_PORT);
  588. // 启动的时候更改为tcp模式
  589. StoragePreferences::putString(STORE_SIGNAL_TYPE, "TCP");
  590. if (StoragePreferences::getBool(STORE_NURSING_TYPE, false)) {
  591. LOGD("===============> 关闭护理状态");
  592. StoragePreferences::putBool(STORE_NURSING_TYPE, false);
  593. isNursing = true;
  594. }
  595. lightControl("CALLLED", "200F");
  596. lightControl("DOORLED", "222F");
  597. //请求版本号
  598. // if(UartContext::Uart3IsOpen()) {
  599. // string heartStr = "ASK,VNF-0";
  600. // const char* sendMsg = heartStr.c_str();
  601. // sendProtocolTo(UART_TTYS3, (byte*)(sendMsg), strlen(sendMsg));
  602. // }
  603. #if 0
  604. std::thread backend([](){
  605. while (true) {
  606. if (feed_dogs > 0) {
  607. --feed_dogs;
  608. LOGD("feed");
  609. int ret = GpioHelper::output("GPIO_2", 1); //拉高
  610. if (ret != 0) {
  611. LOGE("GPIO操作失败");
  612. }
  613. usleep(1000 * 20);
  614. ret = GpioHelper::output("GPIO_2", 0); //拉低
  615. if (ret != 0) {
  616. LOGE("GPIO操作失败");
  617. }
  618. }
  619. usleep(1000 * 1000);
  620. }
  621. });
  622. backend.detach();
  623. #endif
  624. }
  625. /**
  626. * 当切换到该界面时触发
  627. */
  628. static void onUI_intent(const Intent *intentPtr) {
  629. if (intentPtr != NULL) {
  630. //TODO
  631. }
  632. }
  633. /*
  634. * 当界面显示时触发
  635. */
  636. static void onUI_show() {
  637. StoragePreferences::putString(STORE_SIGNAL_TYPE, "TCP");
  638. //进入主界面
  639. EASYUICONTEXT->openActivity("mainActivity");
  640. }
  641. /*
  642. * 当界面隐藏时触发
  643. */
  644. static void onUI_hide() {
  645. }
  646. /*
  647. * 当界面完全退出时触发
  648. */
  649. static void onUI_quit() {
  650. }
  651. /**
  652. * 串口数据回调接口
  653. */
  654. static void onProtocolDataUpdate(const SProtocolData &data) {
  655. LOGD("cmd = %s", data.cmd.c_str());
  656. if (data.state!=""){
  657. LOGD("state = %s",data.state.c_str());
  658. }
  659. // if (data.msg != ""){
  660. // LOGD("msg = %s", data.msg.c_str());
  661. // }
  662. if(UartContext::Uart3IsOpen()) { // 如果是true,表示串口打开了
  663. string heartStr;
  664. //按下
  665. if (data.state=="0"){ // 0表示正被按下,1表示短按松开,2表示长按松开。
  666. if (data.cmd == "KEY5") { // key5是面板拨号按钮
  667. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  668. if (isSleep) {
  669. scrrenOn();
  670. }
  671. else {
  672. voip::CallInfo info = GetTelephone()->GetCallInfo();
  673. //拨打 或 挂断
  674. if (CallingStatus::instance()->busy()){
  675. callActivityFinish(CallFinishType::A1CLICK);
  676. } else {
  677. Intent* intent = new Intent();
  678. intent->putExtra(isOutgoing, "true");
  679. intent->putExtra(audioOnly, "true");
  680. EASYUICONTEXT->openActivity("callActivity", intent);
  681. }
  682. }
  683. }
  684. else {
  685. scrrenOn();
  686. voip::CallInfo info = GetTelephone()->GetCallInfo();
  687. //拨打 或 挂断
  688. if (CallingStatus::instance()->busy()){
  689. callActivityFinish(CallFinishType::A1CLICK);
  690. } else {
  691. Intent* intent = new Intent();
  692. intent->putExtra(isOutgoing, "true");
  693. intent->putExtra(audioOnly, "true");
  694. EASYUICONTEXT->openActivity("callActivity", intent);
  695. }
  696. }
  697. }
  698. else if (data.cmd == "KEY6"){ // key6是手柄触发,手柄只有呼叫和挂断功能,没有取消功能,防止用户不停地按
  699. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  700. if (isSleep) {
  701. scrrenOn();
  702. }
  703. else {
  704. voip::CallInfo info = GetTelephone()->GetCallInfo();
  705. //拨打 或 挂断
  706. if (CallingStatus::instance()->busy()){
  707. callActivityFinish(CallFinishType::KEY6_CLICK);
  708. } else {
  709. Intent* intent = new Intent();
  710. intent->putExtra(isOutgoing, "true");
  711. intent->putExtra(audioOnly, "true");
  712. EASYUICONTEXT->openActivity("callActivity", intent);
  713. }
  714. }
  715. }
  716. else {
  717. scrrenOn();
  718. voip::CallInfo info = GetTelephone()->GetCallInfo();
  719. //拨打 或 挂断
  720. if (CallingStatus::instance()->busy()){
  721. callActivityFinish(CallFinishType::KEY6_CLICK);
  722. } else {
  723. Intent* intent = new Intent();
  724. intent->putExtra(isOutgoing, "true");
  725. intent->putExtra(audioOnly, "true");
  726. EASYUICONTEXT->openActivity("callActivity", intent);
  727. }
  728. }
  729. }
  730. else if (data.cmd == "KEY7"){ // key7是紧急按钮
  731. if (isSleep) {
  732. scrrenOn();
  733. }
  734. if (sosTimerRegistered) {
  735. LOGD("TCP -> SOS_CALL");
  736. TcpModel tcpModel;
  737. tcpModel.type = TcpType::SOS;
  738. tcpModel.action = SosAction::CALL;
  739. tcpModel.from_id = StoragePreferences::getInt(STORE_DEVICE_ID, 0);
  740. tcpModel.tid = base::format("t%d",TimeHelper::getCurrentTime());
  741. std::string req = getTcpModelString(tcpModel);
  742. LOGD("TCP SOS_CALL : %s",req.c_str());
  743. TcpClient::instance()->sendMsg(req.c_str());
  744. // SOS亮红灯
  745. lightControl("ULED", "1F");
  746. // 门灯亮红灯
  747. lightControl("DOORLED", "200F");
  748. mActivityPtr->registerUserTimer(SOS_CLICK_TIME_HANDLE, 10000); // 10秒后才能触发
  749. mActivityPtr->registerUserTimer(SOS_LAMP_TIME_HANDLE, 120000); // 2分钟后才能触发
  750. }
  751. }
  752. else if (data.cmd == "KEY<") { //下左3,红
  753. if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP") {
  754. setCheckbox("KEY<");
  755. return;
  756. }
  757. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  758. if (isSleep) {
  759. scrrenOn();
  760. }
  761. else {
  762. voip::CallInfo info = GetTelephone()->GetCallInfo();
  763. //拨打 或 挂断
  764. if (CallingStatus::instance()->busy()){
  765. callActivityFinish(CallFinishType::A1CLICK);
  766. } else {
  767. Intent* intent = new Intent();
  768. intent->putExtra(isOutgoing, "true");
  769. intent->putExtra(audioOnly, "true");
  770. EASYUICONTEXT->openActivity("callActivity", intent);
  771. }
  772. }
  773. }
  774. else {
  775. scrrenOn();
  776. voip::CallInfo info = GetTelephone()->GetCallInfo();
  777. //拨打 或 挂断
  778. if (CallingStatus::instance()->busy()){
  779. callActivityFinish(CallFinishType::A1CLICK);
  780. } else {
  781. Intent* intent = new Intent();
  782. intent->putExtra(isOutgoing, "true");
  783. intent->putExtra(audioOnly, "true");
  784. EASYUICONTEXT->openActivity("callActivity", intent);
  785. }
  786. }
  787. }
  788. else if (data.cmd == "KEY>") { // key>是面板挂断按钮
  789. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  790. if (isSleep) {
  791. scrrenOn();
  792. }
  793. else {
  794. voip::CallInfo info = GetTelephone()->GetCallInfo();
  795. //拨打 或 挂断
  796. if (CallingStatus::instance()->busy()){
  797. callActivityFinish(CallFinishType::A1CLICK);
  798. lightControl("DOORLED", "000F");
  799. }
  800. }
  801. }
  802. else {
  803. scrrenOn();
  804. voip::CallInfo info = GetTelephone()->GetCallInfo();
  805. //拨打 或 挂断
  806. if (CallingStatus::instance()->busy()){
  807. callActivityFinish(CallFinishType::A1CLICK);
  808. lightControl("DOORLED", "000F");
  809. }
  810. }
  811. }
  812. else if (data.cmd == "KEY;"){ //下左4,白
  813. if (isSleep) {
  814. scrrenOn();
  815. }
  816. lightControl("DOORLED", "111F");
  817. }
  818. else {
  819. if (isSleep) {
  820. scrrenOn();
  821. }
  822. // lightControl("DOORLED", "000F");
  823. }
  824. }
  825. }
  826. }
  827. /**
  828. * 定时器触发函数
  829. * 不建议在此函数中写耗时操作,否则将影响UI刷新
  830. * 参数: id
  831. * 当前所触发定时器的id,与注册时的id相同
  832. * 返回值: true
  833. * 继续运行当前定时器
  834. * false
  835. * 停止运行当前定时器
  836. */
  837. static bool onUI_Timer(int id){
  838. switch (id) {
  839. case 1:
  840. {
  841. //定时器跑在UI线程,这里将喂狗标记加1
  842. //++feed_dogs;
  843. //不用心跳
  844. // if(UartContext::Uart3IsOpen()) {
  845. // string heartStr = "HEART,1E";
  846. // const char* sendMsg = heartStr.c_str();
  847. // sendProtocolTo(UART_TTYS3, (byte*)(sendMsg), strlen(sendMsg));
  848. // }
  849. }
  850. break;
  851. case 2: //TCP心跳
  852. {
  853. const char* req = "0";
  854. TcpClient::instance()->sendMsg(req);
  855. }
  856. break;
  857. case SOS_CLICK_TIME_HANDLE: {
  858. sosTimerRegistered = true;
  859. return false;
  860. }
  861. break;
  862. case SOS_LAMP_TIME_HANDLE: {
  863. // SOS还原成无灯光的状态
  864. lightControl("ULED", "0F");
  865. // 进行门灯还原
  866. lightControl("DOORLED", "000F");
  867. return false;
  868. }
  869. break;
  870. case SLEEP_STRAT_TIME_HANDLE: { // 息屏
  871. if (isSleepTimerRegistered) {
  872. isSleep = true;
  873. BRIGHTNESSHELPER->screenOff();
  874. } else { // 如果是false的话,就需要关闭定时器
  875. isSleep = false;
  876. BRIGHTNESSHELPER->screenOn();
  877. }
  878. }
  879. break;
  880. default:
  881. break;
  882. }
  883. return true;
  884. }
  885. /**
  886. * 有新的触摸事件时触发
  887. * 参数:ev
  888. * 新的触摸事件
  889. * 返回值:true
  890. * 表示该触摸事件在此被拦截,系统不再将此触摸事件传递到控件上
  891. * false
  892. * 触摸事件将继续传递到控件上
  893. */
  894. static bool onstartActivityTouchEvent(const MotionEvent &ev) {
  895. switch (ev.mActionStatus) {
  896. case MotionEvent::E_ACTION_DOWN://触摸按下
  897. //LOGD("时刻 = %ld 坐标 x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
  898. break;
  899. case MotionEvent::E_ACTION_MOVE://触摸滑动
  900. break;
  901. case MotionEvent::E_ACTION_UP: //触摸抬起
  902. break;
  903. default:
  904. break;
  905. }
  906. return false;
  907. }