44// Maintainer: Argo Zhang([email protected] ) Website: https://www.blazor.zone 55
66using BootstrapBlazor . OpcDa ;
7- using System . Globalization ;
87
98namespace BootstrapBlazor . Server . Components . Samples ;
109
@@ -17,12 +16,10 @@ public partial class OpcDa : ComponentBase
1716 [ NotNull ]
1817 private IOpcDaServer ? OpcDaServer { get ; set ; }
1918
20- private string ? _serverName = "opcda://localhost/Kepware.KEPServerEX.V6/Mock " ;
19+ private string ? _serverName = "opcda://localhost/Kepware.KEPServerEX.V6" ;
2120
2221 private const string Tag1 = "Channel1.Device1.Tag1" ;
2322 private const string Tag2 = "Channel1.Device1.Tag2" ;
24- private const string Tag3 = "Channel1.Device1.Tag3" ;
25- private const string Tag4 = "Channel1.Device1.Tag4" ;
2623
2724 private string ? _tagValue1 ;
2825 private string ? _tagValue2 ;
@@ -49,19 +46,25 @@ private void OnDisConnect()
4946
5047 private void OnRead ( )
5148 {
52- var values = OpcDaServer . Read ( Tag1 , Tag2 ) ;
53- _tagValue1 = values . ElementAt ( 0 ) . Value ? . ToString ( ) ;
54-
55- var v = ( int ) values . ElementAt ( 1 ) . Value ! / 100d ;
56- _tagValue2 = v . ToString ( CultureInfo . InvariantCulture ) ;
49+ var items = OpcDaServer . Read ( Tag1 , Tag2 ) ;
50+ var value1 = items . FirstOrDefault ( i => i . Name == Tag1 ) . Value ;
51+ if ( value1 != null )
52+ {
53+ _tagValue1 = value1 . ToString ( ) ;
54+ }
55+ var value2 = items . FirstOrDefault ( i => i . Name == Tag2 ) . Value ;
56+ if ( value2 != null )
57+ {
58+ _tagValue2 = value2 . ToString ( ) ;
59+ }
5760 }
5861
5962 private void OnCreateSubscription ( )
6063 {
6164 _subscribed = true ;
6265 _subscription = OpcDaServer . CreateSubscription ( "Subscription1" , 1000 , true ) ;
6366 _subscription . DataChanged += UpdateValues ;
64- _subscription . AddItems ( [ Tag3 , Tag4 ] ) ;
67+ _subscription . AddItems ( [ Tag1 , Tag2 ] ) ;
6568 }
6669
6770 private void OnCancelSubscription ( )
@@ -76,9 +79,16 @@ private void OnCancelSubscription()
7679
7780 private void UpdateValues ( List < OpcReadItem > items )
7881 {
79- _tagValue3 = items [ 0 ] . Value ? . ToString ( ) ;
80- var v = ( int ) items [ 1 ] . Value ! / 100d ;
81- _tagValue4 = v . ToString ( CultureInfo . InvariantCulture ) ;
82+ var value1 = items . Find ( i => i . Name == Tag1 ) . Value ;
83+ if ( value1 != null )
84+ {
85+ _tagValue3 = value1 . ToString ( ) ;
86+ }
87+ var value2 = items . Find ( i => i . Name == Tag2 ) . Value ;
88+ if ( value2 != null )
89+ {
90+ _tagValue4 = value2 . ToString ( ) ;
91+ }
8292
8393 InvokeAsync ( StateHasChanged ) ;
8494 }
@@ -88,12 +98,12 @@ private void UpdateValues(List<OpcReadItem> items)
8898 private void OnBrowse ( )
8999 {
90100 var elements = OpcDaServer . Browse ( "" , new OpcBrowseFilters ( ) , out _ ) ;
91- _roots = elements . Select ( element => new TreeViewItem < OpcBrowseElement > ( element )
101+ _roots = [ .. elements . Select ( element => new TreeViewItem < OpcBrowseElement > ( element )
92102 {
93103 Text = element . Name ,
94104 HasChildren = element . HasChildren ,
95105 Icon = "fa-solid fa-fw fa-cubes"
96- } ) . ToList ( ) ;
106+ } ) ] ;
97107 }
98108
99109 private Task < IEnumerable < TreeViewItem < OpcBrowseElement > > > OnExpandNodeAsync ( TreeViewItem < OpcBrowseElement > element )
0 commit comments