startLogic.cc 30 KB

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