/// <summary> /// 写入数据 /// </summary> /// <param name="opcUaDataItem"></param> /// <param name="newValue"></param> /// <returns></returns> public async Task <OpcUaStatusCodes> Write(OpcUaDataItem opcUaDataItem, object newValue) { return(await Task.Run(() => { WriteValue valueToWrite = new WriteValue() { NodeId = new NodeId(opcUaDataItem.Name), AttributeId = Attributes.Value }; valueToWrite.Value.Value = Convert.ChangeType(newValue, opcUaDataItem.ValueType ?? typeof(object)); valueToWrite.Value.StatusCode = StatusCodes.Good; valueToWrite.Value.ServerTimestamp = DateTime.MinValue; valueToWrite.Value.SourceTimestamp = DateTime.MinValue; WriteValueCollection valuesToWrite = new WriteValueCollection { valueToWrite }; try { session.Write(null, valuesToWrite, out StatusCodeCollection results, out DiagnosticInfoCollection diagnosticInfos); ClientBase.ValidateResponse(results, valuesToWrite); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, valuesToWrite); opcUaDataItem.OpcUaStatusCodes = (OpcUaStatusCodes)results[0].Code; opcUaDataItem.OldValue = opcUaDataItem.NewValue; if (results[0].Code == 0) { opcUaDataItem.NewValue = newValue; } return (OpcUaStatusCodes)results[0].Code; } catch (ServiceResultException e) { OnErrorHappened?.Invoke(this, new OpcUaErrorEventArgs((OpcUaStatusCodes)e.StatusCode, $"写入数据时错误,{opcUaDataItem.ToString()}", e)); return OpcUaStatusCodes.BadServerHalted; } catch (Exception ex) { OnErrorHappened?.Invoke(this, new OpcUaErrorEventArgs(OpcUaStatusCodes.Uncertain, $"写入数据时发生未知错误,{opcUaDataItem.ToString()}", ex)); return OpcUaStatusCodes.Uncertain; } })); }
///// <summary> ///// 批量写入 ///// </summary> ///// <typeparam name="Tin"></typeparam> ///// <typeparam name="Tout"></typeparam> ///// <param name="writeParams"></param> ///// <returns></returns> //public async Task<OpcUaStatusCodes[]> Writes(IList<OpcUaDataItem> opcUaDataItems, IList<object> newValues) //{ // return await Task.Run(() => // { // //var writeNodes = writeParams as DeviceInputParamEntityBase[]; // //OpcUaDeviceOutParamEntity[] opcUaDeviceOutParamEntitys = new OpcUaDeviceOutParamEntity[writeNodes.Count()]; // if (Equals(opcUaDataItems, null) || Equals(opcUaDataItems, null)) // { // return new OpcUaStatusCodes[1]; // } // if (opcUaDataItems.Count != newValues.Count) // { // OnLogHappened?.Invoke(this, new OpcUaLogEventArgs($"写入节点和值的数量不匹配{opcUaDataItems.Count}/{ newValues.Count}")); // } // WriteValueCollection valuesToWrite = new WriteValueCollection(opcUaDataItems.Count()); // foreach (var writeNode in opcUaDataItems) // { // WriteValue valueToWrite = new WriteValue(); // valueToWrite.NodeId = new NodeId(writeNode.Name); // valueToWrite.AttributeId = Attributes.Value; // valueToWrite.Value.Value = Convert.ChangeType(writeNode.NewValue, writeNode.ValueType ?? typeof(object)); // valueToWrite.Value.StatusCode = StatusCodes.Good; // valueToWrite.Value.ServerTimestamp = DateTime.MinValue; // valueToWrite.Value.SourceTimestamp = DateTime.MinValue; // valuesToWrite.Add(valueToWrite); // } // try // { // session.Write(null, valuesToWrite, out StatusCodeCollection results, out DiagnosticInfoCollection diagnosticInfos); // ClientBase.ValidateResponse(results, valuesToWrite); // ClientBase.ValidateDiagnosticInfos(diagnosticInfos, valuesToWrite); // for (int i = 0; i < results.Count; i++) // { // opcUaDataItems[i].OpcUaStatusCodes = (OpcUaStatusCodes)results[i].Code; // //opcUaDataItem.Message = results[0].StatusCode.ToString(); // opcUaDataItems[i].OldValue = opcUaDataItems[i].NewValue; // if (results[i].Code == 0) // { // opcUaDataItems[i].NewValue = newValues[i]; // } // //return (OpcUaStatusCodes)results[0].Code; // //OpcUaDeviceOutParamEntity opcUaDeviceOutParamEntity = new OpcUaDeviceOutParamEntity(); // //opcUaDeviceOutParamEntity.NodeId = writeNodes[i].NodeId; // //opcUaDeviceOutParamEntity.Value = writeNodes[i].Value; // //opcUaDeviceOutParamEntity.ValueType = valuesToWrite[i].Value.WrappedValue.TypeInfo.BuiltInType.GetTypeCode().ToType(); // //opcUaDeviceOutParamEntity.StatusCode = results[i].Code; // //opcUaDeviceOutParamEntity.Message = OpcUaStatusCodes.GetBrowseName(results[i].Code); // //opcUaDeviceOutParamEntitys[i] = opcUaDeviceOutParamEntity; // } // //return null; // } // catch (ServiceResultException e) // { // OnErrorHappened?.Invoke(this, new OpcUaErrorEventArgs((OpcUaStatusCodes)e.StatusCode, $"批量读取数据时错误", e)); // return new OpcUaStatusCodes[1]; // } // catch (Exception ex) // { // OnErrorHappened?.Invoke(this, new OpcUaErrorEventArgs(OpcUaStatusCodes.Uncertain, $"批量读取数据时发生未知错误", ex)); // return new OpcUaStatusCodes[1]; // } // }); //} #endregion #region 读取数据 /// <summary> /// 读取数据 /// </summary> /// <param name="opcUaDataItem"></param> /// <returns></returns> public async Task <OpcUaDataItem> Read(OpcUaDataItem opcUaDataItem) { return(await Task.Run(() => { ReadValueId nodeToRead = new ReadValueId() { NodeId = new NodeId(opcUaDataItem.Name), AttributeId = Attributes.Value }; ReadValueIdCollection nodesToRead = new ReadValueIdCollection { nodeToRead }; try { session.Read(null, 0, TimestampsToReturn.Neither, nodesToRead, out DataValueCollection results, out DiagnosticInfoCollection diagnosticInfos); ClientBase.ValidateResponse(results, nodesToRead); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToRead); opcUaDataItem.OpcUaStatusCodes = (OpcUaStatusCodes)results[0].StatusCode.Code; //opcUaDataItem.Message = results[0].StatusCode.ToString(); opcUaDataItem.OldValue = opcUaDataItem.NewValue; opcUaDataItem.NewValue = results[0].Value; //opcUaDataItem.NodeId = readNode.NodeId; //opcUaDataItem.ValueType = results[0].WrappedValue.TypeInfo.BuiltInType.GetTypeCode().ToType(); return opcUaDataItem; } catch (ServiceResultException e) { OnErrorHappened?.Invoke(this, new OpcUaErrorEventArgs((OpcUaStatusCodes)e.StatusCode, $"读取数据时错误,{opcUaDataItem.ToString()}", e)); return null; } catch (Exception ex) { OnErrorHappened?.Invoke(this, new OpcUaErrorEventArgs(OpcUaStatusCodes.Uncertain, $"读取数据时发生未知错误,{opcUaDataItem.ToString()}", ex)); return null; } })); }
public OpcUaDataEventArgs(OpcUaStatusCodes opcUaStatusCodes, OpcUaDataItem opcUaDataItem) { this.OpcUaStatusCodes = opcUaStatusCodes; this.OpcUaDataItem = opcUaDataItem; }