startLogic.cc 30 KB

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