startLogic.cc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  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. sync();
  224. reboot(RB_AUTOBOOT);
  225. }
  226. else if (tcpModel.action == DeviceAction::REBOOT) {
  227. const char* req = "-1";
  228. TcpClient::instance()->sendMsg(req);
  229. //重启
  230. sync();
  231. reboot(RB_AUTOBOOT);
  232. }
  233. else if (tcpModel.action == DeviceAction::DEVICE_REFRESH) { // 设备刷新
  234. dataRefresh();
  235. }
  236. else if (tcpModel.action == DeviceAction::SYSTEM_SETTING) { // 修改科室设置
  237. getPartSetting();
  238. }
  239. else if (tcpModel.action == DeviceAction::SERVER_CHANGE) {
  240. StoragePreferences::putString(STORE_GATEWAY, tcpModel.json["server_ip"].asCString());
  241. StoragePreferences::putString(STORE_HTTP_PORT, tcpModel.json["server_port"].asCString());
  242. const char* req = "-1";
  243. TcpClient::instance()->sendMsg(req);
  244. //重启
  245. sync();
  246. reboot(RB_AUTOBOOT);
  247. }
  248. } else if (tcpModel.type == TcpType::DATA){
  249. if (tcpModel.action == DataAction::REFRESH){ // 刷新数据
  250. dataRefresh();
  251. }
  252. } else if (tcpModel.type == TcpType::VOICE){
  253. if (tcpModel.action == VoiceAction::FAILED){
  254. callActivityFinish(CallFinishType::FAILED);
  255. } else if (tcpModel.action == VoiceAction::SUCCESS){
  256. CallingStatus::instance()->setTcpModel(tcpModel); // 只有呼叫成功才闪红灯
  257. lightControl("DOORLED", "200F");
  258. } else if (tcpModel.action == VoiceAction::ACCEPT){
  259. if (CallingStatus::instance()->busy()){
  260. CallingStatus::instance()->setTcpModel(tcpModel);
  261. std::string toSipId = tcpModel.json["toSipId"].asString();
  262. callActivityFinish(CallFinishType::ACCEPT);
  263. //接听
  264. buildSIP(toSipId);
  265. }
  266. // 进行门灯还原
  267. lightControl("DOORLED", "000F");
  268. } else if (tcpModel.action == VoiceAction::REJECT){
  269. if (CallingStatus::instance()->busy()){
  270. callActivityFinish(CallFinishType::REJECT);
  271. // 进行门灯还原
  272. lightControl("DOORLED", "000F");
  273. }
  274. } else if (tcpModel.action == VoiceAction::CANCEL){
  275. if (CallingStatus::instance()->busy()){
  276. callActivityFinish(CallFinishType::CANCEL);
  277. }
  278. } else if (tcpModel.action == VoiceAction::CALLING){
  279. //对方忙线
  280. callActivityFinish(CallFinishType::BUSY);
  281. } else if (tcpModel.action == VoiceAction::CALL){
  282. //我方忙线判断
  283. if (CallingStatus::instance()->busy()){
  284. sendVoiceTcp(VoiceAction::CALLING, tcpModel, tcpModel.from_id);
  285. } else {
  286. CallingStatus::instance()->setTcpModel(tcpModel);
  287. //来电话了
  288. Intent* intent = new Intent();
  289. intent->putExtra(isOutgoing, "false");
  290. intent->putExtra(audioOnly, "true");
  291. EASYUICONTEXT->openActivity("callActivity", intent);
  292. }
  293. } else if (tcpModel.action == VoiceAction::HANDOFF){
  294. //判断 是否同一个对话
  295. long iId = CallingStatus::instance()->getInteractionId();
  296. if (iId > 0 && CallingStatus::instance()->busy()){
  297. long inId = tcpModel.json["id"].asInt();
  298. if (iId == inId && CallingStatus::instance()->busy()){
  299. callActivityFinish(CallFinishType::HANDOFF);
  300. }
  301. }
  302. }
  303. }
  304. else if (tcpModel.type == TcpType::SOS) {
  305. if (tcpModel.action == SosAction::CANCEL) {
  306. // SOS还原成无灯光的状态
  307. lightControl("ULED", "0F");
  308. // 进行门灯还原
  309. lightControl("DOORLED", "000F");
  310. mActivityPtr->unregisterUserTimer(SOS_LAMP_TIME_HANDLE);
  311. }
  312. }
  313. else if (tcpModel.type == TcpType::SIDE) {
  314. if (tcpModel.action == SideAction::NURSING) {
  315. StoragePreferences::putString(STORE_NURSING_INTERACTION_ID, tcpModel.json["id"].asString());
  316. // 收到nursing,需要把通话给挂断了
  317. voip::CallInfo info = GetTelephone()->GetCallInfo();
  318. LOGD("info state = %d",info.state);
  319. if (info.state == voip::STATE_CALL_CALLING
  320. || info.state == voip::STATE_CALL_CALLING
  321. || info.state == voip::STATE_CALL_CONFIRMED){
  322. TcpModel storeModel = CallingStatus::instance()->getTcpModel();
  323. //只回传iId
  324. storeModel.data = storeModel.json["id"].asString();
  325. sendVoiceTcp(VoiceAction::HANDOFF, storeModel, storeModel.from_id);
  326. GetTelephone()->Hangup(info.id, voip::SIP_STATUS_CODE_DECLINE);
  327. GetTelephone()->Hangup();
  328. EASYUICONTEXT->goBack();
  329. }
  330. std::string color = StoragePreferences::getString(STORE_NURSING_COLOR_RGB, "010");
  331. if (color != "" && color.size() == 3) {
  332. color = color + "F";
  333. } else {
  334. color = "010F";
  335. }
  336. lightControl("DOORLED", color);
  337. }
  338. else if (tcpModel.action == SideAction::NURSING_END) {
  339. // 收到nursing_end
  340. lightControl("DOORLED", "000F");
  341. }
  342. else if (tcpModel.action == SideAction::CALL) {
  343. // 收到CALL,需要亮红灯
  344. lightControl("DOORLED", "200F");
  345. }
  346. else if (tcpModel.action == SideAction::ACCEPT) {
  347. // 进行门灯还原
  348. lightControl("DOORLED", "000F");
  349. }
  350. else if (tcpModel.action == SideAction::CANCEL) {
  351. // 进行门灯还原
  352. lightControl("DOORLED", "000F");
  353. }
  354. else if (tcpModel.action == SideAction::SOS_CALL) {
  355. // 收到SOS_CALL,需要亮红灯
  356. // 门灯亮红灯
  357. lightControl("DOORLED", "200F");
  358. sosTimerRegistered = false;
  359. mActivityPtr->registerUserTimer(SOS_CLICK_TIME_HANDLE, 10000); // 10秒后才能触发
  360. mActivityPtr->registerUserTimer(SOS_LAMP_TIME_HANDLE, 120000); // 2分钟后才能触发
  361. }
  362. else if (tcpModel.action == SideAction::SOS_CANCEL) {
  363. // 进行门灯还原
  364. lightControl("DOORLED", "000F");
  365. }
  366. }
  367. else if (tcpModel.type == TcpType::CALLBACK) {
  368. if (tcpModel.action == CallbackAction::ACK) {
  369. LOGD("CALLBACK ACK !!!!");
  370. //回调
  371. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  372. if (callback.tid != "0"){
  373. callback.onSuccess(tcpModel.json);
  374. }
  375. }
  376. else if (tcpModel.action == CallbackAction::SUCCESS) {
  377. //回调
  378. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  379. if (callback.tid != "0"){
  380. callback.onSuccess(tcpModel.json);
  381. }
  382. }
  383. else if (tcpModel.action == CallbackAction::FAILED) {
  384. //回调
  385. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  386. if (callback.tid != "0"){
  387. callback.onFalied(tcpModel.json);
  388. }
  389. if (tcpModel.tid == callTid){
  390. callActivityFinish(CallFinishType::FAILED);
  391. }
  392. }
  393. }
  394. else if (tcpModel.type == TcpType::POSITION) {
  395. if (tcpModel.action == PositionAction::POSITION_START) {
  396. setPosistionItId(tcpModel.json["id"].asInt());
  397. }
  398. }
  399. else if (tcpModel.type == TcpType::ROOMCHECK) {
  400. if (tcpModel.action == RoomCheckAction::START) {
  401. setRoomCheckItId(tcpModel.json["id"].asInt());
  402. }
  403. }
  404. else if (tcpModel.type == TcpType::SCREEN_TIP) {
  405. if (tcpModel.action == ScreenTipAction::START) {
  406. setPromptTimeItId(tcpModel.json["id"].asInt());
  407. }
  408. }
  409. else if (tcpModel.type == TcpType::AUTH) {
  410. if (tcpModel.action == AuthAction::LOGIN) {
  411. // 需要判断fromId跟自己是不是一样的,一样的就代表是自己发送出去,不一样的就代表员工已经登录到其他设备去了
  412. if (tcpModel.from_id == StoragePreferences::getInt(STORE_DEVICE_ID, 0)) {
  413. LOGD("id ===> %d", tcpModel.json["id"].asInt());
  414. setAuthItId(tcpModel.json["id"].asInt());
  415. }
  416. else {
  417. logoutMedicalCare();
  418. const char* currentAppName = EASYUICONTEXT->currentAppName();
  419. string _currentAppName = currentAppName;
  420. if (_currentAppName == "medicalCareActivity") {
  421. goHome();
  422. }
  423. }
  424. }
  425. }
  426. }
  427. bool getSleepTimerRegistered() {
  428. return isSleepTimerRegistered;
  429. }
  430. // 触发定时任务
  431. void setSleepTimerRegistered(bool result) {
  432. LOGD("isSleepTimerRegistered = %d", isSleepTimerRegistered);
  433. LOGD("result = %d", result);
  434. if (mActivityPtr == NULL) {
  435. return;
  436. }
  437. if (result != isSleepTimerRegistered) { // 与定时任务不同时,才进行处理
  438. if (isSleepTimerRegistered) { // 与定时任务开关进行判断,如果定时任务开启,那就要关闭
  439. isSleepTimerRegistered = result;
  440. mActivityPtr->unregisterUserTimer(SLEEP_STRAT_TIME_HANDLE); // 关闭定时器
  441. if (isSleep) {
  442. isSleep = false;
  443. BRIGHTNESSHELPER->screenOn();
  444. }
  445. LOGD("关闭息屏");
  446. } else { // 如果定时任务关着,那就要打开
  447. isSleepTimerRegistered = result;
  448. int sleepTime = StoragePreferences::getInt(STORE_SLEEP_TIME, 3);
  449. if (sleepTime > 0) {
  450. mActivityPtr->registerUserTimer(SLEEP_STRAT_TIME_HANDLE, sleepTime * 60 * 1000);
  451. LOGD("开启息屏");
  452. }
  453. }
  454. }
  455. }
  456. void scrrenOn() {
  457. isSleep = false;
  458. BRIGHTNESSHELPER->screenOn();
  459. }
  460. void setCallTid(std::string tid) {
  461. callTid = tid;
  462. }
  463. //================================= IO 口操作
  464. //紧急按钮灯,明/灭
  465. void setSOS_A5(bool light){
  466. int result = -1;
  467. int slight = light?0:1;
  468. result = GpioHelper::output("A5", slight);
  469. if (result==0){
  470. LOGD("set A5 SOS light %d. success",light);
  471. } else {
  472. LOGD("set A5 SOS light %d. failed",light);
  473. }
  474. }
  475. //A6,A7,A8 RGB灯控制
  476. void setRGB_A678(bool R, bool G, bool B){
  477. int rR = -1, rG = -1, rB = -1;
  478. int sR = R?0:1, sG = G?0:1, sB = B?0:1;
  479. rR = GpioHelper::output("A6", sR);
  480. rG = GpioHelper::output("A7", sG);
  481. rB = GpioHelper::output("A8", sB);
  482. const char *strLog = "set %s light %d. %d";
  483. LOGD(strLog,"A6 R",R, rR);
  484. LOGD(strLog,"A7 G",G, rG);
  485. LOGD(strLog,"A8 B",B, rB);
  486. }
  487. //手柄按钮
  488. class A1GpioListener : public IGpioListener{
  489. public:
  490. bool onGpioEdge(const char *pPin) override {
  491. int state = GpioHelper::input("A1");
  492. LOGD("IGpioListener GPIO IS A1=%d", state);
  493. if (state==0){
  494. voip::CallInfo info = GetTelephone()->GetCallInfo();
  495. //拨打 或 挂断
  496. if (CallingStatus::instance()->busy()){
  497. callActivityFinish(CallFinishType::A1CLICK);
  498. } else {
  499. Intent* intent = new Intent();
  500. intent->putExtra(isOutgoing, "true");
  501. intent->putExtra(audioOnly, "true");
  502. EASYUICONTEXT->openActivity("callActivity", intent);
  503. }
  504. }
  505. return true;
  506. }
  507. void onGpioError(const char *pPin, int error) override {
  508. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  509. }
  510. };
  511. //7寸面板,呼出
  512. class A2GpioListener : public IGpioListener{
  513. public:
  514. bool onGpioEdge(const char *pPin) override {
  515. int state = GpioHelper::input("A2");
  516. LOGD("IGpioListener GPIO IS A2=%d", state);
  517. if (state==0){
  518. //拨打 或 接听
  519. }
  520. return true;
  521. }
  522. void onGpioError(const char *pPin, int error) override {
  523. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  524. }
  525. };
  526. //7寸面板,挂断
  527. class A3GpioListener : public IGpioListener{
  528. public:
  529. bool onGpioEdge(const char *pPin) override {
  530. int state = GpioHelper::input("A3");
  531. LOGD("IGpioListener GPIO IS A3=%d", state);
  532. if (state==0){
  533. //挂断
  534. }
  535. return true;
  536. }
  537. void onGpioError(const char *pPin, int error) override {
  538. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  539. }
  540. };
  541. //紧急按钮
  542. class A4GpioListener : public IGpioListener{
  543. public:
  544. bool onGpioEdge(const char *pPin) override {
  545. int state = GpioHelper::input("A4");
  546. LOGD("IGpioListener GPIO IS A4=%d", state);
  547. if (state==0){
  548. //发出
  549. setSOS_A5(true);
  550. }
  551. return true;
  552. }
  553. void onGpioError(const char *pPin, int error) override {
  554. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  555. }
  556. };
  557. /**
  558. * 注册定时器
  559. * 填充数组用于注册定时器
  560. * 注意:id不能重复
  561. */
  562. static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
  563. {1, 5000},
  564. {2, 30*1000}
  565. };
  566. /**
  567. * 当界面构造时触发
  568. */
  569. static void onUI_init(){
  570. //IO监测
  571. IGpioListener *iGpioListenerA1 = new A1GpioListener();
  572. GpioHelper::registerGpioListener("A1", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  573. IGpioListener *iGpioListenerA2 = new A2GpioListener();
  574. GpioHelper::registerGpioListener("A2", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  575. IGpioListener *iGpioListenerA3 = new A3GpioListener();
  576. GpioHelper::registerGpioListener("A3", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  577. IGpioListener *iGpioListenerA4 = new A4GpioListener();
  578. GpioHelper::registerGpioListener("A4", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  579. //TCP启动
  580. TcpClient::instance()->startTcp();
  581. //监听SIP信令
  582. GetTelephone()->AddCallStateListener(OnCallStateChanged);
  583. //开启网页
  584. httpServer.RunAsync(HTTP_SERVER_PORT);
  585. // 启动的时候更改为tcp模式
  586. StoragePreferences::putString(STORE_SIGNAL_TYPE, "TCP");
  587. if (StoragePreferences::getBool(STORE_NURSING_TYPE, false)) {
  588. LOGD("===============> 关闭护理状态");
  589. StoragePreferences::putBool(STORE_NURSING_TYPE, false);
  590. isNursing = true;
  591. }
  592. //请求版本号
  593. // if(UartContext::Uart3IsOpen()) {
  594. // string heartStr = "ASK,VNF-0";
  595. // const char* sendMsg = heartStr.c_str();
  596. // sendProtocolTo(UART_TTYS3, (byte*)(sendMsg), strlen(sendMsg));
  597. // }
  598. #if 0
  599. std::thread backend([](){
  600. while (true) {
  601. if (feed_dogs > 0) {
  602. --feed_dogs;
  603. LOGD("feed");
  604. int ret = GpioHelper::output("GPIO_2", 1); //拉高
  605. if (ret != 0) {
  606. LOGE("GPIO操作失败");
  607. }
  608. usleep(1000 * 20);
  609. ret = GpioHelper::output("GPIO_2", 0); //拉低
  610. if (ret != 0) {
  611. LOGE("GPIO操作失败");
  612. }
  613. }
  614. usleep(1000 * 1000);
  615. }
  616. });
  617. backend.detach();
  618. #endif
  619. }
  620. /**
  621. * 当切换到该界面时触发
  622. */
  623. static void onUI_intent(const Intent *intentPtr) {
  624. if (intentPtr != NULL) {
  625. //TODO
  626. }
  627. }
  628. /*
  629. * 当界面显示时触发
  630. */
  631. static void onUI_show() {
  632. StoragePreferences::putString(STORE_SIGNAL_TYPE, "TCP");
  633. //进入主界面
  634. EASYUICONTEXT->openActivity("mainActivity");
  635. }
  636. /*
  637. * 当界面隐藏时触发
  638. */
  639. static void onUI_hide() {
  640. }
  641. /*
  642. * 当界面完全退出时触发
  643. */
  644. static void onUI_quit() {
  645. }
  646. /**
  647. * 串口数据回调接口
  648. */
  649. static void onProtocolDataUpdate(const SProtocolData &data) {
  650. LOGD("cmd = %s", data.cmd.c_str());
  651. if (data.state!=""){
  652. LOGD("state = %s",data.state.c_str());
  653. }
  654. // if (data.msg != ""){
  655. // LOGD("msg = %s", data.msg.c_str());
  656. // }
  657. if(UartContext::Uart3IsOpen()) { // 如果是true,表示串口打开了
  658. string heartStr;
  659. //按下
  660. if (data.state=="0"){ // 0表示正被按下,1表示短按松开,2表示长按松开。
  661. if (data.cmd == "KEY5") { // key5是面板拨号按钮
  662. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  663. if (isSleep) {
  664. scrrenOn();
  665. }
  666. else {
  667. voip::CallInfo info = GetTelephone()->GetCallInfo();
  668. //拨打 或 挂断
  669. if (CallingStatus::instance()->busy()){
  670. callActivityFinish(CallFinishType::A1CLICK);
  671. } else {
  672. Intent* intent = new Intent();
  673. intent->putExtra(isOutgoing, "true");
  674. intent->putExtra(audioOnly, "true");
  675. EASYUICONTEXT->openActivity("callActivity", intent);
  676. }
  677. }
  678. }
  679. else {
  680. scrrenOn();
  681. voip::CallInfo info = GetTelephone()->GetCallInfo();
  682. //拨打 或 挂断
  683. if (CallingStatus::instance()->busy()){
  684. callActivityFinish(CallFinishType::A1CLICK);
  685. } else {
  686. Intent* intent = new Intent();
  687. intent->putExtra(isOutgoing, "true");
  688. intent->putExtra(audioOnly, "true");
  689. EASYUICONTEXT->openActivity("callActivity", intent);
  690. }
  691. }
  692. }
  693. else if (data.cmd == "KEY6"){ // key6是手柄触发,手柄只有呼叫和挂断功能,没有取消功能,防止用户不停地按
  694. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  695. if (isSleep) {
  696. scrrenOn();
  697. }
  698. else {
  699. voip::CallInfo info = GetTelephone()->GetCallInfo();
  700. //拨打 或 挂断
  701. if (CallingStatus::instance()->busy()){
  702. callActivityFinish(CallFinishType::KEY6_CLICK);
  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 {
  712. scrrenOn();
  713. voip::CallInfo info = GetTelephone()->GetCallInfo();
  714. //拨打 或 挂断
  715. if (CallingStatus::instance()->busy()){
  716. callActivityFinish(CallFinishType::KEY6_CLICK);
  717. } else {
  718. Intent* intent = new Intent();
  719. intent->putExtra(isOutgoing, "true");
  720. intent->putExtra(audioOnly, "true");
  721. EASYUICONTEXT->openActivity("callActivity", intent);
  722. }
  723. }
  724. }
  725. else if (data.cmd == "KEY7"){ // key7是紧急按钮
  726. if (isSleep) {
  727. scrrenOn();
  728. }
  729. if (sosTimerRegistered) {
  730. LOGD("TCP -> SOS_CALL");
  731. TcpModel tcpModel;
  732. tcpModel.type = TcpType::SOS;
  733. tcpModel.action = SosAction::CALL;
  734. tcpModel.from_id = StoragePreferences::getInt(STORE_DEVICE_ID, 0);
  735. tcpModel.tid = base::format("t%d",TimeHelper::getCurrentTime());
  736. std::string req = getTcpModelString(tcpModel);
  737. LOGD("TCP SOS_CALL : %s",req.c_str());
  738. TcpClient::instance()->sendMsg(req.c_str());
  739. // SOS亮红灯
  740. lightControl("ULED", "1F");
  741. // 门灯亮红灯
  742. lightControl("DOORLED", "200F");
  743. mActivityPtr->registerUserTimer(SOS_CLICK_TIME_HANDLE, 10000); // 10秒后才能触发
  744. mActivityPtr->registerUserTimer(SOS_LAMP_TIME_HANDLE, 120000); // 2分钟后才能触发
  745. }
  746. }
  747. else if (data.cmd == "KEY<") { //下左3,红
  748. if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP") {
  749. setCheckbox("KEY<");
  750. return;
  751. }
  752. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  753. if (isSleep) {
  754. scrrenOn();
  755. }
  756. else {
  757. voip::CallInfo info = GetTelephone()->GetCallInfo();
  758. //拨打 或 挂断
  759. if (CallingStatus::instance()->busy()){
  760. callActivityFinish(CallFinishType::A1CLICK);
  761. } else {
  762. Intent* intent = new Intent();
  763. intent->putExtra(isOutgoing, "true");
  764. intent->putExtra(audioOnly, "true");
  765. EASYUICONTEXT->openActivity("callActivity", intent);
  766. }
  767. }
  768. }
  769. else {
  770. scrrenOn();
  771. voip::CallInfo info = GetTelephone()->GetCallInfo();
  772. //拨打 或 挂断
  773. if (CallingStatus::instance()->busy()){
  774. callActivityFinish(CallFinishType::A1CLICK);
  775. } else {
  776. Intent* intent = new Intent();
  777. intent->putExtra(isOutgoing, "true");
  778. intent->putExtra(audioOnly, "true");
  779. EASYUICONTEXT->openActivity("callActivity", intent);
  780. }
  781. }
  782. }
  783. else if (data.cmd == "KEY>") { // key>是面板挂断按钮
  784. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  785. if (isSleep) {
  786. scrrenOn();
  787. }
  788. else {
  789. voip::CallInfo info = GetTelephone()->GetCallInfo();
  790. //拨打 或 挂断
  791. if (CallingStatus::instance()->busy()){
  792. callActivityFinish(CallFinishType::A1CLICK);
  793. lightControl("DOORLED", "000F");
  794. }
  795. }
  796. }
  797. else {
  798. scrrenOn();
  799. voip::CallInfo info = GetTelephone()->GetCallInfo();
  800. //拨打 或 挂断
  801. if (CallingStatus::instance()->busy()){
  802. callActivityFinish(CallFinishType::A1CLICK);
  803. lightControl("DOORLED", "000F");
  804. }
  805. }
  806. }
  807. else if (data.cmd == "KEY;"){ //下左4,白
  808. if (isSleep) {
  809. scrrenOn();
  810. }
  811. lightControl("DOORLED", "111F");
  812. }
  813. else {
  814. if (isSleep) {
  815. scrrenOn();
  816. }
  817. // lightControl("DOORLED", "000F");
  818. }
  819. }
  820. }
  821. }
  822. /**
  823. * 定时器触发函数
  824. * 不建议在此函数中写耗时操作,否则将影响UI刷新
  825. * 参数: id
  826. * 当前所触发定时器的id,与注册时的id相同
  827. * 返回值: true
  828. * 继续运行当前定时器
  829. * false
  830. * 停止运行当前定时器
  831. */
  832. static bool onUI_Timer(int id){
  833. switch (id) {
  834. case 1:
  835. {
  836. //定时器跑在UI线程,这里将喂狗标记加1
  837. //++feed_dogs;
  838. //不用心跳
  839. // if(UartContext::Uart3IsOpen()) {
  840. // string heartStr = "HEART,1E";
  841. // const char* sendMsg = heartStr.c_str();
  842. // sendProtocolTo(UART_TTYS3, (byte*)(sendMsg), strlen(sendMsg));
  843. // }
  844. }
  845. break;
  846. case 2: //TCP心跳
  847. {
  848. const char* req = "0";
  849. TcpClient::instance()->sendMsg(req);
  850. }
  851. break;
  852. case SOS_CLICK_TIME_HANDLE: {
  853. sosTimerRegistered = true;
  854. return false;
  855. }
  856. break;
  857. case SOS_LAMP_TIME_HANDLE: {
  858. // SOS还原成无灯光的状态
  859. lightControl("ULED", "0F");
  860. // 进行门灯还原
  861. lightControl("DOORLED", "000F");
  862. return false;
  863. }
  864. break;
  865. case SLEEP_STRAT_TIME_HANDLE: { // 息屏
  866. if (isSleepTimerRegistered) {
  867. isSleep = true;
  868. BRIGHTNESSHELPER->screenOff();
  869. } else { // 如果是false的话,就需要关闭定时器
  870. isSleep = false;
  871. BRIGHTNESSHELPER->screenOn();
  872. }
  873. }
  874. break;
  875. default:
  876. break;
  877. }
  878. return true;
  879. }
  880. /**
  881. * 有新的触摸事件时触发
  882. * 参数:ev
  883. * 新的触摸事件
  884. * 返回值:true
  885. * 表示该触摸事件在此被拦截,系统不再将此触摸事件传递到控件上
  886. * false
  887. * 触摸事件将继续传递到控件上
  888. */
  889. static bool onstartActivityTouchEvent(const MotionEvent &ev) {
  890. switch (ev.mActionStatus) {
  891. case MotionEvent::E_ACTION_DOWN://触摸按下
  892. //LOGD("时刻 = %ld 坐标 x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
  893. break;
  894. case MotionEvent::E_ACTION_MOVE://触摸滑动
  895. break;
  896. case MotionEvent::E_ACTION_UP: //触摸抬起
  897. break;
  898. default:
  899. break;
  900. }
  901. return false;
  902. }