dengjunjie
2025-06-12 9429653e8def2729014b45c7c75ec76e9aa6685b
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
using HslCommunication;
using System.ComponentModel;
using System.Reflection;
using WIDESEAWCS_Communicator;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.StackerCrane;
 
namespace WIDESEAWCS_Tasks.StackerCraneJob
{
    public class GetStackerObject
    {
        private SpeStackerCrane _speStackerCrane;
 
        public GetStackerObject(SpeStackerCrane speStackerCrane)
        {
            _speStackerCrane = speStackerCrane;
        }
        /// <summary>
        /// 设备状态
        /// </summary>
        /// <param name="speStackerCrane"></param>
        /// <returns></returns>
        public StackerCraneStatus StackerCraneStatusValue => GetStackerCraneStatus();
 
        /// <summary>
        /// 工作模式
        /// </summary>
        /// <param name="speStackerCrane"></param>
        /// <returns></returns>
        public StackerCraneAutoStatus StackerCraneAutoStatusValue => GetStackerCraneAutoStatus();
 
        /// <summary>
        /// 工作状态
        /// </summary>
        /// <param name="speStackerCrane"></param>
        /// <returns></returns>
        public StackerCraneWorkStatus StackerCraneWorkStatusValue => GetStackerCraneWorkStatus();
 
        /// <summary>
        /// 任务完成
        /// </summary>
        /// <param name="speStackerCrane"></param>
        /// <returns></returns>
        public StackerCraneTaskCompleted StackerCraneTaskCompletedValue => GetStackerCraneTaskCompleted();
 
        /// <summary>
        /// 作业命令
        /// </summary>
        /// <param name="speStackerCrane"></param>
        /// <returns></returns>
        //public int WorkCommandValue() => _speStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkCommand);
 
        /// <summary>
        /// 获取堆垛机设备状态
        /// </summary>
        /// <returns></returns>
        private StackerCraneStatus GetStackerCraneStatus()
        {
            return Enum.Parse<StackerCraneStatus>(GetStatus(nameof(StackerCraneStatus)));
        }
 
        /// <summary>
        /// 获取堆垛机手自动状态
        /// </summary>
        /// <returns></returns>
        private StackerCraneAutoStatus GetStackerCraneAutoStatus()
        {
            return Enum.Parse<StackerCraneAutoStatus>(GetStatus(nameof(StackerCraneAutoStatus)));
        }
 
        /// <summary>
        /// 获取堆垛机工作状态
        /// </summary>
        /// <returns></returns>
        private StackerCraneWorkStatus GetStackerCraneWorkStatus()
        {
            return Enum.Parse<StackerCraneWorkStatus>(GetStatus(nameof(StackerCraneWorkStatus)));
        }
 
        /// <summary>
        /// 获取堆垛机任务状态
        /// </summary>
        /// <returns></returns>
        private StackerCraneTaskCompleted GetStackerCraneTaskCompleted()
        {
            return Enum.Parse<StackerCraneTaskCompleted>(GetStatus(nameof(StackerCraneTaskCompleted)));
        }
 
        private string GetEnumDes<T>(T value) where T : Enum
        {
            FieldInfo? fieldInfo = typeof(T).GetField(value.ToString());
            if (fieldInfo != null)
            {
                DescriptionAttribute? descriptionAttribute = fieldInfo.GetCustomAttribute<DescriptionAttribute>();
                if (descriptionAttribute != null)
                {
                    return descriptionAttribute.Description;
                }
                return "未定义";
            }
            return "未知";
        }
 
