startLogic.cc 29 KB

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