startLogic.cc 29 KB

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