-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMockOpcDaServer.cs
More file actions
68 lines (55 loc) · 1.95 KB
/
MockOpcDaServer.cs
File metadata and controls
68 lines (55 loc) · 1.95 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
// 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/
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);
}
public void Dispose()
{
}
}