-
-
Notifications
You must be signed in to change notification settings - Fork 7
feat(OpcDa): add Mock function #516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cc191f2
refactor: 重构代码
ArgoZhang 01b9b64
refactor: 更改变量名称
ArgoZhang e4cb7aa
chore: bump vesion 9.0.0
ArgoZhang 94bdbb3
refactor: 增加 IOpcSubscription 接口
ArgoZhang 31682fb
feat: 增加 MockOpcDaServer 服务
ArgoZhang 8c17d57
test: 更新单元测试
ArgoZhang 7930e98
refactor: 代码重构
ArgoZhang 6738102
chore: bump version 9.0.0
ArgoZhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 3 additions & 3 deletions
6
src/extensions/BootstrapBlazor.OpcDa/BootstrapBlazor.OpcDa.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/extensions/BootstrapBlazor.OpcDa/Mock/MockOpcDaServer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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() | ||
| { | ||
|
|
||
| } | ||
| } | ||
83 changes: 83 additions & 0 deletions
83
src/extensions/BootstrapBlazor.OpcDa/Mock/MockOpcSubscription.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| // 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; | ||
|
|
||
| sealed class MockOpcDaSubscription : IOpcSubscription, IDisposable | ||
| { | ||
| private readonly int _updateRate; | ||
| private readonly bool _active; | ||
| private readonly List<string> _items = []; | ||
| private CancellationTokenSource? _cts; | ||
|
|
||
| public MockOpcDaSubscription(string name, int updateRate = 1000, bool active = true) | ||
| { | ||
| Name = name; | ||
| _updateRate = updateRate; | ||
| _active = active; | ||
|
|
||
| _cts = new CancellationTokenSource(); | ||
| _ = Task.Run(() => DoTask(_cts.Token)); | ||
| } | ||
|
|
||
| public string Name { get; } | ||
|
|
||
| public bool KeepLastValue { get; set; } | ||
|
|
||
| public Action<List<OpcReadItem>>? DataChanged { get; set; } | ||
|
|
||
| public void AddItems(IEnumerable<string> items) | ||
| { | ||
| _items.AddRange(items); | ||
| } | ||
|
|
||
| private void UpdateValues() | ||
| { | ||
| if (DataChanged != null) | ||
| { | ||
| var values = _items.Select(i => new OpcReadItem(i, Quality.Good, DateTime.Now, Random.Shared.Next(1000, 2000))).ToList(); | ||
| DataChanged.Invoke(values); | ||
| } | ||
| } | ||
|
|
||
| private async Task DoTask(CancellationToken token) | ||
| { | ||
| do | ||
| { | ||
| try | ||
| { | ||
| if (_active) | ||
| { | ||
| UpdateValues(); | ||
| } | ||
|
|
||
| await Task.Delay(_updateRate, token); | ||
| } | ||
| catch (OperationCanceledException) | ||
| { | ||
| // ignored | ||
| } | ||
| } | ||
| while (!token.IsCancellationRequested); | ||
| } | ||
|
|
||
| private void Dispose(bool disposing) | ||
| { | ||
| if (disposing) | ||
| { | ||
| if (_cts != null) | ||
| { | ||
| _cts.Cancel(); | ||
| _cts.Dispose(); | ||
| _cts = null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| Dispose(true); | ||
| GC.SuppressFinalize(this); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Disconnect does not clear or dispose subscriptions.
Please ensure all items in _subscriptions are disposed and cleared in Disconnect to prevent resource leaks or unintended background activity.