示例#1
0
        public static bool getAttributes(object item, List <XmlSchemaAttribute> attributes)
        {
            if (item is XmlSchemaComplexType)
            {
                XmlSchemaComplexType complexType = (XmlSchemaComplexType)item;
                foreach (object entry in complexType.AttributeUses)
                {
                    object o = null;
                    if (entry is System.Collections.DictionaryEntry)
                    {
                        System.Collections.DictionaryEntry de = (System.Collections.DictionaryEntry)entry;
                        object key   = de.Key;
                        object value = de.Value;
                        o = value;
                    }
                    if (o is XmlSchemaAttribute)
                    {
                        attributes.Add((XmlSchemaAttribute)o);
                    }
                    else if (o is XmlSchemaAttributeGroupRef)
                    {
                        XmlSchemaAttributeGroupRef groupRef = (XmlSchemaAttributeGroupRef)o;
                        XmlQualifiedName           qname    = groupRef.RefName;
                        if (qname != null)
                        {
                            XmlSchemaAttributeGroup group = null;
                            if (SchemaManager.GetAttributeGroup(qname.Namespace, qname.Name, out group))
                            {
                                foreach (object oo in group.Attributes)
                                {
                                    if (oo is XmlSchemaAttribute)
                                    {
                                        attributes.Add((XmlSchemaAttribute)oo);
                                    }
                                }
                            }
                        }
                    }
                }

                if (complexType.ContentModel != null && complexType.ContentModel.Content is XmlSchemaComplexContentExtension)
                {
                    XmlSchemaComplexContentExtension ext = (XmlSchemaComplexContentExtension)complexType.ContentModel.Content;
                    XmlSchemaComplexType             baseType;
                    if (ext.BaseTypeName != null && SchemaManager.GetComplexType(ext.BaseTypeName.Namespace, ext.BaseTypeName.Name, out baseType))
                    {
                        getAttributes(baseType, attributes);
                    }
                }
            }

            return(attributes.Count > 0);
        }
