Skip to content

Commit 59eda3a

Browse files
committed
feat: 增加 OpcBrowseFilterType 枚举
1 parent 38cdd4e commit 59eda3a

7 files changed

Lines changed: 116 additions & 25 deletions

File tree

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,25 @@ public static IOpcSubscription ToOpcSubscription(this ISubscription subscription
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 });
2727

28-
public static BrowseFilters ToFilters(this OpcBrowseFilters filtes)
28+
public static BrowseFilters ToFilters(this OpcBrowseFilters filters)
2929
{
3030
return new BrowseFilters
3131
{
32-
ReturnAllProperties = filtes.ReturnAllProperties,
33-
ReturnPropertyValues = filtes.ReturnPropertyValues,
34-
MaxElementsReturned = filtes.MaxElementsReturned,
35-
ElementNameFilter = filtes.ElementNameFilter
32+
ReturnAllProperties = filters.ReturnAllProperties,
33+
ReturnPropertyValues = filters.ReturnPropertyValues,
34+
MaxElementsReturned = filters.MaxElementsReturned,
35+
ElementNameFilter = filters.ElementNameFilter,
36+
BrowseFilter = filters.BrowseFilter.ToBrowseFilter()
37+
};
38+
}
39+
40+
public static browseFilter ToBrowseFilter(this OpcBrowseFilterType filterType)
41+
{
42+
return filterType switch
43+
{
44+
OpcBrowseFilterType.All => browseFilter.all,
45+
OpcBrowseFilterType.Branch => browseFilter.branch,
46+
_ => browseFilter.item
3647
};
3748
}
3849
}

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

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

55
using Opc.Da;
6+
using System.Net.Http.Headers;
67

78
namespace BootstrapBlazor.OpcDa;
89

@@ -73,20 +74,60 @@ public HashSet<OpcWriteItem> Write(params HashSet<OpcWriteItem> items)
7374
public OpcBrowseElement[] Browse(string name, OpcBrowseFilters filters, out OpcBrowsePosition? position)
7475
{
7576
position = null;
76-
return [
77-
new OpcBrowseElement()
78-
{
79-
Name ="Channel1",
80-
IsItem = false,
81-
HasChildren = true
82-
},
83-
new OpcBrowseElement()
84-
{
85-
Name ="Channel2",
86-
IsItem = false,
87-
HasChildren = true
88-
}
89-
];
77+
if (string.IsNullOrEmpty(name))
78+
{
79+
return [
80+
new OpcBrowseElement()
81+
{
82+
Name ="Channel1",
83+
ItemName = "Channel1",
84+
IsItem = false,
85+
HasChildren = true
86+
},
87+
new OpcBrowseElement()
88+
{
89+
Name ="Channel2",
90+
ItemName = "Channel2",
91+
IsItem = false,
92+
HasChildren = true
93+
}
94+
];
95+
}
96+
97+
if (name == "Channel1")
98+
{
99+
return [
100+
new OpcBrowseElement()
101+
{
102+
Name ="Device1",
103+
ItemName = "Channel1.Device1",
104+
IsItem = false,
105+
HasChildren = true
106+
}
107+
];
108+
}
109+
110+
if (name == "Channel1.Device1")
111+
{
112+
return [
113+
new OpcBrowseElement()
114+
{
115+
Name ="Tag1",
116+
ItemName = "Channel1.Device1.Tag1",
117+
IsItem = true,
118+
HasChildren = false
119+
},
120+
new OpcBrowseElement()
121+
{
122+
Name ="Tag2",
123+
ItemName = "Channel1.Device1.Tag2",
124+
IsItem = true,
125+
HasChildren = false
126+
}
127+
];
128+
}
129+
130+
return [];
90131
}
91132

92133
/// <summary>

src/extensions/BootstrapBlazor.OpcDa/OpcBrowseElement.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public class OpcBrowseElement
1616
/// </summary>
1717
public string Name { get; set; }
1818

19+
/// <summary>
20+
/// 获得/设置 Item 名称
21+
/// </summary>
22+
public string ItemName { get; set; }
23+
1924
/// <summary>
2025
/// 获得/设置 是否是数据项
2126
/// </summary>
@@ -32,11 +37,13 @@ public class OpcBrowseElement
3237
public OpcBrowseElement()
3338
{
3439
Name = "";
40+
ItemName = "";
3541
}
3642

3743
internal OpcBrowseElement(BrowseElement element)
3844
{
3945
Name = element.Name;
46+
ItemName = element.ItemName;
4047
IsItem = element.IsItem;
4148
HasChildren = element.HasChildren;
4249
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
/// OpcDa 浏览过滤器类型枚举
9+
/// </summary>
10+
public enum OpcBrowseFilterType
11+
{
12+
/// <summary>
13+
/// 全部
14+
/// </summary>
15+
All,
16+
17+
/// <summary>
18+
/// 分支
19+
/// </summary>
20+
Branch,
21+
22+
/// <summary>
23+
/// 数据项
24+
/// </summary>
25+
Item
26+
}

src/extensions/BootstrapBlazor.OpcDa/OpcBrowseFilters.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ public class OpcBrowseFilters
2828
/// 获得/设置 是否返回属性值
2929
/// </summary>
3030
public bool ReturnPropertyValues { get; set; }
31+
32+
/// <summary>
33+
/// 获得/设置 浏览过滤器类型
34+
/// </summary>
35+
public OpcBrowseFilterType BrowseFilter { get; set; }
3136
}

src/extensions/BootstrapBlazor.OpcDa/OpcBrowsePosition.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ namespace BootstrapBlazor.OpcDa;
99
/// </summary>
1010
public class OpcBrowsePosition
1111
{
12-
internal OpcBrowsePosition(Opc.Da.BrowsePosition position)
12+
internal OpcBrowsePosition(Opc.Da.BrowsePosition? position)
1313
{
1414
Position = position;
1515
}
1616

17-
internal Opc.Da.BrowsePosition Position { get; set; }
17+
internal Opc.Da.BrowsePosition? Position { get; set; }
1818
}

src/extensions/BootstrapBlazor.OpcDa/OpcDaServer.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ private Opc.Da.Server GetOpcServer()
151151
public OpcBrowseElement[] Browse(string name, OpcBrowseFilters filters, out OpcBrowsePosition? position)
152152
{
153153
var server = GetOpcServer();
154-
var results = server.Browse(new ItemIdentifier(name), filters.ToFilters(), out var pos);
155-
position = new OpcBrowsePosition(pos);
156-
return results.Select(element => new OpcBrowseElement(element)).ToArray();
154+
var results = server.Browse(new ItemIdentifier(name), filters.ToFilters(), out var pos) ?? [];
155+
position = pos == null ? null : new OpcBrowsePosition(pos);
156+
return [.. results.Select(element => new OpcBrowseElement(element))];
157157
}
158158

159159
/// <summary>
@@ -165,7 +165,8 @@ public OpcBrowseElement[] BrowseNext(OpcBrowsePosition position)
165165
{
166166
var server = GetOpcServer();
167167
var pos = position.Position;
168-
return server.BrowseNext(ref pos).Select(element => new OpcBrowseElement(element)).ToArray();
168+
var results = server.BrowseNext(ref pos) ?? [];
169+
return [.. results.Select(element => new OpcBrowseElement(element))];
169170
}
170171

171172
/// <summary>

0 commit comments

Comments
 (0)