startLogic.cc 26 KB

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