aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BL/Entities/HardwareVersion.cs
blob: 1688e87fc826a614355daf91428dd01944265b61 (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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL.Enumerations;
using Tango.BL.Serialization;

namespace Tango.BL.Entities
{
    public partial class HardwareVersion : HardwareVersionBase
    {
        [NotMapped]
        [JsonIgnore]
        public MachineTypes ForMachineType
        {
            get
            {
                return (MachineTypes)MachineType;
            }
            set
            {
                MachineType = value.ToInt32();
            }
        }

        public override HardwareVersion Clone()
        {
            var cloned = base.Clone();
            cloned.HardwareMotors = HardwareMotors.Select(x => x.Clone(y => y.HardwareVersion = cloned)).ToSynchronizedObservableCollection();
            cloned.HardwareDancers = HardwareDancers.Select(x => x.Clone(y => y.HardwareVersion = cloned)).ToSynchronizedObservableCollection();
            cloned.HardwarePidControls = HardwarePidControls.Select(x => x.Clone(y => y.HardwareVersion = cloned)).ToSynchronizedObservableCollection();
            cloned.HardwareWinders = HardwareWinders.Select(x => x.Clone(y => y.HardwareVersion = cloned)).ToSynchronizedObservableCollection();
            cloned.HardwareSpeedSensors = HardwareSpeedSensors.Select(x => x.Clone(y => y.HardwareVersion = cloned)).ToSynchronizedObservableCollection();
            cloned.HardwareBlowers = HardwareBlowers.Select(x => x.Clone(y => y.HardwareVersion = cloned)).ToSynchronizedObservableCollection();
            cloned.HardwareBreakSensors = HardwareBreakSensors.Select(x => x.Clone(y => y.HardwareVersion = cloned)).ToSynchronizedObservableCollection();
            return cloned;
        }

        public override void Delete(ObservablesContext context)
        {
            base.Delete(context);

            context.HardwareDancers.RemoveRange(HardwareDancers);
            context.HardwareMotors.RemoveRange(HardwareMotors);
            context.HardwarePidControls.RemoveRange(HardwarePidControls);
            context.HardwareWinders.RemoveRange(HardwareWinders);
            context.HardwareSpeedSensors.RemoveRange(HardwareSpeedSensors);
            context.HardwareBlowers.RemoveRange(HardwareBlowers);
            context.HardwareBreakSensors.RemoveRange(HardwareBreakSensors);

            context.HardwareVersions.Remove(this);
        }

        public Task<String> ToHardwareVersionFile()
        {
            return Task.Factory.StartNew<String>(() =>
            {
                return this.ToJson(new EntitySerializationStrategy()
                    .Ignore(() => this.Configurations)
                    , EntitySerializationFlags.IgnoreReferenceTypes | EntitySerializationFlags.Indented | EntitySerializationFlags.IgnoreGuids);
            });
        }

        public static Task<HardwareVersion> FromHardwareVersionFile(String json)
        {
            return Task.Factory.StartNew<HardwareVersion>(() =>
            {
                HardwareVersion v = HardwareVersion.FromJson(json, new EntitySerializationStrategy(), EntitySerializationFlags.Indented);
                v.Guid = System.Guid.NewGuid().ToString();

                foreach (var item in v.HardwareBlowers)
                {
                    item.HardwareVersionGuid = v.Guid;
                }

                foreach (var item in v.HardwareBreakSensors)
                {
                    item.HardwareVersionGuid = v.Guid;
                }

                foreach (var item in v.HardwareDancers)
                {
                    item.HardwareVersionGuid = v.Guid;
                }

                foreach (var item in v.HardwareMotors)
                {
                    item.HardwareVersionGuid = v.Guid;
                }

                foreach (var item in v.HardwarePidControls)
                {
                    item.HardwareVersionGuid = v.Guid;
                }

                foreach (var item in v.HardwareSpeedSensors)
                {
                    item.HardwareVersionGuid = v.Guid;
                }

                foreach (var item in v.HardwareWinders)
                {
                    item.HardwareVersionGuid = v.Guid;
                }

                return v;
            });
        }

        [NotMapped]
        [JsonIgnore]
        public String FullName
        {
            get { return $"{Name} v{Version} {ForMachineType}"; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="HardwareVersion" /> class.
        /// </summary>
        public HardwareVersion() : base()
        {

        }

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