123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- #pragma once
- #include "uart/ProtocolSender.h"
- #include <string>
- #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 "voip/media_player.h"
- #include "manager/LanguageManager.h"
- std::string _isOutgoing;
- std::string _toIdStr;
- //voip::WAVPlayer wavPlayer; // 播放音频
- std::string tid;
- static base::MediaPlayer mediaPlayer;
- 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();
- mediaPlayer.Stop();
- mTextViewNamePtr->setTextTr("RemoteRefuse");
- mActivityPtr->unregisterUserTimer(CALLING_TIME);
- mActivityPtr->registerUserTimer(goBack, 5000);
- break;
- case BUSY:
- LOGD("BUSY");
- // wavPlayer.Stop();
- mediaPlayer.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();
- mediaPlayer.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, SIGNAL_TYPE) == "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();
- mediaPlayer.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();
- mediaPlayer.Stop();
- 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();
- // GetTelephone()->SetInputPcmGainSize(10.0);
- // player = new ZKMediaPlayer(ZKMediaPlayer::E_MEDIA_TYPE_AUDIO);
- // player->play("/mnt/extsd/ui/hl.mp3");
- //界面操作
- if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "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();
- setSleepTimerRegistered(false);
- // wavPlayer.Play(CONFIGMANAGER->getResFilePath("ring.wav"));
- // mediaPlayer.Play(CONFIGMANAGER->getResFilePath("ring.wav"));
- mediaPlayer.Play(CONFIGMANAGER->getResFilePath("ring.wav"), "", 1000,
- base::MediaPlayer::PlayMode::Loop);
- // 键值解析
- _isOutgoing = intentPtr->getExtra(isOutgoing);
- std::string _audioOnly = intentPtr->getExtra(audioOnly);
- if (_isOutgoing == "true"){
- int toId = 0;
- _toIdStr = intentPtr->getExtra(toIdStr);
- if (_toIdStr != "") {
- toId = atoi(_toIdStr.c_str());
- }
- //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 = toId;
- 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);
- std::string _callName = intentPtr->getExtra(callName);
- if (_callName != "") {
- mTextViewNamePtr->setText(LANGUAGEMANAGER->getValue("Call") + _callName);
- } else {
- mTextViewNamePtr->setTextTr("CallOutTitle");
- }
- mActivityPtr->registerUserTimer(CALLING_TIME, 60000); // 如果对方没接听,则发送一个CANCEL回去
- } else {
- TcpModel storeModel = CallingStatus::instance()->getTcpModel();
- TcpModel backTcp = storeModel;
- backTcp.data = storeModel.json["id"].asString();
- //back voice success
- sendVoiceTcp(VoiceAction::SUCCESS, backTcp, storeModel.from_id);
- 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();
- mediaPlayer.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, SIGNAL_TYPE) == "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();
- if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "TCP"){
- TcpModel storeModel = CallingStatus::instance()->getTcpModel();
- if (storeModel.json != 0) {
- //只回传iId
- storeModel.data = storeModel.json["id"].asString();
- 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();
- } else if (_isOutgoing == "true") {
- string storeModelStr = getTcpModelString(storeModel);
- 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();
- } else {
- GetTelephone()->Hangup();
- }
- return false;
- }
- static bool onButtonClick_ButtonAnswer(ZKButton *pButton) {
- LOGD(" ButtonClick ButtonAnswer !!!\n");
- mButtonAnswerPtr->setVisible(false);
- if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP"){
- voip::CallInfo info = GetTelephone()->GetCallInfo();
- GetTelephone()->Answer();
- } else {
- 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;
- }
|