startLogic.cc 25 KB

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