-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathOpcDaServer.cs
More file actions
192 lines (168 loc) · 5.72 KB
/
OpcDaServer.cs
File metadata and controls
192 lines (168 loc) · 5.72 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using Opc;
using Opc.Da;
using System.Runtime.Versioning;
namespace BootstrapBlazor.OpcDa;
/// <summary>
/// OPC Server 操作类
/// </summary>
[SupportedOSPlatform("windows")]
sealed class OpcDaServer : IOpcDaServer
{
private Opc.Da.Server? _server = null;
/// <summary>
/// 获得 OPC Server 名称
/// </summary>
public string? ServerName { get; private set; }
/// <summary>
/// 获得 OPC Server 状态
/// </summary>
public bool IsConnected => _server?.IsConnected ?? false;
private readonly Dictionary<string, ISubscription> _subscriptions = [];
/// <summary>
/// 连接到 OPCServer 方法
/// </summary>
/// <param name="serverName">服务器名称</param>
/// <remarks>opcda://localhost/Kepware.KEPServerEX.V6</remarks>
/// <returns>成功时返回真</returns>
public bool Connect(string serverName)
{
ServerName = serverName;
// 如果已经连接则先断开
Disconnect();
_server = new Opc.Da.Server(new OpcCom.Factory(), new URL(serverName));
_server.Connect();
return IsConnected;
}
/// <summary>
/// 断开连接方法
/// </summary>
/// <returns></returns>
public void Disconnect()
{
ServerName = string.Empty;
if (_server is { IsConnected: true })
{
foreach (Subscription sub in _server.Subscriptions)
{
_server.CancelSubscription(sub);
}
_server.Disconnect();
_server = null;
}
}
/// <summary>
/// 创建订阅方法
/// </summary>
/// <param name="name">订阅名称</param>
/// <param name="updateRate">更新频率 默认 1000 毫秒</param>
/// <param name="active">是否激活 默认 true</param>
/// <returns></returns>
public IOpcSubscription CreateSubscription(string name, int updateRate = 1000, bool active = true)
{
var server = GetOpcServer();
if (_subscriptions.TryGetValue(name, out var subscription))
{
// 已经存在该订阅
server.CancelSubscription(subscription);
}
subscription = server.CreateSubscription(name, updateRate, active);
_subscriptions.Add(name, subscription);
return subscription.ToOpcSubscription();
}
/// <summary>
/// 取消订阅方法
/// </summary>
/// <param name="subscription">订阅接口 <see cref="IOpcSubscription"/> 实例</param>
/// <returns></returns>
public void CancelSubscription(IOpcSubscription subscription)
{
var server = GetOpcServer();
var name = subscription.Name;
if (_subscriptions.Remove(name, out var sub))
{
server.CancelSubscription(sub);
}
}
/// <summary>
/// 读取指定 Item 值方法
/// </summary>
/// <param name="items"></param>
/// <returns></returns>
public HashSet<OpcReadItem> Read(params HashSet<string> items)
{
var server = GetOpcServer();
var results = server.Read([.. items.Select(i => new Item() { ItemName = i })]);
return results.Select(i => new OpcReadItem(i.ItemName, i.Quality.ToQuality(), i.Timestamp, i.Value)).ToHashSet(OpcItemEqualityComparer<OpcReadItem>.Default);
}
/// <summary>
/// 读取指定 Item 值方法
/// </summary>
/// <param name="items"></param>
/// <returns></returns>
public HashSet<OpcWriteItem> Write(params HashSet<OpcWriteItem> items)
{
var server = GetOpcServer();
var results = server.Write([.. items.Select(i => new ItemValue() { ItemName = i.Name, Value = i.Value })]);
return items.Select(i =>
{
var item = results.FirstOrDefault(v => v.ItemName == i.Name);
return i with { Result = item != null && item.ResultID == ResultID.S_OK };
}).ToHashSet(OpcItemEqualityComparer<OpcWriteItem>.Default);
}
private Opc.Da.Server GetOpcServer()
{
if (_server is not { IsConnected: true })
{
throw new InvalidOperationException("OPC Server is not connected.");
}
return _server;
}
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="name"></param>
/// <param name="filters"></param>
/// <param name="position"></param>
/// <returns></returns>
public OpcBrowseElement[] Browse(string name, OpcBrowseFilters filters, out OpcBrowsePosition? position)
{
var server = GetOpcServer();
var results = server.Browse(new ItemIdentifier(name), filters.ToFilters(), out var pos) ?? [];
position = pos == null ? null : new OpcBrowsePosition(pos);
return [.. results.Select(element => new OpcBrowseElement(element))];
}
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public OpcBrowseElement[] BrowseNext(OpcBrowsePosition position)
{
var server = GetOpcServer();
var pos = position.Position;
var results = server.BrowseNext(ref pos) ?? [];
return [.. results.Select(element => new OpcBrowseElement(element))];
}
/// <summary>
/// Dispose 方法
/// </summary>
/// <param name="disposing"></param>
private void Dispose(bool disposing)
{
if (disposing)
{
Disconnect();
}
}
/// <summary>
/// <inheritdoc/>
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}