callLogic.cc 16 KB

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