-
-
Notifications
You must be signed in to change notification settings - Fork 362
doc(OpcDa): add MockOpcDaServer sample code #6563
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
16 commits
Select commit
Hold shift + click to select a range
f106cf7
chore: 更加 OpcDa 依赖包
ArgoZhang 2c92863
doc: 增加菜单
ArgoZhang 251c400
doc: 增加 OpcDa 示例页面
ArgoZhang 55218a5
refactor: 增加模拟 OpcServer 实现类
ArgoZhang e6710ee
doc: 增加 OpcDa 源码映射
ArgoZhang f362552
doc: 更新示例
ArgoZhang e15a46a
chore: 增加 MockOpcDaServer 支持 Linux 演示
ArgoZhang 6548313
chore: 更新依赖包
ArgoZhang f6dca35
feat: 完善模拟 OpcServer 服务实现类
ArgoZhang e85eea2
doc: 更新示例
ArgoZhang e74a38a
refactor: 更新依赖
ArgoZhang cc9c0a1
doc: 更新按钮可用逻辑
ArgoZhang a0d092c
refactor: 更新注入服务
ArgoZhang 8282b9e
doc: 更新示例
ArgoZhang 0343a2e
refactor: 删除 Mock 服务
ArgoZhang b822d00
chore: 更新依赖包
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| @page "/opc-da" | ||
| @inject IStringLocalizer<OpcDa> Localizer | ||
|
|
||
| <h3>@Localizer["OpcDaTitle"]</h3> | ||
|
|
||
| <h4>@Localizer["OpcDaDescription"]</h4> | ||
|
|
||
| <PackageTips Name="BootstrapBlazor.OpcDa"/> | ||
|
|
||
| <DemoBlock Title="@Localizer["OpcDaNormalTitle"]" Introduction="@Localizer["OpcDaNormalIntro"]" Name="Normal"> | ||
| <p class="code-label">1. 点击 <b>连接</b> 按钮与 <code>OpcDa</code> 服务器建立通讯连接</p> | ||
| <p> | ||
| <BootstrapInputGroup> | ||
| <BootstrapInputGroupLabel DisplayText="OpcDa Server"></BootstrapInputGroupLabel> | ||
| <Display @bind-Value="@_serverName"></Display> | ||
| <Button Text="连接" OnClick="OnConnect" IsDisabled="OpcDaServer.IsConnected"></Button> | ||
| <Button Text="断开" OnClick="OnDisConnect" IsDisabled="!OpcDaServer.IsConnected" | ||
| Color="Color.Danger"></Button> | ||
| </BootstrapInputGroup> | ||
| </p> | ||
|
|
||
| <p class="code-label">2. 点击 <b>读取</b> 按钮读取 <code>OpcDa</code> 服务器上的位号值</p> | ||
| <div class="row g-3 mb-3"> | ||
| <div class="col-12 col-sm-6"> | ||
| <BootstrapInputGroup> | ||
| <BootstrapInputGroupLabel DisplayText="转速"></BootstrapInputGroupLabel> | ||
| <BootstrapInputGroupLabel DisplayText="@Tag1"></BootstrapInputGroupLabel> | ||
| <Display @bind-Value="@_tagValue1"></Display> | ||
| </BootstrapInputGroup> | ||
| </div> | ||
| <div class="col-12 col-sm-6"> | ||
| <BootstrapInputGroup> | ||
| <BootstrapInputGroupLabel DisplayText="流速"></BootstrapInputGroupLabel> | ||
| <BootstrapInputGroupLabel DisplayText="@Tag2"></BootstrapInputGroupLabel> | ||
| <Display @bind-Value="@_tagValue2"></Display> | ||
| </BootstrapInputGroup> | ||
| </div> | ||
| <div class="col-12 col-sm-6"> | ||
| <Button OnClick="OnRead" Text="读取" IsDisabled="!OpcDaServer.IsConnected"></Button> | ||
| </div> | ||
| </div> | ||
|
|
||
| <p class="code-label">3. 订阅功能</p> | ||
| <p>通过订阅可以监控一组 <b>位号</b> 数据改变情况,当数据改变时通过 <code>DataChanged</code> 回调方法通知订阅者</p> | ||
| <div class="row g-3"> | ||
| <div class="col-12 col-sm-6"> | ||
| <BootstrapInputGroup> | ||
| <BootstrapInputGroupLabel DisplayText="订阅名称"></BootstrapInputGroupLabel> | ||
| <BootstrapInputGroupLabel DisplayText="Subscription1"></BootstrapInputGroupLabel> | ||
| </BootstrapInputGroup> | ||
| </div> | ||
| <div class="col-12 col-sm-6"> | ||
| <BootstrapInputGroup> | ||
| <BootstrapInputGroupLabel DisplayText="更新频率"></BootstrapInputGroupLabel> | ||
| <BootstrapInputGroupLabel DisplayText="1000"></BootstrapInputGroupLabel> | ||
| </BootstrapInputGroup> | ||
| </div> | ||
| <div class="col-12 col-sm-6"> | ||
| <BootstrapInputGroup> | ||
| <BootstrapInputGroupLabel DisplayText="转速"></BootstrapInputGroupLabel> | ||
| <BootstrapInputGroupLabel DisplayText="@Tag3"></BootstrapInputGroupLabel> | ||
| <Display @bind-Value="@_tagValue3"></Display> | ||
| </BootstrapInputGroup> | ||
| </div> | ||
| <div class="col-12 col-sm-6"> | ||
| <BootstrapInputGroup> | ||
| <BootstrapInputGroupLabel DisplayText="流速"></BootstrapInputGroupLabel> | ||
| <BootstrapInputGroupLabel DisplayText="@Tag4"></BootstrapInputGroupLabel> | ||
| <Display @bind-Value="@_tagValue4"></Display> | ||
| </BootstrapInputGroup> | ||
| </div> | ||
| <div class="col-12 col-sm-6"> | ||
| <Button OnClick="OnCreateSubscription" Text="订阅" IsDisabled="@(!OpcDaServer.IsConnected || _subscribed)"></Button> | ||
| <Button OnClick="OnCancelSubscription" Text="取消" IsDisabled="!_subscribed"></Button> | ||
| </div> | ||
| </div> | ||
| </DemoBlock> |
86 changes: 86 additions & 0 deletions
86
src/BootstrapBlazor.Server/Components/Samples/OpcDa.razor.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,86 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| using BootstrapBlazor.OpcDa; | ||
| using System.Globalization; | ||
|
|
||
| namespace BootstrapBlazor.Server.Components.Samples; | ||
|
|
||
| /// <summary> | ||
| /// OpcDa 示例 | ||
| /// </summary> | ||
| public partial class OpcDa : ComponentBase | ||
| { | ||
| [Inject] | ||
| [NotNull] | ||
| private IOpcDaServer? OpcDaServer { get; set; } | ||
|
|
||
| private string? _serverName = "opcda://localhost/Kepware.KEPServerEX.V6/Mock"; | ||
|
|
||
| private const string Tag1 = "Channel1.Device1.Tag1"; | ||
| private const string Tag2 = "Channel1.Device1.Tag2"; | ||
| private const string Tag3 = "Channel1.Device1.Tag3"; | ||
| private const string Tag4 = "Channel1.Device1.Tag4"; | ||
|
|
||
| private string? _tagValue1; | ||
| private string? _tagValue2; | ||
| private string? _tagValue3; | ||
| private string? _tagValue4; | ||
|
|
||
| private IOpcSubscription? _subscription; | ||
|
|
||
| private bool _subscribed; | ||
|
|
||
| private void OnConnect() | ||
| { | ||
| if (!string.IsNullOrEmpty(_serverName)) | ||
| { | ||
| OpcDaServer.Connect(_serverName); | ||
| } | ||
| } | ||
|
|
||
| private void OnDisConnect() | ||
| { | ||
| OnCancelSubscription(); | ||
| OpcDaServer.Disconnect(); | ||
| } | ||
|
|
||
| private void OnRead() | ||
| { | ||
| var values = OpcDaServer.Read(Tag1, Tag2); | ||
| _tagValue1 = values.ElementAt(0).Value?.ToString(); | ||
|
|
||
| var v = (int)values.ElementAt(1).Value! / 100d; | ||
| _tagValue2 = v.ToString(CultureInfo.InvariantCulture); | ||
| } | ||
|
|
||
| private void OnCreateSubscription() | ||
| { | ||
| _subscribed = true; | ||
| _subscription = OpcDaServer.CreateSubscription("Subscription1", 1000, true); | ||
| _subscription.DataChanged += UpdateValues; | ||
| _subscription.AddItems([Tag3, Tag4]); | ||
| } | ||
|
|
||
| private void OnCancelSubscription() | ||
| { | ||
| _subscribed = false; | ||
| if (_subscription != null) | ||
| { | ||
| _subscription.DataChanged -= UpdateValues; | ||
| OpcDaServer.CancelSubscription(_subscription); | ||
| } | ||
| } | ||
|
|
||
| private void UpdateValues(List<OpcReadItem> items) | ||
| { | ||
| _tagValue3 = items[0].Value?.ToString(); | ||
| var v = (int)items[1].Value! / 100d; | ||
| _tagValue4 = v.ToString(CultureInfo.InvariantCulture); | ||
|
|
||
| InvokeAsync(StateHasChanged); | ||
| } | ||
| } | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| using BootstrapBlazor.OpcDa; | ||
| using Longbow.Tasks.Services; | ||
| using Microsoft.AspNetCore.SignalR; | ||
| using Microsoft.Extensions.Options; | ||
|
|
@@ -45,6 +46,16 @@ public static IServiceCollection AddBootstrapBlazorServerService(this IServiceCo | |
| services.AddHostedService<MockCustomProtocolSocketServerService>(); | ||
| services.AddHostedService<MockDisconnectServerService>(); | ||
|
|
||
| if (OperatingSystem.IsWindows()) | ||
| { | ||
| services.AddOpcDaServer(); | ||
| } | ||
| else | ||
| { | ||
| // 增加 OpcDa 模拟服务(给 Linux 平台使用) | ||
| services.AddMockOpcDaServer(); | ||
| } | ||
|
|
||
| // 增加通用服务 | ||
| services.AddBootstrapBlazorServices(); | ||
|
|
||
|
|
||
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
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): Potential InvalidCastException if Value is not int.
Casting values.ElementAt(1).Value to int without checking its type can cause a runtime exception if the value is not an int. Use a type check or safe conversion to prevent this.