startLogic.cc 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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. #if 0
  636. std::thread backend([](){
  637. while (true) {
  638. if (feed_dogs > 0) {
  639. --feed_dogs;
  640. LOGD("feed");
  641. int ret = GpioHelper::output("GPIO_2", 1); //拉高
  642. if (ret != 0) {
  643. LOGE("GPIO操作失败");
  644. }
  645. usleep(1000 * 20);
  646. ret = GpioHelper::output("GPIO_2", 0); //拉低
  647. if (ret != 0) {
  648. LOGE("GPIO操作失败");
  649. }
  650. }
  651. usleep(1000 * 1000);
  652. }
  653. });
  654. backend.detach();
  655. #endif
  656. }
  657. /**
  658. * 当切换到该界面时触发
  659. */
  660. static void onUI_intent(const Intent *intentPtr) {
  661. if (intentPtr != NULL) {
  662. //TODO
  663. }
  664. }
  665. /*
  666. * 当界面显示时触发
  667. */
  668. static void onUI_show() {
  669. StoragePreferences::putString(STORE_SIGNAL_TYPE, "TCP");
  670. //进入主界面
  671. EASYUICONTEXT->openActivity("mainActivity");
  672. }
  673. /*
  674. * 当界面隐藏时触发
  675. */
  676. static void onUI_hide() {
  677. }
  678. /*
  679. * 当界面完全退出时触发
  680. */
  681. static void onUI_quit() {
  682. }
  683. /**
  684. * 串口数据回调接口
  685. */
  686. static void onProtocolDataUpdate(const SProtocolData &data) {
  687. LOGD("cmd = %s", data.cmd.c_str());
  688. if (data.state!=""){
  689. LOGD("state = %s",data.state.c_str());
  690. }
  691. // if (data.msg != ""){
  692. // LOGD("msg = %s", data.msg.c_str());
  693. // }
  694. if(UartContext::Uart3IsOpen()) { // 如果是true,表示串口打开了
  695. string heartStr;
  696. //按下
  697. if (data.state=="0"){ // 0表示正被按下,1表示短按松开,2表示长按松开。
  698. if (data.cmd == "KEY5") { // key5是面板拨号按钮
  699. if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP") {
  700. setCheckbox("KEY5");
  701. return;
  702. }
  703. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  704. if (isSleep) {
  705. scrrenOn();
  706. }
  707. else {
  708. voip::CallInfo info = GetTelephone()->GetCallInfo();
  709. //拨打 或 挂断
  710. if (CallingStatus::instance()->busy()){
  711. callActivityFinish(CallFinishType::A1CLICK);
  712. } else {
  713. Intent* intent = new Intent();
  714. intent->putExtra(isOutgoing, "true");
  715. intent->putExtra(audioOnly, "true");
  716. EASYUICONTEXT->openActivity("callActivity", intent);
  717. }
  718. }
  719. }
  720. else {
  721. scrrenOn();
  722. voip::CallInfo info = GetTelephone()->GetCallInfo();
  723. //拨打 或 挂断
  724. if (CallingStatus::instance()->busy()){
  725. callActivityFinish(CallFinishType::A1CLICK);
  726. } else {
  727. Intent* intent = new Intent();
  728. intent->putExtra(isOutgoing, "true");
  729. intent->putExtra(audioOnly, "true");
  730. EASYUICONTEXT->openActivity("callActivity", intent);
  731. }
  732. }
  733. }
  734. else if (data.cmd == "KEY6"){ // key6是手柄触发,手柄只有呼叫和挂断功能,没有取消功能,防止用户不停地按
  735. if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP") {
  736. setCheckbox("KEY6");
  737. return;
  738. }
  739. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  740. if (isSleep) {
  741. scrrenOn();
  742. }
  743. else {
  744. voip::CallInfo info = GetTelephone()->GetCallInfo();
  745. //拨打 或 挂断
  746. if (CallingStatus::instance()->busy()){
  747. callActivityFinish(CallFinishType::KEY6_CLICK);
  748. } else {
  749. Intent* intent = new Intent();
  750. intent->putExtra(isOutgoing, "true");
  751. intent->putExtra(audioOnly, "true");
  752. EASYUICONTEXT->openActivity("callActivity", intent);
  753. }
  754. }
  755. }
  756. else {
  757. scrrenOn();
  758. voip::CallInfo info = GetTelephone()->GetCallInfo();
  759. //拨打 或 挂断
  760. if (CallingStatus::instance()->busy()){
  761. callActivityFinish(CallFinishType::KEY6_CLICK);
  762. } else {
  763. Intent* intent = new Intent();
  764. intent->putExtra(isOutgoing, "true");
  765. intent->putExtra(audioOnly, "true");
  766. EASYUICONTEXT->openActivity("callActivity", intent);
  767. }
  768. }
  769. }
  770. else if (data.cmd == "KEY7"){ // key7是紧急按钮
  771. if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP") {
  772. setCheckbox("KEY7");
  773. return;
  774. }
  775. if (isSleep) {
  776. scrrenOn();
  777. }
  778. if (sosTimerRegistered) {
  779. LOGD("TCP -> SOS_CALL");
  780. TcpModel tcpModel;
  781. tcpModel.type = TcpType::SOS;
  782. tcpModel.action = SosAction::CALL;
  783. tcpModel.from_id = StoragePreferences::getInt(STORE_DEVICE_ID, 0);
  784. tcpModel.tid = base::format("t%d",TimeHelper::getCurrentTime());
  785. std::string req = getTcpModelString(tcpModel);
  786. LOGD("TCP SOS_CALL : %s",req.c_str());
  787. TcpClient::instance()->sendMsg(req.c_str());
  788. // SOS亮红灯
  789. lightControl("ULED", "1F");
  790. // 门灯亮红灯
  791. lightControl("DOORLED", "200F");
  792. lightControl("CALLLED", "100F");
  793. mActivityPtr->registerUserTimer(SOS_CLICK_TIME_HANDLE, 10000); // 10秒后才能触发
  794. mActivityPtr->registerUserTimer(SOS_LAMP_TIME_HANDLE, 120000); // 2分钟后才能触发
  795. Intent* intent = new Intent();
  796. intent->putExtra(functionWindows, "sosCall");
  797. EASYUICONTEXT->openActivity("functionActivity", intent);
  798. }
  799. }
  800. else if (data.cmd == "KEY<") { //下左3,红
  801. if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP") {
  802. setCheckbox("KEY<");
  803. return;
  804. }
  805. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  806. if (isSleep) {
  807. scrrenOn();
  808. }
  809. else {
  810. voip::CallInfo info = GetTelephone()->GetCallInfo();
  811. //拨打 或 挂断
  812. if (CallingStatus::instance()->busy()){
  813. callActivityFinish(CallFinishType::A1CLICK);
  814. } else {
  815. Intent* intent = new Intent();
  816. intent->putExtra(isOutgoing, "true");
  817. intent->putExtra(audioOnly, "true");
  818. EASYUICONTEXT->openActivity("callActivity", intent);
  819. }
  820. }
  821. }
  822. else {
  823. scrrenOn();
  824. voip::CallInfo info = GetTelephone()->GetCallInfo();
  825. //拨打 或 挂断
  826. if (CallingStatus::instance()->busy()){
  827. callActivityFinish(CallFinishType::A1CLICK);
  828. } else {
  829. Intent* intent = new Intent();
  830. intent->putExtra(isOutgoing, "true");
  831. intent->putExtra(audioOnly, "true");
  832. EASYUICONTEXT->openActivity("callActivity", intent);
  833. }
  834. }
  835. }
  836. else if (data.cmd == "KEY>") { // key>是面板挂断按钮
  837. if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP") {
  838. setCheckbox("KEY>");
  839. return;
  840. }
  841. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  842. if (isSleep) {
  843. scrrenOn();
  844. }
  845. else {
  846. voip::CallInfo info = GetTelephone()->GetCallInfo();
  847. //拨打 或 挂断
  848. if (CallingStatus::instance()->busy()){
  849. callActivityFinish(CallFinishType::A1CLICK);
  850. lightControl("DOORLED", "000F");
  851. }
  852. }
  853. }
  854. else {
  855. scrrenOn();
  856. voip::CallInfo info = GetTelephone()->GetCallInfo();
  857. //拨打 或 挂断
  858. if (CallingStatus::instance()->busy()){
  859. callActivityFinish(CallFinishType::A1CLICK);
  860. lightControl("DOORLED", "000F");
  861. }
  862. }
  863. }
  864. else if (data.cmd == "KEY;"){ //下左4,白
  865. if (isSleep) {
  866. scrrenOn();
  867. }
  868. // heartStr = "DOORLED,111F";
  869. lightControl("DOORLED", "111F");
  870. }
  871. else if (data.cmd == "KEY@") {
  872. scrrenOn();
  873. LOGD("Trigger sos cancel");
  874. if (sosInteraction > 0 && sosInteraction.isMember("id")) {
  875. sendSosCancel(sosInteraction["id"].asString());
  876. sosInteraction = Json::Value();
  877. }
  878. }
  879. else {
  880. if (isSleep) {
  881. scrrenOn();
  882. }
  883. // heartStr = "DOORLED,000F";
  884. lightControl("DOORLED", "000F");
  885. }
  886. // 发送给板子的指令
  887. // const char* sendMsg = heartStr.c_str();
  888. // sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  889. }
  890. }
  891. }
  892. /**
  893. * 定时器触发函数
  894. * 不建议在此函数中写耗时操作,否则将影响UI刷新
  895. * 参数: id
  896. * 当前所触发定时器的id,与注册时的id相同
  897. * 返回值: true
  898. * 继续运行当前定时器
  899. * false
  900. * 停止运行当前定时器
  901. */
  902. static bool onUI_Timer(int id){
  903. switch (id) {
  904. case 1:
  905. {
  906. //定时器跑在UI线程,这里将喂狗标记加1
  907. //++feed_dogs;
  908. //不用心跳
  909. // if(UartContext::Uart3IsOpen()) {
  910. // string heartStr = "HEART,1E";
  911. // const char* sendMsg = heartStr.c_str();
  912. // sendProtocolTo(UART_TTYS3, (byte*)(sendMsg), strlen(sendMsg));
  913. // }
  914. }
  915. break;
  916. case 2: //TCP心跳
  917. {
  918. const char* req = "0";
  919. TcpClient::instance()->sendMsg(req);
  920. }
  921. break;
  922. case SOS_CLICK_TIME_HANDLE: {
  923. sosTimerRegistered = true;
  924. return false;
  925. }
  926. break;
  927. case SOS_LAMP_TIME_HANDLE: {
  928. // SOS还原成无灯光的状态
  929. lightControl("ULED", "0F");
  930. // 进行门灯还原
  931. lightControl("DOORLED", "000F");
  932. lightControl("CALLLED", "000F");
  933. return false;
  934. }
  935. break;
  936. case SLEEP_STRAT_TIME_HANDLE: { // 息屏
  937. if (isSleepTimerRegistered) {
  938. isSleep = true;
  939. BRIGHTNESSHELPER->screenOff();
  940. } else { // 如果是false的话,就需要关闭定时器
  941. isSleep = false;
  942. BRIGHTNESSHELPER->screenOn();
  943. }
  944. }
  945. break;
  946. case SYS_RESTART_TIME_HANDLE:
  947. netresetInt += 1;
  948. lightControl("NETRESET", "1F");
  949. if (netresetInt == 5) {
  950. LOGD("网卡重启无效,程序内部重启");
  951. //重启
  952. deviceReboot();
  953. }
  954. break;
  955. case SIP_RECONNECT_TIME_HANDLE: {
  956. removeSipCallStateListener();
  957. removeSipRegisterStateListener();
  958. ReleaseTelephone();
  959. addSipCallStateListener();
  960. addSipRegisterStateListener();
  961. return false;
  962. }
  963. break;
  964. default:
  965. break;
  966. }
  967. return true;
  968. }
  969. /**
  970. * 有新的触摸事件时触发
  971. * 参数:ev
  972. * 新的触摸事件
  973. * 返回值:true
  974. * 表示该触摸事件在此被拦截,系统不再将此触摸事件传递到控件上
  975. * false
  976. * 触摸事件将继续传递到控件上
  977. */
  978. static bool onstartActivityTouchEvent(const MotionEvent &ev) {
  979. switch (ev.mActionStatus) {
  980. case MotionEvent::E_ACTION_DOWN://触摸按下
  981. //LOGD("时刻 = %ld 坐标 x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
  982. break;
  983. case MotionEvent::E_ACTION_MOVE://触摸滑动
  984. break;
  985. case MotionEvent::E_ACTION_UP: //触摸抬起
  986. break;
  987. default:
  988. break;
  989. }
  990. return false;
  991. }