startLogic.cc 31 KB

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