callLogic.cc 16 KB

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