Skip to content

Commit 996851d

Browse files
committed
feat: 新增 OpcItemEqualityComparer 比较器
1 parent d633fa3 commit 996851d

2 files changed

Lines changed: 42 additions & 32 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
namespace BootstrapBlazor.OpcDa;
6+
7+
/// <summary>
8+
/// <see cref="OpcItem"/> 比较器
9+
/// </summary>
10+
public class OpcItemEqualityComparer : IEqualityComparer<OpcItem>
11+
{
12+
/// <summary>
13+
/// 获得 <see cref="OpcItemEqualityComparer"/> 实例
14+
/// </summary>
15+
public static OpcItemEqualityComparer Default { get; } = new();
16+
17+
/// <summary>
18+
/// <inheritdoc/>
19+
/// </summary>
20+
/// <param name="x"></param>
21+
/// <param name="y"></param>
22+
/// <returns></returns>
23+
public bool Equals(OpcItem x, OpcItem y) => x.Name == y.Name;
24+
25+
/// <summary>
26+
/// <inheritdoc/>
27+
/// </summary>
28+
/// <param name="item"></param>
29+
/// <returns></returns>
30+
public int GetHashCode([DisallowNull] OpcItem item) => item.Name.GetHashCode();
31+
}

src/extensions/BootstrapBlazor.OpcDa/OpcServer.cs

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55
using Opc;
66
using Opc.Da;
77
using System.Collections.Concurrent;
8+
using System.Runtime.Versioning;
89

910
namespace BootstrapBlazor.OpcDa;
1011

1112
/// <summary>
1213
/// OPC Server 操作类
1314
/// </summary>
15+
[SupportedOSPlatform("windows")]
1416
public partial class OpcServer : IDisposable
1517
{
1618
private Opc.Da.Server? _server = null;
1719
private readonly ConcurrentDictionary<string, HashSet<OpcItem>> _valuesCache = [];
18-
private readonly TaskCompletionSource _readTask = new();
1920

2021
/// <summary>
2122
/// 获得 OPC Server 名称
@@ -31,28 +32,17 @@ public partial class OpcServer : IDisposable
3132
/// 连接到 OPCServer 方法
3233
/// </summary>
3334
/// <param name="serverName">服务器名称</param>
34-
/// <param name="token"></param>
3535
/// <remarks>opcda://localhost/Kepware.KEPServerEX.V6</remarks>
3636
/// <returns>成功时返回真</returns>
37-
public async Task<bool> Connect(string serverName, CancellationToken? token = null)
37+
public bool Connect(string serverName)
3838
{
3939
ServerName = serverName;
4040

41-
try
42-
{
43-
// 如果已经连接则先断开
44-
Disconnect();
41+
// 如果已经连接则先断开
42+
Disconnect();
4543

46-
await Task.Run(() =>
47-
{
48-
_server = new Opc.Da.Server(new OpcCom.Factory(), new URL(serverName));
49-
_server.Connect();
50-
}, token ?? CancellationToken.None);
51-
}
52-
catch (OperationCanceledException)
53-
{
54-
55-
}
44+
_server = new Opc.Da.Server(new OpcCom.Factory(), new URL(serverName));
45+
_server.Connect();
5646
return IsConnected;
5747
}
5848

@@ -103,24 +93,22 @@ public ISubscription CreateSubscription(string name, int updateRate, bool active
10393
{
10494
_valuesCache.AddOrUpdate(name, key => AddFactory(value), (key, v) => UpdateFactory(v, value));
10595
}
106-
107-
_readTask.TrySetResult();
10896
};
10997
}
11098
return subscription;
11199
}
112100

113101
private static HashSet<OpcItem> AddFactory(ItemValueResult value)
114102
{
115-
return new HashSet<OpcItem>(OpcItemEqualityComparer.Instance)
103+
return new HashSet<OpcItem>(OpcItemEqualityComparer.Default)
116104
{
117-
new(value.ItemName, value.Quality, value.Timestamp, value.Value)
105+
new(value.ItemName, value.Quality.ToQuality() , value.Timestamp, value.Value)
118106
};
119107
}
120108

121109
private static HashSet<OpcItem> UpdateFactory(HashSet<OpcItem> items, ItemValueResult value)
122110
{
123-
var item = new OpcItem(value.ItemName, value.Quality, value.Timestamp, value.Value);
111+
var item = new OpcItem(value.ItemName, value.Quality.ToQuality(), value.Timestamp, value.Value);
124112
if (items.TryGetValue(item, out var v))
125113
{
126114
item.LastValue = v.Value;
@@ -151,7 +139,7 @@ public HashSet<OpcItem> Read(params List<string> items)
151139
{
152140
var server = GetOpcServer();
153141
var results = server.Read([.. items.Select(i => new Item() { ItemName = i })]);
154-
return results.Select(i => new OpcItem(i.ItemName, i.Quality, i.Timestamp, i.Value)).ToHashSet(OpcItemEqualityComparer.Instance);
142+
return results.Select(i => new OpcItem(i.ItemName, i.Quality.ToQuality(), i.Timestamp, i.Value)).ToHashSet(OpcItemEqualityComparer.Default);
155143
}
156144

157145
private Opc.Da.Server GetOpcServer()
@@ -183,13 +171,4 @@ public void Dispose()
183171
Dispose(true);
184172
GC.SuppressFinalize(this);
185173
}
186-
187-
class OpcItemEqualityComparer : IEqualityComparer<OpcItem>
188-
{
189-
public static OpcItemEqualityComparer Instance = new();
190-
191-
public bool Equals(OpcItem x, OpcItem y) => x.Name == y.Name;
192-
193-
public int GetHashCode([DisallowNull] OpcItem item) => item.Name.GetHashCode();
194-
}
195174
}

0 commit comments

Comments
 (0)