startLogic.cc 27 KB

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