Skip to content

Commit 012c7ef

Browse files
committed
feat: 增加浏览功能
1 parent d14ad08 commit 012c7ef

7 files changed

Lines changed: 129 additions & 7 deletions

File tree

src/extensions/BootstrapBlazor.OpcDa/Extensions/Extensions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,15 @@ public static IOpcSubscription ToOpcSubscription(this ISubscription subscription
2424
}
2525

2626
public static ISubscription CreateSubscription(this Server server, string name, int updateRate = 1000, bool active = true) => server.CreateSubscription(new SubscriptionState { Name = name, Deadband = 0, UpdateRate = updateRate, Active = active });
27+
28+
public static BrowseFilters ToFilters(this OpcBrowseFilters filtes)
29+
{
30+
return new BrowseFilters
31+
{
32+
ReturnAllProperties = filtes.ReturnAllProperties,
33+
ReturnPropertyValues = filtes.ReturnPropertyValues,
34+
MaxElementsReturned = filtes.MaxElementsReturned,
35+
ElementNameFilter = filtes.ElementNameFilter
36+
};
37+
}
2738
}

src/extensions/BootstrapBlazor.OpcDa/IOpcDaServer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ public interface IOpcDaServer : IDisposable
6969
/// <param name="filters"></param>
7070
/// <param name="position"></param>
7171
/// <returns></returns>
72-
BrowseElement[] Browser(string name, BrowseFilters filters, out BrowsePosition position);
72+
OpcBrowseElement[] Browse(string name, OpcBrowseFilters filters, out OpcBrowsePosition position);
7373

7474
/// <summary>
7575
/// 浏览 OPC Server 中的位号 (即数据项或者标签)
7676
/// </summary>
7777
/// <param name="position"></param>
7878
/// <returns></returns>
79-
BrowseElement[] BrowserNext(ref BrowsePosition position);
79+
OpcBrowseElement[] BrowseNext(OpcBrowsePosition position);
8080
}

src/extensions/BootstrapBlazor.OpcDa/Mock/MockOpcDaServer.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

5+
using Opc.Da;
6+
57
namespace BootstrapBlazor.OpcDa;
68

79
/// <summary>
@@ -61,6 +63,29 @@ public HashSet<OpcWriteItem> Write(params HashSet<OpcWriteItem> items)
6163
.ToHashSet(OpcItemEqualityComparer<OpcWriteItem>.Default);
6264
}
6365

66+
/// <summary>
67+
/// 浏览 OPC Server 中的位号 (即数据项或者标签)
68+
/// </summary>
69+
/// <param name="name"></param>
70+
/// <param name="filters"></param>
71+
/// <param name="position"></param>
72+
/// <returns></returns>
73+
public OpcBrowseElement[] Browse(string name, OpcBrowseFilters filters, out OpcBrowsePosition? position)
74+
{
75+
position = null;
76+
return [];
77+
}
78+
79+
/// <summary>
80+
/// 浏览 OPC Server 中的位号 (即数据项或者标签)
81+
/// </summary>
82+
/// <param name="position"></param>
83+
/// <returns></returns>
84+
public OpcBrowseElement[] BrowseNext(OpcBrowsePosition position)
85+
{
86+
return [];
87+
}
88+
6489
public void Dispose()
6590
{
6691

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
using Opc.Da;
6+
7+
namespace BootstrapBlazor.OpcDa;
8+
9+
/// <summary>
10+
/// 对 OpcDataServer BrowseElement 的封装类
11+
/// </summary>
12+
public class OpcBrowseElement
13+
{
14+
/// <summary>
15+
/// 获得/设置 节点名称
16+
/// </summary>
17+
public string Name => _element.Name;
18+
19+
/// <summary>
20+
/// 获得/设置 是否是数据项
21+
/// </summary>
22+
public bool IsItem => _element.IsItem;
23+
24+
/// <summary>
25+
/// 获得/设置 是否有子节点
26+
/// </summary>
27+
public bool HasChildren => _element.HasChildren;
28+
29+
internal OpcBrowseElement(BrowseElement element)
30+
{
31+
_element = element;
32+
}
33+
34+
private readonly BrowseElement _element;
35+
}
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+
/// 对 OpcDataServer BrowseFilters 的封装类
9+
/// </summary>
10+
public class OpcBrowseFilters
11+
{
12+
/// <summary>
13+
/// 获得/设置 最大返回节点数量
14+
/// </summary>
15+
public int MaxElementsReturned { get; set; }
16+
17+
/// <summary>
18+
/// 获得/设置 元素名称过滤器
19+
/// </summary>
20+
public string? ElementNameFilter { get; set; }
21+
22+
/// <summary>
23+
/// 获得/设置 是否返回所有属性
24+
/// </summary>
25+
public bool ReturnAllProperties { get; set; }
26+
27+
/// <summary>
28+
/// 获得/设置 是否返回属性值
29+
/// </summary>
30+
public bool ReturnPropertyValues { get; set; }
31+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
/// 对 OpcDataServer BrowsePosition 的封装类
9+
/// </summary>
10+
public class OpcBrowsePosition
11+
{
12+
internal OpcBrowsePosition(Opc.Da.BrowsePosition position)
13+
{
14+
Position = position;
15+
}
16+
17+
internal Opc.Da.BrowsePosition Position { get; set; }
18+
}

src/extensions/BootstrapBlazor.OpcDa/OpcDaServer.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,30 +55,32 @@ public bool Connect(string serverName)
5555
/// <param name="filters"></param>
5656
/// <param name="position"></param>
5757
/// <returns></returns>
58-
public BrowseElement[] Browser(string name, BrowseFilters filters, out BrowsePosition position)
58+
public OpcBrowseElement[] Browse(string name, OpcBrowseFilters filters, out OpcBrowsePosition position)
5959
{
6060
if (_server is not { IsConnected: true })
6161
{
6262
throw new InvalidOperationException("OPC Server is not connected.");
6363
}
6464

65-
return _server.Browse(new ItemIdentifier(name), filters, out position);
65+
var results = _server.Browse(new ItemIdentifier(name), filters.ToFilters(), out var pos);
66+
position = new OpcBrowsePosition(pos);
67+
return results.Select(element => new OpcBrowseElement(element)).ToArray();
6668
}
6769

6870
/// <summary>
6971
/// <inheritdoc/>
7072
/// </summary>
7173
/// <param name="position"></param>
7274
/// <returns></returns>
73-
public BrowseElement[] BrowserNext(ref BrowsePosition position)
75+
public OpcBrowseElement[] BrowseNext(OpcBrowsePosition position)
7476
{
7577
if (_server is not { IsConnected: true })
7678
{
7779
throw new InvalidOperationException("OPC Server is not connected.");
7880
}
7981

80-
81-
return _server.BrowseNext(ref position);
82+
var pos = position.Position;
83+
return _server.BrowseNext(ref pos).Select(element => new OpcBrowseElement(element)).ToArray();
8284
}
8385

8486
/// <summary>

0 commit comments

Comments
 (0)