/// <summary> /// Reads an item property from an XML stream. /// </summary> private void ReadProperty(XmlTextReader reader) { // look up the property id. PropertyDescription property = null; if (reader.MoveToAttribute(PROPERTY_ID)) { try { property = PropertyDescription.Find(new PropertyID(((int)System.Convert.ToInt32(reader.Value)))); } catch { property = null; } } // ignore invalid properties. if (property == null) { return; } // read value. object value = ReadValue(reader, property.Type); switch (property.ID.Code) { case QUALITY: { m_quality = (Quality)value; break; } case TIMESTAMP: { m_timestamp = (DateTime)value; break; } case ACCESSRIGHTS: { m_accessRights = (accessRights)value; break; } case SCANRATE: { m_scanRate = (float)value; break; } case EUTYPE: { m_euType = (euType)((int)value + 1); break; } case EUINFO: { m_euInfo = (string[])value; break; } case HIGHEU: { m_maxValue = (double)value; break; } case LOWEU: { m_minValue = (double)value; break; } default: { m_properties[property.ID] = value; break; } } }
private void Browse(ItemIdentifier itemID, OPCBROWSETYPE dwBrowseFilterType, string szFilterCriteria, short vtDataTypeFilter, int dwAccessRightsFilter, ArrayList hits) { BrowseFilters filters = new BrowseFilters { MaxElementsReturned = 0, BrowseFilter = browseFilter.all, ElementNameFilter = (dwBrowseFilterType != OPCBROWSETYPE.OPC_FLAT) ? szFilterCriteria : "", VendorFilter = null, ReturnAllProperties = false }; filters.PropertyIDs = new PropertyID[] { Property.DATATYPE, Property.ACCESSRIGHTS }; filters.ReturnPropertyValues = true; BrowseElement[] elementArray = null; try { Opc.Da.BrowsePosition position = null; elementArray = this.m_server.Browse(itemID, filters, out position); if (position != null) { position.Dispose(); position = null; } } catch { throw new ExternalException("BrowseOPCItemIDs", -2147467259); } foreach (BrowseElement element in elementArray) { if (dwBrowseFilterType == OPCBROWSETYPE.OPC_FLAT) { if (element.HasChildren) { this.Browse(new ItemIdentifier(element.ItemName), dwBrowseFilterType, szFilterCriteria, vtDataTypeFilter, dwAccessRightsFilter, hits); } } else { if (dwBrowseFilterType == OPCBROWSETYPE.OPC_BRANCH) { if (element.HasChildren) { goto Label_00F9; } continue; } if ((dwBrowseFilterType == OPCBROWSETYPE.OPC_LEAF) && element.HasChildren) { continue; } } Label_00F9: if (element.IsItem) { if (vtDataTypeFilter != 0) { short type = (short)OpcCom.Interop.GetType((System.Type)element.Properties[0].Value); if (type != vtDataTypeFilter) { continue; } } if (dwAccessRightsFilter != 0) { accessRights rights = (accessRights)element.Properties[1].Value; if (((dwAccessRightsFilter == 1) && (rights == accessRights.writable)) || ((dwAccessRightsFilter == 2) && (rights == accessRights.readable))) { continue; } } } if (dwBrowseFilterType != OPCBROWSETYPE.OPC_FLAT) { hits.Add(element.Name); } else if (element.IsItem && ((szFilterCriteria.Length == 0) || Opc.Convert.Match(element.ItemName, szFilterCriteria, true))) { hits.Add(element.ItemName); } } }
/// <summary> /// Checks if the specified property is valid for the specifed access type. /// </summary> private ResultID ValidatePropertyID(PropertyID propertyID, accessRights accessRequired) { switch (propertyID.Code) { // check access rights for value properties. case VALUE: case QUALITY: case TIMESTAMP: { if (m_accessRights != accessRights.readWritable && m_accessRights != accessRequired) { switch (accessRequired) { case accessRights.readable: { return(ResultID.Da.E_WRITEONLY); } case accessRights.writable: { return(ResultID.Da.E_READONLY); } } } break; } // no checks required for intrinsic properties. case DATATYPE: case ACCESSRIGHTS: case SCANRATE: case EUTYPE: case EUINFO: { break; } // eu limits only valid for analog items. case HIGHEU: case LOWEU: { if (m_euType != euType.analog) { return(ResultID.Da.E_INVALID_PID); } break; } // data type limits properties are always read only. case MINIMUM_VALUE: case MAXIMUM_VALUE: case VALUE_PRECISION: { if (accessRequired == accessRights.writable) { return(ResultID.Da.E_READONLY); } break; } // lookup any addition property. default: { if (!m_properties.Contains(propertyID)) { return(ResultID.Da.E_INVALID_PID); } break; } } // property is valid. return(ResultID.S_OK); }
/// <summary> /// Checks if the specified property is valid for the specifed access type. /// </summary> private ResultID ValidatePropertyID(PropertyID propertyID, accessRights accessRequired) { switch (propertyID.Code) { // check access rights for value properties. case VALUE: case QUALITY: case TIMESTAMP: { if (m_accessRights != accessRights.readWritable && m_accessRights != accessRequired) { switch (accessRequired) { case accessRights.readable: { return ResultID.Da.E_WRITEONLY; } case accessRights.writable: { return ResultID.Da.E_READONLY; } } } break; } // no checks required for intrinsic properties. case DATATYPE: case ACCESSRIGHTS: case SCANRATE: case EUTYPE: case EUINFO: { break; } // eu limits only valid for analog items. case HIGHEU: case LOWEU: { if (m_euType != euType.analog) { return ResultID.Da.E_INVALID_PID; } break; } // data type limits properties are always read only. case MINIMUM_VALUE: case MAXIMUM_VALUE: case VALUE_PRECISION: { if (accessRequired == accessRights.writable) { return ResultID.Da.E_READONLY; } break; } // lookup any addition property. default: { if (!m_properties.Contains(propertyID)) { return ResultID.Da.E_INVALID_PID; } break; } } // property is valid. return ResultID.S_OK; }
/// <summary> /// Reads an item property from an XML stream. /// </summary> private void ReadProperty(XmlTextReader reader) { // look up the property id. PropertyDescription property = null; if (reader.MoveToAttribute(PROPERTY_ID)) { try { property = PropertyDescription.Find(new PropertyID(((int)System.Convert.ToInt32(reader.Value)))); } catch { property = null; } } // ignore invalid properties. if (property == null) { return; } // read value. object value = ReadValue(reader, property.Type); switch (property.ID.Code) { case QUALITY: { m_quality = (Quality)value; break; } case TIMESTAMP: { m_timestamp = (DateTime)value; break; } case ACCESSRIGHTS: { m_accessRights = (accessRights)value; break; } case SCANRATE: { m_scanRate = (float)value; break; } case EUTYPE: { m_euType = (euType)((int)value+1); break; } case EUINFO: { m_euInfo = (string[])value; break; } case HIGHEU: { m_maxValue = (double)value; break; } case LOWEU: { m_minValue = (double)value; break; } default: { m_properties[property.ID] = value; break; } } }
/// <summary> /// Writes the value of the specified item property. /// </summary> public Opc.IdentifiedResult Write( PropertyID propertyID, Opc.Da.ItemValue value) { // initialize result and validate property. IdentifiedResult result = new IdentifiedResult(); result.ItemName = m_itemID; result.ItemPath = null; result.ResultID = ValidatePropertyID(propertyID, accessRights.writable); result.DiagnosticInfo = null; // handle value writes. if (propertyID == Property.VALUE) { // copy value. m_value = Opc.Convert.Clone(value.Value); // update quality if specified. if (value.QualitySpecified) { m_quality = value.Quality; } // update timestamp if specified. if (value.TimestampSpecified) { m_timestamp = value.Timestamp; } // return results. return result; } // lookup property description. PropertyDescription property = PropertyDescription.Find(propertyID); if (property == null) { result.ResultID = ResultID.Da.E_INVALID_PID; return result; } // check datatype. if (!property.Type.IsInstanceOfType(value.Value)) { result.ResultID = ResultID.Da.E_BADTYPE; return result; } // write non-value switch (propertyID.Code) { // standard properties. case DATATYPE: { m_datatype = (System.Type)value.Value; return result; } case QUALITY: { m_quality = (Quality)value.Value; return result; } case TIMESTAMP: { m_timestamp = (DateTime)value.Value; return result; } case ACCESSRIGHTS: { m_accessRights = (accessRights)value.Value; return result; } case SCANRATE: { m_scanRate = (float)value.Value; return result; } case EUTYPE: { m_euType = (euType)value.Value; return result; } case EUINFO: { m_euInfo = (string[])Opc.Convert.Clone(value.Value); return result; } case HIGHEU: { m_maxValue = (double)value.Value; return result; } case LOWEU: { m_minValue = (double)value.Value; return result; } // other defined properties. default: { if (!m_properties.Contains(propertyID)) { result.ResultID = ResultID.Da.E_INVALID_PID; return result; } m_properties[propertyID] = Opc.Convert.Clone(value.Value); break; } } // write complete. return result; }