wanshenmean
6 天以前 5171d3f59b89389bf75293afd210cfa6de4ccff7
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
using System;
using System.Collections.Generic;
using System.Linq;
using WIDESEAWCS_Communicator;
using WIDESEAWCS_QuartzJob.DTO;
 
namespace WIDESEAWCS_QuartzJob.StackerCrane.Common
{
    public class StackerCraneBase : IStackerCraneBase
    {
        public bool IsCanSendTask(BaseCommunicator communicator, List<DeviceProDTO> DeviceProDTOs, List<DeviceProtocolDetailDTO> DeviceProtocolDetailDTOs)
        {
            List<string> paramNames = ResolveDefaultParamNames(DeviceProDTOs, DeviceProtocolDetailDTOs);
            if (paramNames.Count == 0)
            {
                return false;
            }
 
            return IsCanSendTaskByParamNames(communicator, DeviceProDTOs, DeviceProtocolDetailDTOs, paramNames);
        }
 
        public bool IsCanSendTask(BaseCommunicator communicator, List<DeviceProDTO> DeviceProDTOs, List<DeviceProtocolDetailDTO> DeviceProtocolDetailDTOs, params string[] suffixes)
        {
            if (suffixes == null || suffixes.Length == 0)
            {
                return false;
            }
 
            List<string> paramNames = new List<string>(suffixes.Length);
            foreach (string suffix in suffixes)
            {
                string? paramName = ResolveParamName(DeviceProDTOs, DeviceProtocolDetailDTOs, suffix);
                if (paramName == null)
                {
                    return false;
                }
 
                paramNames.Add(paramName);
            }
 
            return IsCanSendTaskByParamNames(communicator, DeviceProDTOs, DeviceProtocolDetailDTOs, paramNames);
        }
 
        private static List<string> ResolveDefaultParamNames(List<DeviceProDTO> deviceProDTOs, List<DeviceProtocolDetailDTO> deviceProtocolDetailDTOs)
        {
            List<string> paramNames = new List<string>();
            foreach (var devicePro in deviceProDTOs)
            {
                if (!devicePro.DeviceProParamName.Contains("StackerCrane", StringComparison.Ordinal))
                {
                    continue;
                }
 
                if (!devicePro.DeviceProParamName.EndsWith("Status", StringComparison.Ordinal))
                {
                    continue;
                }
 
                if (deviceProtocolDetailDTOs.Any(x => x.DeviceProParamName == devicePro.DeviceProParamName))
                {
                    paramNames.Add(devicePro.DeviceProParamName);
                }
            }
 
            return paramNames;
        }
 
        private bool IsCanSendTaskByParamNames(BaseCommunicator communicator, List<DeviceProDTO> DeviceProDTOs, List<DeviceProtocolDetailDTO> DeviceProtocolDetailDTOs, List<string> paramNames)
        {
            for (int i = 0; i < paramNames.Count; i++)
            {
                DeviceProDTO? devicePro = DeviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == paramNames[i]);
                if (devicePro == null)
                {
                    throw new Exception();
                }
 
                DeviceProtocolDetailDTO? deviceProtocolDetail = DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == paramNames[i]);
                if (deviceProtocolDetail == null)
                {
                    throw new Exception();
                }
 
                object obj = communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
 
                if (!(obj?.ToString() ?? "").Equals(deviceProtocolDetail.ProtocalDetailValue))
                {
                    return false;
                }
            }
 
            return true;
        }
 
        private static string? ResolveParamName(List<DeviceProDTO> deviceProDTOs, List<DeviceProtocolDetailDTO> deviceProtocolDetailDTOs, string suffix)
        {
            foreach (var devicePro in deviceProDTOs)
            {
                if (!devicePro.DeviceProParamName.EndsWith(suffix, StringComparison.Ordinal))
                {
                    continue;
                }
 
                if (deviceProtocolDetailDTOs.Any(x => x.DeviceProParamName == devicePro.DeviceProParamName))
                {
                    return devicePro.DeviceProParamName;
                }
            }
 
            return null;
        }
    }
}