startLogic.cc 27 KB

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