dathlin / OpcUaHelper

一个通用的opc ua客户端类库,基于.net 4.6.1创建,基于官方opc ua基金会跨平台库创建,封装了节点读写,批量节点读写,引用读取,特性读取,历史数据读取,方法调用,节点订阅,批量订阅等操作。还提供了一个节点浏览器工具。
GNU Lesser General Public License v3.0
830 stars 383 forks source link

建议添加批量写异步操作 #17

Open wuyang26 opened 4 years ago

wuyang26 commented 4 years ago

基于网络的数据读写,异步操作是非常重要的,这里依照项目中的其他代码修改了一个类似的: public Task<List> WriteNodeAsync(string[] tags, object[] values) { WriteValueCollection valuesToWrite = new WriteValueCollection(); for (int i = 0; i < tags.Length; i++) { if (i < values.Length) { WriteValue valueToWrite = new WriteValue() { NodeId = new NodeId(tags[i]), AttributeId = Attributes.Value }; valueToWrite.Value.Value = values[i]; valueToWrite.Value.StatusCode = StatusCodes.Good; valueToWrite.Value.ServerTimestamp = DateTime.MinValue; valueToWrite.Value.SourceTimestamp = DateTime.MinValue; valuesToWrite.Add(valueToWrite); } }

        // Wrap the WriteAsync logic in a TaskCompletionSource, so we can use C# async/await syntax to call it:
        var taskCompletionSource = new TaskCompletionSource<List<bool>>();
        m_session.BeginWrite(
            requestHeader: null,
            nodesToWrite: valuesToWrite,
            callback: ar =>
            {
                var response = m_session.EndWrite(
                  result: ar,
                  results: out StatusCodeCollection results,
                  diagnosticInfos: out DiagnosticInfoCollection diag);

                try
                {
                    ClientBase.ValidateResponse(results, valuesToWrite);
                    ClientBase.ValidateDiagnosticInfos(diag, valuesToWrite);
                    List<bool> retList = new List<bool>();
                    foreach(var item in results)
                    {
                        retList.Add(StatusCode.IsGood(item));
                    }

                    taskCompletionSource.SetResult(retList);
                }
                catch (Exception ex)
                {
                    taskCompletionSource.TrySetException(ex);
                }
            },
            asyncState: null);
        return taskCompletionSource.Task;
    }
wuyang26 commented 4 years ago

上面粘贴可能有点问题: `public Task<List> WriteNodeAsync(string[] tags, object[] values) { WriteValueCollection valuesToWrite = new WriteValueCollection(); for (int i = 0; i < tags.Length; i++) { if (i < values.Length) { WriteValue valueToWrite = new WriteValue() { NodeId = new NodeId(tags[i]), AttributeId = Attributes.Value }; valueToWrite.Value.Value = values[i]; valueToWrite.Value.StatusCode = StatusCodes.Good; valueToWrite.Value.ServerTimestamp = DateTime.MinValue; valueToWrite.Value.SourceTimestamp = DateTime.MinValue; valuesToWrite.Add(valueToWrite); } }

        // Wrap the WriteAsync logic in a TaskCompletionSource, so we can use C# async/await syntax to call it:
        var taskCompletionSource = new TaskCompletionSource<List<bool>>();
        m_session.BeginWrite(
            requestHeader: null,
            nodesToWrite: valuesToWrite,
            callback: ar =>
            {
                var response = m_session.EndWrite(
                  result: ar,
                  results: out StatusCodeCollection results,
                  diagnosticInfos: out DiagnosticInfoCollection diag);

                try
                {
                    ClientBase.ValidateResponse(results, valuesToWrite);
                    ClientBase.ValidateDiagnosticInfos(diag, valuesToWrite);
                    List<bool> retList = new List<bool>();
                    foreach(var item in results)
                    {
                        retList.Add(StatusCode.IsGood(item));
                    }

                    taskCompletionSource.SetResult(retList);
                }
                catch (Exception ex)
                {
                    taskCompletionSource.TrySetException(ex);
                }
            },
            asyncState: null);
        return taskCompletionSource.Task;
    }`
wuyang26 commented 4 years ago

没办法:

Task<List>应该是Task<List<bool>>

dathlin commented 4 years ago

嗯,是个好的方式。