aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Technician/ViewModels/MachineTechViewVM.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Technician/ViewModels/MachineTechViewVM.cs')
-rw-r--r--Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Technician/ViewModels/MachineTechViewVM.cs153
1 files changed, 145 insertions, 8 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Technician/ViewModels/MachineTechViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Technician/ViewModels/MachineTechViewVM.cs
index 51c2b840d..4420aa4c9 100644
--- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Technician/ViewModels/MachineTechViewVM.cs
+++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Technician/ViewModels/MachineTechViewVM.cs
@@ -1,42 +1,179 @@
-using System;
+using Google.Protobuf.Collections;
+using RealTimeGraphEx.Controllers;
+using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
+using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Tango.Editors;
using Tango.Integration.Observables;
+using Tango.Integration.Operators;
+using Tango.MachineStudio.Common.StudioApplication;
using Tango.MachineStudio.Technician.Editors;
-using Tango.MachineStudio.Technician.Items;
+using Tango.MachineStudio.Technician.TechItems;
+using Tango.PMR.Diagnostics;
using Tango.SharedUI;
namespace Tango.MachineStudio.Technician.ViewModels
{
public class MachineTechViewVM : ViewModel
{
- private ObservableCollection<IElementEditor> _elements;
+ private List<PropertyInfo> _diagnoticsDataProperties;
+ private Dictionary<SingleGraphItem, GraphController> _singleControllers;
+ private static object _elementsLock = new object();
+ private ObservableCollection<IElementEditor> _elements;
public ObservableCollection<IElementEditor> Elements
{
get { return _elements; }
set { _elements = value; RaisePropertyChangedAuto(); }
}
+ private ObservableCollection<TechItem> _availableTechItems;
+ public ObservableCollection<TechItem> AvailableTechItems
+ {
+ get { return _availableTechItems; }
+ set { _availableTechItems = value; RaisePropertyChangedAuto(); }
+ }
+
+ private TechItem _selectedTechItem;
+
+ public TechItem SelectedTechItem
+ {
+ get { return _selectedTechItem; }
+ set { _selectedTechItem = value; RaisePropertyChangedAuto(); }
+ }
+
public ObservablesEntitiesAdapter Adapter { get; set; }
- public MachineTechViewVM()
+ public IStudioApplicationManager ApplicationManager { get; set; }
+
+ private IMachineOperator _machineOperator;
+
+ public IMachineOperator MachineOperator
{
+ get { return _machineOperator; }
+ set { _machineOperator = value; RaisePropertyChangedAuto(); }
+ }
+
+ public MachineTechViewVM(IStudioApplicationManager applicationManager)
+ {
+ _singleControllers = new Dictionary<SingleGraphItem, GraphController>();
+ AvailableTechItems = TechItem.GetAvailableTechItems().ToObservableCollection();
+ SelectedTechItem = AvailableTechItems.FirstOrDefault();
+ _diagnoticsDataProperties = typeof(PushDiagnosticsResponse).GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();
+ ApplicationManager = applicationManager;
+ ApplicationManager.ConnectedMachineChanged += ApplicationManager_ConnectedMachineChanged;
+
Adapter = ObservablesEntitiesAdapter.Instance;
- Adapter.Initialize(); //TODO: Remove on Machine Studio.
+ //Adapter.Initialize(); //TODO: Remove on Machine Studio.
Elements = new ObservableCollection<IElementEditor>();
}
+ private void ApplicationManager_ConnectedMachineChanged(object sender, Integration.Services.IExternalBridgeClient machine)
+ {
+ MachineOperator = machine;
+
+ if (MachineOperator != null)
+ {
+ MachineOperator.DiagnosticsDataAvailable -= MachineOperator_DiagnosticsDataAvailable;
+ MachineOperator.DiagnosticsDataAvailable += MachineOperator_DiagnosticsDataAvailable;
+ }
+ }
+
+ private void MachineOperator_DiagnosticsDataAvailable(object sender, PushDiagnosticsResponse response)
+ {
+ PopulateDiagnosticsData(response);
+ }
+
+ private void PopulateDiagnosticsData(PushDiagnosticsResponse data)
+ {
+ lock (_elementsLock)
+ {
+ var elements = Elements.ToList();
+
+ foreach (var item in elements.Select(x => x.HostedElement as TechItem))
+ {
+ if (item.GetType() == typeof(MonitorItem))
+ {
+ MonitorItem monitorItem = item as MonitorItem;
+
+ var prop = _diagnoticsDataProperties.SingleOrDefault(x => x.Name == monitorItem.TechMonitor.Name);
+
+ if (prop != null)
+ {
+ monitorItem.Value = GetLastMonitorValue(monitorItem.TechMonitor, prop.GetValue(data));
+ }
+ }
+ else if (item.GetType() == typeof(SingleGraphItem))
+ {
+ SingleGraphItem graphItem = item as SingleGraphItem;
+
+ var prop = _diagnoticsDataProperties.SingleOrDefault(x => x.Name == graphItem.TechMonitor.Name);
+
+ if (prop != null)
+ {
+ GraphController controller = null;
+
+ if (_singleControllers.TryGetValue(graphItem, out controller))
+ {
+ controller.PushData(GetSingleGraphValues(graphItem.TechMonitor, prop.GetValue(data)));
+ }
+ }
+ }
+ }
+ }
+ }
+
+ private double GetLastMonitorValue(TechMonitor monitor, object value)
+ {
+ if (!monitor.MultiChannel)
+ {
+ RepeatedField<double> arr = value as RepeatedField<double>;
+ return arr.LastOrDefault();
+ }
+ else
+ {
+ RepeatedField<DoubleArray> arr = value as RepeatedField<DoubleArray>;
+ return arr.Last().Data.Last();
+ }
+ }
+
+ private List<double> GetSingleGraphValues(TechMonitor monitor, object value)
+ {
+ return (value as RepeatedField<double>).ToList();
+ }
+
public void AddElement(Rect bounds)
{
- MonitorItem item = new MonitorItem(Adapter.TechMonitors.FirstOrDefault());
- MonitorElementEditor editor = new MonitorElementEditor(item, bounds);
- Elements.Add(editor);
+ lock (_elementsLock)
+ {
+ if (SelectedTechItem is MonitorItem)
+ {
+ var monitorItem = new MonitorItem(Adapter.TechMonitors.FirstOrDefault());
+ MonitorElementEditor editor = new MonitorElementEditor(monitorItem, bounds);
+ Elements.Add(editor);
+ }
+ else if (SelectedTechItem is SingleGraphItem)
+ {
+ var graphItem = new SingleGraphItem(Adapter.TechMonitors.FirstOrDefault());
+ SingleGraphElementEditor editor = new SingleGraphElementEditor(graphItem, bounds);
+ editor.InnerGraph.InnerGraph.MaxPoints = Common.Helpers.GraphsHelper.GetMaxPoints(graphItem.TechMonitor.PointsPerFrame);
+ editor.DataContext = graphItem;
+ graphItem.Editor = editor;
+
+
+ GraphController controller = new GraphController();
+ editor.InnerGraph.Controller = controller;
+
+ _singleControllers.Add(graphItem, controller);
+
+ Elements.Add(editor);
+ }
+ }
}
}
}