startLogic.cc 30 KB

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