void ReadMeasurement(PolledMeasurement measurement) { OpcMetric metric = new OpcMetric(); metric.Measurement = (MeasurementDto)measurement; if (!l_session.Connected) { l_logger.ErrorFormat(string.Format("Server not connected. Cannot read path {0}.", measurement.Name)); throw new Exception(string.Format("Server not connected. Cannot read path {0}.", measurement.Name)); } var nodesToRead = OpcUaHelper.GetReadValueIdCollection(measurement.Path, l_session); DataValueCollection results; DiagnosticInfoCollection diag; l_session.Read( requestHeader: null, maxAge: 0, timestampsToReturn: TimestampsToReturn.Neither, nodesToRead: nodesToRead, results: out results, diagnosticInfos: out diag); var val = results[0]; metric.OpcValue = val.Value; metric.Opcstatus = val.StatusCode.ToString(); if (OpcHelper.QualifyMetric(ref metric, l_cache)) { l_cache[metric.Measurement.Name] = metric.OpcValue; l_metricCollection.Add(metric); } }
void InitializeMeasurements() { MonitoredMeasurements = new List <MonitoredMeasurement>(); PolledMeasurements = new List <PolledMeasurement>(); foreach (MeasurementConfig item in l_opcDaPluginConfig.Measurements) { Dictionary <string, string> tags = null; if (!item.Tags.Equals("")) { tags = item.Tags.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries) .Select(part => part.Split(':')) .ToDictionary(split => split[0], split => split[1]); } switch (item.CollectionType) { case "monitored": var monitoredpoint = new MonitoredMeasurement() { Name = item.Name, Path = item.Path, MonitorResolution = item.MonitorResolution > 0 ? item.MonitorResolution : 0, DeadbandAbsolute = item.DeadbandAbsolute > 0 ? item.DeadbandAbsolute : 0, DeadbandRelative = item.DeadbandRelative > 0 ? item.DeadbandRelative : 0, DataType = item.DataType, Tags = tags }; MonitoredMeasurements.Add(monitoredpoint); break; case "polled": var polledPoint = new PolledMeasurement() { Name = item.Name, Path = item.Path, DeadbandAbsolute = item.DeadbandAbsolute > 0 ? item.DeadbandAbsolute : 0, DeadbandRelative = item.DeadbandRelative > 0 ? item.DeadbandRelative : 0, PollInterval = item.PollInterval, DataType = item.DataType, Tags = tags }; PolledMeasurements.Add(polledPoint); break; default: //no collection type, log l_logger.DebugFormat("no collection type at: {0}", item.Name); break; } } }
void ReadMeasurement(PolledMeasurement measurement) { var item = new OpcDa.Item { ItemName = measurement.Path }; if (l_server == null || l_server.GetStatus().ServerState != OpcDa.serverState.running) { l_logger.ErrorFormat(string.Format("Server not connected. Cannot read path {0}.", measurement.Name)); throw new Exception(string.Format("Server not connected. Cannot read path {0}.", measurement.Name)); } var result = l_server.Read(new[] { item })[0]; if (result == null) { l_logger.Error("the server replied with an empty response!!!"); throw new Exception("the server replied with an empty response");//if any item cannot read, throw exeption } if (result.ResultID.ToString() != "S_OK") { l_logger.ErrorFormat(string.Format("Invalid response from the server. (Response Status: {0}, Opc Tag: {1})", result.ResultID, measurement.Path)); throw new Exception(string.Format("Invalid response from the server. (Response Status: {0}, Opc Tag: {1})", result.ResultID, measurement.Path)); } OpcMetric metric = new OpcMetric() { Measurement = measurement, Opcstatus = result.ResultID.ToString(), OpcValue = result.Value, Timestamp = result.Timestamp }; if (OpcHelper.QualifyMetric(ref metric, l_cache)) { l_cache[metric.Measurement.Name] = metric.OpcValue; l_metricCollection.Add(metric); } }