startLogic.cc 31 KB

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