示例#1
0
        public static JObject ExportSMtoJson(AdminShell.Submodel sm)
        {
            JObject exportData = new JObject();

            try
            {
                JObject TDJson = new JObject();
                if (sm.qualifiers != null)
                {
                    foreach (AdminShell.Qualifier smQualifier in sm.qualifiers)
                    {
                        TDJson[smQualifier.type] = smQualifier.value.ToString();
                    }
                }

                // description
                if (sm.description != null)
                {
                    AdminShell.ListOfLangStr tdDescription = sm.description.langString;
                    if (tdDescription.Count != 1)
                    {
                        TDJson["description"] = tdDescription[0].str;
                        int     index        = 1;
                        JObject descriptions = new JObject();
                        for (index = 1; index < tdDescription.Count; index++)
                        {
                            AdminShell.LangStr desc = tdDescription[index];
                            descriptions[desc.lang] = desc.str;
                        }
                        TDJson["descriptions"] = descriptions;
                    }
                    else
                    {
                        TDJson["description"] = tdDescription[0].str;
                    }
                }
                //version
                if (sm.administration != null)
                {
                    JObject versionInfo           = new JObject();
                    AdminShell.Administration adm = sm.administration;
                    if (adm.version != "")
                    {
                        versionInfo["instance"] = adm.version;
                    }
                    if (adm.revision != "")
                    {
                        versionInfo["model"] = adm.version;
                    }
                    if (versionInfo.Count != 0)
                    {
                        TDJson["version"] = versionInfo;
                    }
                }
                // id
                TDJson["id"] = sm.identification.id;
                if (sm.submodelElements != null)
                {
                    foreach (AdminShell.SubmodelElementWrapper tdElementWrapper in sm.submodelElements)
                    {
                        AdminShell.SubmodelElement tdElement = tdElementWrapper.submodelElement;
                        if (tdElement.idShort == "@type")
                        {
                            List <object> typeList = new List <object>();
                            foreach (AdminShell.Qualifier _typeQual in tdElement.qualifiers)
                            {
                                typeList.Add((_typeQual.value));
                            }
                            TDJson["@type"] = JToken.FromObject(typeList);
                        }
                        if (tdElement.idShort == "titles")
                        {
                            JObject _titlesJObject = new JObject();
                            AdminShell.MultiLanguageProperty mlp     = new AdminShell.MultiLanguageProperty(tdElement);
                            AdminShell.LangStringSet         _titles = new AdminShell.LangStringSet(mlp.value);
                            foreach (AdminShell.LangStr _title in _titles.langString)
                            {
                                _titlesJObject[_title.lang] = _title.str;
                            }
                            TDJson["titles"] = _titlesJObject;
                        }
                        if (tdElement.idShort == "@context")
                        {
                            List <object> contextList  = new List <object>();
                            JObject       _conSemantic = new JObject();
                            foreach (AdminShell.Qualifier _con in tdElement.qualifiers)
                            {
                                if (_con.type == "@context")
                                {
                                    contextList.Add((_con.value));
                                }
                                else
                                {
                                    _conSemantic[_con.type] = _con.value;
                                }
                            }
                            if (_conSemantic.Count != 0)
                            {
                                contextList.Add(_conSemantic);
                            }
                            TDJson["@context"] = JToken.FromObject(contextList);
                        }
                        if (tdElement.idShort == "properties")
                        {
                            TDJson["properties"] = createTDProperties(tdElement);
                        }
                        else if (tdElement.idShort == "actions")
                        {
                            TDJson["actions"] = createTDActions(tdElement);
                        }
                        else if (tdElement.idShort == "events")
                        {
                            TDJson["events"] = createTDEvents(tdElement);
                        }
                        else if (tdElement.idShort == "links")
                        {
                            TDJson["links"] = createTDLinks(tdElement)["links"];
                        }
                        else if (tdElement.idShort == "forms")
                        {
                            TDJson["forms"] = createForms(tdElement)["forms"];
                        }
                        else if (tdElement.idShort == "security")
                        {
                            TDJson["security"] = createTDSecurity(tdElement)["security"];
                        }
                        else if (tdElement.idShort == "securityDefinitions")
                        {
                            TDJson["securityDefinitions"] = createTDSecurityDefinitions(tdElement);
                        }
                        else if (tdElement.idShort == "profile")
                        {
                            TDJson["profile"] = createTDProfile(tdElement);
                        }
                        else if (tdElement.idShort == "schemaDefinitions")
                        {
                            TDJson["schemaDefinitions"] = createTDSchemaDefinitions(tdElement);
                        }
                    }
                }
                exportData["status"] = "success";
                exportData["data"]   = TDJson;
            }
            catch (Exception ex)
            {
                exportData["status"] = "error";
                exportData["data"]   = ex.ToString();
            }
            return(exportData);
        }
