wanshenmean
2025-04-15 21cd52c5592aad3687be74599a932012d9dd77a4
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
using HslCommunication;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Communicator;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.StackerCrane;
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
 
namespace WIDESEAWCS_QuartzJob
{
    /// <summary>
    /// 自定义堆垛机
    /// </summary>
    [Description("堆垛机2")]
    public class SpeStackerCrane : IStackerCrane
    {
        #region Private Member
        /// <summary>
        /// 堆垛机通讯对象
        /// </summary>
        private BaseCommunicator _communicator;
        /// <summary>
        /// 堆垛机协议信息
        /// </summary>
        private readonly List<DeviceProDTO> _deviceProDTOs;
        /// <summary>
        /// 堆垛机协议明细信息
        /// </summary>
        private readonly List<DeviceProtocolDetailDTO> _deviceProtocolDetailDTOs;
        /// <summary>
        /// 设备编号
        /// </summary>
        public readonly string _deviceCode;
        /// <summary>
        /// 设备名称
        /// </summary>
        public readonly string _deviceName;
        /// <summary>
        /// 上一次任务号
        /// </summary>
        private int _lastTaskNum;
 
        private bool _isChecked = false;
 
        private bool _heartStatr = true;
 
        private bool _isConnected = true;
        #endregion Private Member
 
        #region Public Member
        /// <summary>
        /// 通讯对象
        /// </summary>
        public BaseCommunicator Communicator => _communicator;
 
        /// <summary>
        /// 通讯协议信息
        /// </summary>
        public List<DeviceProDTO> DeviceProDTOs => _deviceProDTOs;
 
        /// <summary>
        /// 通讯协议明细信息
        /// </summary>
        public List<DeviceProtocolDetailDTO> DeviceProtocolDetailDTOs => _deviceProtocolDetailDTOs;
 
        /// <summary>
        /// 上一次执行的任务号
        /// </summary>
        public int LastTaskNum => _lastTaskNum;
 
        /// <summary>
        /// 当前正在执行的任务号
        /// </summary>
        public int CurrentTaskNum => GetCurrentTaskNum();
 
        /// <summary>
        /// 上一次执行的任务类型
        /// </summary>
        public int? LastTaskType { get; set; } = null;
 
        /// <summary>
        /// 设备编号
        /// </summary>
        public string DeviceCode => _deviceCode;
 
        /// <summary>
        /// 设备名称
        /// </summary>
        public string DeviceName => _deviceName;
 
        /// <summary>
        /// 设备是否已连接
        /// </summary>
        public bool IsConnected => Communicator.IsConnected && _isConnected;
 
        /// <summary>
        /// 堆垛机任务命令
        /// </summary>
        public object StackerCraneTaskCommand { get; set; }
        #endregion
 
        #region Constructor Function
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="communicator">堆垛机通讯对象</param>
        /// <param name="deviceProDTOs">堆垛机协议信息</param>
        /// <param name="deviceProtocolDetailDTOs">堆垛机协议明细信息</param>
        /// <param name="deviceCode">设备编号</param>
        /// <param name="deviceName">设备名称</param>
        public SpeStackerCrane(BaseCommunicator communicator, List<DeviceProDTO> deviceProDTOs, List<DeviceProtocolDetailDTO> deviceProtocolDetailDTOs, string deviceCode, string deviceName)
        {
            _communicator = communicator;
            _deviceProDTOs = deviceProDTOs;
            _deviceProtocolDetailDTOs = deviceProtocolDetailDTOs;
            _deviceCode = deviceCode;
            _deviceName = deviceName;
            CheckConnect();
        }
        #endregion
 
