startLogic.cc 26 KB

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