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
147
148
149
150
151
152
153
154
155
156
157
158
159
using Autofac;
using Bro.Common.Helper;
using Bro.Common.Interface;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Bro.Common.Model
{
    /// <summary>
    /// PLC监听设置
    /// </summary>
    public class MonitorSet : IComplexDisplay
    {
        //[Category("监听配置")]
        //[Description("监听设备")]
        //[TypeConverter(typeof(MonitorDeviceConverter))]
        //public string MonitorDevice { get; set; }
 
        [Category("监听配置")]
        [Description("调用方法")]
        [TypeConverter(typeof(MonitorCodeConverter))]
        public string MethodCode { get; set; }
 
        [Category("监听配置")]
        [Description("方法操作配置")]
        [TypeConverter(typeof(ComplexObjectConvert))]
        [Editor(typeof(IOperationConfigEditor), typeof(UITypeEditor))]
        public IOperationConfig OpConfig { get; set; }
 
        /// <summary>
        /// 监听地址索引 按照PLC监听地址从0开始的索引
        /// </summary>
        [Category("监听设置")]
        [Description("监听地址索引 按照PLC监听地址从0开始的索引")]
        public int TriggerIndex { get; set; } = -1;
 
        /// <summary>
        /// 触发值
        /// </summary>
        [Category("监听设置")]
        [Description("触发值,设置为-999时变化即触发")]
        public int TriggerValue { get; set; } = -1;
 
        /// <summary>
        /// 传入数据地址的索引 按照PLC监听地址从0开始的索引集合
        /// </summary>
        [Category("监听设置")]
        [Description("传入数据地址的索引 按照PLC监听地址从0开始的索引")]
        [TypeConverter(typeof(SimpleCollectionConvert<int>))]
        public List<int> InputDataIndex { get; set; } = new List<int>();
 
        /// <summary>
        /// 数据地址 实际PLC寄存器的地址,例如 40012
        /// </summary>
        [Category("回传设置")]
        [Description("回传数据地址 实际PLC寄存器的地址,10进制,例如 40012")]
        public int ReplyDataAddress { get; set; } = -1;
 
        /// <summary>
        /// 通知地址 实际PLC寄存器的地址,例如 40012
        /// </summary>
        [Category("回传设置")]
        [Description("通知地址 实际PLC寄存器的地址,10进制,例如 40012")]
        public int NoticeAddress { get; set; } = -1;
 
        //[Browsable(false)]
        //[JsonIgnore]
        //public List<int> InputDatas { get; set; } = new List<int>();
 
        [Browsable(false)]
        [JsonIgnore]
        public ProcessResponse Response { get; set; } = new ProcessResponse();
 
        public MonitorSet() { }
 
        //public MonitorSet(int triggerIndex, int triggerValue, List<int> inputDataIndex, int dataIndex, int noticeIndex)
        //{
        //    TriggerIndex = triggerIndex;
        //    TriggerValue = triggerValue;
        //    InputDataIndex = inputDataIndex;
        //    ReplyDataAddress = dataIndex;
        //    NoticeAddress = noticeIndex;
        //}
 
        public string GetDisplayText()
        {
            using (var scope = GlobalVar.Container.BeginLifetimeScope())
            {
                List<ProcessMethodAttribute> methodList = scope.Resolve<List<ProcessMethodAttribute>>();
                //List<IDevice> deviceList = scope.Resolve<List<IDevice>>();
 
                string desc = "";
 
                //var device = deviceList.FirstOrDefault(u => u.Id == MonitorDevice);
                //if (device != null)
                //{
                //    desc += ($"{device.Name}");
                //}
                //else
                //{
                //    desc += "无设备";
                //}
 
                desc += "  ";
                var method = methodList.FirstOrDefault(u => u.MethodCode == MethodCode);
                if (method != null)
                {
                    desc += ($"{method.MethodCode}-{method.MethodDesc}");
                }
                else
                {
                    desc += "无调用";
                }
 
 
                desc += $"_{TriggerIndex}:{TriggerValue}";
                return desc;
            }
        }
    }
 
    public class MonitorCodeConverter : ComboBoxItemTypeConvert
    {
        public override void GetConvertHash()
        {
            using (var scope = GlobalVar.Container.BeginLifetimeScope())
            {
                List<ProcessMethodAttribute> methodList = scope.Resolve<List<ProcessMethodAttribute>>();
 
                methodList.ForEach(d =>
                {
                    _hash[d.MethodCode] = d.MethodCode + "_" + d.MethodDesc;
                });
            }
        }
    }
 
    public class MonitorDeviceConverter : ComboBoxItemTypeConvert
    {
        public override void GetConvertHash()
        {
            using (var scope = GlobalVar.Container.BeginLifetimeScope())
            {
                List<IDevice> deviceList = scope.Resolve<List<IDevice>>();
 
                deviceList.ForEach(d =>
                {
                    _hash[d.Id] = d.Name;
                });
            }
        }
    }
}