        #region Private Method
        private object GetStatus(string protocolParamType)
        {
            if (Communicator.IsConnected)
            {
                List<DeviceProDTO> devicePros = _deviceProDTOs.Where(x => x.DeviceProParamType == protocolParamType).ToList();
                if (devicePros.Count == 0)
                {
                    throw new Exception("未获取到协议信息");
                }
                for (int i = 0; i < devicePros.Count; i++)
                {
                    object readStatus = Communicator.ReadAsObj(devicePros[i].DeviceProAddress, devicePros[i].DeviceDataType);
                    //todo 协议明细信息未获取到时抛出异常
                    DeviceProtocolDetailDTO? deviceProtocolDetail = _deviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == devicePros[i].DeviceProParamName) ?? throw new Exception();
                    deviceProtocolDetail = _deviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == devicePros[i].DeviceProParamType && x.ProtocalDetailValue.Equals(readStatus.ToString()));
                    if (deviceProtocolDetail != null)
                    {
                        return Convert.ToInt32(readStatus);
                    }
                    return -1;
                }
            }
            //todo 通讯未连接时抛出异常
            return -1;
        }
 
        private int GetCurrentTaskNum()
        {
            DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CurrentTaskNum));
            return devicePro == null ? throw new Exception($"读取当前任务号错误,未获取到协议信息,请检查配置参数名称是否配置,且配置为{nameof(CurrentTaskNum)}") : (int)Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
        }
 
        private void CheckConnect()
        {
            Task.Run(() =>
            {
                while (_heartStatr)
                {
                    try
                    {
                        DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault();
                        if (devicePro == null)
                            _isConnected = false;
                        else
                            Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
                        _isConnected = true;
                    }
                    catch (Exception ex)
                    {
                        _isConnected = false;
                    }
                    Thread.Sleep(500);
                }
            });
        }
        #endregion
 
        #region Public Method
        /// <summary>
        /// 获取堆垛机状态
        /// </summary>
        public T GetStackerCraneStatus<T>() where T : notnull, Enum
        {
            return (T)GetStatus(typeof(T).Name);
        }
 
        /// <summary>
        /// 释放对象
        /// </summary>
        public void Dispose()
        {
            _heartStatr = false;
            _communicator.Dispose();
            GC.SuppressFinalize(this);
        }
 
        /// <summary>
        /// 根据参数名称读取堆垛机对应的数据。
        /// </summary>
        /// <typeparam name="TEnum">参数名称枚举类型。</typeparam>
        /// <typeparam name="TResult">读取结果的返回值类型。</typeparam>
        /// <param name="value">参数名称。</param>
        /// <returns>返回读取到的数据。</returns>
        /// <exception cref="Exception"></exception>
        public TResult GetValue<TEnum, TResult>(TEnum value) where TEnum : Enum
        {
            if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
            DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == value.ToString());
            return devicePro == null ? throw new Exception($"读取数据错误,未在协议信息里面找到参数{value.ToString()}") : (TResult)Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
        }
 
        /// <summary>
        /// 心跳
        /// </summary>
        public void Heartbeat()
        {
 
        }
 
        /// <summary>
        /// 发送任务命令
        /// </summary>
        /// <param name="command">任务命令</param>
        /// <returns></returns>
        public bool SendCommand<T>(T command) where T : IDataTransfer, new()
        {
            if (Communicator is SiemensS7)
            {
                if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
                DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == nameof(DeviceCommand)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                if (devicePro == null)
                {
                    return false;
                }
                if (Communicator.WriteCustomer(devicePro.DeviceProAddress, command))
                {
                    StackerCraneTaskCommand = command;
                    //CheckStackerCraneTaskCompleted();
                    return true;
                }
                return false;
            }
            else
            {
                throw new Exception("暂不支持除西门子之外的PLC");
            }
        }
 
        /// <summary>
        /// 根据参数名称写入堆垛机对应的数据。
        /// </summary>
        /// <typeparam name="TEnum">参数名称枚举类型。</typeparam>
        /// <typeparam name="TValue">要写入的数据类型。</typeparam>
        /// <param name="enum">参数名称。</param>
        /// <param name="value">要写入的数据。</param>
        /// <returns>返回写入成功或失败</returns>
        /// <exception cref="Exception"></exception>
        public bool SetValue<TEnum, TValue>(TEnum @enum, TValue value)
            where TEnum : Enum
            where TValue : notnull
        {
            if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
            DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == @enum.ToString());
            return devicePro == null ? throw new Exception($"写入数据错误,未在协议信息里面找到参数{value.ToString()}") : Communicator.WriteObj(devicePro.DeviceProAddress, devicePro.DeviceDataType, value);
        }
        #endregion
    }
}