startLogic.cc 26 KB

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