-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMockOpcDaServer.cs
More file actions
147 lines (128 loc) · 4.1 KB
/
MockOpcDaServer.cs
File metadata and controls
147 lines (128 loc) · 4.1 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
// 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.Da;
using System.Net.Http.Headers;
namespace BootstrapBlazor.OpcDa;
/// <summary>
/// 模拟 OpcDa Server 实现类
/// </summary>
sealed class MockOpcDaServer : IOpcDaServer
{
public bool IsConnected { get; set; }
public string? ServerName { get; set; }
private readonly Dictionary<string, IOpcSubscription> _subscriptions = [];
public bool Connect(string serverName)
{
ServerName = serverName;
IsConnected = true;
return true;
}
public void Disconnect()
{
IsConnected = false;
ServerName = null;
}
public IOpcSubscription CreateSubscription(string name, int updateRate = 1000, bool active = true)
{
if (_subscriptions.TryGetValue(name, out var subscription))
{
CancelSubscription(subscription);
}
subscription = new MockOpcDaSubscription(name, updateRate, active);
_subscriptions.Add(name, subscription);
return subscription;
}
public void CancelSubscription(IOpcSubscription subscription)
{
_subscriptions.Remove(subscription.Name);
if (subscription is IDisposable disposable)
{
disposable.Dispose();
}
}
public HashSet<OpcReadItem> Read(params HashSet<string> items)
{
return items.Select(i => new OpcReadItem(i, Quality.Good, DateTime.Now, Random.Shared.Next(1000, 2000)))
.ToHashSet(OpcItemEqualityComparer<OpcReadItem>.Default);
}
public HashSet<OpcWriteItem> Write(params HashSet<OpcWriteItem> items)
{
return items.Select(i => new OpcWriteItem(i.Name, i.Value) { Result = true })
.ToHashSet(OpcItemEqualityComparer<OpcWriteItem>.Default);
}
/// <summary>
/// 浏览 OPC Server 中的位号 (即数据项或者标签)
/// </summary>
/// <param name="name"></param>
/// <param name="filters"></param>
/// <param name="position"></param>
/// <returns></returns>
public OpcBrowseElement[] Browse(string name, OpcBrowseFilters filters, out OpcBrowsePosition? position)
{
position = null;
if (string.IsNullOrEmpty(name))
{
return [
new OpcBrowseElement()
{
Name ="Channel1",
ItemName = "Channel1",
IsItem = false,
HasChildren = true
},
new OpcBrowseElement()
{
Name ="Channel2",
ItemName = "Channel2",
IsItem = false,
HasChildren = true
}
];
}
if (name == "Channel1")
{
return [
new OpcBrowseElement()
{
Name ="Device1",
ItemName = "Channel1.Device1",
IsItem = false,
HasChildren = true
}
];
}
if (name == "Channel1.Device1")
{
return [
new OpcBrowseElement()
{
Name ="Tag1",
ItemName = "Channel1.Device1.Tag1",
IsItem = true,
HasChildren = false
},
new OpcBrowseElement()
{
Name ="Tag2",
ItemName = "Channel1.Device1.Tag2",
IsItem = true,
HasChildren = false
}
];
}
return [];
}
/// <summary>
/// 浏览 OPC Server 中的位号 (即数据项或者标签)
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public OpcBrowseElement[] BrowseNext(OpcBrowsePosition position)
{
return [];
}
public void Dispose()
{
}
}