Skip to content

Commit 026fbad

Browse files
committed
refactor: 增加 OpcWriteItem 类
1 parent 0911d5d commit 026fbad

8 files changed

Lines changed: 70 additions & 15 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
///
9+
/// </summary>
10+
public interface IOpcItem
11+
{
12+
/// <summary>
13+
///
14+
/// </summary>
15+
string Name { get; }
16+
}

src/extensions/BootstrapBlazor.OpcDa/IOpcServer.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,12 @@ public interface IOpcServer : IDisposable
5151
/// </summary>
5252
/// <param name="items"></param>
5353
/// <returns></returns>
54-
HashSet<OpcItem> Read(params List<string> items);
54+
HashSet<OpcReadItem> Read(params HashSet<string> items);
55+
56+
/// <summary>
57+
/// 读取 Item 值方法
58+
/// </summary>
59+
/// <param name="items"></param>
60+
/// <returns></returns>
61+
HashSet<OpcWriteItem> Write(params HashSet<OpcWriteItem> items);
5562
}

src/extensions/BootstrapBlazor.OpcDa/ISubscription.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public interface ISubscription
1212
/// <summary>
1313
/// 数据变更回调
1414
/// </summary>
15-
Func<List<OpcItem>>? DataChanged { get; set; }
15+
Func<List<OpcReadItem>>? DataChanged { get; set; }
1616

1717
/// <summary>
1818
/// 获得 <see cref="Opc.Da.ISubscription"/> 实例

src/extensions/BootstrapBlazor.OpcDa/OpcItemEqualityComparer.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,27 @@
55
namespace BootstrapBlazor.OpcDa;
66

77
/// <summary>
8-
/// <see cref="OpcItem"/> 比较器
8+
/// <see cref="IOpcItem"/> 比较器
99
/// </summary>
10-
public class OpcItemEqualityComparer : IEqualityComparer<OpcItem>
10+
public class OpcItemEqualityComparer<TItem> : IEqualityComparer<TItem> where TItem : IOpcItem
1111
{
1212
/// <summary>
13-
/// 获得 <see cref="OpcItemEqualityComparer"/> 实例
13+
/// 获得 <see cref="OpcItemEqualityComparer{TItem}"/> 实例
1414
/// </summary>
15-
public static OpcItemEqualityComparer Default { get; } = new();
15+
public static OpcItemEqualityComparer<TItem> Default { get; } = new();
1616

1717
/// <summary>
1818
/// <inheritdoc/>
1919
/// </summary>
2020
/// <param name="x"></param>
2121
/// <param name="y"></param>
2222
/// <returns></returns>
23-
public bool Equals(OpcItem x, OpcItem y) => x.Name == y.Name;
23+
public bool Equals(TItem? x, TItem? y) => x?.Name == y?.Name;
2424

2525
/// <summary>
2626
/// <inheritdoc/>
2727
/// </summary>
2828
/// <param name="item"></param>
2929
/// <returns></returns>
30-
public int GetHashCode([DisallowNull] OpcItem item) => item.Name.GetHashCode();
30+
public int GetHashCode([DisallowNull] TItem item) => item.Name.GetHashCode();
3131
}

src/extensions/BootstrapBlazor.OpcDa/OpcItem.cs renamed to src/extensions/BootstrapBlazor.OpcDa/OpcReadItem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
namespace BootstrapBlazor.OpcDa;
66

77
/// <summary>
8-
/// OPC Item 配置实体类
8+
/// OPC Item 读取实体类
99
/// </summary>
10-
public record struct OpcItem(string Name, Quality Quality, DateTime Timestamp, object? Value)
10+
public record struct OpcReadItem(string Name, Quality Quality, DateTime Timestamp, object? Value) : IOpcItem
1111
{
1212
/// <summary>
1313
/// 获得 Opc Item 上次值

src/extensions/BootstrapBlazor.OpcDa/OpcServer.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace BootstrapBlazor.OpcDa;
1616
class OpcServer : IOpcServer
1717
{
1818
private Opc.Da.Server? _server = null;
19-
private readonly ConcurrentDictionary<string, HashSet<OpcItem>> _valuesCache = [];
19+
private readonly ConcurrentDictionary<string, HashSet<OpcReadItem>> _valuesCache = [];
2020

2121
/// <summary>
2222
/// 获得 OPC Server 名称
@@ -101,12 +101,28 @@ public void CancelSubscription(ISubscription subscription)
101101
/// </summary>
102102
/// <param name="items"></param>
103103
/// <returns></returns>
104-
/// <exception cref="InvalidOperationException"></exception>
105-
public HashSet<OpcItem> Read(params List<string> items)
104+
public HashSet<OpcReadItem> Read(params HashSet<string> items)
106105
{
107106
var server = GetOpcServer();
108107
var results = server.Read([.. items.Select(i => new Item() { ItemName = i })]);
109-
return results.Select(i => new OpcItem(i.ItemName, i.Quality.ToQuality(), i.Timestamp, i.Value)).ToHashSet(OpcItemEqualityComparer.Default);
108+
return results.Select(i => new OpcReadItem(i.ItemName, i.Quality.ToQuality(), i.Timestamp, i.Value)).ToHashSet(OpcItemEqualityComparer<OpcReadItem>.Default);
109+
}
110+
111+
/// <summary>
112+
/// 读取指定 Item 值方法
113+
/// </summary>
114+
/// <param name="items"></param>
115+
/// <returns></returns>
116+
public HashSet<OpcWriteItem> Write(params HashSet<OpcWriteItem> items)
117+
{
118+
var server = GetOpcServer();
119+
var results = server.Write([.. items.Select(i => new ItemValue() { ItemName = i.Name, Value = i.Value })]);
120+
121+
return items.Select(i =>
122+
{
123+
var item = results.FirstOrDefault(v => v.ItemName == i.Name);
124+
return new OpcWriteItem(i.Name, i.Value) { Result = item != null && item.ResultID == ResultID.S_OK };
125+
}).ToHashSet(OpcItemEqualityComparer<OpcWriteItem>.Default);
110126
}
111127

112128
private Opc.Da.Server GetOpcServer()

src/extensions/BootstrapBlazor.OpcDa/OpcSubscription.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace BootstrapBlazor.OpcDa;
66

77
class OpcSubscription(Opc.Da.ISubscription subscription) : ISubscription
88
{
9-
public Func<List<OpcItem>>? DataChanged { get; set; }
9+
public Func<List<OpcReadItem>>? DataChanged { get; set; }
1010

1111
public Opc.Da.ISubscription GetSubscription() => subscription;
1212
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
/// OPC Item 写入实体类
9+
/// </summary>
10+
public record struct OpcWriteItem(string Name, object? Value) : IOpcItem
11+
{
12+
/// <summary>
13+
/// 获得/设置 写入结果
14+
/// </summary>
15+
public bool Result { get; set; }
16+
}

0 commit comments

Comments
 (0)