wanshenmean
2026-03-17 737dec3c384f394fd6f9849b4480b697d1ba35d5
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
using Microsoft.Extensions.Options;
using WIDESEAWCS_S7Simulator.Core.Interfaces;
using WIDESEAWCS_S7Simulator.Core.Protocol;
 
namespace WIDESEAWCS_S7Simulator.Application.Protocol;
 
/// <summary>
/// 化成堆垛机设备协议处理器。
/// </summary>
public class PlcLinkStackerProtocolHandler : IDeviceProtocolHandler
{
    private readonly MirrorAckProtocolHandler _mirrorAckHandler;
    private readonly ProtocolMonitoringOptions _options;
 
    public PlcLinkStackerProtocolHandler(
        MirrorAckProtocolHandler mirrorAckHandler,
        IOptions<ProtocolMonitoringOptions> options)
    {
        _mirrorAckHandler = mirrorAckHandler;
        _options = options.Value;
    }
 
    public string ProtocolName => "PlcLinkStackerProtocol";
 
    public bool Process(IMemoryStore memoryStore, ProtocolTemplate template, ProtocolRuntimeState runtimeState)
    {
        var ruleIds = _options.PlcLinkStackerRuleIds
            .Where(x => !string.IsNullOrWhiteSpace(x))
            .Distinct(StringComparer.OrdinalIgnoreCase)
            .ToArray();
        if (ruleIds.Length == 0)
        {
            return false;
        }
 
        bool changed = false;
        foreach (var ruleId in ruleIds)
        {
            var rule = _options.MirrorAckRules
                .FirstOrDefault(x => string.Equals(x.RuleId, ruleId, StringComparison.OrdinalIgnoreCase));
            if (rule == null)
            {
                continue;
            }
 
            changed |= _mirrorAckHandler.Process(memoryStore, template, runtimeState, rule, $"{ProtocolName}:{ruleId}");
        }
 
        return changed;
    }
}