using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using Tango.BL.Entities; using Tango.Core; namespace Tango.BL.ValueObjects { /// /// Represents a machine hardware configuration (overrides) that can be embeeded as a string in the HARDWARE_CONFIGURATION field of a machine configuration. /// public class HardwareConfiguration { /// /// Represents a hardware configuration parameter. /// public class HardwareConfigurationParameter { /// /// Gets or sets the name of the component. /// public String ComponentName { get; set; } /// /// Gets or sets the name of the parameter. /// public String ParameterName { get; set; } /// /// Gets or sets the value. /// public Object Value { get; set; } /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override string ToString() { return $"{ParameterName}: {Value}"; } } /// /// Gets or sets the parameters. /// public List Parameters { get; set; } /// /// Initializes a new instance of the class. /// public HardwareConfiguration() { Parameters = new List(); } /// /// Merge this hardware configuration to the specified hardware version which will result in a new instance of hardware version. /// /// The hw. /// public HardwareVersion Merge(HardwareVersion hw) { return Merge(this, hw); } /// /// Merge the specified hardware configuration to the specified hardware version which will result in a new instance of hardware version. /// /// The configuration. /// The hw. /// public static HardwareVersion Merge(HardwareConfiguration config, HardwareVersion hw) { var cloned = hw.Clone(); MergeCollection(config, cloned.HardwareMotors, (x) => x.HardwareMotorType.Name); MergeCollection(config, cloned.HardwareBlowers, (x) => x.HardwareBlowerType.Name); MergeCollection(config, cloned.HardwareBreakSensors, (x) => x.HardwareBreakSensorType.Name); MergeCollection(config, cloned.HardwareDancers, (x) => x.HardwareDancerType.Name); MergeCollection(config, cloned.HardwarePidControls, (x) => x.HardwarePidControlType.Name); MergeCollection(config, cloned.HardwareSpeedSensors, (x) => x.HardwareSpeedSensorType.Name); MergeCollection(config, cloned.HardwareWinders, (x) => x.HardwareWinderType.Name); return cloned; } /// /// Merges a hardware component collection. /// /// /// The configuration. /// The collection. /// The function property. private static void MergeCollection(HardwareConfiguration config, SynchronizedObservableCollection collection, Func funcProp) { foreach (var component in collection) { foreach (var param in config.Parameters) { if (param.ComponentName == funcProp(component)) { var prop = component.GetType().GetProperty(param.ParameterName); if (prop != null && param.Value != null) { prop.SetValue(component, Convert.ChangeType(param.Value, prop.PropertyType)); } } } } } /// /// Converts this hardware configuration to a json string. /// /// public String ToJson() { return JsonConvert.SerializeObject(this); } /// /// Creates an instance of from the specified json string. /// /// The json. /// public static HardwareConfiguration FromJson(String json) { if (json != null) { return JsonConvert.DeserializeObject(json); } else { return new HardwareConfiguration(); } } } }