startLogic.cc 30 KB

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