        private string GetStatus(string protocolParamType)
        {
            List<DeviceProDTO> devicePros = _speStackerCrane.DeviceProDTOs.Where(x => x.DeviceProParamName == protocolParamType).ToList();
            if (devicePros.Count == 0)
            {
                throw new Exception("未获取到协议信息");
            }
            for (int i = 0; i < devicePros.Count; i++)
            {
                object readStatus = _speStackerCrane.Communicator.ReadAsObj(devicePros[i].DeviceProAddress, devicePros[i].DeviceDataType);
                DeviceProtocolDetailDTO? deviceProtocolDetail = _speStackerCrane.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == devicePros[i].DeviceProParamName) ?? throw new Exception();
                deviceProtocolDetail = _speStackerCrane.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == devicePros[i].DeviceProParamName && x.ProtocalDetailValue.Equals(readStatus.ToString()));
                if (deviceProtocolDetail != null)
                {
                    return deviceProtocolDetail.ProtocolDetailType;
                }
                return StackerCraneStatus.Unkonw.ToString();
            }
            return StackerCraneStatus.Unkonw.ToString();
        }
 
        /// <summary>
        /// 堆垛机任务完成事件
        /// </summary>
        public event EventHandler<StackerCraneTaskCompletedEventArgs> StackerCraneTaskCompletedEventHandler;
 
 
        /// <summary>
        /// 堆垛机完成事件是否已订阅
        /// </summary>
        public bool IsEventSubscribed => StackerCraneTaskCompletedEventHandler != null;
 
 
        private bool _isChecked = false;
 
        /// <summary>
        /// 监测堆垛机任务是否完成
        /// </summary>
        public void CheckStackerCraneTaskCompleted()
        {
            if (_isChecked)
                return;
 
            Task.Run(() =>
            {
                _isChecked = true;
                try
                {
                    DeviceProDTO? devicePro = _speStackerCrane.DeviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(StackerCraneTaskCompleted));
                    if (devicePro != null)
                    {
                        DeviceProtocolDetailDTO? deviceProtocolDetail = _speStackerCrane.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == devicePro.DeviceProParamName && x.ProtocolDetailType == StackerCraneTaskCompleted.WorkCompleted.ToString());
                        if (deviceProtocolDetail != null)
                        {
                            OperateResult<TimeSpan> operateResult = new OperateResult<TimeSpan>();
                            TypeCode typeCode = SiemensDBDataType.GetTypeCode(devicePro.DeviceDataType);
                            switch (typeCode)
                            {
                                case TypeCode.Boolean:
                                    operateResult = _speStackerCrane.Communicator.Wait(devicePro.DeviceProAddress, 500, 10 * 6000, Convert.ToBoolean(deviceProtocolDetail.ProtocalDetailValue));
                                    break;
                                case TypeCode.Byte:
                                    operateResult = _speStackerCrane.Communicator.Wait(devicePro.DeviceProAddress, 500, 10 * 6000, Convert.ToByte(deviceProtocolDetail.ProtocalDetailValue));
                                    break;
                                case TypeCode.Int16:
                                    operateResult = _speStackerCrane.Communicator.Wait(devicePro.DeviceProAddress, 500, 10 * 6000, Convert.ToInt16(deviceProtocolDetail.ProtocalDetailValue));
                                    break;
                                case TypeCode.Int32:
                                    operateResult = _speStackerCrane.Communicator.Wait(devicePro.DeviceProAddress, 500, 10 * 6000, Convert.ToInt32(deviceProtocolDetail.ProtocalDetailValue));
                                    break;
                                case TypeCode.UInt16:
                                    operateResult = _speStackerCrane.Communicator.Wait(devicePro.DeviceProAddress, 500, 10 * 6000, Convert.ToUInt16(deviceProtocolDetail.ProtocalDetailValue));
                                    break;
                                case TypeCode.UInt32:
                                    operateResult = _speStackerCrane.Communicator.Wait(devicePro.DeviceProAddress, 500, 10 * 6000, Convert.ToUInt32(deviceProtocolDetail.ProtocalDetailValue));
                                    break;
                                default:
                                    break;
                            }
                            int taskNum = _speStackerCrane.CurrentTaskNum;
                            if (operateResult.IsSuccess)
                            {
                                StackerCraneTaskCompletedEventArgs args = new(taskNum);
                                StackerCraneTaskCompletedEventHandler?.Invoke(_speStackerCrane, args);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
 
                }
                finally
                {
                    _isChecked = false;
                }
            });
        }
 
        public bool SendCommand(StackerCraneTaskCommand command)
        {
            try
            {
                if(_speStackerCrane.IsConnected)
                {
                    _speStackerCrane.SetValue(StackerCraneDBName.TaskNum, command.TaskNum);
                    _speStackerCrane.SetValue(StackerCraneDBName.StartRow, command.StartRow);
                    _speStackerCrane.SetValue(StackerCraneDBName.StartColumn, command.StartColumn);
                    _speStackerCrane.SetValue(StackerCraneDBName.StartLayer, command.StartLayer);
                    _speStackerCrane.SetValue(StackerCraneDBName.EndRow, command.EndRow);
                    _speStackerCrane.SetValue(StackerCraneDBName.EndColumn, command.EndColumn);
                    _speStackerCrane.SetValue(StackerCraneDBName.EndLayer, command.EndLayer);
                    //_speStackerCrane.SetValue(StackerCraneDBName.Barcode, command.Barcode);
                    _speStackerCrane.SetValue(StackerCraneDBName.WorkType, command.WorkType);
                    //_speStackerCrane.SetValue(StackerCraneDBName.WorkType, command.TrayType);
                    return true;
                }
                return false;
 
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}