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 { /// /// Represents the data capture main view, view model. /// /// public class MainViewVM : ViewModel { private INotificationProvider _notification; private IStudioApplicationManager _applicationManager; private IDiagnosticsFrameProvider _frameProvider; private String _recordingsFolder; #region Properties private ObservableCollection _recordings; /// /// Gets or sets the recordings collection. /// public ObservableCollection Recordings { get { return _recordings; } set { _recordings = value; RaisePropertyChangedAuto(); } } private DataRecording _selectedRecording; /// /// Gets or sets the selected recording. /// public DataRecording SelectedRecording { get { return _selectedRecording; } set { _selectedRecording = value; RaisePropertyChangedAuto(); } } /// /// Gets or sets the video capture provider. /// public IVideoCaptureProvider VideoCaptureProvider { get; set; } private DiagnosticsFileRecorder _recorder; /// /// Gets or sets the diagnostics file recorder. /// public DiagnosticsFileRecorder Recorder { get { return _recorder; } set { _recorder = value; RaisePropertyChangedAuto(); } } private DiagnosticsFilePlayer _player; /// /// Gets or sets the diagnostics file player. /// public DiagnosticsFilePlayer Player { get { return _player; } set { _player = value; RaisePropertyChangedAuto(); } } /// /// Gets or sets the machine operator. /// public IMachineOperator MachineOperator { get; set; } #endregion #region Commands /// /// Gets or sets the remove recording command. /// public RelayCommand RemoveRecordingCommand { get; set; } /// /// Gets or sets the toggle camera command. /// public RelayCommand ToggleCameraCommand { get; set; } /// /// Gets or sets the media recording command. /// public RelayCommand MediaRecordingCommand { get; set; } /// /// Gets or sets the media stop command. /// public RelayCommand MediaStopCommand { get; set; } /// /// Gets or sets the media toggle play pause command. /// public RelayCommand MediaTogglePlayPauseCommand { get; set; } /// /// Gets or sets the media play pause command. /// public RelayCommand MediaPlayPauseCommand { get; set; } #endregion #region Constructors /// /// Initializes a new instance of the class. /// 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(); ToggleCameraCommand = new RelayCommand(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(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 /// /// Removes the recording. /// /// The recording. private void RemoveRecording(DataRecording recording) { Recordings.Remove(recording); } /// /// Toggles the camera. /// /// The capture device. 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 } }