startLogic.cc 25 KB

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