using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Data; using Tango.BL; using Tango.BL.Entities; using Tango.BL.Enumerations; using Tango.BL.ValueObjects; using Tango.Core; using Tango.Core.Commands; using Tango.MachineStudio.Common.Notifications; using Tango.MachineStudio.MachineDesigner.Models; using Tango.SharedUI; namespace Tango.MachineStudio.MachineDesigner.ViewModels { public class HardwareConfigurationViewVM : ViewModel { private INotificationProvider _notification; private HardwareConfiguration _hwConfig; #region Properties public SynchronizedObservableCollection Collections { get; set; } private bool _isShowDifference; public bool IsShowDifference { get { return _isShowDifference; } set { _isShowDifference = value; RaisePropertyChangedAuto(); } } private ICollectionView _collectionViewSource; public ICollectionView CollectionsViewSource { get { return _collectionViewSource; } set { _collectionViewSource = value; RaisePropertyChangedAuto(); } } private String _PropertyFilter; public String PropertyFilter { get { return _PropertyFilter; } set { _PropertyFilter = value; RaisePropertyChangedAuto(); OnFilterChanged(); } } private String _componentFilter; public String ComponentFilter { get { return _componentFilter; } set { _componentFilter = value; RaisePropertyChangedAuto(); OnFilterChanged(); } } #endregion private void OnFilterChanged() { CollectionsViewSource.Refresh(); } public HardwareConfigurationViewVM(INotificationProvider notification) { _notification = notification; Collections = new SynchronizedObservableCollection(); CollectionsViewSource = CollectionViewSource.GetDefaultView(Collections); CollectionsViewSource.Filter = new Predicate(FilterCollection); //Test(); } #region Build Collection public void Init(Configuration configuration) { HardwareVersion hardwareVersion = configuration.HardwareVersion; if (hardwareVersion != null) { _hwConfig = configuration.GetHardwareConfiguration(); Collections.Clear(); Collections.Add(CreateMotorsCollection(hardwareVersion)); Collections.Add(CreateDancerCollection(hardwareVersion)); Collections.Add(CreatePidCollection(hardwareVersion)); Collections.Add(CreateWindersCollection(hardwareVersion)); Collections.Add(CreateSpeedSensorsCollection(hardwareVersion)); Collections.Add(CreateBlowersCollection(hardwareVersion)); Collections.Add(CreateBreakSensorCollection(hardwareVersion)); } } private HardwareCollection CreateMotorsCollection(HardwareVersion hardwareVersion) { HardwareCollection collection = new HardwareCollection() { CollectionName = "MOTORS" }; foreach (var motorTypeCode in Enum.GetValues(typeof(HardwareMotorTypes)).Cast()) { var motor = hardwareVersion.HardwareMotors.SingleOrDefault(x => x.HardwareMotorType.Code == motorTypeCode.ToInt32()); if (motor != null) { var componentResult = CreateComponent( motor.HardwareMotorType.Name, motorTypeCode.ToDescription(), GetComponentProperties(typeof(HardwareMotorBase)), motor); collection.Components.Add(componentResult); } } collection.Components = collection.Components.OrderByAlphaNumeric(x => x.Description).ToSynchronizedObservableCollection(); return collection; } private HardwareCollection CreateDancerCollection(HardwareVersion hardwareVersion) { HardwareCollection collection = new HardwareCollection() { CollectionName = "DANCERS" }; foreach (var dancerTypeCode in Enum.GetValues(typeof(HardwareDancerTypes)).Cast()) { var dancer = hardwareVersion.HardwareDancers.SingleOrDefault(x => x.HardwareDancerType.Code == dancerTypeCode.ToInt32()); if (dancer != null) { var componentResult = CreateComponent( dancer.HardwareDancerType.Name, dancerTypeCode.ToDescription(), GetComponentProperties(typeof(HardwareDancerBase)), dancer); collection.Components.Add(componentResult); } } collection.Components = collection.Components.OrderByAlphaNumeric(x => x.Description).ToSynchronizedObservableCollection(); return collection; } private HardwareCollection CreatePidCollection(HardwareVersion hardwareVersion) { HardwareCollection collection = new HardwareCollection() { CollectionName = "PID CONTROLS" }; foreach (var pidTypeCode in Enum.GetValues(typeof(HardwarePidControlTypes)).Cast()) { var pidControl = hardwareVersion.HardwarePidControls.SingleOrDefault(x => x.HardwarePidControlType.Code == pidTypeCode.ToInt32()); if (pidControl != null) { var componentResult = CreateComponent( pidControl.HardwarePidControlType.Name, pidTypeCode.ToDescription(), GetComponentProperties(typeof(HardwarePidControlBase)), pidControl); collection.Components.Add(componentResult); } } collection.Components = collection.Components.OrderByAlphaNumeric(x => x.Description).ToSynchronizedObservableCollection(); return collection; } private HardwareCollection CreateWindersCollection(HardwareVersion hardwareVersion) { HardwareCollection collection = new HardwareCollection() { CollectionName = "WINDERS" }; foreach (var winderTypeCode in Enum.GetValues(typeof(HardwareWinderTypes)).Cast()) { var winderControl = hardwareVersion.HardwareWinders.SingleOrDefault(x => x.HardwareWinderType.Code == winderTypeCode.ToInt32()); if (winderControl != null) { var componentResult = CreateComponent( winderControl.HardwareWinderType.Name, winderTypeCode.ToDescription(), GetComponentProperties(typeof(HardwareWinderBase)), winderControl); collection.Components.Add(componentResult); } } collection.Components = collection.Components.OrderByAlphaNumeric(x => x.Description).ToSynchronizedObservableCollection(); return collection; } private HardwareCollection CreateSpeedSensorsCollection(HardwareVersion hardwareVersion) { HardwareCollection collection = new HardwareCollection() { CollectionName = "SPEED SENSORS" }; foreach (var speedSensorTypeCode in Enum.GetValues(typeof(HardwareSpeedSensorTypes)).Cast()) { var speedSensor = hardwareVersion.HardwareSpeedSensors.SingleOrDefault(x => x.HardwareSpeedSensorType.Code == speedSensorTypeCode.ToInt32()); if (speedSensor != null) { var componentResult = CreateComponent( speedSensor.HardwareSpeedSensorType.Name, speedSensorTypeCode.ToDescription(), GetComponentProperties(typeof(HardwareSpeedSensorBase)), speedSensor); collection.Components.Add(componentResult); } } collection.Components = collection.Components.OrderByAlphaNumeric(x => x.Description).ToSynchronizedObservableCollection(); return collection; } private HardwareCollection CreateBlowersCollection(HardwareVersion hardwareVersion) { HardwareCollection collection = new HardwareCollection() { CollectionName = "BLOWERS" }; foreach (var blowersTypeCode in Enum.GetValues(typeof(HardwareBlowerTypes)).Cast()) { var blower = hardwareVersion.HardwareBlowers.SingleOrDefault(x => x.HardwareBlowerType.Code == blowersTypeCode.ToInt32()); if (blower != null) { var componentResult = CreateComponent( blower.HardwareBlowerType.Name, blowersTypeCode.ToDescription(), GetComponentProperties(typeof(HardwareBlowerBase)), blower); collection.Components.Add(componentResult); } } collection.Components= collection.Components.OrderByAlphaNumeric(x => x.Description).ToSynchronizedObservableCollection(); return collection; } private HardwareCollection CreateBreakSensorCollection(HardwareVersion hardwareVersion) { HardwareCollection collection = new HardwareCollection() { CollectionName = "BREAK SENSOR" }; foreach (var breakSensorsTypeCode in Enum.GetValues(typeof(HardwareBreakSensorTypes)).Cast()) { var breakSensor = hardwareVersion.HardwareBreakSensors.SingleOrDefault(x => x.HardwareBreakSensorType.Code == breakSensorsTypeCode.ToInt32()); if (breakSensor != null) { var componentResult = CreateComponent( breakSensor.HardwareBreakSensorType.Name, breakSensorsTypeCode.ToDescription(), GetComponentProperties(typeof(HardwareBreakSensorBase)), breakSensor); collection.Components.Add(componentResult); } } collection.Components = collection.Components.OrderByAlphaNumeric(x => x.Description).ToSynchronizedObservableCollection(); return collection; } private HardwareComponent CreateComponent(String name, String description, List properties, Object component) { HardwareComponent hwComponent = new HardwareComponent() { Description = description, ComponentName = name }; foreach (var prop in properties) { var hProp = new HardwareParameter(); hProp.Component = hwComponent; hProp.PropertyName = prop.Name; hProp.DefaultValue = prop.GetValue(component); hProp.Selected -= HProp_Selected; hProp.Selected += HProp_Selected; var configParam = _hwConfig.Parameters.SingleOrDefault(x => x.ComponentName == name && x.ParameterName == prop.Name); hProp.ActualValue = configParam != null ? configParam.Value : null; hwComponent.Properties.Add(hProp); } return hwComponent; } private void HProp_Selected(object sender, EventArgs e) { if (Collections != null) { Collections.SelectMany(x => x.Components).SelectMany(x => x.Properties).Where(x => x != sender).ToList().ForEach(x => x.IsSelected = false); } } public void ClearSelections() { if (Collections != null) { Collections.SelectMany(x => x.Components).SelectMany(x => x.Properties).ToList().ForEach(x => x.IsSelected = false); } } private List GetComponentProperties(Type componentType) { List exclude = new List() { "Guid", "ID", "LastUpdated", "Parameters", }; return componentType.GetProperties(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public).Where(x => !x.PropertyType.IsClass && !exclude.Contains(x.Name)).ToList(); } #endregion public HardwareConfiguration GetResultingHardwareConfiguration() { HardwareConfiguration hwConfig = new HardwareConfiguration(); foreach (var parameter in Collections.SelectMany(x => x.Components).SelectMany(x => x.Properties).Where(x => x.HasDifferences)) { hwConfig.Parameters.Add(new HardwareConfiguration.HardwareConfigurationParameter() { ComponentName = parameter.Component.ComponentName, ParameterName = parameter.PropertyName, Value = parameter.ActualValue }); } return hwConfig; } #region Filter private bool FilterCollection(object obj) { HardwareCollection collection = obj as HardwareCollection; var components = CollectionViewSource.GetDefaultView(collection.Components); components.Filter = new Predicate(FilterComponent); return true; } private bool FilterComponent(object obj) { HardwareComponent component = obj as HardwareComponent; var properties = CollectionViewSource.GetDefaultView(component.Properties); properties.Filter = new Predicate(FilterProperty); if (!String.IsNullOrWhiteSpace(ComponentFilter)) { var words = ComponentFilter.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList(); return words.Any(x => component.Description.ToLower().Contains(x.ToLower())); } else { return true; } } private bool FilterProperty(object obj) { HardwareParameter hparameter = obj as HardwareParameter; if (!String.IsNullOrWhiteSpace(PropertyFilter)) { var words = PropertyFilter.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList(); return words.Any(x => hparameter.PropertyName.ToLower().Contains(x.ToLower())); } else { return true; } } #endregion #region test private void Test() { HardwareCollection col1 = new HardwareCollection() { CollectionName = "Motors", }; HardwareParameter par1 = new HardwareParameter() { PropertyName = "Prop1", ActualValue = null, DefaultValue = 12, }; HardwareParameter par2 = new HardwareParameter() { PropertyName = "Prop2", ActualValue = null, DefaultValue = true }; HardwareComponent comp1 = new HardwareComponent() { ComponentName = "H_C_1", Description = "HardwareComponent" }; comp1.Properties.Add(par1); comp1.Properties.Add(par2); col1.Components.Add(comp1); Collections.Add(col1); } #endregion } }