startLogic.cc 28 KB

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