startLogic.cc 28 KB

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