startLogic.cc 31 KB

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