diff options
| author | Roy <roy.mail.net@gmail.com> | 2018-02-14 22:51:52 +0200 |
|---|---|---|
| committer | Roy <roy.mail.net@gmail.com> | 2018-02-14 22:51:52 +0200 |
| commit | b934b152eea671fa06678baa0cdf7f8811e6d2d9 (patch) | |
| tree | 35c092bff1e06905c70c23e60ed1a9fdf9825e5a /Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.DataCapture/ViewModels | |
| parent | 12967bd645e92f7b08b8d323b93225e027ad69f0 (diff) | |
| download | Tango-b934b152eea671fa06678baa0cdf7f8811e6d2d9.tar.gz Tango-b934b152eea671fa06678baa0cdf7f8811e6d2d9.zip | |
MERGE.
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.DataCapture/ViewModels')
| -rw-r--r-- | Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.DataCapture/ViewModels/MainViewVM.cs | 297 |
1 files changed, 297 insertions, 0 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.DataCapture/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.DataCapture/ViewModels/MainViewVM.cs new file mode 100644 index 000000000..b63ee51d0 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.DataCapture/ViewModels/MainViewVM.cs @@ -0,0 +1,297 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Controls; +using Tango.Core.Commands; +using Tango.Integration.Diagnostics; +using Tango.Integration.Operators; +using Tango.MachineStudio.Common.Diagnostics; +using Tango.MachineStudio.Common.Notifications; +using Tango.MachineStudio.Common.StudioApplication; +using Tango.MachineStudio.Common.Video; +using Tango.MachineStudio.DataCapture.Recording; +using Tango.MachineStudio.DataCapture.Views; +using Tango.PMR.Diagnostics; +using Tango.Settings; +using Tango.SharedUI; +using Tango.Video.DirectCapture; + +namespace Tango.MachineStudio.DataCapture.ViewModels +{ + /// <summary> + /// Represents the data capture main view, view model. + /// </summary> + /// <seealso cref="Tango.SharedUI.ViewModel" /> + public class MainViewVM : ViewModel + { + private INotificationProvider _notification; + private IStudioApplicationManager _applicationManager; + private IDiagnosticsFrameProvider _frameProvider; + private String _recordingsFolder; + + #region Properties + + private ObservableCollection<DataRecording> _recordings; + /// <summary> + /// Gets or sets the recordings collection. + /// </summary> + public ObservableCollection<DataRecording> Recordings + { + get { return _recordings; } + set { _recordings = value; RaisePropertyChangedAuto(); } + } + + private DataRecording _selectedRecording; + /// <summary> + /// Gets or sets the selected recording. + /// </summary> + public DataRecording SelectedRecording + { + get { return _selectedRecording; } + set { _selectedRecording = value; RaisePropertyChangedAuto(); } + } + + /// <summary> + /// Gets or sets the video capture provider. + /// </summary> + public IVideoCaptureProvider VideoCaptureProvider { get; set; } + + private DiagnosticsFileRecorder _recorder; + /// <summary> + /// Gets or sets the diagnostics file recorder. + /// </summary> + public DiagnosticsFileRecorder Recorder + { + get { return _recorder; } + set { _recorder = value; RaisePropertyChangedAuto(); } + } + + private DiagnosticsFilePlayer _player; + /// <summary> + /// Gets or sets the diagnostics file player. + /// </summary> + public DiagnosticsFilePlayer Player + { + get { return _player; } + set { _player = value; RaisePropertyChangedAuto(); } + } + + /// <summary> + /// Gets or sets the machine operator. + /// </summary> + public IMachineOperator MachineOperator { get; set; } + + #endregion + + #region Commands + + /// <summary> + /// Gets or sets the remove recording command. + /// </summary> + public RelayCommand<DataRecording> RemoveRecordingCommand { get; set; } + + /// <summary> + /// Gets or sets the toggle camera command. + /// </summary> + public RelayCommand<CaptureDevice> ToggleCameraCommand { get; set; } + + /// <summary> + /// Gets or sets the media recording command. + /// </summary> + public RelayCommand MediaRecordingCommand { get; set; } + + /// <summary> + /// Gets or sets the media stop command. + /// </summary> + public RelayCommand MediaStopCommand { get; set; } + + /// <summary> + /// Gets or sets the media toggle play pause command. + /// </summary> + public RelayCommand MediaTogglePlayPauseCommand { get; set; } + + /// <summary> + /// Gets or sets the media play pause command. + /// </summary> + public RelayCommand MediaPlayPauseCommand { get; set; } + + #endregion + + #region Constructors + + /// <summary> + /// Initializes a new instance of the <see cref="MainViewVM"/> class. + /// </summary> + public MainViewVM(IVideoCaptureProvider videoCaptureProvider, INotificationProvider notification, IStudioApplicationManager applicationManager, IDiagnosticsFrameProvider frameProvider) + { + _notification = notification; + _applicationManager = applicationManager; + _frameProvider = frameProvider; + + Recorder = new DiagnosticsFileRecorder(); + Player = new DiagnosticsFilePlayer(); + + VideoCaptureProvider = videoCaptureProvider; + Recordings = new ObservableCollection<DataRecording>(); + + ToggleCameraCommand = new RelayCommand<CaptureDevice>(ToggleCamera); + + Recordings.Add(new DataRecording() + { + Name = "Recording 1" + }); + Recordings.Add(new DataRecording() + { + Name = "Recording 2" + }); + Recordings.Add(new DataRecording() + { + Name = "Recording 3" + }); + + RemoveRecordingCommand = new RelayCommand<DataRecording>(RemoveRecording); + + MediaRecordingCommand = new RelayCommand(StartDiagnosticsRecording, () => !Recorder.IsRecording && MachineOperator != null && !Player.IsPlaying); + MediaStopCommand = new RelayCommand(StopRecorderOrPlayer, () => Recorder.IsRecording || Player.IsPlaying); + MediaPlayPauseCommand = new RelayCommand(DiagnosticsTogglePlayPause, () => !Recorder.IsRecording && Player.IsLoaded); + + _recordingsFolder = Path.Combine(SettingsManager.DefaultFolder, "Recordings"); + Directory.CreateDirectory(_recordingsFolder); + + _frameProvider.FrameReceived += _frameProvider_FrameReceived; + + _notification.PushBarItem(new RecordingBarView() { DataContext = this }); + + applicationManager.ConnectedMachineChanged += ApplicationManager_ConnectedMachineChanged; + } + + private void ApplicationManager_ConnectedMachineChanged(object sender, Integration.Services.IExternalBridgeClient machine) + { + MachineOperator = machine; + InvalidateRelayCommands(); + } + + private void _frameProvider_FrameReceived(object sender, PushDiagnosticsResponse frame) + { + if (!_frameProvider.Disable) + { + if (Recorder.IsRecording) + { + Recorder.Write(frame); + } + } + } + + #endregion + + #region Private Methods + + /// <summary> + /// Removes the recording. + /// </summary> + /// <param name="recording">The recording.</param> + private void RemoveRecording(DataRecording recording) + { + Recordings.Remove(recording); + } + + /// <summary> + /// Toggles the camera. + /// </summary> + /// <param name="captureDevice">The capture device.</param> + private void ToggleCamera(CaptureDevice captureDevice) + { + if (captureDevice.Device != null) + { + captureDevice.IsStarted = !captureDevice.IsStarted; + } + } + + private void DiagnosticsTogglePlayPause() + { + if (!Player.IsPlaying || Player.IsPaused) + { + _frameProvider.Disable = true; + Player.Play(); + } + else + { + Player.Pause(); + } + + InvalidateRelayCommands(); + } + + private async void LoadSelectedRecording() + { + using (_notification.PushTaskItem("Loading Recording...")) + { + if (Player != null) + { + Player.Dispose(); + } + + Player = new DiagnosticsFilePlayer(); + Player.FrameReceived += Player_FrameReceived; + await Player.Load(SelectedRecording.FilePath); + } + + InvalidateRelayCommands(); + } + + private void Player_FrameReceived(object sender, DataFileFrame frame) + { + if (_frameProvider.Disable) + { + _frameProvider.PushFrame(frame.PushDiagnosticsResponse); + } + } + + private void StartDiagnosticsRecording() + { + using (_notification.PushTaskItem("Starting Recording...")) + { + Recorder.Start(); + } + + InvalidateRelayCommands(); + } + + private async void StopRecorderOrPlayer() + { + if (Recorder.IsRecording) + { + using (_notification.PushTaskItem("Stopping Recording...")) + { + await Recorder.Stop(); + } + + String recordingName = _notification.ShowTextInput("Enter recording name", "Recording name"); + + if (!String.IsNullOrWhiteSpace(recordingName)) + { + using (_notification.PushTaskItem("Saving Recording...")) + { + await Recorder.Save(Path.Combine(_recordingsFolder, recordingName + ".tdr")); + } + } + + Recorder.Dispose(); + Recorder = new DiagnosticsFileRecorder(); + } + else if (Player.IsPlaying) + { + await Player.Stop(); + _frameProvider.Disable = false; + } + + InvalidateRelayCommands(); + } + + #endregion + } +} |
