1
Huangxiaoqiang-03
2024-11-01 6e9f630e4e12738d98241b684e6227e02010b6c5
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
using HslCommunication;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Communicator;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.StackerCrane;
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
 
namespace WIDESEAWCS_Tasks.StackerCraneJob
{
    public class GetStackerObject
    {
 
        /// <summary>
        /// 设备状态
        /// </summary>
        /// <param name="speStackerCrane"></param>
        /// <returns></returns>
        public StackerCraneStatus StackerCraneStatusValue(SpeStackerCrane speStackerCrane) => GetStackerCraneStatus(speStackerCrane);
 
        /// <summary>
        /// 工作模式
        /// </summary>
        /// <param name="speStackerCrane"></param>
        /// <returns></returns>
        public StackerCraneAutoStatus StackerCraneAutoStatusValue(SpeStackerCrane speStackerCrane) => GetStackerCraneAutoStatus(speStackerCrane);
 
        /// <summary>
        /// 工作状态
        /// </summary>
        /// <param name="speStackerCrane"></param>
        /// <returns></returns>
        public StackerCraneWorkStatus StackerCraneWorkStatusValue(SpeStackerCrane speStackerCrane) => GetStackerCraneWorkStatus(speStackerCrane);
 
        /// <summary>
        /// 任务完成
        /// </summary>
        /// <param name="speStackerCrane"></param>
        /// <returns></returns>
        public StackerCraneTaskCompleted StackerCraneTaskCompletedValue(SpeStackerCrane speStackerCrane) => GetStackerCraneTaskCompleted(speStackerCrane);
 
        /// <summary>
        /// 作业命令
        /// </summary>
        /// <param name="speStackerCrane"></param>
        /// <returns></returns>
        public int WorkCommandValue(SpeStackerCrane speStackerCrane) => speStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkCommand);
 
        /// <summary>
        /// 获取堆垛机设备状态
        /// </summary>
        /// <returns></returns>
        private StackerCraneStatus GetStackerCraneStatus(SpeStackerCrane speStackerCrane)
        {
            return Enum.Parse<StackerCraneStatus>(GetStatus(nameof(StackerCraneStatus), speStackerCrane));
        }
 
        /// <summary>
        /// 获取堆垛机手自动状态
        /// </summary>
        /// <returns></returns>
        private StackerCraneAutoStatus GetStackerCraneAutoStatus(SpeStackerCrane speStackerCrane)
        {
            return Enum.Parse<StackerCraneAutoStatus>(GetStatus(nameof(StackerCraneAutoStatus), speStackerCrane));
        }
 
        /// <summary>
        /// 获取堆垛机工作状态
        /// </summary>
        /// <returns></returns>
        private StackerCraneWorkStatus GetStackerCraneWorkStatus(SpeStackerCrane speStackerCrane)
        {
            return Enum.Parse<StackerCraneWorkStatus>(GetStatus(nameof(StackerCraneWorkStatus), speStackerCrane));
        }
 
        /// <summary>
        /// 获取堆垛机任务状态
        /// </summary>
        /// <returns></returns>
        private StackerCraneTaskCompleted GetStackerCraneTaskCompleted(SpeStackerCrane speStackerCrane)
        {
            return Enum.Parse<StackerCraneTaskCompleted>(GetStatus(nameof(StackerCraneTaskCompleted), speStackerCrane));
        }
 
        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, SpeStackerCrane speStackerCrane)
        {
            List<DeviceProDTO> devicePros = speStackerCrane.DeviceProDTOs.Where(x => x.DeviceProParamType == 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);
                //todo 协议明细信息未获取到时抛出异常
                DeviceProtocolDetailDTO? deviceProtocolDetail = speStackerCrane.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == devicePros[i].DeviceProParamName) ?? throw new Exception();
                deviceProtocolDetail = speStackerCrane.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == devicePros[i].DeviceProParamType && x.ProtocalDetailValue.Equals(readStatus.ToString()));
                if (deviceProtocolDetail != null)
                {
                    return deviceProtocolDetail.ProtocolDetailType;
                }
                return StackerCraneStatus.Unkonw.ToString();
            }
            return StackerCraneStatus.Unkonw.ToString();
        }
    }
}