Dreamine.PLC.Core 1.0.1
Dreamine.PLC.Core 산업 자동화 및 I/O 기능을 제공합니다.
로딩중...
검색중...
일치하는것 없음
DefaultPlcAddressParser.cs
이 파일의 문서화 페이지로 가기
1using System.Globalization;
2using Dreamine.PLC.Abstractions.Devices;
3using Dreamine.PLC.Abstractions.Results;
4
6
15public sealed class DefaultPlcAddressParser : IPlcAddressParser
16{
25 private static readonly string[] KnownDevicePrefixes =
26 [
27 "ZR",
28 "D",
29 "M",
30 "X",
31 "Y",
32 "B",
33 "W",
34 "R"
35 ];
36
61 public PlcResult<PlcAddress> Parse(string text)
62 {
63 if (string.IsNullOrWhiteSpace(text))
64 {
65 return PlcResult<PlcAddress>.Failure("The PLC address text must not be empty.");
66 }
67
68 var normalizedText = text.Trim().ToUpperInvariant();
69 var devicePrefix = FindDevicePrefix(normalizedText);
70
71 if (devicePrefix is null)
72 {
73 return PlcResult<PlcAddress>.Failure($"Unsupported PLC device prefix. Address: {text}");
74 }
75
76 var deviceType = ConvertDeviceType(devicePrefix);
77 if (deviceType == PlcDeviceType.Unknown)
78 {
79 return PlcResult<PlcAddress>.Failure($"Unknown PLC device type. Address: {text}");
80 }
81
82 var body = normalizedText[devicePrefix.Length..];
83 if (string.IsNullOrWhiteSpace(body))
84 {
85 return PlcResult<PlcAddress>.Failure($"PLC address offset is missing. Address: {text}");
86 }
87
88 var split = body.Split('.', StringSplitOptions.TrimEntries);
89 if (split.Length > 2)
90 {
91 return PlcResult<PlcAddress>.Failure($"Invalid PLC bit address format. Address: {text}");
92 }
93
94 if (!int.TryParse(split[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out var offset))
95 {
96 return PlcResult<PlcAddress>.Failure($"Invalid PLC address offset. Address: {text}");
97 }
98
99 if (offset < 0)
100 {
101 return PlcResult<PlcAddress>.Failure($"PLC address offset must be greater than or equal to zero. Address: {text}");
102 }
103
104 int? bitOffset = null;
105
106 if (split.Length == 2)
107 {
108 if (!int.TryParse(split[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsedBitOffset))
109 {
110 return PlcResult<PlcAddress>.Failure($"Invalid PLC bit offset. Address: {text}");
111 }
112
113 if (parsedBitOffset is < 0 or > 15)
114 {
115 return PlcResult<PlcAddress>.Failure($"PLC bit offset must be between 0 and 15. Address: {text}");
116 }
117
118 bitOffset = parsedBitOffset;
119 }
120
121 return PlcResult<PlcAddress>.Success(new PlcAddress(deviceType, offset, bitOffset));
122 }
123
156 public bool TryParse(string text, out PlcAddress address)
157 {
158 var result = Parse(text);
159
160 if (!result.IsSuccess)
161 {
162 address = default;
163 return false;
164 }
165
166 address = result.Value;
167 return true;
168 }
169
194 private static string? FindDevicePrefix(string text)
195 {
196 foreach (var prefix in KnownDevicePrefixes)
197 {
198 if (text.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
199 {
200 return prefix;
201 }
202 }
203
204 return null;
205 }
206
231 private static PlcDeviceType ConvertDeviceType(string devicePrefix)
232 {
233 return devicePrefix switch
234 {
235 "D" => PlcDeviceType.D,
236 "M" => PlcDeviceType.M,
237 "X" => PlcDeviceType.X,
238 "Y" => PlcDeviceType.Y,
239 "B" => PlcDeviceType.B,
240 "W" => PlcDeviceType.W,
241 "R" => PlcDeviceType.R,
242 "ZR" => PlcDeviceType.ZR,
243 _ => PlcDeviceType.Unknown
244 };
245 }
246}
bool TryParse(string text, out PlcAddress address)
static PlcDeviceType ConvertDeviceType(string devicePrefix)