wanshenmean
4 天以前 64a2aa2301946f777659239247233e47ad1e3076
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
using System.Text.Json;
using WIDESEAWCS_S7Simulator.Application.Protocol;
using WIDESEAWCS_S7Simulator.Core.Protocol;
 
namespace WIDESEAWCS_S7Simulator.Server.Services;
 
public class FileProtocolTemplateService : IProtocolTemplateService
{
    private readonly string _filePath;
    private readonly SemaphoreSlim _lock = new(1, 1);
    private readonly JsonSerializerOptions _jsonOptions = new()
    {
        WriteIndented = true,
        PropertyNamingPolicy = JsonNamingPolicy.CamelCase
    };
 
    public FileProtocolTemplateService(string dataPath)
    {
        Directory.CreateDirectory(dataPath);
        _filePath = Path.Combine(dataPath, "protocol-templates.json");
    }
 
    public async Task<IReadOnlyList<ProtocolTemplate>> GetAllAsync()
    {
        await _lock.WaitAsync();
        try
        {
            var templates = await LoadUnsafeAsync();
            return templates.AsReadOnly();
        }
        finally
        {
            _lock.Release();
        }
    }
 
    public async Task<ProtocolTemplate?> GetByIdAsync(string id)
    {
        if (string.IsNullOrWhiteSpace(id))
        {
            return null;
        }
 
        await _lock.WaitAsync();
        try
        {
            var templates = await LoadUnsafeAsync();
            return templates.FirstOrDefault(t => string.Equals(t.Id, id, StringComparison.OrdinalIgnoreCase));
        }
        finally
        {
            _lock.Release();
        }
    }
 
    public async Task<ProtocolTemplate> UpsertAsync(ProtocolTemplate template)
    {
        if (template == null) throw new ArgumentNullException(nameof(template));
        if (string.IsNullOrWhiteSpace(template.Id)) throw new ArgumentException("Template id is required.", nameof(template));
 
        await _lock.WaitAsync();
        try
        {
            var templates = await LoadUnsafeAsync();
            var idx = templates.FindIndex(t => string.Equals(t.Id, template.Id, StringComparison.OrdinalIgnoreCase));
            if (idx >= 0)
            {
                templates[idx] = template;
            }
            else
            {
                templates.Add(template);
            }
 
            await SaveUnsafeAsync(templates);
            return template;
        }
        finally
        {
            _lock.Release();
        }
    }
 
    public async Task<bool> DeleteAsync(string id)
    {
        if (string.IsNullOrWhiteSpace(id))
        {
            return false;
        }
 
        await _lock.WaitAsync();
        try
        {
            var templates = await LoadUnsafeAsync();
            var removed = templates.RemoveAll(t => string.Equals(t.Id, id, StringComparison.OrdinalIgnoreCase)) > 0;
            if (removed)
            {
                await SaveUnsafeAsync(templates);
            }
 
            return removed;
        }
        finally
        {
            _lock.Release();
        }
    }
 
    public async Task<bool> ExistsAsync(string id)
    {
        return (await GetByIdAsync(id)) != null;
    }
 
    private async Task<List<ProtocolTemplate>> LoadUnsafeAsync()
    {
        if (!File.Exists(_filePath))
        {
            var defaults = CreateDefaultTemplates();
            await SaveUnsafeAsync(defaults);
            return defaults;
        }
 
        var content = await File.ReadAllTextAsync(_filePath);
        if (string.IsNullOrWhiteSpace(content))
        {
            return new List<ProtocolTemplate>();
        }
 
        return JsonSerializer.Deserialize<List<ProtocolTemplate>>(content, _jsonOptions) ?? new List<ProtocolTemplate>();
    }
 
    private async Task SaveUnsafeAsync(List<ProtocolTemplate> templates)
    {
        var content = JsonSerializer.Serialize(templates, _jsonOptions);
        await File.WriteAllTextAsync(_filePath, content);
    }
 
    private static List<ProtocolTemplate> CreateDefaultTemplates()
    {
        return new List<ProtocolTemplate>
        {
            new()
            {
                Id = "wcs-line-v260202",
                Name = "WCS-输送线对接协议 V260202",
                Version = "V260202",
                Fields = new List<ProtocolFieldMapping>
                {
                    // SEG01
                    new() { FieldKey = "SEG01_WCS_ACK", DbNumber = 1, Offset = 0, DataType = ProtocolDataType.Byte, Direction = ProtocolFieldDirection.WcsToPlc },
                    new() { FieldKey = "SEG01_WCS_TASK_ID", DbNumber = 1, Offset = 2, DataType = ProtocolDataType.DInt, Direction = ProtocolFieldDirection.WcsToPlc },
                    new() { FieldKey = "SEG01_WCS_TARGET_ID", DbNumber = 1, Offset = 6, DataType = ProtocolDataType.Int, Direction = ProtocolFieldDirection.WcsToPlc },
                    new() { FieldKey = "SEG01_PLC_STB", DbNumber = 1, Offset = 10, DataType = ProtocolDataType.Byte, Direction = ProtocolFieldDirection.PlcToWcs },
                    new() { FieldKey = "SEG01_PLC_TASK_ID", DbNumber = 1, Offset = 20, DataType = ProtocolDataType.DInt, Direction = ProtocolFieldDirection.PlcToWcs },
                    new() { FieldKey = "SEG01_PLC_TARGET_ID", DbNumber = 1, Offset = 24, DataType = ProtocolDataType.Int, Direction = ProtocolFieldDirection.PlcToWcs },
                    new() { FieldKey = "SEG01_PALLET_CODE", DbNumber = 1, Offset = 30, DataType = ProtocolDataType.String, Length = 32, Direction = ProtocolFieldDirection.Bidirectional },
                    // SEG02
                    new() { FieldKey = "SEG02_WCS_ACK", DbNumber = 2, Offset = 0, DataType = ProtocolDataType.Byte, Direction = ProtocolFieldDirection.WcsToPlc },
                    new() { FieldKey = "SEG02_WCS_TASK_ID", DbNumber = 2, Offset = 2, DataType = ProtocolDataType.DInt, Direction = ProtocolFieldDirection.WcsToPlc },
                    new() { FieldKey = "SEG02_WCS_TARGET_ID", DbNumber = 2, Offset = 6, DataType = ProtocolDataType.Int, Direction = ProtocolFieldDirection.WcsToPlc },
                    new() { FieldKey = "SEG02_PLC_STB", DbNumber = 2, Offset = 10, DataType = ProtocolDataType.Byte, Direction = ProtocolFieldDirection.PlcToWcs },
                    new() { FieldKey = "SEG02_PLC_TASK_ID", DbNumber = 2, Offset = 20, DataType = ProtocolDataType.DInt, Direction = ProtocolFieldDirection.PlcToWcs },
                    new() { FieldKey = "SEG02_PLC_TARGET_ID", DbNumber = 2, Offset = 24, DataType = ProtocolDataType.Int, Direction = ProtocolFieldDirection.PlcToWcs },
                    new() { FieldKey = "SEG02_PALLET_CODE", DbNumber = 2, Offset = 30, DataType = ProtocolDataType.String, Length = 32, Direction = ProtocolFieldDirection.Bidirectional }
                }
            }
        };
    }
}