public void UpdateItemEuInfo(
ComDaGroup group,
IList<ComDaGroupItem> items)
{
// get the session to use for the operation.
Session session = m_session;
if (session == null)
{
throw ComUtils.CreateComException(ResultIds.E_FAIL);
}
// build list of properties that need to be read.
BrowsePathCollection browsePaths = new BrowsePathCollection();
for (int ii = 0; ii < items.Count; ii++)
{
ComDaGroupItem item = (ComDaGroupItem)items[ii];
// ignore invalid items or items which have already checked their EU type.
if (item == null || item.EuType >= 0)
{
continue;
}
BrowsePath browsePath = new BrowsePath();
browsePath.StartingNode = item.NodeId;
RelativePathElement element = new RelativePathElement();
element.ReferenceTypeId = ReferenceTypeIds.HasProperty;
element.IsInverse = false;
element.IncludeSubtypes = false;
element.TargetName = Opc.Ua.BrowseNames.EURange;
browsePath.RelativePath.Elements.Add(element);
browsePath.Handle = item;
browsePaths.Add(browsePath);
browsePath = new BrowsePath();
browsePath.StartingNode = item.NodeId;
element = new RelativePathElement();
element.ReferenceTypeId = ReferenceTypeIds.HasProperty;
element.IsInverse = false;
element.IncludeSubtypes = false;
element.TargetName = Opc.Ua.BrowseNames.EnumStrings;
browsePath.RelativePath.Elements.Add(element);
browsePath.Handle = item;
browsePaths.Add(browsePath);
}
// check if nothing to do.
if (browsePaths.Count == 0)
{
return;
}
// translate browse paths.
BrowsePathResultCollection results = null;
DiagnosticInfoCollection diagnosticInfos = null;
try
{
session.TranslateBrowsePathsToNodeIds(
null,
browsePaths,
out results,
out diagnosticInfos);
ClientBase.ValidateResponse(results, browsePaths);
ClientBase.ValidateDiagnosticInfos(diagnosticInfos, browsePaths);
}
catch (Exception)
{
for (int ii = 0; ii < browsePaths.Count; ii++)
{
ComDaGroupItem item = (ComDaGroupItem)browsePaths[ii].Handle;
item.EuType = 0;
}
return;
}
// build list of properties that need to be read.
ReadValueIdCollection propertiesToRead = new ReadValueIdCollection();
for (int ii = 0; ii < results.Count; ii++)
{
ComDaGroupItem item = (ComDaGroupItem)browsePaths[ii].Handle;
BrowsePathResult result = results[ii];
if (StatusCode.IsBad(result.StatusCode))
{
if (item.EuType < 0 && result.StatusCode == StatusCodes.BadNoMatch)
{
item.EuType = (int)OpcRcw.Da.OPCEUTYPE.OPC_NOENUM;
}
continue;
}
if (result.Targets.Count == 0 || result.Targets[0].TargetId.IsAbsolute)
{
if (item.EuType < 0)
{
item.EuType = (int)OpcRcw.Da.OPCEUTYPE.OPC_NOENUM;
}
continue;
}
ReadValueId propertyToRead = new ReadValueId();
propertyToRead.NodeId = (NodeId)result.Targets[0].TargetId;
propertyToRead.AttributeId = Attributes.Value;
propertyToRead.Handle = item;
propertiesToRead.Add(propertyToRead);
if (browsePaths[ii].RelativePath.Elements[0].TargetName.Name == Opc.Ua.BrowseNames.EURange)
{
item.EuType = (int)OpcRcw.Da.OPCEUTYPE.OPC_ANALOG;
}
else
{
item.EuType = (int)OpcRcw.Da.OPCEUTYPE.OPC_ENUMERATED;
}
}
// check if nothing to do.
if (propertiesToRead.Count == 0)
{
return;
}
// read attribute values from the server.
DataValueCollection values = null;
try
{
session.Read(
null,
0,
TimestampsToReturn.Neither,
propertiesToRead,
out values,
out diagnosticInfos);
ClientBase.ValidateResponse(values, propertiesToRead);
ClientBase.ValidateDiagnosticInfos(diagnosticInfos, propertiesToRead);
}
catch (Exception)
{
for (int ii = 0; ii < propertiesToRead.Count; ii++)
{
ComDaGroupItem item = (ComDaGroupItem)propertiesToRead[ii].Handle;
item.EuType = 0;
}
return;
}
// process results.
for (int ii = 0; ii < values.Count; ii++)
{
ComDaGroupItem item = (ComDaGroupItem)propertiesToRead[ii].Handle;
if (StatusCode.IsBad(values[ii].StatusCode))
{
item.EuType = 0;
continue;
}
if (item.EuType == (int)OpcRcw.Da.OPCEUTYPE.OPC_ANALOG)
{
Range range = (Range)values[ii].GetValue<Range>(null);
if (range == null)
{
item.EuType = 0;
continue;
}
item.EuInfo = new double[] { range.Low, range.High };
continue;
}
if (item.EuType == (int)OpcRcw.Da.OPCEUTYPE.OPC_ENUMERATED)
{
LocalizedText[] texts = (LocalizedText[])values[ii].GetValue<LocalizedText[]>(null);
if (texts == null)
{
item.EuType = 0;
continue;
}
string[] strings = new string[texts.Length];
for (int jj = 0; jj < strings.Length; jj++)
{
if (!LocalizedText.IsNullOrEmpty(texts[jj]))
{
strings[jj] = texts[jj].Text;
}
}
item.EuInfo = strings;
continue;
}
}
}