startLogic.cc 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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. }
  379. }
  380. else if (tcpModel.type == TcpType::CALLBACK) {
  381. if (tcpModel.action == CallbackAction::ACK) {
  382. LOGD("CALLBACK ACK !!!!");
  383. //回调
  384. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  385. if (callback.tid != "0"){
  386. callback.onSuccess(tcpModel.json);
  387. }
  388. }
  389. else if (tcpModel.action == CallbackAction::SUCCESS) {
  390. //回调
  391. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  392. if (callback.tid != "0"){
  393. callback.onSuccess(tcpModel.json);
  394. }
  395. }
  396. else if (tcpModel.action == CallbackAction::FAILED) {
  397. //回调
  398. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  399. if (callback.tid != "0"){
  400. callback.onFalied(tcpModel.json);
  401. }
  402. if (tcpModel.tid == callTid){
  403. callActivityFinish(CallFinishType::FAILED);
  404. }
  405. }
  406. }
  407. else if (tcpModel.type == TcpType::POSITION) {
  408. if (tcpModel.action == PositionAction::POSITION_START) {
  409. setPosistionItId(tcpModel.json["id"].asInt());
  410. }
  411. }
  412. else if (tcpModel.type == TcpType::ROOMCHECK) {
  413. if (tcpModel.action == RoomCheckAction::START) {
  414. setRoomCheckItId(tcpModel.json["id"].asInt());
  415. }
  416. }
  417. else if (tcpModel.type == TcpType::SCREEN_TIP) {
  418. if (tcpModel.action == ScreenTipAction::START) {
  419. setPromptTimeItId(tcpModel.json["id"].asInt());
  420. }
  421. }
  422. else if (tcpModel.type == TcpType::AUTH) {
  423. if (tcpModel.action == AuthAction::LOGIN) {
  424. // 需要判断fromId跟自己是不是一样的,一样的就代表是自己发送出去,不一样的就代表员工已经登录到其他设备去了
  425. if (tcpModel.from_id == StoragePreferences::getInt(STORE_DEVICE_ID, 0)) {
  426. LOGD("id ===> %d", tcpModel.json["id"].asInt());
  427. setAuthItId(tcpModel.json["id"].asInt());
  428. }
  429. else {
  430. logoutMedicalCare();
  431. const char* currentAppName = EASYUICONTEXT->currentAppName();
  432. string _currentAppName = currentAppName;
  433. if (_currentAppName == "medicalCareActivity") {
  434. goHome();
  435. }
  436. }
  437. }
  438. }
  439. }
  440. bool getSleepTimerRegistered() {
  441. return isSleepTimerRegistered;
  442. }
  443. // 触发定时任务
  444. void setSleepTimerRegistered(bool result) {
  445. LOGD("isSleepTimerRegistered = %d", isSleepTimerRegistered);
  446. LOGD("result = %d", result);
  447. if (mActivityPtr == NULL) {
  448. return;
  449. }
  450. if (result != isSleepTimerRegistered) { // 与定时任务不同时,才进行处理
  451. if (isSleepTimerRegistered) { // 与定时任务开关进行判断,如果定时任务开启,那就要关闭
  452. isSleepTimerRegistered = result;
  453. mActivityPtr->unregisterUserTimer(SLEEP_STRAT_TIME_HANDLE); // 关闭定时器
  454. if (isSleep) {
  455. isSleep = false;
  456. BRIGHTNESSHELPER->screenOn();
  457. }
  458. LOGD("关闭息屏");
  459. } else { // 如果定时任务关着,那就要打开
  460. isSleepTimerRegistered = result;
  461. int sleepTime = StoragePreferences::getInt(STORE_SLEEP_TIME, 3);
  462. if (sleepTime > 0) {
  463. mActivityPtr->registerUserTimer(SLEEP_STRAT_TIME_HANDLE, sleepTime * 60 * 1000);
  464. LOGD("开启息屏");
  465. }
  466. }
  467. }
  468. }
  469. void scrrenOn() {
  470. isSleep = false;
  471. BRIGHTNESSHELPER->screenOn();
  472. }
  473. void setCallTid(std::string tid) {
  474. callTid = tid;
  475. }
  476. //================================= IO 口操作
  477. //紧急按钮灯,明/灭
  478. void setSOS_A5(bool light){
  479. int result = -1;
  480. int slight = light?0:1;
  481. result = GpioHelper::output("A5", slight);
  482. if (result==0){
  483. LOGD("set A5 SOS light %d. success",light);
  484. } else {
  485. LOGD("set A5 SOS light %d. failed",light);
  486. }
  487. }
  488. //A6,A7,A8 RGB灯控制
  489. void setRGB_A678(bool R, bool G, bool B){
  490. int rR = -1, rG = -1, rB = -1;
  491. int sR = R?0:1, sG = G?0:1, sB = B?0:1;
  492. rR = GpioHelper::output("A6", sR);
  493. rG = GpioHelper::output("A7", sG);
  494. rB = GpioHelper::output("A8", sB);
  495. const char *strLog = "set %s light %d. %d";
  496. LOGD(strLog,"A6 R",R, rR);
  497. LOGD(strLog,"A7 G",G, rG);
  498. LOGD(strLog,"A8 B",B, rB);
  499. }
  500. //手柄按钮
  501. class A1GpioListener : public IGpioListener{
  502. public:
  503. bool onGpioEdge(const char *pPin) override {
  504. int state = GpioHelper::input("A1");
  505. LOGD("IGpioListener GPIO IS A1=%d", state);
  506. if (state==0){
  507. voip::CallInfo info = GetTelephone()->GetCallInfo();
  508. //拨打 或 挂断
  509. if (CallingStatus::instance()->busy()){
  510. callActivityFinish(CallFinishType::A1CLICK);
  511. } else {
  512. Intent* intent = new Intent();
  513. intent->putExtra(isOutgoing, "true");
  514. intent->putExtra(audioOnly, "true");
  515. EASYUICONTEXT->openActivity("callActivity", intent);
  516. }
  517. }
  518. return true;
  519. }
  520. void onGpioError(const char *pPin, int error) override {
  521. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  522. }
  523. };
  524. //7寸面板,呼出
  525. class A2GpioListener : public IGpioListener{
  526. public:
  527. bool onGpioEdge(const char *pPin) override {
  528. int state = GpioHelper::input("A2");
  529. LOGD("IGpioListener GPIO IS A2=%d", state);
  530. if (state==0){
  531. //拨打 或 接听
  532. }
  533. return true;
  534. }
  535. void onGpioError(const char *pPin, int error) override {
  536. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  537. }
  538. };
  539. //7寸面板,挂断
  540. class A3GpioListener : public IGpioListener{
  541. public:
  542. bool onGpioEdge(const char *pPin) override {
  543. int state = GpioHelper::input("A3");
  544. LOGD("IGpioListener GPIO IS A3=%d", state);
  545. if (state==0){
  546. //挂断
  547. }
  548. return true;
  549. }
  550. void onGpioError(const char *pPin, int error) override {
  551. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  552. }
  553. };
  554. //紧急按钮
  555. class A4GpioListener : public IGpioListener{
  556. public:
  557. bool onGpioEdge(const char *pPin) override {
  558. int state = GpioHelper::input("A4");
  559. LOGD("IGpioListener GPIO IS A4=%d", state);
  560. if (state==0){
  561. //发出
  562. setSOS_A5(true);
  563. }
  564. return true;
  565. }
  566. void onGpioError(const char *pPin, int error) override {
  567. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  568. }
  569. };
  570. /**
  571. * 注册定时器
  572. * 填充数组用于注册定时器
  573. * 注意:id不能重复
  574. */
  575. static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
  576. {1, 5000},
  577. {2, 30*1000}
  578. };
  579. /**
  580. * 当界面构造时触发
  581. */
  582. static void onUI_init(){
  583. //IO监测
  584. IGpioListener *iGpioListenerA1 = new A1GpioListener();
  585. GpioHelper::registerGpioListener("A1", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  586. IGpioListener *iGpioListenerA2 = new A2GpioListener();
  587. GpioHelper::registerGpioListener("A2", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  588. IGpioListener *iGpioListenerA3 = new A3GpioListener();
  589. GpioHelper::registerGpioListener("A3", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  590. IGpioListener *iGpioListenerA4 = new A4GpioListener();
  591. GpioHelper::registerGpioListener("A4", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  592. //TCP启动
  593. TcpClient::instance()->startTcp();
  594. //监听SIP信令
  595. GetTelephone()->AddCallStateListener(OnCallStateChanged);
  596. //开启网页
  597. httpServer.RunAsync(HTTP_SERVER_PORT);
  598. // 启动的时候更改为tcp模式
  599. StoragePreferences::putString(STORE_SIGNAL_TYPE, "TCP");
  600. if (StoragePreferences::getBool(STORE_NURSING_TYPE, false)) {
  601. LOGD("===============> 关闭护理状态");
  602. StoragePreferences::putBool(STORE_NURSING_TYPE, false);
  603. isNursing = true;
  604. }
  605. //请求版本号
  606. // if(UartContext::Uart3IsOpen()) {
  607. // string heartStr = "ASK,VNF-0";
  608. // const char* sendMsg = heartStr.c_str();
  609. // sendProtocolTo(UART_TTYS3, (byte*)(sendMsg), strlen(sendMsg));
  610. // }
  611. lightControl("CALLLED", "011F");
  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::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP") {
  713. setCheckbox("KEY6");
  714. return;
  715. }
  716. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  717. if (isSleep) {
  718. scrrenOn();
  719. }
  720. else {
  721. voip::CallInfo info = GetTelephone()->GetCallInfo();
  722. //拨打 或 挂断
  723. if (CallingStatus::instance()->busy()){
  724. callActivityFinish(CallFinishType::KEY6_CLICK);
  725. } else {
  726. Intent* intent = new Intent();
  727. intent->putExtra(isOutgoing, "true");
  728. intent->putExtra(audioOnly, "true");
  729. EASYUICONTEXT->openActivity("callActivity", intent);
  730. }
  731. }
  732. }
  733. else {
  734. scrrenOn();
  735. voip::CallInfo info = GetTelephone()->GetCallInfo();
  736. //拨打 或 挂断
  737. if (CallingStatus::instance()->busy()){
  738. callActivityFinish(CallFinishType::KEY6_CLICK);
  739. } else {
  740. Intent* intent = new Intent();
  741. intent->putExtra(isOutgoing, "true");
  742. intent->putExtra(audioOnly, "true");
  743. EASYUICONTEXT->openActivity("callActivity", intent);
  744. }
  745. }
  746. }
  747. else if (data.cmd == "KEY7"){ // key7是紧急按钮
  748. if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP") {
  749. setCheckbox("KEY7");
  750. return;
  751. }
  752. if (isSleep) {
  753. scrrenOn();
  754. }
  755. if (sosTimerRegistered) {
  756. LOGD("TCP -> SOS_CALL");
  757. TcpModel tcpModel;
  758. tcpModel.type = TcpType::SOS;
  759. tcpModel.action = SosAction::CALL;
  760. tcpModel.from_id = StoragePreferences::getInt(STORE_DEVICE_ID, 0);
  761. tcpModel.tid = base::format("t%d",TimeHelper::getCurrentTime());
  762. std::string req = getTcpModelString(tcpModel);
  763. LOGD("TCP SOS_CALL : %s",req.c_str());
  764. TcpClient::instance()->sendMsg(req.c_str());
  765. // SOS亮红灯
  766. lightControl("ULED", "1F");
  767. // 门灯亮红灯
  768. lightControl("DOORLED", "200F");
  769. lightControl("CALLLED", "100F");
  770. mActivityPtr->registerUserTimer(SOS_CLICK_TIME_HANDLE, 10000); // 10秒后才能触发
  771. mActivityPtr->registerUserTimer(SOS_LAMP_TIME_HANDLE, 120000); // 2分钟后才能触发
  772. }
  773. }
  774. else if (data.cmd == "KEY<") { //下左3,红
  775. if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP") {
  776. setCheckbox("KEY<");
  777. return;
  778. }
  779. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  780. if (isSleep) {
  781. scrrenOn();
  782. }
  783. else {
  784. voip::CallInfo info = GetTelephone()->GetCallInfo();
  785. //拨打 或 挂断
  786. if (CallingStatus::instance()->busy()){
  787. callActivityFinish(CallFinishType::A1CLICK);
  788. } else {
  789. Intent* intent = new Intent();
  790. intent->putExtra(isOutgoing, "true");
  791. intent->putExtra(audioOnly, "true");
  792. EASYUICONTEXT->openActivity("callActivity", intent);
  793. }
  794. }
  795. }
  796. else {
  797. scrrenOn();
  798. voip::CallInfo info = GetTelephone()->GetCallInfo();
  799. //拨打 或 挂断
  800. if (CallingStatus::instance()->busy()){
  801. callActivityFinish(CallFinishType::A1CLICK);
  802. } else {
  803. Intent* intent = new Intent();
  804. intent->putExtra(isOutgoing, "true");
  805. intent->putExtra(audioOnly, "true");
  806. EASYUICONTEXT->openActivity("callActivity", intent);
  807. }
  808. }
  809. }
  810. else if (data.cmd == "KEY>") { // key>是面板挂断按钮
  811. if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP") {
  812. setCheckbox("KEY>");
  813. return;
  814. }
  815. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  816. if (isSleep) {
  817. scrrenOn();
  818. }
  819. else {
  820. voip::CallInfo info = GetTelephone()->GetCallInfo();
  821. //拨打 或 挂断
  822. if (CallingStatus::instance()->busy()){
  823. callActivityFinish(CallFinishType::A1CLICK);
  824. lightControl("DOORLED", "000F");
  825. }
  826. }
  827. }
  828. else {
  829. scrrenOn();
  830. voip::CallInfo info = GetTelephone()->GetCallInfo();
  831. //拨打 或 挂断
  832. if (CallingStatus::instance()->busy()){
  833. callActivityFinish(CallFinishType::A1CLICK);
  834. lightControl("DOORLED", "000F");
  835. }
  836. }
  837. }
  838. else if (data.cmd == "KEY;"){ //下左4,白
  839. if (isSleep) {
  840. scrrenOn();
  841. }
  842. // heartStr = "DOORLED,111F";
  843. lightControl("DOORLED", "111F");
  844. }
  845. else {
  846. if (isSleep) {
  847. scrrenOn();
  848. }
  849. // heartStr = "DOORLED,000F";
  850. lightControl("DOORLED", "000F");
  851. }
  852. // 发送给板子的指令
  853. // const char* sendMsg = heartStr.c_str();
  854. // sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  855. }
  856. }
  857. }
  858. /**
  859. * 定时器触发函数
  860. * 不建议在此函数中写耗时操作,否则将影响UI刷新
  861. * 参数: id
  862. * 当前所触发定时器的id,与注册时的id相同
  863. * 返回值: true
  864. * 继续运行当前定时器
  865. * false
  866. * 停止运行当前定时器
  867. */
  868. static bool onUI_Timer(int id){
  869. switch (id) {
  870. case 1:
  871. {
  872. //定时器跑在UI线程,这里将喂狗标记加1
  873. //++feed_dogs;
  874. //不用心跳
  875. // if(UartContext::Uart3IsOpen()) {
  876. // string heartStr = "HEART,1E";
  877. // const char* sendMsg = heartStr.c_str();
  878. // sendProtocolTo(UART_TTYS3, (byte*)(sendMsg), strlen(sendMsg));
  879. // }
  880. }
  881. break;
  882. case 2: //TCP心跳
  883. {
  884. const char* req = "0";
  885. TcpClient::instance()->sendMsg(req);
  886. }
  887. break;
  888. case SOS_CLICK_TIME_HANDLE: {
  889. sosTimerRegistered = true;
  890. return false;
  891. }
  892. break;
  893. case SOS_LAMP_TIME_HANDLE: {
  894. // SOS还原成无灯光的状态
  895. lightControl("ULED", "0F");
  896. // 进行门灯还原
  897. lightControl("DOORLED", "000F");
  898. lightControl("CALLLED", "000F");
  899. return false;
  900. }
  901. break;
  902. case SLEEP_STRAT_TIME_HANDLE: { // 息屏
  903. if (isSleepTimerRegistered) {
  904. isSleep = true;
  905. BRIGHTNESSHELPER->screenOff();
  906. } else { // 如果是false的话,就需要关闭定时器
  907. isSleep = false;
  908. BRIGHTNESSHELPER->screenOn();
  909. }
  910. }
  911. break;
  912. case SYS_RESTART_TIME_HANDLE:
  913. netresetInt += 1;
  914. lightControl("NETRESET", "1F");
  915. if (netresetInt == 5) {
  916. LOGD("网卡重启无效,程序内部重启");
  917. //重启
  918. sync();
  919. reboot(RB_AUTOBOOT);
  920. }
  921. break;
  922. default:
  923. break;
  924. }
  925. return true;
  926. }
  927. /**
  928. * 有新的触摸事件时触发
  929. * 参数:ev
  930. * 新的触摸事件
  931. * 返回值:true
  932. * 表示该触摸事件在此被拦截,系统不再将此触摸事件传递到控件上
  933. * false
  934. * 触摸事件将继续传递到控件上
  935. */
  936. static bool onstartActivityTouchEvent(const MotionEvent &ev) {
  937. switch (ev.mActionStatus) {
  938. case MotionEvent::E_ACTION_DOWN://触摸按下
  939. //LOGD("时刻 = %ld 坐标 x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
  940. break;
  941. case MotionEvent::E_ACTION_MOVE://触摸滑动
  942. break;
  943. case MotionEvent::E_ACTION_UP: //触摸抬起
  944. break;
  945. default:
  946. break;
  947. }
  948. return false;
  949. }