callLogic.cc 15 KB

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