callLogic.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. #pragma once
  2. #include "uart/ProtocolSender.h"
  3. #include <string>
  4. #include "core/utilities.h"
  5. #include "voip/telephone.h"
  6. #include "base/strings.hpp"
  7. #include "storage/StoragePreferences.h"
  8. #include "core/sip_config.h"
  9. #include "zkaudio.h"
  10. #include "service/BusinessConfig.h"
  11. #include "net/tcp_client.h"
  12. #include "manager/ConfigManager.h"
  13. #include "voip/wav_player.h"
  14. #include "manager/LanguageManager.h"
  15. #include "media/ZKMediaPlayer.h"
  16. static ZKMediaPlayer* player;
  17. std::string _isOutgoing;
  18. voip::WAVPlayer wavPlayer; // 播放音频
  19. std::string tid;
  20. static bool _isSleepTimerRegistered = false;
  21. #define CALLING_TIME 60 // 用于控制呼叫时间,定时器
  22. const int goBack = 200;
  23. void callActivityFinish(CallFinishType type){
  24. switch (type){
  25. case CANCEL:
  26. LOGD("cancel");
  27. setSleepTimerRegistered(_isSleepTimerRegistered);
  28. EASYUICONTEXT->goBack();
  29. break;
  30. case REJECT:
  31. LOGD("reject");
  32. wavPlayer.Stop();
  33. mTextViewNamePtr->setTextTr("RemoteRefuse");
  34. mActivityPtr->unregisterUserTimer(CALLING_TIME);
  35. mActivityPtr->registerUserTimer(goBack, 5000);
  36. break;
  37. case BUSY:
  38. LOGD("BUSY");
  39. wavPlayer.Stop();
  40. mTextViewNamePtr->setTextTr("RemoteBusy");
  41. mActivityPtr->unregisterUserTimer(CALLING_TIME);
  42. mActivityPtr->registerUserTimer(goBack, 5000);
  43. break;
  44. case HANDOFF:
  45. LOGD("handoff");
  46. setSleepTimerRegistered(_isSleepTimerRegistered);
  47. EASYUICONTEXT->goBack();
  48. break;
  49. case FAILED:
  50. wavPlayer.Stop();
  51. mTextViewNamePtr->setTextTr("CallFailed");
  52. mActivityPtr->registerUserTimer(goBack, 5000);
  53. break;
  54. case ACCEPT:
  55. mActivityPtr->unregisterUserTimer(CALLING_TIME);
  56. break;
  57. case A1CLICK:
  58. LOGD("a1 click");
  59. if (StoragePreferences::getString(STORE_SIGNAL_TYPE,"TCP")=="TCP"){
  60. TcpModel storeModel = CallingStatus::instance()->getTcpModel();
  61. //只回传iId
  62. storeModel.data = storeModel.json["id"].asString();
  63. voip::CallInfo info = GetTelephone()->GetCallInfo();
  64. LOGD("info state = %d",info.state);
  65. if (info.state == voip::STATE_CALL_CALLING
  66. || info.state == voip::STATE_CALL_CALLING
  67. || info.state == voip::STATE_CALL_CONFIRMED){
  68. sendVoiceTcp(VoiceAction::HANDOFF, storeModel, storeModel.from_id);
  69. GetTelephone()->Hangup(info.id, voip::SIP_STATUS_CODE_DECLINE);
  70. GetTelephone()->Hangup();
  71. setSleepTimerRegistered(_isSleepTimerRegistered);
  72. EASYUICONTEXT->goBack();
  73. } else if (_isOutgoing == "false"){ //state = 5
  74. mButtonAnswerPtr->setVisible(false);
  75. TcpModel model = CallingStatus::instance()->getTcpModel();
  76. model.data = model.json["id"].asString();
  77. sendVoiceTcp(VoiceAction::ACCEPT, model, model.from_id);
  78. mActivityPtr->unregisterUserTimer(CALLING_TIME);
  79. } else if (_isOutgoing == "true") {
  80. sendVoiceTcp(VoiceAction::CANCEL, storeModel, storeModel.from_id);
  81. setSleepTimerRegistered(_isSleepTimerRegistered);
  82. EASYUICONTEXT->goBack();
  83. }
  84. }
  85. break;
  86. case KEY6_CLICK:
  87. LOGD("KEY6 click");
  88. if (StoragePreferences::getString(STORE_SIGNAL_TYPE,"TCP")=="TCP"){
  89. TcpModel storeModel = CallingStatus::instance()->getTcpModel();
  90. //只回传iId
  91. storeModel.data = storeModel.json["id"].asString();
  92. voip::CallInfo info = GetTelephone()->GetCallInfo();
  93. LOGD("info state = %d",info.state);
  94. if (info.state == voip::STATE_CALL_CALLING
  95. || info.state == voip::STATE_CALL_CALLING
  96. || info.state == voip::STATE_CALL_CONFIRMED){
  97. sendVoiceTcp(VoiceAction::HANDOFF, storeModel, storeModel.from_id);
  98. GetTelephone()->Hangup(info.id, voip::SIP_STATUS_CODE_DECLINE);
  99. GetTelephone()->Hangup();
  100. setSleepTimerRegistered(_isSleepTimerRegistered);
  101. EASYUICONTEXT->goBack();
  102. std::string heartStr = "DOORLED,000F";
  103. const char* sendMsg = heartStr.c_str();
  104. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  105. } else if (_isOutgoing == "false"){ //state = 5
  106. mButtonAnswerPtr->setVisible(false);
  107. TcpModel model = CallingStatus::instance()->getTcpModel();
  108. model.data = model.json["id"].asString();
  109. sendVoiceTcp(VoiceAction::ACCEPT, model, model.from_id);
  110. mActivityPtr->unregisterUserTimer(CALLING_TIME);
  111. }
  112. }
  113. break;
  114. }
  115. }
  116. void buildSIP(std::string toSipId){
  117. wavPlayer.Stop();
  118. int port = StoragePreferences::getInt(SIP_REG_PORT, SIP_REG_PORT_DEFAULT);
  119. std::string domain = StoragePreferences::getString(SIP_REG_DOMAIN, SIP_REG_DOMAIN_DEFAULT);
  120. //std::string od_number = StoragePreferences::getString(SIP_REG_DOOR_ACCOUNT, SIP_REG_DOOR_ACCOUNT_DEFAULT);
  121. std::string od_number_uri = URI(toSipId, domain, port);
  122. LOGD("xxxxxxxxxxxxxx %s",od_number_uri.c_str());
  123. GetTelephone()->MakeCall(od_number_uri);
  124. }
  125. void acceptSIP(){
  126. wavPlayer.Stop();
  127. voip::CallInfo info = GetTelephone()->GetCallInfo();
  128. GetTelephone()->Answer();
  129. }
  130. //static ZKMediaPlayer sPlayer(ZKMediaPlayer::E_MEDIA_TYPE_AUDIO);
  131. namespace {
  132. const int kTimerAutoAnswer = 100;
  133. std::string StateString(voip::State state) {
  134. switch (state) {
  135. case voip::STATE_CALL_CALLING:
  136. case voip::STATE_CALL_EARLY:
  137. return LANGUAGEMANAGER->getValue("Calling");
  138. case voip::STATE_CALL_CONFIRMED:
  139. return LANGUAGEMANAGER->getValue("CallConfirmed");
  140. case voip::STATE_CALL_CONNECTING:
  141. return LANGUAGEMANAGER->getValue("CallConnecting");
  142. case voip::STATE_CALL_INCOMING:
  143. return LANGUAGEMANAGER->getValue("CallInComing");
  144. default:
  145. break;
  146. }
  147. return "";
  148. }
  149. } /* anonymous namespace */
  150. /**
  151. * 注册定时器
  152. * 填充数组用于注册定时器
  153. * 注意:id不能重复
  154. */
  155. static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
  156. //{0, 6000}, //定时器id=0, 时间间隔6秒
  157. {1, 1000},
  158. {kTimerAutoAnswer, 2000},
  159. };
  160. /**
  161. * 当界面构造时触发
  162. */
  163. static void onUI_init(){
  164. player = new ZKMediaPlayer(ZKMediaPlayer::E_MEDIA_TYPE_AUDIO);
  165. CallingStatus::instance()->setBusy(true);
  166. EASYUICONTEXT->hideStatusBar();
  167. EASYUICONTEXT->hideNaviBar();
  168. //界面操作
  169. if (StoragePreferences::getString(STORE_SIGNAL_TYPE,"TCP")=="SIP"){
  170. voip::CallInfo info = GetTelephone()->GetCallInfo();
  171. mTextViewNamePtr->setText(info.remote_uri);
  172. if (info.state == voip::STATE_CALL_CALLING) {
  173. mButtonAnswerPtr->setVisible(false);
  174. }
  175. }
  176. mSeekBarOutputVolumePtr->setMax(zk_audio_output_volume_native_max());
  177. //打开时的声音大小
  178. int serverVol = StoragePreferences::getInt(STORE_DAY_VOL,PartSetting::dayBedVol);
  179. if (checkIsDay()){
  180. serverVol = StoragePreferences::getInt(STORE_NIGHT_VOL,PartSetting::nightBedVol);
  181. }
  182. double percent = (double)serverVol/(double)100;
  183. int vol = (int)(zk_audio_output_volume_native_max()*percent);
  184. LOGD("bed vol is %d. server is %d. max is %d",vol,serverVol, zk_audio_output_volume_native_max());
  185. zk_audio_output_set_volume_native(vol);
  186. mSeekBarOutputVolumePtr->setProgress(vol);
  187. mTextViewOutputVolumePtr->setTouchPass(true);
  188. // mTextViewNamePtr->setVisible(false);
  189. // mTextViewStatePtr->setVisible(false);
  190. // mTextViewDurationPtr->setVisible(false);
  191. }
  192. /**
  193. * 当切换到该界面时触发
  194. */
  195. static void onUI_intent(const Intent *intentPtr) {
  196. player->play("/mnt/extsd/ui/hl.mp3");
  197. if(intentPtr != NULL) {
  198. BRIGHTNESSHELPER->screenOn();
  199. _isSleepTimerRegistered = getSleepTimerRegistered();
  200. LOGD("_isSleepTimerRegistered = %d", _isSleepTimerRegistered);
  201. setSleepTimerRegistered(false);
  202. // wavPlayer.Play(CONFIGMANAGER->getResFilePath("ring.wav"));
  203. // 键值解析
  204. _isOutgoing = intentPtr->getExtra(isOutgoing);
  205. std::string _audioOnly = intentPtr->getExtra(audioOnly);
  206. if (_isOutgoing == "true"){
  207. //TCP call
  208. TcpModel tcpModel;
  209. tcpModel.tid = base::format("t%d", TimeHelper::getCurrentTime());
  210. tcpModel.type = TcpType::VOICE;
  211. tcpModel.action = VoiceAction::CALL;
  212. tcpModel.from_id = StoragePreferences::getInt(STORE_DEVICE_ID,0);
  213. tcpModel.to_id = 0;
  214. std::string req = getTcpModelString(tcpModel);
  215. LOGD("TCP CALL : %s",req.c_str());
  216. TcpClient::instance()->sendMsg(req.c_str());
  217. //回调注册
  218. TcpCallback callback;
  219. callback.tid = tcpModel.tid;
  220. callback.jsonStr = req;
  221. callback.onSuccess = [](Json::Value json){
  222. LOGD("voice callback success");
  223. return 0;
  224. };
  225. callback.onFalied = [](Json::Value json){
  226. LOGD("voice callback failed");
  227. return 0;
  228. };
  229. TcpClient::instance()->sendMsgWithCb(req.c_str(), callback);
  230. // 呼叫的时候需要把tid放进去
  231. tid = tcpModel.tid;
  232. mButtonAnswerPtr->setVisible(false);
  233. mTextViewNamePtr->setTextTr("CallOutTitle");
  234. mActivityPtr->registerUserTimer(CALLING_TIME, 60000); // 如果对方没接听,则发送一个CANCEL回去
  235. } else {
  236. TcpModel storeModel = CallingStatus::instance()->getTcpModel();
  237. mTextViewNamePtr->setText(storeModel.json["fromFrameName"].asString());
  238. //自动接听处理
  239. if (StoragePreferences::getBool(STORE_AUDO_ANSWER,false)){
  240. mButtonAnswerPtr->setVisible(false);
  241. voip::CallInfo info = GetTelephone()->GetCallInfo();
  242. if (info.state == voip::STATE_CALL_INCOMING
  243. || info.state == voip::STATE_CALL_EARLY) {
  244. GetTelephone()->Answer();
  245. //发出TCP
  246. sendVoiceTcp(VoiceAction::ACCEPT, storeModel, storeModel.from_id);
  247. }
  248. }
  249. }
  250. }
  251. }
  252. /*
  253. * 当界面显示时触发
  254. */
  255. static void onUI_show() {
  256. }
  257. /*
  258. * 当界面隐藏时触发
  259. */
  260. static void onUI_hide() {
  261. }
  262. /*
  263. * 当界面完全退出时触发
  264. */
  265. static void onUI_quit() {
  266. EASYUICONTEXT->showStatusBar();
  267. EASYUICONTEXT->showNaviBar();
  268. wavPlayer.Stop();
  269. CallingStatus::instance()->setBusy(false);
  270. CallingStatus::instance()->clearTcpModel();
  271. mActivityPtr->unregisterUserTimer(goBack);
  272. }
  273. /**
  274. * 串口数据回调接口
  275. */
  276. static void onProtocolDataUpdate(const SProtocolData &data) {
  277. }
  278. /**
  279. * 定时器触发函数
  280. * 不建议在此函数中写耗时操作,否则将影响UI刷新
  281. * 参数: id
  282. * 当前所触发定时器的id,与注册时的id相同
  283. * 返回值: true
  284. * 继续运行当前定时器
  285. * false
  286. * 停止运行当前定时器
  287. */
  288. static bool onUI_Timer(int id){
  289. switch (id) {
  290. case 1: {
  291. if (StoragePreferences::getString(STORE_SIGNAL_TYPE,"TCP")=="SIP"){
  292. voip::CallInfo info = GetTelephone()->GetCallInfo();
  293. if (info.state == voip::STATE_CALL_CONFIRMED){
  294. mButtonAnswerPtr->setVisible(false);
  295. }
  296. mTextViewNamePtr->setText(info.remote_uri);
  297. mTextViewStatePtr->setText(StateString(info.state));
  298. mTextViewDurationPtr->setText(
  299. base::format("%02d:%02d",
  300. info.duration/1000/60,
  301. info.duration/1000%60));
  302. } else {
  303. voip::CallInfo info = GetTelephone()->GetCallInfo();
  304. if (info.state == voip::STATE_CALL_CONFIRMED){
  305. mButtonAnswerPtr->setVisible(false);
  306. }
  307. mTextViewStatePtr->setText(StateString(info.state));
  308. mTextViewDurationPtr->setText(
  309. base::format("%02d:%02d",
  310. info.duration/1000/60,
  311. info.duration/1000%60));
  312. }
  313. }
  314. break;
  315. case kTimerAutoAnswer: {
  316. voip::CallInfo info = GetTelephone()->GetCallInfo();
  317. if (info.state == voip::STATE_CALL_INCOMING
  318. || info.state == voip::STATE_CALL_EARLY) {
  319. bool auto_answer = StoragePreferences::getBool(SIP_AUTO_ANSWER, false);
  320. LOGD("is auto answer: %d", auto_answer);
  321. if (auto_answer) {
  322. GetTelephone()->Answer();
  323. }
  324. }
  325. }
  326. return false;
  327. break;
  328. case goBack:{
  329. setSleepTimerRegistered(_isSleepTimerRegistered);
  330. EASYUICONTEXT->goBack();
  331. break;
  332. }
  333. case CALLING_TIME: {
  334. //TCP call
  335. TcpModel tcpModel;
  336. tcpModel.type = TcpType::VOICE;
  337. tcpModel.action = VoiceAction::CANCEL;
  338. tcpModel.from_id = StoragePreferences::getInt(STORE_DEVICE_ID,0);
  339. tcpModel.to_id = 0;
  340. std::string req = getTcpModelString(tcpModel);
  341. LOGD("TCP CALL : %s",req.c_str());
  342. TcpClient::instance()->sendMsg(req.c_str());
  343. std::string heartStr = "DOORLED,000F";
  344. const char* sendMsg = heartStr.c_str();
  345. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  346. setSleepTimerRegistered(_isSleepTimerRegistered);
  347. EASYUICONTEXT->goBack();
  348. break;
  349. }
  350. default:
  351. break;
  352. }
  353. return true;
  354. }
  355. /**
  356. * 有新的触摸事件时触发
  357. * 参数:ev
  358. * 新的触摸事件
  359. * 返回值:true
  360. * 表示该触摸事件在此被拦截,系统不再将此触摸事件传递到控件上
  361. * false
  362. * 触摸事件将继续传递到控件上
  363. */
  364. static bool oncallActivityTouchEvent(const MotionEvent &ev) {
  365. switch (ev.mActionStatus) {
  366. case MotionEvent::E_ACTION_DOWN://触摸按下
  367. //LOGD("时刻 = %ld 坐标 x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
  368. break;
  369. case MotionEvent::E_ACTION_MOVE://触摸滑动
  370. break;
  371. case MotionEvent::E_ACTION_UP: //触摸抬起
  372. break;
  373. default:
  374. break;
  375. }
  376. return false;
  377. }
  378. static void onVideoViewPlayerMessageListener_VideoView1(ZKVideoView *pVideoView, int msg) {
  379. switch (msg) {
  380. case ZKVideoView::E_MSGTYPE_VIDEO_PLAY_STARTED:
  381. break;
  382. case ZKVideoView::E_MSGTYPE_VIDEO_PLAY_COMPLETED:
  383. break;
  384. case ZKVideoView::E_MSGTYPE_VIDEO_PLAY_ERROR:
  385. break;
  386. }
  387. }
  388. static void onProgressChanged_SeekBarOutputVolume(ZKSeekBar *pSeekBar, int progress) {
  389. zk_audio_output_set_volume_native(progress);
  390. mTextViewOutputVolumePtr->setText(base::format("%d", progress));
  391. }
  392. static bool onButtonClick_ButtonHangup(ZKButton *pButton) {
  393. LOGD("handup click");
  394. voip::CallInfo info = GetTelephone()->GetCallInfo();
  395. LOGD("info id = %d",info.id);
  396. if (StoragePreferences::getString(STORE_SIGNAL_TYPE,"TCP")=="TCP"){
  397. TcpModel storeModel = CallingStatus::instance()->getTcpModel();
  398. if (storeModel.json != 0) {
  399. //只回传iId
  400. storeModel.data = storeModel.json["id"].asString();
  401. if (info.id>0){
  402. sendVoiceTcp(VoiceAction::HANDOFF, storeModel, storeModel.from_id);
  403. GetTelephone()->Hangup(info.id, voip::SIP_STATUS_CODE_DECLINE);
  404. GetTelephone()->Hangup();
  405. } else if (_isOutgoing == "true") {
  406. sendVoiceTcp(VoiceAction::CANCEL, storeModel, storeModel.from_id);
  407. // 还原成无灯光的状态
  408. std::string heartStr = "DOORLED,000F";
  409. const char* sendMsg = heartStr.c_str();
  410. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  411. } else {
  412. storeModel.data = storeModel.json["id"].asString();
  413. sendVoiceTcp(VoiceAction::REJECT, storeModel, storeModel.from_id);
  414. }
  415. } else {
  416. TcpModel tcpModel;
  417. tcpModel.tid = tid;
  418. sendVoiceTcp(VoiceAction::CANCEL, tcpModel, StoragePreferences::getInt(STORE_DEVICE_ID,0));
  419. // 还原成无灯光的状态
  420. std::string heartStr = "DOORLED,000F";
  421. const char* sendMsg = heartStr.c_str();
  422. sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
  423. }
  424. }
  425. setSleepTimerRegistered(_isSleepTimerRegistered);
  426. EASYUICONTEXT->goBack();
  427. return false;
  428. }
  429. static bool onButtonClick_ButtonAnswer(ZKButton *pButton) {
  430. LOGD(" ButtonClick ButtonAnswer !!!\n");
  431. mButtonAnswerPtr->setVisible(false);
  432. // voip::CallInfo info = GetTelephone()->GetCallInfo();
  433. // GetTelephone()->Answer();
  434. TcpModel model = CallingStatus::instance()->getTcpModel();
  435. //LOGD("storeModel :::: type=%s,action=%s,from=%d,to=%d",model.type.c_str(),model.action.c_str(),model.from_id,model.to_id);
  436. //std::string storeModelStr = getTcpModelString(model);
  437. //LOGD("storeModelStr ===== %s",storeModelStr.c_str());
  438. //只回传iId
  439. model.data = model.json["id"].asString();
  440. sendVoiceTcp(VoiceAction::ACCEPT, model, model.from_id);
  441. return false;
  442. }
  443. static bool onButtonClick_ButtonVol(ZKButton *pButton) {
  444. LOGD(" ButtonClick ButtonVol !!!\n");
  445. return false;
  446. }