startLogic.cc 26 KB

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