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