Newer
Older
Correlator / Correlator / ViewModels / SimplyAuditionDialogViewModel.cs
using System;
using System.Runtime.InteropServices;
using System.Windows;
using Correlator.DataService;
using Correlator.SensorHubTag;
using Correlator.Util;
using NAudio.CoreAudioApi;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;

namespace Correlator.ViewModels
{
    public class SimplyAuditionDialogViewModel : BindableBase, IDialogAware
    {
        public event Action<IDialogResult> RequestClose;

        #region VM

        public string Title { get; private set; } = string.Empty;

        private int _currentVolume;

        public int CurrentVolume
        {
            get => _currentVolume;
            set
            {
                _currentVolume = value;
                RaisePropertyChanged();
            }
        }

        private bool _isRedRecording;

        public bool IsRedRecording
        {
            get => _isRedRecording;
            set
            {
                _isRedRecording = value;
                RaisePropertyChanged();
            }
        }

        private bool _isBlueRecording;

        public bool IsBlueRecording
        {
            get => _isBlueRecording;
            set
            {
                _isBlueRecording = value;
                RaisePropertyChanged();
            }
        }

        #endregion

        #region DelegateCommand

        public DelegateCommand GoBackCommand { get; }

        public DelegateCommand ListenRedSensorCommand { get; }
        public DelegateCommand ListenBlueSensorCommand { get; }
        public DelegateCommand SensorMuteCommand { get; }

        #endregion

        private readonly ISerialPortService _serialPortService;

        #region 静音

        //函数名不能改,否则会报找不到函数错误,dll里面定好了的
        [DllImport("user32.dll")]
        private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

        [DllImport("user32.dll")]
        private static extern byte MapVirtualKey(uint uCode, uint uMapType);

        #endregion

        private int GetCurrentMicVolume()
        {
            var enumerator = new MMDeviceEnumerator();

            //获取音频输入设备
            var device = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
            return (int)(device.AudioEndpointVolume.MasterVolumeLevelScalar * 100);
        }

        public SimplyAuditionDialogViewModel(ISerialPortService serialPortService, IAudioService audioService)
        {
            _serialPortService = serialPortService;

            GoBackCommand = new DelegateCommand(delegate
            {
                RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
            });

            CurrentVolume = GetCurrentMicVolume();

            ListenRedSensorCommand = new DelegateCommand(delegate
            {
                if (_isBlueRecording)
                {
                    MessageBox.Show("请先停止蓝色传感器听音", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                if (_isRedRecording)
                {
                    CommandSender.SendSoundStopCmd(_serialPortService.Sp, DevCode.Dev1);
                    audioService.Stop();
                    IsRedRecording = false;
                    FlowStatus.RedSoundCaches.SaveSoundData();
                    FlowStatus.RedSoundCaches.Clear();
                }
                else
                {
                    CommandSender.SendSoundCollectCmd(_serialPortService.Sp, DevCode.Dev1);
                    audioService.Start();
                    IsRedRecording = true;
                }
            });

            SensorMuteCommand = new DelegateCommand(delegate
            {
                if (_currentVolume > 0)
                {
                    //静音
                    keybd_event(0xAD, MapVirtualKey(0xAD, 0), 0x0001, 0);
                    keybd_event(0xAD, MapVirtualKey(0xAD, 0), 0x0001 | 0x0002, 0);

                    CurrentVolume = 0;
                }
                else
                {
                    //非静音
                    keybd_event(0xAD, MapVirtualKey(0xAD, 0), 0x0001, 0);
                    keybd_event(0xAD, MapVirtualKey(0xAD, 0), 0x0001 | 0x0002, 0);

                    CurrentVolume = GetCurrentMicVolume();
                }
            });

            ListenBlueSensorCommand = new DelegateCommand(delegate
            {
                if (_isRedRecording)
                {
                    MessageBox.Show("请先停止红色传感器听音", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                if (_isBlueRecording)
                {
                    CommandSender.SendSoundStopCmd(_serialPortService.Sp, DevCode.Dev2);
                    audioService.Stop();
                    IsBlueRecording = false;
                    FlowStatus.BlueSoundCaches.SaveSoundData();
                    FlowStatus.BlueSoundCaches.Clear();
                }
                else
                {
                    CommandSender.SendSoundCollectCmd(_serialPortService.Sp, DevCode.Dev2);
                    audioService.Start();
                    IsBlueRecording = true;
                }
            });
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            if (_isRedRecording)
            {
                CommandSender.SendSoundStopCmd(_serialPortService.Sp, DevCode.Dev1);
            }

            if (_isBlueRecording)
            {
                CommandSender.SendSoundStopCmd(_serialPortService.Sp, DevCode.Dev2);
            }
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
        }
    }
}