@@ -13,7 +13,7 @@ namespace BootstrapBlazor.OpcDa;
1313/// OPC Server 操作类
1414/// </summary>
1515[ SupportedOSPlatform ( "windows" ) ]
16- public partial class OpcServer : IDisposable
16+ class OpcServer : IOpcServer
1717{
1818 private Opc . Da . Server ? _server = null ;
1919 private readonly ConcurrentDictionary < string , HashSet < OpcItem > > _valuesCache = [ ] ;
@@ -69,53 +69,20 @@ public void Disconnect()
6969 /// 创建订阅方法
7070 /// </summary>
7171 /// <param name="name">订阅名称</param>
72- /// <param name="updateRate"></param>
73- /// <param name="active"></param>
74- /// <param name="items"></param>
72+ /// <param name="updateRate">更新频率 默认 1000 毫秒</param>
73+ /// <param name="active">是否激活 默认 true</param>
7574 /// <returns></returns>
76- public ISubscription CreateSubscription ( string name , int updateRate , bool active = true , Item [ ] ? items = null )
75+ public ISubscription CreateSubscription ( string name , int updateRate = 1000 , bool active = true )
7776 {
7877 var server = GetOpcServer ( ) ;
7978 var subscription = server . CreateSubscription ( new SubscriptionState
8079 {
8180 Name = name ,
82- Active = active ,
81+ Deadband = 0 ,
8382 UpdateRate = updateRate ,
84- Deadband = 0
83+ Active = active
8584 } ) ;
86-
87- if ( items is { Length : > 0 } )
88- {
89- subscription . AddItems ( items ) ;
90- subscription . DataChanged += ( subscriptionHandle , requestHandle , values ) =>
91- {
92- foreach ( var value in values )
93- {
94- _valuesCache . AddOrUpdate ( name , key => AddFactory ( value ) , ( key , v ) => UpdateFactory ( v , value ) ) ;
95- }
96- } ;
97- }
98- return subscription ;
99- }
100-
101- private static HashSet < OpcItem > AddFactory ( ItemValueResult value )
102- {
103- return new HashSet < OpcItem > ( OpcItemEqualityComparer . Default )
104- {
105- new ( value . ItemName , value . Quality . ToQuality ( ) , value . Timestamp , value . Value )
106- } ;
107- }
108-
109- private static HashSet < OpcItem > UpdateFactory ( HashSet < OpcItem > items , ItemValueResult value )
110- {
111- var item = new OpcItem ( value . ItemName , value . Quality . ToQuality ( ) , value . Timestamp , value . Value ) ;
112- if ( items . TryGetValue ( item , out var v ) )
113- {
114- item . LastValue = v . Value ;
115- }
116- items . Remove ( item ) ;
117- items . Add ( item ) ;
118- return items ;
85+ return subscription . ToOpcSubscription ( ) ;
11986 }
12087
12188 /// <summary>
@@ -126,7 +93,7 @@ private static HashSet<OpcItem> UpdateFactory(HashSet<OpcItem> items, ItemValueR
12693 public void CancelSubscription ( ISubscription subscription )
12794 {
12895 var server = GetOpcServer ( ) ;
129- server . CancelSubscription ( subscription ) ;
96+ server . CancelSubscription ( subscription . GetSubscription ( ) ) ;
13097 }
13198
13299 /// <summary>
0 commit comments