ningshuxia
2022-11-23 bd74555117bade457bc42e1ee93e0872b92265bd
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.Model.Monitor
{
    /// <summary>
    /// 转换参数
    /// </summary>
    public class ConvertParameters : JsonModel<ConvertParameters>, IConvert
    {
        /// <summary>
        /// 
        /// </summary>
        public ConvertParameters() { }
 
        /// <summary>
        /// 
        /// </summary>
        public ConvertParameters(ConvertParameters rhs)
        {
            this.ConvertType = rhs.ConvertType;
            this.ConvertParas = rhs.ConvertParas;
        }
 
        /// <summary>
        /// 
        /// </summary>
        public ConvertParameters(eConvertType convertType, IConvert convertParas)
        {
            this.ConvertType = convertType;
            this.ConvertParas = convertParas;
        }
        /// <summary>
        /// 转换类型
        /// </summary>
        public eConvertType ConvertType { get; set; }
 
        /// <summary>
        /// 转换参数
        /// </summary>
        public IConvert ConvertParas { get; set; }
 
        /// <summary>
        /// 转换
        /// </summary>
        public double Convert(double value, out bool succeed)
        {
            succeed = false;
            if (this.ConvertParas == null)
                return value;
            return this.ConvertParas.Convert(value, out succeed);
        }
 
        /// <summary>
        /// To Model
        /// </summary>
        public new static ConvertParameters ToModel(string json)
        {
            if (string.IsNullOrEmpty(json))
                return default;
            try
            {
                var dict = JsonHelper.Json2Object<Dictionary<string, object>>(json);
                var convertType = (eConvertType)System.Convert.ToInt32(dict["ConvertType"]);
                IConvert convertParas = null;
                switch (convertType)
                {
                    case eConvertType.Ratio:
                        convertParas = RatioConvert.ToModel(JsonHelper.Object2Json(dict["ConvertParas"]));
                        break;
                    case eConvertType.Mapping:
                        convertParas = MappingConvert.ToModel(JsonHelper.Object2Json(dict["ConvertParas"]));
                        break;
                    default: break;
                }
                return new ConvertParameters(convertType, convertParas);
            }
            catch
            {
                return default;
            }
        }
 
    }
}