startLogic.cc 30 KB

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