示例#2
0
        public static JObject createDataSchema(AdminShell.SubmodelElement sem)
        {
            JObject semJObject = new JObject();

            AdminShell.SubmodelElementCollection _tempCollection = new AdminShell.SubmodelElementCollection(sem);
            string dschemaType = "";

            foreach (AdminShell.SubmodelElementWrapper _tempChild in _tempCollection.EnumerateChildren())
            {
                AdminShell.SubmodelElement dsElement = _tempChild.submodelElement;
                if (dsElement.idShort == "titles")
                {
                    JObject _titlesJObject = new JObject();
                    AdminShell.MultiLanguageProperty mlp     = new AdminShell.MultiLanguageProperty(dsElement);
                    AdminShell.LangStringSet         _titles = new AdminShell.LangStringSet(mlp.value);
                    foreach (AdminShell.LangStr _title in _titles.langString)
                    {
                        _titlesJObject[_title.lang] = _title.str;
                    }
                    semJObject["titles"] = _titlesJObject;
                }
                if (dsElement.idShort == "oneOf")
                {
                    List <JObject> oneOfJObjects = new List <JObject>();
                    AdminShell.SubmodelElementCollection _enumCOllection =
                        new AdminShell.SubmodelElementCollection(dsElement);
                    foreach (AdminShell.SubmodelElementWrapper _temponeOf in _enumCOllection.EnumerateChildren())
                    {
                        AdminShell.SubmodelElement _oneOf = _temponeOf.submodelElement;
                        oneOfJObjects.Add(createDataSchema(_oneOf));
                    }
                    semJObject["oneOf"] = JToken.FromObject(oneOfJObjects);
                }
                if (dsElement.idShort == "enum")
                {
                    semJObject["enum"] = JToken.FromObject(enumELement(dsElement.qualifiers));
                }
            }
            if (sem.description != null)
            {
                AdminShell.ListOfLangStr tdDescription = sem.description.langString;
                if (tdDescription.Count != 1)
                {
                    semJObject["description"] = tdDescription[0].str;
                    int     index        = 1;
                    JObject descriptions = new JObject();
                    for (index = 1; index < tdDescription.Count; index++)
                    {
                        AdminShell.LangStr desc = tdDescription[index];
                        descriptions[desc.lang] = desc.str;
                    }
                    semJObject["descriptions"] = JToken.FromObject(descriptions);
                }
                else
                {
                    semJObject["description"] = tdDescription[0].str;
                }
            }
            foreach (AdminShell.Qualifier smQualifier in sem.qualifiers)
            {
                if (smQualifier.type == "readOnly" || smQualifier.type == "writeOnly")
                {
                    semJObject[smQualifier.type] = Convert.ToBoolean(smQualifier.value);
                }
                else if (smQualifier.type == "minItems" || smQualifier.type == "maxItems" ||
                         smQualifier.type == "minLength" || smQualifier.type == "maxLength")
                {
                    semJObject[smQualifier.type] = Convert.ToUInt32(smQualifier.value);
                }
                else if (smQualifier.type == "data1.type" || smQualifier.type == "type")
                {
                    if (smQualifier.type == "type")
                    {
                        semJObject[smQualifier.type] = smQualifier.value;
                    }
                    if (smQualifier.type == "data1.type")
                    {
                        semJObject["data1"] = JToken.FromObject(new JObject {
                            ["type"] = smQualifier.value
                        });
                    }
                    dschemaType = smQualifier.value;
                }
                else
                {
                    semJObject[smQualifier.type] = smQualifier.value;
                }
            }
            if (dschemaType == "array")
            {
                JObject arrayObject = createArraySchema(sem);
                if (arrayObject.ContainsKey("items"))
                {
                    semJObject["items"] = arrayObject["items"];
                }
            }
            else if (dschemaType == "object")
            {
                JObject objectSchemaJObject = createObjectSchema(sem);
                if (objectSchemaJObject.ContainsKey("properties"))
                {
                    semJObject["properties"] = JToken.FromObject(objectSchemaJObject["properties"]);
                }
                if (objectSchemaJObject.ContainsKey("required"))
                {
                    semJObject["required"] = JToken.FromObject(objectSchemaJObject["required"]);
                }
            }
            else if (dschemaType == "integer")
            {
                List <string> integerSchema = new List <string> {
                    "minimum", "exclusiveMinimum", "maximum",
                    "exclusiveMaximum", "multipleOf"
                };
                foreach (string elem in integerSchema)
                {
                    foreach (AdminShell.Qualifier semQual in sem.qualifiers)
                    {
                        if (elem == semQual.type)
                        {
                            semJObject[semQual.type] = (int)Convert.ToDouble(semQual.value);
                        }
                    }
                }
            }
            else if (dschemaType == "number")
            {
                List <string> numberSchema = new List <string> {
                    "minimum", "exclusiveMinimum", "maximum",
                    "exclusiveMaximum", "multipleOf"
                };
                foreach (string elem in numberSchema)
                {
                    foreach (AdminShell.Qualifier semQual in sem.qualifiers)
                    {
                        if (elem == semQual.type)
                        {
                            semJObject[semQual.type] = Convert.ToDecimal(semQual.value.ToString());
                        }
                    }
                }
            }



            return(semJObject);
        }