示例#2
0
        private void InstrumentControl_Validating(object sender, CancelEventArgs e)
        {
            if (!String.IsNullOrEmpty(SchemaTypeName) && !String.IsNullOrEmpty(TargetNamespace))
            {
                XmlSchemaComplexType complexType;
                if (SchemaManager.GetComplexType(TargetNamespace, SchemaTypeName, out complexType))
                {
                }
            }

            errorProvider1.SetError(this, "");
            if (InstrumentDescription == null)
            {
                errorProvider1.SetError(this, "You cannot save an empty Instrument");
                e.Cancel = true;
            }
            //else if (InstrumentDescription.Interface == null)
            // {
            //     errorProvider1.SetError(this, Resources.errmsg_at_least_one_interface_item);
            //     e.Cancel = true;
            // }
        }
        public static void Save(IAtmlObject atmlObject, Capability capability)
        {
            string id  = atmlObject.GetAtmlId();
            var    dao = new EquipmentDAO();

            if (capability.SignalDescription != null)
            {
                List <Signal> signals = SignalManager.ExtractSignalsFromExtension(capability.SignalDescription);
                foreach (Signal signal in signals)
                {
                    string   signalName      = signal.name;
                    string   signalNameSpace = signal.GetSignalNameSpace();
                    string   capabilityName  = capability.name;
                    object[] items           = signal.Items;

                    foreach (object item in items)
                    {
                        var element = item as XmlElement;
                        if (element != null)
                        {
                            XmlSchemaComplexType complexType;
                            string    elementName = element.Name;
                            string    localName   = element.LocalName;
                            XmlSchema schema      = SchemaManager.GetSchema(element.NamespaceURI);
                            SchemaManager.GetComplexType(element.NamespaceURI, localName, out complexType);
                            var xmlSchemaObjects =
                                new Dictionary <string, XmlSchemaObject>();
                            SchemaManager.ExtractAttributes(complexType, xmlSchemaObjects);


                            foreach (XmlAttribute attribute in element.Attributes)
                            {
                                string propertyName = attribute.Name;
                                string value        = attribute.Value;
                                try
                                {
                                    XmlSchemaAttribute schemaAttribute;
                                    SchemaManager.FindAttribute(element.NamespaceURI, localName, propertyName,
                                                                out schemaAttribute);
                                    bool   isPhysicalType = false;
                                    string typeName       = "";
                                    if (schemaAttribute != null)
                                    {
                                        XmlSchemaSimpleType simpleType = schemaAttribute.AttributeSchemaType;
                                        XmlQualifiedName    qn         = schemaAttribute.SchemaTypeName;
                                        isPhysicalType = SchemaManager.IsPhysicalType(simpleType);
                                        if (!isPhysicalType)
                                        {
                                            isPhysicalType = SchemaManager.IsPhysicalType(qn);
                                        }
                                    }

                                    Physical physical;

                                    try
                                    {
                                        physical = new Physical(value);
                                    }
                                    catch (Exception)
                                    {
                                        continue;
                                    }

                                    RangingInformation range = physical.GetMergedRange();
                                    //TODO: need SignalType, SignalNamespace
                                    if (range != null)
                                    {
                                        InstrumentCapabilitiesBean bean =
                                            dao.GetSignalCapabilityAttribute(Guid.Parse(id),
                                                                             capabilityName,
                                                                             signalName,
                                                                             propertyName);
                                        if (bean == null)
                                        {
                                            bean           = new InstrumentCapabilitiesBean();
                                            bean.DataState = BASEBean.eDataState.DS_ADD;
                                        }
                                        else
                                        {
                                            bean.DataState = atmlObject.IsDeleted()
                                                                 ? BASEBean.eDataState.DS_DELETE
                                                                 : BASEBean.eDataState.DS_EDIT;
                                        }
                                        //bean.signalType =;
                                        bean.capabilityName     = capabilityName;
                                        bean.signalNamespace    = signalNameSpace;
                                        bean.IncludeKeyOnInsert = true;
                                        bean.instrumentUuid     = id;
                                        bean.attribute          = propertyName;
                                        bean.signalName         = signalName;
                                        if (range.FromQuantity != null)
                                        {
                                            bean.lowValue = range.FromQuantity.NominalValue;
                                            bean.lowUnit  = range.FromQuantity.Unit.BaseUnitString;
                                        }
                                        if (range.ToQuantity != null)
                                        {
                                            bean.highValue = range.ToQuantity.NominalValue;
                                            bean.highUnit  = range.ToQuantity.Unit.BaseUnitString;
                                        }
                                        bean.save();
                                    }
                                }
                                catch (Exception)
                                {
                                    throw;
                                }
                            }
                        }
                        else
                        {
                            string       signalFunctionName = item.GetType().Name;
                            PropertyInfo piName             = item.GetType().GetProperty("name");
                            if (piName != null)
                            {
                                object value = piName.GetValue(item, null);
                                //if (value != null)
                                //   propertyName = value as string;
                            }
                            foreach (
                                PropertyInfo pi in
                                item.GetType()
                                .GetProperties(BindingFlags.Public | BindingFlags.Instance |
                                               BindingFlags.DeclaredOnly))
                            {
                                string propertyName = pi.Name;
                                object value        = pi.GetValue(item, null);
                                if (value != null)
                                {
                                    try
                                    {
                                        //TODO: need SignalType, SignalNamespace
                                        if (
                                            !SchemaManager.IsPhysicalType("urn:IEEE-1641:2010:STDBSC",
                                                                          signalFunctionName, propertyName))
                                        {
                                            continue;
                                        }
                                        var physicalValue = new Physical(value.ToString());
                                        Console.WriteLine(physicalValue.ToString());
                                        RangingInformation         range = physicalValue.GetMergedRange();
                                        InstrumentCapabilitiesBean bean  =
                                            dao.GetSignalCapabilityAttribute(Guid.Parse(id),
                                                                             capabilityName,
                                                                             signalName,
                                                                             pi.Name);
                                        if (bean == null)
                                        {
                                            bean           = new InstrumentCapabilitiesBean();
                                            bean.DataState = BASEBean.eDataState.DS_ADD;
                                        }
                                        else
                                        {
                                            bean.DataState = atmlObject.IsDeleted()
                                                                 ? BASEBean.eDataState.DS_DELETE
                                                                 : BASEBean.eDataState.DS_EDIT;
                                        }
                                        //bean.signalType =;
                                        bean.capabilityName     = capabilityName;
                                        bean.signalNamespace    = signalNameSpace;
                                        bean.IncludeKeyOnInsert = true;
                                        bean.instrumentUuid     = id;
                                        bean.attribute          = pi.Name;
                                        bean.signalName         = signalName;
                                        if (range != null)
                                        {
                                            if (range.FromQuantity != null)
                                            {
                                                bean.lowValue = range.FromQuantity.NominalValue;
                                                bean.lowUnit  = range.FromQuantity.Unit.BaseUnitString;
                                            }
                                            if (range.ToQuantity != null)
                                            {
                                                bean.highValue = range.ToQuantity.NominalValue;
                                                bean.highUnit  = range.ToQuantity.Unit.BaseUnitString;
                                            }
                                        }
                                        bean.save();
                                    }
                                    catch (Exception err)
                                    {
                                        LogManager.Error(err,
                                                         "Error Saving Capability [{0}] - Item:{1}, Value:{2}",
                                                         capability.name,
                                                         item.GetType().Name,
                                                         value
                                                         );
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
示例#4
0
        private void signalComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                HourGlass.Enabled = true;
                SignalModel signalModel = awbDropListTree.SelectedSignalModel;
                if (signalModel != null)
                {
                    var signal = signalModel.Signal;  //signalComboBox.SelectedItem as dbSignal;
                    if (signal != null)
                    {
                        //---------------------------------------------------//
                        //--- Find "name" in data grid and set it's value ---//
                        //---------------------------------------------------//
                        foreach (DataGridViewRow row in signalAttributes.Rows)
                        {
                            if (row.IsNewRow)
                            {
                                continue;
                            }

                            var name  = row.Cells[0].Value as string;
                            var type  = row.Cells[1].Value as string;
                            var value = row.Cells[2].Value as string;
                            if ("type".Equals(name))
                            {
                                row.Cells[1].Value = edtName.Text;
                                break;
                            }
                        }
                        signalAttributes.Rows.Clear();

                        if (string.IsNullOrEmpty(edtName.Text))
                        {
                            edtName.Text = signal.name;
                        }
                        var      dao        = new SignalDAO();
                        dbSignal dataSignal = dao.getSignal(signal.name, signalModel.SignalNameSpace);

                        string xmlns = signalModel.SignalNameSpace;
                        XmlSchemaComplexType complexType;
                        XmlSchemaElement     element = null;
                        SchemaManager.GetComplexType(xmlns, signal.name, out complexType);
                        if (complexType == null)
                        {
                            SchemaManager.GetElement(xmlns, signal.name, out element);
                        }
                        if (complexType != null || element != null)
                        {
                            signalAttributes.Rows.Clear();
                            List <XmlSchemaAttribute> schemaAttributes = complexType != null
                                                                            ? SchemaManager.GetAttributes(complexType)
                                                                            : SchemaManager.GetAttributes(element);

                            List <dbSignalAttribute> dbAttributes = dao.getAllSignalAttributes(signal.name, signalModel.SignalNameSpace);
                            var foundAttributes = new Dictionary <string, dbSignalAttribute>();
                            foreach (dbSignalAttribute attribute in dbAttributes)
                            {
                                foundAttributes.Add(attribute.attributeName, attribute);
                                object value = null;
                                try
                                {
                                    if (_signalFunctionType != null)
                                    {
                                        PropertyInfo pi =
                                            _signalFunctionType.GetType().GetProperty(attribute.attributeName);
                                        if (pi != null)
                                        {
                                            value = pi.GetValue(_signalFunctionType, null);
                                        }
                                    }
                                }
                                catch (Exception err)
                                {
                                    LogManager.Error(err);
                                }
                                int idx =
                                    signalAttributes.Rows.Add(new[] { attribute.attributeName, attribute.type, value });
                                signalAttributes.Rows[idx].Tag = attribute;
                            }

                            //-----------------------------------------------------------------------------//
                            //--- Check the database for each of the attributes found in the schema. If ---//
                            //--- the attribute does not exist in the database the add it.              ---//
                            //-----------------------------------------------------------------------------//
                            signalAttributes.Rows.Clear();
                            foreach (XmlSchemaAttribute attribute in schemaAttributes)
                            {
                                string name = attribute.Name;
                                if (!foundAttributes.ContainsKey(name))
                                {
                                    var dbSignalAttribute = new dbSignalAttribute();
                                    dbSignalAttribute.signalId      = dataSignal.signalId;
                                    dbSignalAttribute.attributeName = name;
                                    dbSignalAttribute.defaultValue  = attribute.DefaultValue;
                                    dbSignalAttribute.fixedValue    = attribute.FixedValue;
                                    if (attribute.AttributeSchemaType != null)
                                    {
                                        dbSignalAttribute.type = attribute.AttributeSchemaType.Name;
                                    }
                                    dbSignalAttribute.DataState = BASEBean.eDataState.DS_ADD;
                                    dbSignalAttribute.save();
                                    int idx = signalAttributes.Rows.Add(new[] { name, dbSignalAttribute.type, null });
                                    signalAttributes.Rows[idx].Tag = attribute;
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                HourGlass.Enabled = false;
            }
        }