aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BL/Entities/Rml.cs
blob: b9224a6d6d7df58d68cede0575c9b0fe056ca3e5 (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
using ColorMine.ColorSpaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using Tango.BL.Builders;
using Tango.BL.Serialization;

namespace Tango.BL.Entities
{
    public partial class Rml : RmlBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Rml" /> class.
        /// </summary>
        public Rml() : base()
        {
            WhitePointL = 92.1815;
            WhitePointA = 2.2555;
            WhitePointB = -10.9325;
        }

        protected override void OnWhitePointLChanged(double whitepointl)
        {
            base.OnWhitePointLChanged(whitepointl);
            UpdateColor();
        }

        protected override void OnWhitePointAChanged(double whitepointa)
        {
            base.OnWhitePointAChanged(whitepointa);
            UpdateColor();
        }

        protected override void OnWhitePointBChanged(double whitepointb)
        {
            base.OnWhitePointBChanged(whitepointb);
            UpdateColor();
        }

        private void UpdateColor()
        {
            RaisePropertyChanged(nameof(Color));
        }

        public Color Color
        {
            get
            {
                Lab lab = new Lab(WhitePointA, WhitePointA, WhitePointB);
                Rgb rgb = lab.To<Rgb>();
                return Color.FromRgb((byte)rgb.R, (byte)rgb.G, (byte)rgb.B);
            }
        }

        public ProcessParametersTablesGroup GetActiveProcessGroup()
        {
            return ProcessParametersTablesGroups.FirstOrDefault(x => x.Active);
        }

        public Task<String> ToRmlFile(ObservablesContext context)
        {
            return Task.Factory.StartNew<String>(() =>
            {
                var rml = new RmlBuilder(context).Set(Guid).WithActiveParametersGroup().WithCCT().WithLiquidFactors().Build();

                String result = rml.ToJson(new EntitySerializationStrategy()
                    .Include(() => rml.Cct)
                    .Include(() => rml.LiquidTypesRmls)
                    .Include(() => rml.ProcessParametersTablesGroups)
                    .Include(typeof(ProcessParametersTablesGroup).GetProperty(nameof(ProcessParametersTablesGroup.ProcessParametersTables))),
                    EntitySerializationFlags.IgnoreCollections | EntitySerializationFlags.IgnoreReferenceTypes | EntitySerializationFlags.Indented);

                return result;
            });
        }

        public static Task<Rml> FromRmlFile(ObservablesContext context, String json)
        {
            return Task.Factory.StartNew<Rml>(() =>
            {
                Rml rml = Rml.FromJson(json,new EntitySerializationStrategy(),EntitySerializationFlags.Indented);

                if (context.Rmls.Any(x => x.Guid == rml.Guid || x.Name == rml.Name))
                {
                    throw new InvalidOperationException($"Thread {rml.Name} already exist.");
                }

                if (rml.Cct != null)
                {
                    if (context.Ccts.Any(x => x.Guid == rml.Cct.Guid))
                    {
                        rml.Cct = null;
                    }
                }

                rml.LastUpdated = DateTime.UtcNow;
                rml.Code = context.Rmls.Max(x => x.Code) + 1;

                return rml;
            });
        }
    }
}