startLogic.cc 30 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 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.type == TcpType::DATA){
  231. if (tcpModel.action == DataAction::REFRESH){ // 刷新数据
  232. dataRefresh();
  233. }
  234. } else if (tcpModel.type == TcpType::VOICE){
  235. if (tcpModel.action == VoiceAction::FAILED){
  236. string _cencalByDoorTid = getCencalByDoorTid();
  237. if (tcpModel.tid != _cencalByDoorTid) {
  238. callActivityFinish(CallFinishType::FAILED);
  239. }
  240. } else if (tcpModel.action == VoiceAction::SUCCESS){
  241. CallingStatus::instance()->setTcpModel(tcpModel); // 只有呼叫成功才闪红灯
  242. // std::string heartStr = "DOORLED,200F"; // 红灯闪烁
  243. // const char* sendMsg = heartStr.c_str();
  244. // sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  245. lightControl("DOORLED", "200F");
  246. } else if (tcpModel.action == VoiceAction::ACCEPT){
  247. if (CallingStatus::instance()->busy()){
  248. CallingStatus::instance()->setTcpModel(tcpModel);
  249. std::string toSipId = tcpModel.json["toSipId"].asString();
  250. callActivityFinish(CallFinishType::ACCEPT);
  251. //接听
  252. buildSIP(toSipId);
  253. }
  254. // 进行门灯还原
  255. // doorLampRestoration();
  256. lightControl("DOORLED", "000F");
  257. } else if (tcpModel.action == VoiceAction::REJECT){
  258. if (CallingStatus::instance()->busy()){
  259. callActivityFinish(CallFinishType::REJECT);
  260. // 进行门灯还原
  261. // doorLampRestoration();
  262. lightControl("DOORLED", "000F");
  263. }
  264. } else if (tcpModel.action == VoiceAction::CANCEL){
  265. //CallingStatus::instance()->setTcpModel(tcpModel);
  266. if (CallingStatus::instance()->busy()){
  267. callActivityFinish(CallFinishType::CANCEL);
  268. }
  269. } else if (tcpModel.action == VoiceAction::CALLING){
  270. //CallingStatus::instance()->setTcpModel(tcpModel);
  271. //对方忙线
  272. callActivityFinish(CallFinishType::BUSY);
  273. } else if (tcpModel.action == VoiceAction::CALL){
  274. //我方忙线判断
  275. if (CallingStatus::instance()->busy()){
  276. sendVoiceTcp(VoiceAction::CALLING, tcpModel, tcpModel.from_id);
  277. } else {
  278. CallingStatus::instance()->setTcpModel(tcpModel);
  279. //来电话了
  280. Intent* intent = new Intent();
  281. intent->putExtra(isOutgoing, "false");
  282. intent->putExtra(audioOnly, "true");
  283. EASYUICONTEXT->openActivity("callActivity", intent);
  284. }
  285. } else if (tcpModel.action == VoiceAction::HANDOFF){
  286. //判断 是否同一个对话
  287. long iId = CallingStatus::instance()->getInteractionId();
  288. if (iId > 0 && CallingStatus::instance()->busy()){
  289. long inId = tcpModel.json["id"].asInt();
  290. if (iId == inId && CallingStatus::instance()->busy()){
  291. callActivityFinish(CallFinishType::HANDOFF);
  292. }
  293. }
  294. }
  295. }
  296. else if (tcpModel.type == TcpType::SOS) {
  297. if (tcpModel.action == SosAction::CANCEL) {
  298. // SOS还原成无灯光的状态
  299. // std::string heartStr = "ULED,0F";
  300. // const char* sendMsg = heartStr.c_str();
  301. // sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  302. lightControl("ULED", "0F");
  303. // 进行门灯还原
  304. // doorLampRestoration();
  305. lightControl("DOORLED", "000F");
  306. mActivityPtr->unregisterUserTimer(SOS_LAMP_TIME_HANDLE);
  307. }
  308. }
  309. else if (tcpModel.type == TcpType::SIDE) {
  310. if (tcpModel.action == SideAction::NURSING) {
  311. StoragePreferences::putString(STORE_NURSING_INTERACTION_ID, tcpModel.json["id"].asString());
  312. // 收到nursing,需要把通话给挂断了
  313. voip::CallInfo info = GetTelephone()->GetCallInfo();
  314. LOGD("info state = %d",info.state);
  315. if (info.state == voip::STATE_CALL_CALLING
  316. || info.state == voip::STATE_CALL_CALLING
  317. || info.state == voip::STATE_CALL_CONFIRMED){
  318. TcpModel storeModel = CallingStatus::instance()->getTcpModel();
  319. //只回传iId
  320. storeModel.data = storeModel.json["id"].asString();
  321. sendVoiceTcp(VoiceAction::HANDOFF, storeModel, storeModel.from_id);
  322. GetTelephone()->Hangup(info.id, voip::SIP_STATUS_CODE_DECLINE);
  323. GetTelephone()->Hangup();
  324. EASYUICONTEXT->goBack();
  325. }
  326. // std::string color = StoragePreferences::getString(STORE_NURSING_COLOR_RGB, "010");
  327. // std::string heartStr;
  328. // if (color != "" && color.size() == 3) {
  329. // heartStr = "DOORLED," + color + "F";
  330. // } else {
  331. // heartStr = "DOORLED,010F";
  332. // }
  333. // const char* sendMsg = heartStr.c_str();
  334. // sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  335. std::string color = StoragePreferences::getString(STORE_NURSING_COLOR_RGB, "010");
  336. if (color != "" && color.size() == 3) {
  337. color = color + "F";
  338. } else {
  339. color = "010F";
  340. }
  341. lightControl("DOORLED", color);
  342. }
  343. else if (tcpModel.action == SideAction::NURSING_END) {
  344. // 收到nursing_end
  345. // std::string heartStr = "DOORLED,000F";
  346. // const char* sendMsg = heartStr.c_str();
  347. // sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  348. lightControl("DOORLED", "000F");
  349. }
  350. else if (tcpModel.action == SideAction::CALL) {
  351. // 收到CALL,需要亮红灯
  352. // std::string heartStr = "DOORLED,200F"; // 红灯闪烁
  353. // const char* sendMsg = heartStr.c_str();
  354. // sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  355. lightControl("DOORLED", "200F");
  356. int deviceId = tcpModel.from_id;
  357. string fromFrameFullName = tcpModel.json["fromFrameFullName"].asString();
  358. setPainterInfo(deviceId, 0xFF2F9DF1, fromFrameFullName + " " + LANGUAGEMANAGER->getValue("CallInfo"), "VOICE", "ADD", 60);
  359. }
  360. else if (tcpModel.action == SideAction::ACCEPT) {
  361. // 先处理painterInfoList里的数据
  362. int deviceId = tcpModel.to_id;
  363. setPainterInfo(deviceId, 0xFFFFFFFF, "", "VOICE", "DELETE", 0);
  364. // 获取painterInfoList
  365. Json::Value _painterInfoList = getPainterInfoList();
  366. if (_painterInfoList.size() > 0) { // 如果painterInfoList.size > 0
  367. // 如果列表里还有VOICE,就不修改门灯,直接结束
  368. for (int i = 0; i < _painterInfoList.size(); i++) {
  369. if (_painterInfoList[i]["type"] == "VOICE") {
  370. return;
  371. }
  372. }
  373. }
  374. // 进行门灯还原
  375. // doorLampRestoration();
  376. lightControl("DOORLED", "000F");
  377. }
  378. else if (tcpModel.action == SideAction::CANCEL) {
  379. int deviceId = tcpModel.from_id;
  380. if (tcpModel.json.size() != 0) {
  381. deviceId = tcpModel.json["fromDeviceId"].asInt();
  382. }
  383. setPainterInfo(deviceId, 0xFFFFFFFF, "", "VOICE", "DELETE", 0);
  384. // 获取painterInfoList
  385. Json::Value _painterInfoList = getPainterInfoList();
  386. if (_painterInfoList.size() > 0) { // 如果painterInfoList.size > 0
  387. // 如果列表里还有VOICE,就不修改门灯,直接结束
  388. for (int i = 0; i < _painterInfoList.size(); i++) {
  389. if (_painterInfoList[i]["type"] == "VOICE") {
  390. return;
  391. }
  392. }
  393. }
  394. // 进行门灯还原
  395. // doorLampRestoration();
  396. lightControl("DOORLED", "000F");
  397. }
  398. else if (tcpModel.action == SideAction::SOS_CALL) {
  399. // 收到SOS_CALL,需要亮红灯
  400. // SOS亮红灯
  401. // string heartStr = "ULED,1F";
  402. // 门灯亮红灯
  403. // std::string heartStr2 = "DOORLED,200F";
  404. // const char* sendMsg2 = heartStr2.c_str();
  405. // sendProtocolTo(UART_TTYS2, (byte*)(sendMsg2), strlen(sendMsg2));
  406. lightControl("DOORLED", "200F");
  407. sosTimerRegistered = false;
  408. mActivityPtr->registerUserTimer(SOS_CLICK_TIME_HANDLE, 10000); // 10秒后才能触发
  409. mActivityPtr->registerUserTimer(SOS_LAMP_TIME_HANDLE, 120000); // 2分钟后才能触发
  410. int deviceId = tcpModel.from_id;
  411. string fromFrameFullName = tcpModel.json["fromFrameFullName"].asString();
  412. setPainterInfo(deviceId, 0xFFFF0000, fromFrameFullName + " " + LANGUAGEMANAGER->getValue("SOSCallInfo"), "SOS", "ADD", 120);
  413. }
  414. else if (tcpModel.action == SideAction::SOS_CANCEL) {
  415. int deviceId = tcpModel.to_id;
  416. setPainterInfo(deviceId, 0xFFFFFFFF, "", "SOS", "DELETE", 0);
  417. // 获取painterInfoList
  418. Json::Value _painterInfoList = getPainterInfoList();
  419. if (_painterInfoList.size() > 0) { // 如果painterInfoList.size > 0
  420. // 如果列表里还有VOICE,就不修改门灯,直接结束
  421. for (int i = 0; i < _painterInfoList.size(); i++) {
  422. if (_painterInfoList[i]["type"] == "SOS") {
  423. return;
  424. }
  425. }
  426. }
  427. // 进行门灯还原
  428. // doorLampRestoration();
  429. lightControl("DOORLED", "000F");
  430. }
  431. }
  432. else if (tcpModel.type == TcpType::CALLBACK) {
  433. if (tcpModel.action == CallbackAction::ACK) {
  434. LOGD("CALLBACK ACK !!!!");
  435. //回调
  436. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  437. if (callback.tid != "0"){
  438. callback.onSuccess(tcpModel.json);
  439. }
  440. }
  441. else if (tcpModel.action == CallbackAction::SUCCESS) {
  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::FAILED) {
  449. //回调
  450. TcpCallback callback = TcpCacheManager::instance()->getFunc(tcpModel.tid);
  451. if (callback.tid != "0"){
  452. callback.onFalied(tcpModel.json);
  453. }
  454. if (tcpModel.tid == callTid){
  455. callActivityFinish(CallFinishType::FAILED);
  456. }
  457. }
  458. }
  459. }
  460. bool getSleepTimerRegistered() {
  461. return isSleepTimerRegistered;
  462. }
  463. // 触发定时任务
  464. void setSleepTimerRegistered(bool result) {
  465. LOGD("isSleepTimerRegistered = %d", isSleepTimerRegistered);
  466. LOGD("result = %d", result);
  467. if (mActivityPtr == NULL) {
  468. return;
  469. }
  470. if (result != isSleepTimerRegistered) { // 与定时任务不同时,才进行处理
  471. if (isSleepTimerRegistered) { // 与定时任务开关进行判断,如果定时任务开启,那就要关闭
  472. isSleepTimerRegistered = result;
  473. mActivityPtr->unregisterUserTimer(SLEEP_STRAT_TIME_HANDLE); // 关闭定时器
  474. if (isSleep) {
  475. isSleep = false;
  476. BRIGHTNESSHELPER->screenOn();
  477. }
  478. LOGD("关闭息屏");
  479. } else { // 如果定时任务关着,那就要打开
  480. isSleepTimerRegistered = result;
  481. int sleepTime = StoragePreferences::getInt(STORE_SLEEP_TIME, 3);
  482. if (sleepTime > 0) {
  483. mActivityPtr->registerUserTimer(SLEEP_STRAT_TIME_HANDLE, sleepTime * 60 * 1000);
  484. LOGD("开启息屏");
  485. }
  486. }
  487. }
  488. }
  489. void scrrenOn() {
  490. isSleep = false;
  491. BRIGHTNESSHELPER->screenOn();
  492. }
  493. void setCallTid(std::string tid) {
  494. callTid = tid;
  495. }
  496. //================================= IO 口操作
  497. //紧急按钮灯,明/灭
  498. void setSOS_A5(bool light){
  499. int result = -1;
  500. int slight = light?0:1;
  501. result = GpioHelper::output("A5", slight);
  502. if (result==0){
  503. LOGD("set A5 SOS light %d. success",light);
  504. } else {
  505. LOGD("set A5 SOS light %d. failed",light);
  506. }
  507. }
  508. //A6,A7,A8 RGB灯控制
  509. void setRGB_A678(bool R, bool G, bool B){
  510. int rR = -1, rG = -1, rB = -1;
  511. int sR = R?0:1, sG = G?0:1, sB = B?0:1;
  512. rR = GpioHelper::output("A6", sR);
  513. rG = GpioHelper::output("A7", sG);
  514. rB = GpioHelper::output("A8", sB);
  515. const char *strLog = "set %s light %d. %d";
  516. LOGD(strLog,"A6 R",R, rR);
  517. LOGD(strLog,"A7 G",G, rG);
  518. LOGD(strLog,"A8 B",B, rB);
  519. }
  520. //手柄按钮
  521. class A1GpioListener : public IGpioListener{
  522. public:
  523. bool onGpioEdge(const char *pPin) override {
  524. int state = GpioHelper::input("A1");
  525. LOGD("IGpioListener GPIO IS A1=%d", state);
  526. if (state==0){
  527. voip::CallInfo info = GetTelephone()->GetCallInfo();
  528. //拨打 或 挂断
  529. if (CallingStatus::instance()->busy()){
  530. callActivityFinish(CallFinishType::A1CLICK);
  531. } else {
  532. Intent* intent = new Intent();
  533. intent->putExtra(isOutgoing, "true");
  534. intent->putExtra(audioOnly, "true");
  535. EASYUICONTEXT->openActivity("callActivity", intent);
  536. }
  537. }
  538. return true;
  539. }
  540. void onGpioError(const char *pPin, int error) override {
  541. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  542. }
  543. };
  544. //7寸面板,呼出
  545. class A2GpioListener : public IGpioListener{
  546. public:
  547. bool onGpioEdge(const char *pPin) override {
  548. int state = GpioHelper::input("A2");
  549. LOGD("IGpioListener GPIO IS A2=%d", state);
  550. if (state==0){
  551. //拨打 或 接听
  552. }
  553. return true;
  554. }
  555. void onGpioError(const char *pPin, int error) override {
  556. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  557. }
  558. };
  559. //7寸面板,挂断
  560. class A3GpioListener : public IGpioListener{
  561. public:
  562. bool onGpioEdge(const char *pPin) override {
  563. int state = GpioHelper::input("A3");
  564. LOGD("IGpioListener GPIO IS A3=%d", state);
  565. if (state==0){
  566. //挂断
  567. }
  568. return true;
  569. }
  570. void onGpioError(const char *pPin, int error) override {
  571. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  572. }
  573. };
  574. //紧急按钮
  575. class A4GpioListener : public IGpioListener{
  576. public:
  577. bool onGpioEdge(const char *pPin) override {
  578. int state = GpioHelper::input("A4");
  579. LOGD("IGpioListener GPIO IS A4=%d", state);
  580. if (state==0){
  581. //发出
  582. setSOS_A5(true);
  583. }
  584. return true;
  585. }
  586. void onGpioError(const char *pPin, int error) override {
  587. LOGD("IGpioListener ERROR GPIO IS %s, %d", pPin, error);
  588. }
  589. };
  590. /**
  591. * 注册定时器
  592. * 填充数组用于注册定时器
  593. * 注意:id不能重复
  594. */
  595. static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
  596. {1, 5000},
  597. {2, 30*1000}
  598. };
  599. /**
  600. * 当界面构造时触发
  601. */
  602. static void onUI_init(){
  603. //IO监测
  604. IGpioListener *iGpioListenerA1 = new A1GpioListener();
  605. GpioHelper::registerGpioListener("A1", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  606. IGpioListener *iGpioListenerA2 = new A2GpioListener();
  607. GpioHelper::registerGpioListener("A2", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  608. IGpioListener *iGpioListenerA3 = new A3GpioListener();
  609. GpioHelper::registerGpioListener("A3", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  610. IGpioListener *iGpioListenerA4 = new A4GpioListener();
  611. GpioHelper::registerGpioListener("A4", iGpioListenerA1, E_GPIO_EDGE_TYPE_FALLING);
  612. //TCP启动
  613. TcpClient::instance()->startTcp();
  614. //监听SIP信令
  615. GetTelephone()->AddCallStateListener(OnCallStateChanged);
  616. //开启网页
  617. httpServer.RunAsync(HTTP_SERVER_PORT);
  618. // 启动的时候更改为tcp模式
  619. StoragePreferences::putString(STORE_SIGNAL_TYPE, "TCP");
  620. //请求版本号
  621. // if(UartContext::Uart3IsOpen()) {
  622. // string heartStr = "ASK,VNF-0";
  623. // const char* sendMsg = heartStr.c_str();
  624. // sendProtocolTo(UART_TTYS3, (byte*)(sendMsg), strlen(sendMsg));
  625. // }
  626. #if 0
  627. std::thread backend([](){
  628. while (true) {
  629. if (feed_dogs > 0) {
  630. --feed_dogs;
  631. LOGD("feed");
  632. int ret = GpioHelper::output("GPIO_2", 1); //拉高
  633. if (ret != 0) {
  634. LOGE("GPIO操作失败");
  635. }
  636. usleep(1000 * 20);
  637. ret = GpioHelper::output("GPIO_2", 0); //拉低
  638. if (ret != 0) {
  639. LOGE("GPIO操作失败");
  640. }
  641. }
  642. usleep(1000 * 1000);
  643. }
  644. });
  645. backend.detach();
  646. #endif
  647. }
  648. /**
  649. * 当切换到该界面时触发
  650. */
  651. static void onUI_intent(const Intent *intentPtr) {
  652. if (intentPtr != NULL) {
  653. //TODO
  654. }
  655. }
  656. /*
  657. * 当界面显示时触发
  658. */
  659. static void onUI_show() {
  660. StoragePreferences::putString(STORE_SIGNAL_TYPE, "TCP");
  661. //进入主界面
  662. EASYUICONTEXT->openActivity("mainActivity");
  663. }
  664. /*
  665. * 当界面隐藏时触发
  666. */
  667. static void onUI_hide() {
  668. }
  669. /*
  670. * 当界面完全退出时触发
  671. */
  672. static void onUI_quit() {
  673. }
  674. /**
  675. * 串口数据回调接口
  676. */
  677. static void onProtocolDataUpdate(const SProtocolData &data) {
  678. LOGD("cmd = %s", data.cmd.c_str());
  679. if (data.state!=""){
  680. LOGD("state = %s",data.state.c_str());
  681. }
  682. // if (data.msg != ""){
  683. // LOGD("msg = %s", data.msg.c_str());
  684. // }
  685. if(UartContext::Uart3IsOpen()) { // 如果是true,表示串口打开了
  686. string heartStr;
  687. //按下
  688. if (data.state=="0"){ // 0表示正被按下,1表示短按松开,2表示长按松开。
  689. if (data.cmd == "KEY5") { // key5是面板拨号按钮
  690. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  691. if (isSleep) {
  692. scrrenOn();
  693. }
  694. else {
  695. voip::CallInfo info = GetTelephone()->GetCallInfo();
  696. //拨打 或 挂断
  697. if (CallingStatus::instance()->busy()){
  698. callActivityFinish(CallFinishType::A1CLICK);
  699. } else {
  700. Intent* intent = new Intent();
  701. intent->putExtra(isOutgoing, "true");
  702. intent->putExtra(audioOnly, "true");
  703. EASYUICONTEXT->openActivity("callActivity", intent);
  704. }
  705. }
  706. }
  707. else {
  708. scrrenOn();
  709. voip::CallInfo info = GetTelephone()->GetCallInfo();
  710. //拨打 或 挂断
  711. if (CallingStatus::instance()->busy()){
  712. callActivityFinish(CallFinishType::A1CLICK);
  713. } else {
  714. Intent* intent = new Intent();
  715. intent->putExtra(isOutgoing, "true");
  716. intent->putExtra(audioOnly, "true");
  717. EASYUICONTEXT->openActivity("callActivity", intent);
  718. }
  719. }
  720. }
  721. else if (data.cmd == "KEY6"){ // key6是手柄触发,手柄只有呼叫和挂断功能,没有取消功能,防止用户不停地按
  722. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  723. if (isSleep) {
  724. scrrenOn();
  725. }
  726. else {
  727. voip::CallInfo info = GetTelephone()->GetCallInfo();
  728. //拨打 或 挂断
  729. if (CallingStatus::instance()->busy()){
  730. callActivityFinish(CallFinishType::KEY6_CLICK);
  731. } else {
  732. Intent* intent = new Intent();
  733. intent->putExtra(isOutgoing, "true");
  734. intent->putExtra(audioOnly, "true");
  735. EASYUICONTEXT->openActivity("callActivity", intent);
  736. }
  737. }
  738. }
  739. else {
  740. scrrenOn();
  741. voip::CallInfo info = GetTelephone()->GetCallInfo();
  742. //拨打 或 挂断
  743. if (CallingStatus::instance()->busy()){
  744. callActivityFinish(CallFinishType::KEY6_CLICK);
  745. } else {
  746. Intent* intent = new Intent();
  747. intent->putExtra(isOutgoing, "true");
  748. intent->putExtra(audioOnly, "true");
  749. EASYUICONTEXT->openActivity("callActivity", intent);
  750. }
  751. }
  752. }
  753. else if (data.cmd == "KEY7"){ // key7是紧急按钮
  754. if (isSleep) {
  755. scrrenOn();
  756. }
  757. if (sosTimerRegistered) {
  758. LOGD("TCP -> SOS_CALL");
  759. TcpModel tcpModel;
  760. tcpModel.type = TcpType::SOS;
  761. tcpModel.action = SosAction::CALL;
  762. tcpModel.from_id = StoragePreferences::getInt(STORE_DEVICE_ID, 0);
  763. tcpModel.tid = base::format("t%d",TimeHelper::getCurrentTime());
  764. std::string req = getTcpModelString(tcpModel);
  765. LOGD("TCP SOS_CALL : %s",req.c_str());
  766. TcpClient::instance()->sendMsg(req.c_str());
  767. // SOS亮红灯
  768. // heartStr = "ULED,1F";
  769. lightControl("ULED", "1F");
  770. // 门灯亮红灯
  771. // std::string heartStr2 = "DOORLED,200F";
  772. // const char* sendMsg2 = heartStr2.c_str();
  773. // sendProtocolTo(UART_TTYS2, (byte*)(sendMsg2), strlen(sendMsg2));
  774. lightControl("DOORLED", "200F");
  775. mActivityPtr->registerUserTimer(SOS_CLICK_TIME_HANDLE, 10000); // 10秒后才能触发
  776. mActivityPtr->registerUserTimer(SOS_LAMP_TIME_HANDLE, 120000); // 2分钟后才能触发
  777. }
  778. }
  779. else if (data.cmd == "KEY<") { //下左3,红
  780. if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP") {
  781. setCheckbox("KEY<");
  782. return;
  783. }
  784. if (isSleep) {
  785. scrrenOn();
  786. }
  787. // heartStr = "DOORLED,000F";
  788. }
  789. else if (data.cmd == "KEY>") { // key>是面板挂断按钮
  790. if (!StoragePreferences::getInt(STORE_SCREEN_LIGHT, 0)) {
  791. if (isSleep) {
  792. scrrenOn();
  793. }
  794. else {
  795. voip::CallInfo info = GetTelephone()->GetCallInfo();
  796. //拨打 或 挂断
  797. if (CallingStatus::instance()->busy()){
  798. callActivityFinish(CallFinishType::A1CLICK);
  799. // heartStr = "DOORLED,000F";
  800. // const char* sendMsg = heartStr.c_str();
  801. // sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  802. lightControl("DOORLED", "000F");
  803. }
  804. }
  805. }
  806. else {
  807. scrrenOn();
  808. voip::CallInfo info = GetTelephone()->GetCallInfo();
  809. //拨打 或 挂断
  810. if (CallingStatus::instance()->busy()){
  811. callActivityFinish(CallFinishType::A1CLICK);
  812. // heartStr = "DOORLED,000F";
  813. // const char* sendMsg = heartStr.c_str();
  814. // sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  815. lightControl("DOORLED", "000F");
  816. }
  817. }
  818. }
  819. else if (data.cmd == "KEY;"){ //下左4,白
  820. if (isSleep) {
  821. scrrenOn();
  822. }
  823. // heartStr = "DOORLED,111F";
  824. lightControl("DOORLED", "111F");
  825. }
  826. else {
  827. if (isSleep) {
  828. scrrenOn();
  829. }
  830. // heartStr = "DOORLED,000F";
  831. lightControl("DOORLED", "000F");
  832. }
  833. // 发送给板子的指令
  834. // const char* sendMsg = heartStr.c_str();
  835. // sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  836. }
  837. }
  838. }
  839. /**
  840. * 定时器触发函数
  841. * 不建议在此函数中写耗时操作,否则将影响UI刷新
  842. * 参数: id
  843. * 当前所触发定时器的id,与注册时的id相同
  844. * 返回值: true
  845. * 继续运行当前定时器
  846. * false
  847. * 停止运行当前定时器
  848. */
  849. static bool onUI_Timer(int id){
  850. switch (id) {
  851. case 1:
  852. {
  853. //定时器跑在UI线程,这里将喂狗标记加1
  854. //++feed_dogs;
  855. //不用心跳
  856. // if(UartContext::Uart3IsOpen()) {
  857. // string heartStr = "HEART,1E";
  858. // const char* sendMsg = heartStr.c_str();
  859. // sendProtocolTo(UART_TTYS3, (byte*)(sendMsg), strlen(sendMsg));
  860. // }
  861. }
  862. break;
  863. case 2: //TCP心跳
  864. {
  865. const char* req = "0";
  866. TcpClient::instance()->sendMsg(req);
  867. }
  868. break;
  869. // case HELP_TIMER_HANDLE:
  870. // {
  871. // SetPainterInfo(0xFFFFFFFF,"");
  872. // setHelpButton("/button/button_help2.png", true);
  873. // setReinforce(false); // 设置增援状态为false
  874. //// //取消计时
  875. //// if (isHelpTimerRegistered) {
  876. //// mActivityPtr->unregisterUserTimer(HELP_TIMER_HANDLE);
  877. //// isHelpTimerRegistered = false;
  878. //// }
  879. // return false;
  880. // }
  881. // break;
  882. case SOS_CLICK_TIME_HANDLE: {
  883. sosTimerRegistered = true;
  884. return false;
  885. }
  886. break;
  887. case SOS_LAMP_TIME_HANDLE: {
  888. // SOS还原成无灯光的状态
  889. // std::string heartStr = "ULED,0F";
  890. // const char* sendMsg = heartStr.c_str();
  891. // sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  892. lightControl("ULED", "0F");
  893. // 进行门灯还原
  894. // doorLampRestoration();
  895. lightControl("DOORLED", "000F");
  896. return false;
  897. }
  898. break;
  899. case SLEEP_STRAT_TIME_HANDLE: { // 息屏
  900. if (isSleepTimerRegistered) {
  901. isSleep = true;
  902. BRIGHTNESSHELPER->screenOff();
  903. } else { // 如果是false的话,就需要关闭定时器
  904. isSleep = false;
  905. BRIGHTNESSHELPER->screenOn();
  906. }
  907. }
  908. break;
  909. default:
  910. break;
  911. }
  912. return true;
  913. }
  914. /**
  915. * 有新的触摸事件时触发
  916. * 参数:ev
  917. * 新的触摸事件
  918. * 返回值:true
  919. * 表示该触摸事件在此被拦截,系统不再将此触摸事件传递到控件上
  920. * false
  921. * 触摸事件将继续传递到控件上
  922. */
  923. static bool onstartActivityTouchEvent(const MotionEvent &ev) {
  924. switch (ev.mActionStatus) {
  925. case MotionEvent::E_ACTION_DOWN://触摸按下
  926. //LOGD("时刻 = %ld 坐标 x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
  927. break;
  928. case MotionEvent::E_ACTION_MOVE://触摸滑动
  929. break;
  930. case MotionEvent::E_ACTION_UP: //触摸抬起
  931. break;
  932. default:
  933. break;
  934. }
  935. return false;
  936. }