aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BL/ValueObjects/HardwareConfiguration.cs
blob: 7ad362c121eb91d9e99f927234619e9cba932e55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
{
    /// <summary>
    /// Represents a machine hardware configuration (overrides) that can be embeeded as a string in the HARDWARE_CONFIGURATION field of a machine configuration.
    /// </summary>
    public class HardwareConfiguration
    {
        /// <summary>
        /// Represents a hardware configuration parameter.
        /// </summary>
        public class HardwareConfigurationParameter
        {
            /// <summary>
            /// Gets or sets the name of the component.
            /// </summary>
            public String ComponentName { get; set; }

            /// <summary>
            /// Gets or sets the name of the parameter.
            /// </summary>
            public String ParameterName { get; set; }

            /// <summary>
            /// Gets or sets the value.
            /// </summary>
            public Object Value { get; set; }

            /// <summary>
            /// Returns a <see cref="System.String" /> that represents this instance.
            /// </summary>
            /// <returns>
            /// A <see cref="System.String" /> that represents this instance.
            /// </returns>
            public override string ToString()
            {
                return $"{ParameterName}: {Value}";
            }
        }

        /// <summary>
        /// Gets or sets the parameters.
        /// </summary>
        public List<HardwareConfigurationParameter> Parameters { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="HardwareConfiguration"/> class.
        /// </summary>
        public HardwareConfiguration()
        {
            Parameters = new List<HardwareConfigurationParameter>();
        }

        /// <summary>
        /// Merge this hardware configuration to the specified hardware version which will result in a new instance of hardware version.
        /// </summary>
        /// <param name="hw">The hw.</param>
        /// <returns></returns>
        public HardwareVersion Merge(HardwareVersion hw)
        {
            return Merge(this, hw);
        }

        /// <summary>
        /// Merge the specified hardware configuration to the specified hardware version which will result in a new instance of hardware version.
        /// </summary>
        /// <param name="config">The configuration.</param>
        /// <param name="hw">The hw.</param>
        /// <returns></returns>
        public static HardwareVersion Merge(HardwareConfiguration config, HardwareVersion hw)
        {
            var cloned = hw.Clone();

            MergeCollection<HardwareMotor>(config, cloned.HardwareMotors, (x) => x.HardwareMotorType.Name);
            MergeCollection<HardwareBlower>(config, cloned.HardwareBlowers, (x) => x.HardwareBlowerType.Name);
            MergeCollection<HardwareBreakSensor>(config, cloned.HardwareBreakSensors, (x) => x.HardwareBreakSensorType.Name);
            MergeCollection<HardwareDancer>(config, cloned.HardwareDancers, (x) => x.HardwareDancerType.Name);
            MergeCollection<HardwarePidControl>(config, cloned.HardwarePidControls, (x) => x.HardwarePidControlType.Name);
            MergeCollection<HardwareSpeedSensor>(config, cloned.HardwareSpeedSensors, (x) => x.HardwareSpeedSensorType.Name);
            MergeCollection<HardwareWinder>(config, cloned.HardwareWinders, (x) => x.HardwareWinderType.Name);

            return cloned;
        }

        /// <summary>
        /// Merges a hardware component collection.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="config">The configuration.</param>
        /// <param name="collection">The collection.</param>
        /// <param name="funcProp">The function property.</param>
        private static void MergeCollection<T>(HardwareConfiguration config, SynchronizedObservableCollection<T> collection, Func<T, String> 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));
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Converts this hardware configuration to a json string.
        /// </summary>
        /// <returns></returns>
        public String ToJson()
        {
            return JsonConvert.SerializeObject(this);
        }

        /// <summary>
        /// Creates an instance of <see cref="HardwareConfiguration"/> from the specified json string.
        /// </summary>
        /// <param name="json">The json.</param>
        /// <returns></returns>
        public static HardwareConfiguration FromJson(String json)
        {
            if (json != null)
            {
                return JsonConvert.DeserializeObject<HardwareConfiguration>(json);
            }
            else
            {
                return new HardwareConfiguration();
            }
        }
    }
}