startLogic.cc 29 KB

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