startLogic.cc 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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. const char* currentAppName = EASYUICONTEXT->currentAppName();
  283. string _currentAppName = currentAppName;
  284. if (_currentAppName == "callActivity") {
  285. callActivityFinish(CallFinishType::FAILED);
  286. }
  287. } else if (tcpModel.action == VoiceAction::SUCCESS){
  288. CallingStatus::instance()->setTcpModel(tcpModel); // 只有呼叫成功才闪红灯
  289. lightControl("DOORLED", "200F");
  290. } else if (tcpModel.action == VoiceAction::ACCEPT){
  291. if (CallingStatus::instance()->busy()){
  292. CallingStatus::instance()->setTcpModel(tcpModel);
  293. std::string toSipId = tcpModel.json["toSipId"].asString();
  294. callActivityFinish(CallFinishType::ACCEPT);
  295. //接听
  296. buildSIP(toSipId);
  297. }
  298. // 进行门灯还原
  299. lightControl("DOORLED", "000F");
  300. } else if (tcpModel.action == VoiceAction::REJECT){
  301. if (CallingStatus::instance()->busy()){
  302. callActivityFinish(CallFinishType::REJECT);
  303. // 进行门灯还原
  304. lightControl("DOORLED", "000F");
  305. }
  306. } else if (tcpModel.action == VoiceAction::CANCEL){
  307. if (CallingStatus::instance()->busy()){
  308. callActivityFinish(CallFinishType::CANCEL);
  309. }
  310. } else if (tcpModel.action == VoiceAction::CALLING){
  311. //对方忙线
  312. callActivityFinish(CallFinishType::BUSY);
  313. } else if (tcpModel.action == VoiceAction::CALL){
  314. //我方忙线判断
  315. if (CallingStatus::instance()->busy()){
  316. sendVoiceTcp(VoiceAction::CALLING, tcpModel, tcpModel.from_id);
  317. } else {
  318. CallingStatus::instance()->setTcpModel(tcpModel);
  319. //来电话了
  320. Intent* intent = new Intent();
  321. intent->putExtra(isOutgoing, "false");
  322. intent->putExtra(audioOnly, "true");
  323. EASYUICONTEXT->openActivity("callActivity", intent);
  324. }
  325. } else if (tcpModel.action == VoiceAction::HANDOFF){
  326. //判断 是否同一个对话
  327. long iId = CallingStatus::instance()->getInteractionId();
  328. if (iId > 0 && CallingStatus::instance()->busy()){
  329. long inId = tcpModel.json["id"].asInt();
  330. if (iId == inId && CallingStatus::instance()->busy()){
  331. callActivityFinish(CallFinishType::HANDOFF);
  332. }
  333. }
  334. }
  335. else if (tcpModel.action == VoiceAction::SIP_RECONNECT) {
  336. mActivityPtr->registerUserTimer(SIP_RECONNECT_TIME_HANDLE, 1 * 1000);
  337. }
  338. }
  339. else if (tcpModel.type == TcpType::SOS) {
  340. if (tcpModel.action == SosAction::CANCEL) {
  341. // SOS还原成无灯光的状态
  342. lightControl("ULED", "0F");
  343. // 进行门灯还原
  344. lightControl("DOORLED", "000F");
  345. mActivityPtr->unregisterUserTimer(SOS_LAMP_TIME_HANDLE);
  346. }
  347. }
  348. else if (tcpModel.type == TcpType::SIDE) {
  349. if (tcpModel.action == SideAction::NURSING) {
  350. StoragePreferences::putString(STORE_NURSING_INTERACTION_ID, tcpModel.json["id"].asString());
  351. // 收到nursing,需要把通话给挂断了
  352. voip::CallInfo info = GetTelephone()->GetCallInfo();
  353. LOGD("info state = %d",info.state);
  354. if (info.state == voip::STATE_CALL_CALLING
  355. || info.state == voip::STATE_CALL_CALLING
  356. || info.state == voip::STATE_CALL_CONFIRMED){
  357. TcpModel storeModel = CallingStatus::instance()->getTcpModel();
  358. //只回传iId
  359. storeModel.data = storeModel.json["id"].asString();
  360. sendVoiceTcp(VoiceAction::HANDOFF, storeModel, storeModel.from_id);
  361. GetTelephone()->Hangup(info.id, voip::SIP_STATUS_CODE_DECLINE);
  362. GetTelephone()->Hangup();
  363. EASYUICONTEXT->goBack();
  364. }
  365. std::string color = StoragePreferences::getString(STORE_NURSING_COLOR_RGB, "010");
  366. if (color != "" && color.size() == 3) {
  367. color = color + "F";
  368. } else {
  369. color = "010F";
  370. }
  371. lightControl("DOORLED", color);
  372. }
  373. else if (tcpModel.action == SideAction::NURSING_END) {
  374. // 收到nursing_end
  375. lightControl("DOORLED", "000F");
  376. }
  377. else if (tcpModel.action == SideAction::CALL) {
  378. // 收到CALL,需要亮红灯
  379. lightControl("DOORLED", "200F");
  380. }
  381. else if (tcpModel.action == SideAction::ACCEPT) {
  382. // 进行门灯还原
  383. lightControl("DOORLED", "000F");
  384. }
  385. else if (tcpModel.action == SideAction::CANCEL) {
  386. // 进行门灯还原
  387. lightControl("DOORLED", "000F");
  388. }
  389. else if (tcpModel.action == SideAction::SOS_CALL) {
  390. // 收到SOS_CALL,需要亮红灯
  391. // 门灯亮红灯
  392. lightControl("DOORLED", "200F");
  393. lightControl("CALLLED", "100F");
  394. sosTimerRegistered = false;
  395. mActivityPtr->registerUserTimer(SOS_CLICK_TIME_HANDLE, 10000); // 10秒后才能触发
  396. mActivityPtr->registerUserTimer(SOS_LAMP_TIME_HANDLE, 120000); // 2分钟后才能触发
  397. if (tcpModel.json["fromEthMac"].asString() == StoragePreferences::getString(STORE_MAC_ADDR, "0.0.0.0")) {
  398. sosInteraction = tcpModel.json;
  399. }
  400. }
  401. else if (tcpModel.action == SideAction::SOS_CANCEL) {
  402. // 进行门灯还原
  403. lightControl("DOORLED", "000F");
  404. }
  405. }
  406. else if (tcpModel.type == TcpType::CALLBACK) {
  407. if (tcpModel.action == CallbackAction::ACK) {
  408. LOGD("CALLBACK ACK !!!!");
  409. //回调
  410. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  411. if (callback.tid != "0"){
  412. callback.onSuccess(tcpModel.json);
  413. }
  414. }
  415. else if (tcpModel.action == CallbackAction::SUCCESS) {
  416. //回调
  417. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  418. if (callback.tid != "0"){
  419. callback.onSuccess(tcpModel.json);
  420. }
  421. }
  422. else if (tcpModel.action == CallbackAction::FAILED) {
  423. //回调
  424. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  425. if (callback.tid != "0"){
  426. callback.onFalied(tcpModel.json);
  427. }
  428. if (tcpModel.tid == callTid){
  429. callActivityFinish(CallFinishType::FAILED);
  430. }
  431. }
  432. }
  433. else if (tcpModel.type == TcpType::POSITION) {
  434. if (tcpModel.action == PositionAction::POSITION_START) {
  435. setPosistionItId(tcpModel.json["id"].asInt());
  436. }
  437. }
  438. else if (tcpModel.type == TcpType::ROOMCHECK) {
  439. if (tcpModel.action == RoomCheckAction::START) {
  440. setRoomCheckItId(tcpModel.json["id"].asInt());
  441. }
  442. }
  443. else if (tcpModel.type == TcpType::SCREEN_TIP) {
  444. if (tcpModel.action == ScreenTipAction::START) {
  445. setPromptTimeItId(tcpModel.json["id"].asInt());
  446. }
  447. }
  448. else if (tcpModel.type == TcpType::AUTH) {
  449. if (tcpModel.action == AuthAction::LOGIN) {
  450. // 需要判断fromId跟自己是不是一样的,一样的就代表是自己发送出去,不一样的就代表员工已经登录到其他设备去了
  451. if (tcpModel.from_id == StoragePreferences::getInt(STORE_DEVICE_ID, 0)) {
  452. LOGD("id ===> %d", tcpModel.json["id"].asInt());
  453. setAuthItId(tcpModel.json["id"].asInt());
  454. }
  455. else {
  456. logoutMedicalCare();
  457. const char* currentAppName = EASYUICONTEXT->currentAppName();
  458. string _currentAppName = currentAppName;
  459. if (_currentAppName == "medicalCareActivity") {
  460. goHome();
  461. }
  462. }
  463. }
  464. }
  465. }
  466. bool getSleepTimerRegistered() {
  467. return isSleepTimerRegistered;
  468. }
  469. // 触发定时任务
  470. void setSleepTimerRegistered(bool result) {
  471. LOGD("isSleepTimerRegistered = %d", isSleepTimerRegistered);
  472. LOGD("result = %d", result);
  473. if (mActivityPtr == NULL) {
  474. return;
  475. }
  476. if (result != isSleepTimerRegistered) { // 与定时任务不同时,才进行处理
  477. if (isSleepTimerRegistered) { // 与定时任务开关进行判断,如果定时任务开启,那就要关闭
  478. isSleepTimerRegistered = result;
  479. mActivityPtr->unregisterUserTimer(SLEEP_STRAT_TIME_HANDLE); // 关闭定时器
  480. if (isSleep) {
  481. isSleep = false;
  482. BRIGHTNESSHELPER->screenOn();
  483. }
  484. LOGD("关闭息屏");
  485. } else { // 如果定时任务关着,那就要打开
  486. isSleepTimerRegistered = result;
  487. int sleepTime = StoragePreferences::getInt(STORE_SLEEP_TIME, 3);
  488. if (sleepTime > 0) {
  489. mActivityPtr->registerUserTimer(SLEEP_STRAT_TIME_HANDLE, sleepTime * 60 * 1000);
  490. LOGD("开启息屏");
  491. }
  492. }
  493. }
  494. }
  495. void scrrenOn() {
  496. isSleep = false;
  497. BRIGHTNESSHELPER->screenOn();
  498. }
  499. void setCallTid(std::string tid) {
  500. callTid = tid;
  501. }
  502. //================================= IO 口操作
  503. //紧急按钮灯,明/灭
  504. void setSOS_A5(bool light){
  505. int result = -1;
  506. int slight = light?0:1;
  507. result = GpioHelper::output("A5", slight);
  508. if (result==0){
  509. LOGD("set A5 SOS light %d. success",light);
  510. } else {
  511. LOGD("set A5 SOS light %d. failed",light);
  512. }
  513. }
  514. //A6,A7,A8 RGB灯控制
  515. void setRGB_A678(bool R, bool G, bool B){
  516. int rR = -1, rG = -1, rB = -1;
  517. int sR = R?0:1, sG = G?0:1, sB = B?0:1;
  518. rR = GpioHelper::output("A6", sR);
  519. rG = GpioHelper::output("A7", sG);
  520. rB = GpioHelper::output("A8", sB);
  521. const char *strLog = "set %s light %d. %d";
  522. LOGD(strLog,"A6 R",R, rR);
  523. LOGD(strLog,"A7 G",G, rG);
  524. LOGD(strLog,"A8 B",B, rB);
  525. }
  526. //手柄按钮
  527. class A1GpioListener : public IGpioListener{
  528. public:
  529. bool onGpioEdge(const char *pPin) override {
  530. int state = GpioHelper::input("A1");
  531. LOGD("IGpioListener GPIO IS A1=%d", state);
  532. if (state==0){
  533. voip::CallInfo info = GetTelephone()->GetCallInfo();
  534. //拨打 或 挂断
  535. if (CallingStatus::instance()->busy()){
  536. callActivityFinish(CallFinishType::A1CLICK);
  537. } else {
  538. Intent* intent = new Intent();
  539. intent->putExtra(isOutgoing, "true");
  540. intent->putExtra(audioOnly, "true");
  541. EASYUICONTEXT->openActivity("callActivity", intent);
  542. }
  543. }
  544. return true;
  545. }
  546. void onGpioError(const char *pPin, int error) override {
  547. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  548. }
  549. };
  550. //7寸面板,呼出
  551. class A2GpioListener : public IGpioListener{
  552. public:
  553. bool onGpioEdge(const char *pPin) override {
  554. int state = GpioHelper::input("A2");
  555. LOGD("IGpioListener GPIO IS A2=%d", state);
  556. if (state==0){
  557. //拨打 或 接听
  558. }
  559. return true;
  560. }
  561. void onGpioError(const char *pPin, int error) override {
  562. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  563. }
  564. };
  565. //7寸面板,挂断
  566. class A3GpioListener : public IGpioListener{
  567. public:
  568. bool onGpioEdge(const char *pPin) override {
  569. int state = GpioHelper::input("A3");
  570. LOGD("IGpioListener GPIO IS A3=%d", state);
  571. if (state==0){
  572. //挂断
  573. }
  574. return true;
  575. }
  576. void onGpioError(const char *pPin, int error) override {
  577. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  578. }
  579. };
  580. //紧急按钮
  581. class A4GpioListener : public IGpioListener{
  582. public:
  583. bool onGpioEdge(const char *pPin) override {
  584. int state = GpioHelper::input("A4");
  585. LOGD("IGpioListener GPIO IS A4=%d", state);
  586. if (state==0){
  587. //发出
  588. setSOS_A5(true);
  589. }
  590. return true;
  591. }
  592. void onGpioError(const char *pPin, int error) override {
  593. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  594. }
  595. };
  596. /**
  597. * 注册定时器
  598. * 填充数组用于注册定时器
  599. * 注意:id不能重复
  600. */
  601. static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
  602. {1, 5000},
  603. {2, 30*1000}
  604. };
  605. /**
  606. * 当界面构造时触发
  607. */
  608. static void onUI_init(){
  609. //IO监测
  610. // IGpioListener *iGpioListenerA1 = new A1GpioListener();
  611. // GpioHelper::registerGpioListener("A1", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  612. //
  613. // IGpioListener *iGpioListenerA2 = new A2GpioListener();
  614. // GpioHelper::registerGpioListener("A2", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  615. //
  616. // IGpioListener *iGpioListenerA3 = new A3GpioListener();
  617. // GpioHelper::registerGpioListener("A3", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  618. //
  619. // IGpioListener *iGpioListenerA4 = new A4GpioListener();
  620. // GpioHelper::registerGpioListener("A4", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  621. //TCP启动
  622. TcpClient::instance()->startTcp();
  623. //监听SIP信令
  624. addSipCallStateListener();
  625. //开启网页
  626. httpServer.RunAsync(HTTP_SERVER_PORT);
  627. // 启动的时候更改为tcp模式
  628. StoragePreferences::putString(STORE_SIGNAL_TYPE, "TCP");
  629. if (StoragePreferences::getBool(STORE_NURSING_TYPE, false)) {
  630. LOGD("===============> 关闭护理状态");
  631. StoragePreferences::putBool(STORE_NURSING_TYPE, false);
  632. isNursing = true;
  633. }
  634. //请求版本号
  635. // if(UartContext::Uart3IsOpen()) {
  636. // string heartStr = "ASK,VNF-0";
  637. // const char* sendMsg = heartStr.c_str();
  638. // sendProtocolTo(UART_TTYS3, (byte*)(sendMsg), strlen(sendMsg));
  639. // }
  640. lightControl("CALLLED", "011F");
  641. #if 0
  642. std::thread backend([](){
  643. while (true) {
  644. if (feed_dogs > 0) {
  645. --feed_dogs;
  646. LOGD("feed");
  647. int ret = GpioHelper::output("GPIO_2", 1); //拉高
  648. if (ret != 0) {
  649. LOGE("GPIO操作失败");
  650. }
  651. usleep(1000 * 20);
  652. ret = GpioHelper::output("GPIO_2", 0); //拉低
  653. if (ret != 0) {
  654. LOGE("GPIO操作失败");
  655. }
  656. }
  657. usleep(1000 * 1000);
  658. }
  659. });
  660. backend.detach();
  661. #endif
  662. }
  663. /**
  664. * 当切换到该界面时触发
  665. */
  666. static void onUI_intent(const Intent *intentPtr) {
  667. if (intentPtr != NULL) {
  668. //TODO
  669. }
  670. }
  671. /*
  672. * 当界面显示时触发
  673. */
  674. static void onUI_show() {
  675. StoragePreferences::putString(STORE_SIGNAL_TYPE, "TCP");
  676. //进入主界面
  677. EASYUICONTEXT->openActivity("mainActivity");
  678. }
  679. /*
  680. * 当界面隐藏时触发
  681. */
  682. static void onUI_hide() {
  683. }
  684. /*
  685. * 当界面完全退出时触发
  686. */
  687. static void onUI_quit() {
  688. }
  689. /**
  690. * 串口数据回调接口
  691. */
  692. static void onProtocolDataUpdate(const SProtocolData &data) {
  693. LOGD("cmd = %s", data.cmd.c_str());
  694. if (data.state!=""){
  695. LOGD("state = %s",data.state.c_str());
  696. }
  697. // if (data.msg != ""){
  698. // LOGD("msg = %s", data.msg.c_str());
  699. // }
  700. if(UartContext::Uart3IsOpen()) { // 如果是true,表示串口打开了
  701. string heartStr;
  702. //按下
  703. if (data.state=="0"){ // 0表示正被按下,1表示短按松开,2表示长按松开。
  704. if (data.cmd == "KEY5") { // key5是面板拨号按钮
  705. if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP") {
  706. setCheckbox("KEY5");
  707. return;
  708. }
  709. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  710. if (isSleep) {
  711. scrrenOn();
  712. }
  713. else {
  714. voip::CallInfo info = GetTelephone()->GetCallInfo();
  715. //拨打 或 挂断
  716. if (CallingStatus::instance()->busy()){
  717. callActivityFinish(CallFinishType::A1CLICK);
  718. } else {
  719. Intent* intent = new Intent();
  720. intent->putExtra(isOutgoing, "true");
  721. intent->putExtra(audioOnly, "true");
  722. EASYUICONTEXT->openActivity("callActivity", intent);
  723. }
  724. }
  725. }
  726. else {
  727. scrrenOn();
  728. voip::CallInfo info = GetTelephone()->GetCallInfo();
  729. //拨打 或 挂断
  730. if (CallingStatus::instance()->busy()){
  731. callActivityFinish(CallFinishType::A1CLICK);
  732. } else {
  733. Intent* intent = new Intent();
  734. intent->putExtra(isOutgoing, "true");
  735. intent->putExtra(audioOnly, "true");
  736. EASYUICONTEXT->openActivity("callActivity", intent);
  737. }
  738. }
  739. }
  740. else if (data.cmd == "KEY6"){ // key6是手柄触发,手柄只有呼叫和挂断功能,没有取消功能,防止用户不停地按
  741. if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP") {
  742. setCheckbox("KEY6");
  743. return;
  744. }
  745. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  746. if (isSleep) {
  747. scrrenOn();
  748. }
  749. else {
  750. voip::CallInfo info = GetTelephone()->GetCallInfo();
  751. //拨打 或 挂断
  752. if (CallingStatus::instance()->busy()){
  753. callActivityFinish(CallFinishType::KEY6_CLICK);
  754. } else {
  755. Intent* intent = new Intent();
  756. intent->putExtra(isOutgoing, "true");
  757. intent->putExtra(audioOnly, "true");
  758. EASYUICONTEXT->openActivity("callActivity", intent);
  759. }
  760. }
  761. }
  762. else {
  763. scrrenOn();
  764. voip::CallInfo info = GetTelephone()->GetCallInfo();
  765. //拨打 或 挂断
  766. if (CallingStatus::instance()->busy()){
  767. callActivityFinish(CallFinishType::KEY6_CLICK);
  768. } else {
  769. Intent* intent = new Intent();
  770. intent->putExtra(isOutgoing, "true");
  771. intent->putExtra(audioOnly, "true");
  772. EASYUICONTEXT->openActivity("callActivity", intent);
  773. }
  774. }
  775. }
  776. else if (data.cmd == "KEY7"){ // key7是紧急按钮
  777. if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP") {
  778. setCheckbox("KEY7");
  779. return;
  780. }
  781. if (isSleep) {
  782. scrrenOn();
  783. }
  784. if (sosTimerRegistered) {
  785. LOGD("TCP -> SOS_CALL");
  786. TcpModel tcpModel;
  787. tcpModel.type = TcpType::SOS;
  788. tcpModel.action = SosAction::CALL;
  789. tcpModel.from_id = StoragePreferences::getInt(STORE_DEVICE_ID, 0);
  790. tcpModel.tid = base::format("t%d",TimeHelper::getCurrentTime());
  791. std::string req = getTcpModelString(tcpModel);
  792. LOGD("TCP SOS_CALL : %s",req.c_str());
  793. TcpClient::instance()->sendMsg(req.c_str());
  794. // SOS亮红灯
  795. lightControl("ULED", "1F");
  796. // 门灯亮红灯
  797. lightControl("DOORLED", "200F");
  798. lightControl("CALLLED", "100F");
  799. mActivityPtr->registerUserTimer(SOS_CLICK_TIME_HANDLE, 10000); // 10秒后才能触发
  800. mActivityPtr->registerUserTimer(SOS_LAMP_TIME_HANDLE, 120000); // 2分钟后才能触发
  801. Intent* intent = new Intent();
  802. intent->putExtra(functionWindows, "sosCall");
  803. EASYUICONTEXT->openActivity("functionActivity", intent);
  804. }
  805. }
  806. else if (data.cmd == "KEY<") { //下左3,红
  807. if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP") {
  808. setCheckbox("KEY<");
  809. return;
  810. }
  811. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  812. if (isSleep) {
  813. scrrenOn();
  814. }
  815. else {
  816. voip::CallInfo info = GetTelephone()->GetCallInfo();
  817. //拨打 或 挂断
  818. if (CallingStatus::instance()->busy()){
  819. callActivityFinish(CallFinishType::A1CLICK);
  820. } else {
  821. Intent* intent = new Intent();
  822. intent->putExtra(isOutgoing, "true");
  823. intent->putExtra(audioOnly, "true");
  824. EASYUICONTEXT->openActivity("callActivity", intent);
  825. }
  826. }
  827. }
  828. else {
  829. scrrenOn();
  830. voip::CallInfo info = GetTelephone()->GetCallInfo();
  831. //拨打 或 挂断
  832. if (CallingStatus::instance()->busy()){
  833. callActivityFinish(CallFinishType::A1CLICK);
  834. } else {
  835. Intent* intent = new Intent();
  836. intent->putExtra(isOutgoing, "true");
  837. intent->putExtra(audioOnly, "true");
  838. EASYUICONTEXT->openActivity("callActivity", intent);
  839. }
  840. }
  841. }
  842. else if (data.cmd == "KEY>") { // key>是面板挂断按钮
  843. if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP") {
  844. setCheckbox("KEY>");
  845. return;
  846. }
  847. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  848. if (isSleep) {
  849. scrrenOn();
  850. }
  851. else {
  852. voip::CallInfo info = GetTelephone()->GetCallInfo();
  853. //拨打 或 挂断
  854. if (CallingStatus::instance()->busy()){
  855. callActivityFinish(CallFinishType::A1CLICK);
  856. lightControl("DOORLED", "000F");
  857. }
  858. }
  859. }
  860. else {
  861. scrrenOn();
  862. voip::CallInfo info = GetTelephone()->GetCallInfo();
  863. //拨打 或 挂断
  864. if (CallingStatus::instance()->busy()){
  865. callActivityFinish(CallFinishType::A1CLICK);
  866. lightControl("DOORLED", "000F");
  867. }
  868. }
  869. }
  870. else if (data.cmd == "KEY;"){ //下左4,白
  871. if (isSleep) {
  872. scrrenOn();
  873. }
  874. // heartStr = "DOORLED,111F";
  875. lightControl("DOORLED", "111F");
  876. }
  877. else if (data.cmd == "KEY@") {
  878. scrrenOn();
  879. LOGD("Trigger sos cancel");
  880. if (sosInteraction > 0 && sosInteraction.isMember("id")) {
  881. sendSosCancel(sosInteraction["id"].asString());
  882. sosInteraction = Json::Value();
  883. }
  884. }
  885. else {
  886. if (isSleep) {
  887. scrrenOn();
  888. }
  889. // heartStr = "DOORLED,000F";
  890. lightControl("DOORLED", "000F");
  891. }
  892. // 发送给板子的指令
  893. // const char* sendMsg = heartStr.c_str();
  894. // sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  895. }
  896. }
  897. }
  898. /**
  899. * 定时器触发函数
  900. * 不建议在此函数中写耗时操作,否则将影响UI刷新
  901. * 参数: id
  902. * 当前所触发定时器的id,与注册时的id相同
  903. * 返回值: true
  904. * 继续运行当前定时器
  905. * false
  906. * 停止运行当前定时器
  907. */
  908. static bool onUI_Timer(int id){
  909. switch (id) {
  910. case 1:
  911. {
  912. //定时器跑在UI线程,这里将喂狗标记加1
  913. //++feed_dogs;
  914. //不用心跳
  915. // if(UartContext::Uart3IsOpen()) {
  916. // string heartStr = "HEART,1E";
  917. // const char* sendMsg = heartStr.c_str();
  918. // sendProtocolTo(UART_TTYS3, (byte*)(sendMsg), strlen(sendMsg));
  919. // }
  920. }
  921. break;
  922. case 2: //TCP心跳
  923. {
  924. const char* req = "0";
  925. TcpClient::instance()->sendMsg(req);
  926. }
  927. break;
  928. case SOS_CLICK_TIME_HANDLE: {
  929. sosTimerRegistered = true;
  930. return false;
  931. }
  932. break;
  933. case SOS_LAMP_TIME_HANDLE: {
  934. // SOS还原成无灯光的状态
  935. lightControl("ULED", "0F");
  936. // 进行门灯还原
  937. lightControl("DOORLED", "000F");
  938. lightControl("CALLLED", "000F");
  939. return false;
  940. }
  941. break;
  942. case SLEEP_STRAT_TIME_HANDLE: { // 息屏
  943. if (isSleepTimerRegistered) {
  944. isSleep = true;
  945. BRIGHTNESSHELPER->screenOff();
  946. } else { // 如果是false的话,就需要关闭定时器
  947. isSleep = false;
  948. BRIGHTNESSHELPER->screenOn();
  949. }
  950. }
  951. break;
  952. case SYS_RESTART_TIME_HANDLE:
  953. netresetInt += 1;
  954. lightControl("NETRESET", "1F");
  955. if (netresetInt == 5) {
  956. LOGD("网卡重启无效,程序内部重启");
  957. //重启
  958. sync();
  959. reboot(RB_AUTOBOOT);
  960. }
  961. break;
  962. case SIP_RECONNECT_TIME_HANDLE: {
  963. removeSipCallStateListener();
  964. removeSipRegisterStateListener();
  965. ReleaseTelephone();
  966. addSipCallStateListener();
  967. addSipRegisterStateListener();
  968. return false;
  969. }
  970. break;
  971. default:
  972. break;
  973. }
  974. return true;
  975. }
  976. /**
  977. * 有新的触摸事件时触发
  978. * 参数:ev
  979. * 新的触摸事件
  980. * 返回值:true
  981. * 表示该触摸事件在此被拦截,系统不再将此触摸事件传递到控件上
  982. * false
  983. * 触摸事件将继续传递到控件上
  984. */
  985. static bool onstartActivityTouchEvent(const MotionEvent &ev) {
  986. switch (ev.mActionStatus) {
  987. case MotionEvent::E_ACTION_DOWN://触摸按下
  988. //LOGD("时刻 = %ld 坐标 x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
  989. break;
  990. case MotionEvent::E_ACTION_MOVE://触摸滑动
  991. break;
  992. case MotionEvent::E_ACTION_UP: //触摸抬起
  993. break;
  994. default:
  995. break;
  996. }
  997. return false;
  998. }