aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BL/ValueObjects/HardwareConfiguration.cs
blob: 4b94f24affe11bc970c54d01ec4c984e9efea164 (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
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
{
    public class HardwareConfiguration
    {
        public class HardwareConfigurationParameter
        {
            public String ComponentName { get; set; }
            public String ParameterName { get; set; }
            public Object Value { get; set; }

            public override string ToString()
            {
                return $"{ParameterName}: {Value}";
            }
        }

        public List<HardwareConfigurationParameter> Parameters { get; set; }

        public HardwareConfiguration()
        {
            Parameters = new List<HardwareConfigurationParameter>();
        }

        public HardwareVersion Merge(HardwareVersion hw)
        {
            return Merge(this, hw);
        }

        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;
        }

        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));
                        }
                    }
                }
            }
        }

        public String ToJson()
        {
            return JsonConvert.SerializeObject(this);
        }

        public static HardwareConfiguration FromJson(String json)
        {
            if (json != null)
            {
                return JsonConvert.DeserializeObject<HardwareConfiguration>(json);
            }
            else
            {
                return new HardwareConfiguration();
            }
        }
    }
}