@@ -16,7 +16,7 @@ namespace BootstrapBlazor.OpcDa;
1616class OpcServer : IOpcServer
1717{
1818 private Opc . Da . Server ? _server = null ;
19- private readonly ConcurrentDictionary < string , HashSet < OpcItem > > _valuesCache = [ ] ;
19+ private readonly ConcurrentDictionary < string , HashSet < OpcReadItem > > _valuesCache = [ ] ;
2020
2121 /// <summary>
2222 /// 获得 OPC Server 名称
@@ -101,12 +101,28 @@ public void CancelSubscription(ISubscription subscription)
101101 /// </summary>
102102 /// <param name="items"></param>
103103 /// <returns></returns>
104- /// <exception cref="InvalidOperationException"></exception>
105- public HashSet < OpcItem > Read ( params List < string > items )
104+ public HashSet < OpcReadItem > Read ( params HashSet < string > items )
106105 {
107106 var server = GetOpcServer ( ) ;
108107 var results = server . Read ( [ .. items . Select ( i => new Item ( ) { ItemName = i } ) ] ) ;
109- return results . Select ( i => new OpcItem ( i . ItemName , i . Quality . ToQuality ( ) , i . Timestamp , i . Value ) ) . ToHashSet ( OpcItemEqualityComparer . Default ) ;
108+ return results . Select ( i => new OpcReadItem ( i . ItemName , i . Quality . ToQuality ( ) , i . Timestamp , i . Value ) ) . ToHashSet ( OpcItemEqualityComparer < OpcReadItem > . Default ) ;
109+ }
110+
111+ /// <summary>
112+ /// 读取指定 Item 值方法
113+ /// </summary>
114+ /// <param name="items"></param>
115+ /// <returns></returns>
116+ public HashSet < OpcWriteItem > Write ( params HashSet < OpcWriteItem > items )
117+ {
118+ var server = GetOpcServer ( ) ;
119+ var results = server . Write ( [ .. items . Select ( i => new ItemValue ( ) { ItemName = i . Name , Value = i . Value } ) ] ) ;
120+
121+ return items . Select ( i =>
122+ {
123+ var item = results . FirstOrDefault ( v => v . ItemName == i . Name ) ;
124+ return new OpcWriteItem ( i . Name , i . Value ) { Result = item != null && item . ResultID == ResultID . S_OK } ;
125+ } ) . ToHashSet ( OpcItemEqualityComparer < OpcWriteItem > . Default ) ;
110126 }
111127
112128 private Opc . Da . Server GetOpcServer ( )
0 commit comments