startLogic.cc 29 KB

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