#pragma once #include "uart/ProtocolSender.h" #include #include "core/utilities.h" #include "voip/telephone.h" #include "base/strings.hpp" #include "storage/StoragePreferences.h" #include "core/sip_config.h" #include "zkaudio.h" #include "service/BusinessConfig.h" #include "net/tcp_client.h" #include "manager/ConfigManager.h" #include "voip/wav_player.h" #include "manager/LanguageManager.h" std::string _isOutgoing; voip::WAVPlayer wavPlayer; // 播放音频 std::string tid; static bool _isSleepTimerRegistered = false; #define CALLING_TIME 60 // 用于控制呼叫时间,定时器 const int goBack = 200; void callActivityFinish(CallFinishType type){ switch (type){ case CANCEL: LOGD("cancel"); setSleepTimerRegistered(_isSleepTimerRegistered); EASYUICONTEXT->goBack(); break; case REJECT: LOGD("reject"); wavPlayer.Stop(); mTextViewNamePtr->setTextTr("RemoteRefuse"); mActivityPtr->unregisterUserTimer(CALLING_TIME); mActivityPtr->registerUserTimer(goBack, 5000); break; case BUSY: LOGD("BUSY"); wavPlayer.Stop(); mTextViewNamePtr->setTextTr("RemoteBusy"); mActivityPtr->unregisterUserTimer(CALLING_TIME); mActivityPtr->registerUserTimer(goBack, 5000); break; case HANDOFF: LOGD("handoff"); setSleepTimerRegistered(_isSleepTimerRegistered); EASYUICONTEXT->goBack(); break; case FAILED: wavPlayer.Stop(); mTextViewNamePtr->setTextTr("CallFailed"); mActivityPtr->registerUserTimer(goBack, 5000); break; case ACCEPT: mActivityPtr->unregisterUserTimer(CALLING_TIME); break; case A1CLICK: LOGD("a1 click"); if (StoragePreferences::getString(STORE_SIGNAL_TYPE,"TCP")=="TCP"){ TcpModel storeModel = CallingStatus::instance()->getTcpModel(); //只回传iId storeModel.data = storeModel.json["id"].asString(); voip::CallInfo info = GetTelephone()->GetCallInfo(); LOGD("info state = %d",info.state); if (info.state == voip::STATE_CALL_CALLING || info.state == voip::STATE_CALL_CALLING || info.state == voip::STATE_CALL_CONFIRMED){ sendVoiceTcp(VoiceAction::HANDOFF, storeModel, storeModel.from_id); GetTelephone()->Hangup(info.id, voip::SIP_STATUS_CODE_DECLINE); GetTelephone()->Hangup(); setSleepTimerRegistered(_isSleepTimerRegistered); EASYUICONTEXT->goBack(); } else if (_isOutgoing == "false"){ //state = 5 mButtonAnswerPtr->setVisible(false); TcpModel model = CallingStatus::instance()->getTcpModel(); model.data = model.json["id"].asString(); sendVoiceTcp(VoiceAction::ACCEPT, model, model.from_id); mActivityPtr->unregisterUserTimer(CALLING_TIME); } else if (_isOutgoing == "true") { sendVoiceTcp(VoiceAction::CANCEL, storeModel, storeModel.from_id); setSleepTimerRegistered(_isSleepTimerRegistered); EASYUICONTEXT->goBack(); } } break; case KEY6_CLICK: LOGD("KEY6 click"); if (StoragePreferences::getString(STORE_SIGNAL_TYPE,"TCP")=="TCP"){ TcpModel storeModel = CallingStatus::instance()->getTcpModel(); //只回传iId storeModel.data = storeModel.json["id"].asString(); voip::CallInfo info = GetTelephone()->GetCallInfo(); LOGD("info state = %d",info.state); if (info.state == voip::STATE_CALL_CALLING || info.state == voip::STATE_CALL_CALLING || info.state == voip::STATE_CALL_CONFIRMED){ sendVoiceTcp(VoiceAction::HANDOFF, storeModel, storeModel.from_id); GetTelephone()->Hangup(info.id, voip::SIP_STATUS_CODE_DECLINE); GetTelephone()->Hangup(); setSleepTimerRegistered(_isSleepTimerRegistered); EASYUICONTEXT->goBack(); std::string heartStr = "DOORLED,000F"; const char* sendMsg = heartStr.c_str(); sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg)); } else if (_isOutgoing == "false"){ //state = 5 mButtonAnswerPtr->setVisible(false); TcpModel model = CallingStatus::instance()->getTcpModel(); model.data = model.json["id"].asString(); sendVoiceTcp(VoiceAction::ACCEPT, model, model.from_id); mActivityPtr->unregisterUserTimer(CALLING_TIME); } } break; } } void buildSIP(std::string toSipId){ wavPlayer.Stop(); int port = StoragePreferences::getInt(SIP_REG_PORT, SIP_REG_PORT_DEFAULT); std::string domain = StoragePreferences::getString(SIP_REG_DOMAIN, SIP_REG_DOMAIN_DEFAULT); //std::string od_number = StoragePreferences::getString(SIP_REG_DOOR_ACCOUNT, SIP_REG_DOOR_ACCOUNT_DEFAULT); std::string od_number_uri = URI(toSipId, domain, port); LOGD("xxxxxxxxxxxxxx %s",od_number_uri.c_str()); GetTelephone()->MakeCall(od_number_uri); } void acceptSIP(){ wavPlayer.Stop(); voip::CallInfo info = GetTelephone()->GetCallInfo(); GetTelephone()->Answer(); } //static ZKMediaPlayer sPlayer(ZKMediaPlayer::E_MEDIA_TYPE_AUDIO); namespace { const int kTimerAutoAnswer = 100; std::string StateString(voip::State state) { switch (state) { case voip::STATE_CALL_CALLING: case voip::STATE_CALL_EARLY: return LANGUAGEMANAGER->getValue("Calling"); case voip::STATE_CALL_CONFIRMED: return LANGUAGEMANAGER->getValue("CallConfirmed"); case voip::STATE_CALL_CONNECTING: return LANGUAGEMANAGER->getValue("CallConnecting"); case voip::STATE_CALL_INCOMING: return LANGUAGEMANAGER->getValue("CallInComing"); default: break; } return ""; } } /* anonymous namespace */ /** * 注册定时器 * 填充数组用于注册定时器 * 注意:id不能重复 */ static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = { //{0, 6000}, //定时器id=0, 时间间隔6秒 {1, 1000}, {kTimerAutoAnswer, 2000}, }; /** * 当界面构造时触发 */ static void onUI_init(){ CallingStatus::instance()->setBusy(true); EASYUICONTEXT->hideStatusBar(); EASYUICONTEXT->hideNaviBar(); //界面操作 if (StoragePreferences::getString(STORE_SIGNAL_TYPE,"TCP")=="SIP"){ voip::CallInfo info = GetTelephone()->GetCallInfo(); mTextViewNamePtr->setText(info.remote_uri); if (info.state == voip::STATE_CALL_CALLING) { mButtonAnswerPtr->setVisible(false); } } mSeekBarOutputVolumePtr->setMax(zk_audio_output_volume_native_max()); //打开时的声音大小 int serverVol = StoragePreferences::getInt(STORE_DAY_VOL,PartSetting::dayBedVol); if (checkIsDay()){ serverVol = StoragePreferences::getInt(STORE_NIGHT_VOL,PartSetting::nightBedVol); } double percent = (double)serverVol/(double)100; int vol = (int)(zk_audio_output_volume_native_max()*percent); LOGD("bed vol is %d. server is %d. max is %d",vol,serverVol, zk_audio_output_volume_native_max()); zk_audio_output_set_volume_native(vol); mSeekBarOutputVolumePtr->setProgress(vol); mTextViewOutputVolumePtr->setTouchPass(true); // mTextViewNamePtr->setVisible(false); // mTextViewStatePtr->setVisible(false); // mTextViewDurationPtr->setVisible(false); } /** * 当切换到该界面时触发 */ static void onUI_intent(const Intent *intentPtr) { if(intentPtr != NULL) { BRIGHTNESSHELPER->screenOn(); _isSleepTimerRegistered = getSleepTimerRegistered(); LOGD("_isSleepTimerRegistered = %d", _isSleepTimerRegistered); setSleepTimerRegistered(false); wavPlayer.Play(CONFIGMANAGER->getResFilePath("ring.wav")); // 键值解析 _isOutgoing = intentPtr->getExtra(isOutgoing); std::string _audioOnly = intentPtr->getExtra(audioOnly); if (_isOutgoing == "true"){ //TCP call TcpModel tcpModel; tcpModel.tid = base::format("t%d", TimeHelper::getCurrentTime()); tcpModel.type = TcpType::VOICE; tcpModel.action = VoiceAction::CALL; tcpModel.from_id = StoragePreferences::getInt(STORE_DEVICE_ID,0); tcpModel.to_id = 0; std::string req = getTcpModelString(tcpModel); LOGD("TCP CALL : %s",req.c_str()); TcpClient::instance()->sendMsg(req.c_str()); //回调注册 TcpCallback callback; callback.tid = tcpModel.tid; callback.jsonStr = req; callback.onSuccess = [](Json::Value json){ LOGD("voice callback success"); return 0; }; callback.onFalied = [](Json::Value json){ LOGD("voice callback failed"); return 0; }; TcpClient::instance()->sendMsgWithCb(req.c_str(), callback); // 呼叫的时候需要把tid放进去 tid = tcpModel.tid; mButtonAnswerPtr->setVisible(false); mTextViewNamePtr->setTextTr("CallOutTitle"); mActivityPtr->registerUserTimer(CALLING_TIME, 60000); // 如果对方没接听,则发送一个CANCEL回去 } else { TcpModel storeModel = CallingStatus::instance()->getTcpModel(); mTextViewNamePtr->setText(storeModel.json["fromFrameName"].asString()); //自动接听处理 if (StoragePreferences::getBool(STORE_AUDO_ANSWER,false)){ mButtonAnswerPtr->setVisible(false); voip::CallInfo info = GetTelephone()->GetCallInfo(); if (info.state == voip::STATE_CALL_INCOMING || info.state == voip::STATE_CALL_EARLY) { GetTelephone()->Answer(); //发出TCP sendVoiceTcp(VoiceAction::ACCEPT, storeModel, storeModel.from_id); } } } } } /* * 当界面显示时触发 */ static void onUI_show() { } /* * 当界面隐藏时触发 */ static void onUI_hide() { } /* * 当界面完全退出时触发 */ static void onUI_quit() { EASYUICONTEXT->showStatusBar(); EASYUICONTEXT->showNaviBar(); wavPlayer.Stop(); CallingStatus::instance()->setBusy(false); CallingStatus::instance()->clearTcpModel(); mActivityPtr->unregisterUserTimer(goBack); } /** * 串口数据回调接口 */ static void onProtocolDataUpdate(const SProtocolData &data) { } /** * 定时器触发函数 * 不建议在此函数中写耗时操作,否则将影响UI刷新 * 参数: id * 当前所触发定时器的id,与注册时的id相同 * 返回值: true * 继续运行当前定时器 * false * 停止运行当前定时器 */ static bool onUI_Timer(int id){ switch (id) { case 1: { if (StoragePreferences::getString(STORE_SIGNAL_TYPE,"TCP")=="SIP"){ voip::CallInfo info = GetTelephone()->GetCallInfo(); if (info.state == voip::STATE_CALL_CONFIRMED){ mButtonAnswerPtr->setVisible(false); } mTextViewNamePtr->setText(info.remote_uri); mTextViewStatePtr->setText(StateString(info.state)); mTextViewDurationPtr->setText( base::format("%02d:%02d", info.duration/1000/60, info.duration/1000%60)); } else { voip::CallInfo info = GetTelephone()->GetCallInfo(); if (info.state == voip::STATE_CALL_CONFIRMED){ mButtonAnswerPtr->setVisible(false); } mTextViewStatePtr->setText(StateString(info.state)); mTextViewDurationPtr->setText( base::format("%02d:%02d", info.duration/1000/60, info.duration/1000%60)); } } break; case kTimerAutoAnswer: { voip::CallInfo info = GetTelephone()->GetCallInfo(); if (info.state == voip::STATE_CALL_INCOMING || info.state == voip::STATE_CALL_EARLY) { bool auto_answer = StoragePreferences::getBool(SIP_AUTO_ANSWER, false); LOGD("is auto answer: %d", auto_answer); if (auto_answer) { GetTelephone()->Answer(); } } } return false; break; case goBack:{ setSleepTimerRegistered(_isSleepTimerRegistered); EASYUICONTEXT->goBack(); break; } case CALLING_TIME: { //TCP call TcpModel tcpModel; tcpModel.type = TcpType::VOICE; tcpModel.action = VoiceAction::CANCEL; tcpModel.from_id = StoragePreferences::getInt(STORE_DEVICE_ID,0); tcpModel.to_id = 0; std::string req = getTcpModelString(tcpModel); LOGD("TCP CALL : %s",req.c_str()); TcpClient::instance()->sendMsg(req.c_str()); std::string heartStr = "DOORLED,000F"; const char* sendMsg = heartStr.c_str(); sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg)); setSleepTimerRegistered(_isSleepTimerRegistered); EASYUICONTEXT->goBack(); break; } default: break; } return true; } /** * 有新的触摸事件时触发 * 参数:ev * 新的触摸事件 * 返回值:true * 表示该触摸事件在此被拦截,系统不再将此触摸事件传递到控件上 * false * 触摸事件将继续传递到控件上 */ static bool oncallActivityTouchEvent(const MotionEvent &ev) { switch (ev.mActionStatus) { case MotionEvent::E_ACTION_DOWN://触摸按下 //LOGD("时刻 = %ld 坐标 x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY); break; case MotionEvent::E_ACTION_MOVE://触摸滑动 break; case MotionEvent::E_ACTION_UP: //触摸抬起 break; default: break; } return false; } static void onVideoViewPlayerMessageListener_VideoView1(ZKVideoView *pVideoView, int msg) { switch (msg) { case ZKVideoView::E_MSGTYPE_VIDEO_PLAY_STARTED: break; case ZKVideoView::E_MSGTYPE_VIDEO_PLAY_COMPLETED: break; case ZKVideoView::E_MSGTYPE_VIDEO_PLAY_ERROR: break; } } static void onProgressChanged_SeekBarOutputVolume(ZKSeekBar *pSeekBar, int progress) { zk_audio_output_set_volume_native(progress); mTextViewOutputVolumePtr->setText(base::format("%d", progress)); } static bool onButtonClick_ButtonHangup(ZKButton *pButton) { LOGD("handup click"); voip::CallInfo info = GetTelephone()->GetCallInfo(); LOGD("info id = %d",info.id); if (StoragePreferences::getString(STORE_SIGNAL_TYPE,"TCP")=="TCP"){ TcpModel storeModel = CallingStatus::instance()->getTcpModel(); if (storeModel.json != 0) { //只回传iId storeModel.data = storeModel.json["id"].asString(); if (info.id>0){ sendVoiceTcp(VoiceAction::HANDOFF, storeModel, storeModel.from_id); GetTelephone()->Hangup(info.id, voip::SIP_STATUS_CODE_DECLINE); GetTelephone()->Hangup(); } else if (_isOutgoing == "true") { sendVoiceTcp(VoiceAction::CANCEL, storeModel, storeModel.from_id); // 还原成无灯光的状态 std::string heartStr = "DOORLED,000F"; const char* sendMsg = heartStr.c_str(); sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg)); } else { storeModel.data = storeModel.json["id"].asString(); sendVoiceTcp(VoiceAction::REJECT, storeModel, storeModel.from_id); } } else { TcpModel tcpModel; tcpModel.tid = tid; sendVoiceTcp(VoiceAction::CANCEL, tcpModel, StoragePreferences::getInt(STORE_DEVICE_ID,0)); // 还原成无灯光的状态 std::string heartStr = "DOORLED,000F"; const char* sendMsg = heartStr.c_str(); sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg)); } } setSleepTimerRegistered(_isSleepTimerRegistered); EASYUICONTEXT->goBack(); return false; } static bool onButtonClick_ButtonAnswer(ZKButton *pButton) { LOGD(" ButtonClick ButtonAnswer !!!\n"); mButtonAnswerPtr->setVisible(false); // voip::CallInfo info = GetTelephone()->GetCallInfo(); // GetTelephone()->Answer(); TcpModel model = CallingStatus::instance()->getTcpModel(); //LOGD("storeModel :::: type=%s,action=%s,from=%d,to=%d",model.type.c_str(),model.action.c_str(),model.from_id,model.to_id); //std::string storeModelStr = getTcpModelString(model); //LOGD("storeModelStr ===== %s",storeModelStr.c_str()); //只回传iId model.data = model.json["id"].asString(); sendVoiceTcp(VoiceAction::ACCEPT, model, model.from_id); return false; } static bool onButtonClick_ButtonVol(ZKButton *pButton) { LOGD(" ButtonClick ButtonVol !!!\n"); return false; }