-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathOpcDaServer.cs
More file actions
166 lines (144 loc) · 4.84 KB
/
OpcDaServer.cs
File metadata and controls
166 lines (144 loc) · 4.84 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
// 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.Collections.Concurrent;
using System.Runtime.Versioning;
namespace BootstrapBlazor.OpcDa;
/// <summary>
/// OPC Server 操作类
/// </summary>
[SupportedOSPlatform("windows")]
sealed class OpcDaServer : IOpcDaServer
{
private Opc.Da.Server? _server = null;
private readonly ConcurrentDictionary<string, HashSet<OpcReadItem>> _valuesCache = [];
/// <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>
/// 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);
}
}