using GalaSoft.MvvmLight.Ioc; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.Core.Commands; using Tango.DAL.Observables; using Tango.MachineStudio.Common.Controls; using Tango.MachineStudio.Common.Notifications; using Tango.MachineStudio.Common.StudioApplication; using Tango.SharedUI; namespace Tango.MachineStudio.Developer.ViewModels { /// /// Represents the developer module main view, view model. /// /// public class MainViewVM : ViewModel { private INotificationProvider _notification; #region Properties /// /// Gets or sets the application manager. /// public IStudioApplicationManager ApplicationManager { get; set; } /// /// Gets or sets observable entites database the adapter. /// public ObservablesEntitiesAdapter Adapter { get; set; } private Machine _selectedMachine; /// /// Gets or sets the selected machine. /// public Machine SelectedMachine { get { return _selectedMachine; } set { _selectedMachine = value; RaisePropertyChangedAuto(); OnMachineChanged(); InvalidateRelayCommands(); } } private List _liquidTypesRmls; /// /// Gets or sets the liquid types RMLS. /// public List LiquidTypesRmls { get { return _liquidTypesRmls; } set { _liquidTypesRmls = value; RaisePropertyChangedAuto(); } } private ProcessParametersTablesGroup _rmlProcessParametersTablesGroup; /// /// Gets or sets the RML process parameters table group (cloned). /// public ProcessParametersTablesGroup RmlProcessParametersTableGroup { get { return _rmlProcessParametersTablesGroup; } set { _rmlProcessParametersTablesGroup = value; RaisePropertyChangedAuto(); } } private ObservableCollection _groupsHistory; /// /// Gets or sets the RML process parameters groups history. /// public ObservableCollection GroupsHistory { get { return _groupsHistory; } set { _groupsHistory = value; RaisePropertyChangedAuto(); } } private ProcessParametersTablesGroup _selectedGroupHistory; /// /// Gets or sets the selected process parameters tables group history. /// public ProcessParametersTablesGroup SelectedGroupHistory { get { return _selectedGroupHistory; } set { _selectedGroupHistory = value; RaisePropertyChangedAuto(); OnSelectedGroupHistoryChanged(); } } private Job _selectedJob; /// /// Gets or sets the selected machine job. /// public Job SelectedJob { get { return _selectedJob; } set { _selectedJob = value; RaisePropertyChangedAuto(); } } private Rml _selectedRML; /// /// Gets or sets the selected RML. /// public Rml SelectedRML { get { return _selectedRML; } set { _selectedRML = value; RaisePropertyChangedAuto(); InvalidateLiquidFactorsAndProcessTables(); InvalidateRelayCommands(); } } private bool _isSideBarOpened; /// /// Gets or sets a value indicating whether the configuration panels are opened. /// public bool IsSideBarOpened { get { return _isSideBarOpened; } set { _isSideBarOpened = value; RaisePropertyChangedAuto(); } } private ObservableCollection _availableSensors; /// /// Gets or sets the available sensors. /// public ObservableCollection AvailableSensors { get { return _availableSensors; } set { _availableSensors = value; RaisePropertyChangedAuto(); } } private ObservableCollection _graphs; /// /// Gets or sets the collection of displayed graph controls. /// public ObservableCollection Graphs { get { return _graphs; } set { _graphs = value; RaisePropertyChangedAuto(); } } #endregion #region Commands /// /// Gets or sets the edit machine command. /// public RelayCommand EditMachineCommand { get; set; } /// /// Gets or sets the edit RML command. /// public RelayCommand EditRMLCommand { get; set; } /// /// Gets or sets the toggle side bar command. /// public RelayCommand ToggleSideBarCommand { get; set; } /// /// Gets or sets the save process parameters command. /// public RelayCommand SaveProcessParametersCommand { get; set; } /// /// Gets or sets the save liquid factors command. /// public RelayCommand SaveLiquidFactorsCommand { get; set; } #endregion #region Constructors /// /// Initializes a new instance of the class. /// public MainViewVM() { IsSideBarOpened = true; if (!this.DesignMode) { Adapter = ObservablesEntitiesAdapter.Instance; AvailableSensors = Adapter.Sensors.ToObservableCollection(); } Graphs = new ObservableCollection(); } /// /// Initializes a new instance of the class. /// /// The application manager. /// The notification provider. [PreferredConstructor] public MainViewVM(IStudioApplicationManager applicationManager, INotificationProvider notificationProvider) : this() { _notification = notificationProvider; EditMachineCommand = new RelayCommand(EditMachine, (x) => SelectedMachine != null); ApplicationManager = applicationManager; EditRMLCommand = new RelayCommand(EditRML, (x) => SelectedRML != null); ToggleSideBarCommand = new RelayCommand(() => IsSideBarOpened = !IsSideBarOpened); SaveProcessParametersCommand = new RelayCommand(SaveProcessParameters); SaveLiquidFactorsCommand = new RelayCommand(SaveLiquidFactors); } #endregion #region Virtual Methods /// /// Called when the selected group history has been changed /// protected virtual void OnSelectedGroupHistoryChanged() { if (SelectedGroupHistory != null) { RmlProcessParametersTableGroup = SelectedGroupHistory.CloneGroup(); } } /// /// Called when the machine has been changed /// protected virtual void OnMachineChanged() { InvalidateLiquidFactorsAndProcessTables(); } #endregion #region Private Methods /// /// Saves the liquid factors. /// private async void SaveLiquidFactors() { if (SelectedRML != null) { using (_notification.PushTaskItem("Saving Liquid Factors...")) { String machineGuid = SelectedMachine.Guid; String rmlGuid = SelectedRML.Guid; await SelectedRML.SaveAsync(); SelectedMachine = Adapter.Machines.SingleOrDefault(x => x.Guid == machineGuid); SelectedRML = Adapter.Rmls.SingleOrDefault(x => x.Guid == rmlGuid); } } } /// /// Navigates to the DB Module in order to edit the selected RML. /// private void EditRML() { ApplicationManager.RequestModule("Data Base", SelectedRML); } /// /// Navigates to the Machine Designer Module in order to edit the selected machine. /// private void EditMachine() { ApplicationManager.RequestModule("Machine Designer", SelectedMachine); } /// /// Saves the process parameters group. /// private async void SaveProcessParameters() { var response = _notification.ShowTextInput("Enter Group Name", "Group Name"); if (response == null) return; using (_notification.PushTaskItem("Saving Parameters Group...")) { ProcessParametersTablesGroup group = new ProcessParametersTablesGroup(); List tables = new List(); foreach (var table in RmlProcessParametersTableGroup.ProcessParametersTables) { var newTable = table.CloneEntity(); newTable.ProcessParametersTablesGroups = group; tables.Add(newTable); } group.Active = true; group.ProcessParametersTables = tables.ToObservableCollection(); group.Rml = SelectedRML; group.Name = response; group.SaveDate = DateTime.UtcNow; foreach (var g in SelectedRML.ProcessParametersTablesGroups) { g.Active = false; } String machineGuid = SelectedMachine.Guid; String rmlGuid = SelectedRML.Guid; SelectedRML.ProcessParametersTablesGroups.Add(group); await SelectedRML.SaveAsync(); SelectedMachine = Adapter.Machines.SingleOrDefault(x => x.Guid == machineGuid); SelectedRML = Adapter.Rmls.SingleOrDefault(x => x.Guid == rmlGuid); } } /// /// Invalidates the liquid factors and process parameters tables. /// private void InvalidateLiquidFactorsAndProcessTables() { if (SelectedRML != null && SelectedMachine != null) { LiquidTypesRmls = SelectedMachine.Configuration.IdsPacks.OrderBy(x => x.PackIndex).Select(x => x.LiquidTypes).SelectMany(x => x.LiquidTypesRmls).Where(x => x.Rml.Guid == SelectedRML.Guid).ToList(); RmlProcessParametersTableGroup = SelectedRML.ProcessParametersTablesGroups.SingleOrDefault(x => x.Active); if (RmlProcessParametersTableGroup != null) { RmlProcessParametersTableGroup = RmlProcessParametersTableGroup.CloneGroup(); RmlProcessParametersTableGroup.ProcessParametersTables = RmlProcessParametersTableGroup.ProcessParametersTables.OrderBy(x => x.TableIndex).ToObservableCollection(); } GroupsHistory = SelectedRML.ProcessParametersTablesGroups.OrderByDescending(x => x.SaveDate).OrderBy(x => !x.Active).ToObservableCollection(); } } #endregion #region Public Events /// /// Add sensor graph from available sensors to displayed graphs. /// /// The sensor. public void OnDropAvailableSensor(Sensor sensor) { if (Graphs.Count < 8) { RealTimeGraphControl graphControl = new RealTimeGraphControl(); graphControl.Tag = sensor; graphControl.SensorName = sensor.Description; graphControl.SensorUnits = sensor.Units; graphControl.Graph.Minimum = sensor.Min; graphControl.Graph.Maximum = sensor.Max; graphControl.Graph.MaxPoints = Common.Helpers.GraphsHelper.GetMaxPoints(sensor.PointsPerFrame); graphControl.GraphRemoveButtonPressed += (sender, __) => { RemoveGraph(sender as RealTimeGraphControl); }; Graphs.Add(graphControl); AvailableSensors.Remove(sensor); } else { _notification.ShowInfo("The maximum number of real-time graphs is eight. Please remove a graph to add another."); } } /// /// Removes the graph. /// /// The graph. public void RemoveGraph(RealTimeGraphControl graph) { Graphs.Remove(graph); AvailableSensors.Insert(0, graph.Tag as Sensor); } #endregion } }