public async Task <MethodResponse> OnConfigurationUpdate(MethodRequest methodRequest, object userContext)
        {
            if (_deviceManagementTask != null && !_deviceManagementTask.IsCompleted)
            {
                return(await Task.FromResult(BuildMethodRespose(new
                {
                    Message = "Device is busy"
                }, 409)));
            }

            try
            {
                var operation = new ConfigurationUpdate(methodRequest);
                _deviceManagementTask = operation.Run(Transport);

                return(await Task.FromResult(BuildMethodRespose(new
                {
                    Message = "ConfigurationUpdate accepted",
                    Uri = operation.Uri
                })));
            }
            catch (Exception ex)
            {
                return(await Task.FromResult(BuildMethodRespose(new
                {
                    Message = ex.Message
                }, 400)));
            }
        }
        public ConfigurationSaveResults UpdateConfigurationSectionJson(ConfigurationUpdate update)
        {
            var results = new ConfigurationSaveResults();

            var id = update.Id;
            var json = update.Value;

            ConfigurationValue record = Context.ConfigurationValues.Find(id);
            if (record != null)
            {

                if (ValidateJson(json, record.JSONSchema))
                {
                    var xmlSchema = GetSchema(record.XMLSchema);
                    //TODO some checks on the type throw exception
                    string xml = JsonConvert.DeserializeXmlNode(json).OuterXml;
                    if (ValidateXml(xml, xmlSchema))
                    {
                        record.XML = xml;
                        record.JSON = json;
                        Context.SetModified(record);
                        var changes = Context.SaveChanges();
                        if (changes == 0)
                            results.AddError("No records were removed ensure the id is correct.", DataStorageError.RecordsUnchanged);
                        else
                            results.Record = record;
                    }
                    else
                    {
                        results.AddError("The Xml does not match the schema", DataStorageError.DoesNotMatchXMlSchema);
                    }

                }
                else
                {
                    results.AddError("The Json does not match the schema", DataStorageError.DoesNotMatchJsonSchema);
                }

            }
            else
            {
                results.AddError("Record not found.", DataStorageError.RecordNotFound);
            }

            return results;
        }
        public ConfigurationSaveResults UpdateConfigurationSectionXml(ConfigurationUpdate update)
        {
            var results = new ConfigurationSaveResults();
            var id = update.Id;
            var xml = update.Value;
            ConfigurationValue record = Context.ConfigurationValues.Find(id);
            if (record != null)
            {

                var xmlSchema = GetSchema(record.XMLSchema);
                XmlDocument document = null;
                if (ValidateXml(xml, xmlSchema, out document))
                {
                    this.AssignJsonArrayAttributes(document, document.ChildNodes);
                    string json = JsonConvert.SerializeXmlNode(document, Newtonsoft.Json.Formatting.Indented, false);
                    if (ValidateJson(json, record.JSONSchema))
                    {
                        record.XML = xml;
                        record.JSON = json;
                        Context.SetModified(record);
                        var changes = Context.SaveChanges();
                        if (changes == 0)
                            results.AddError("No records were removed ensure the id is correct.", DataStorageError.RecordsUnchanged);
                        else
                            results.Record = record;
                    }
                    else
                    {
                        results.AddError("The Json does not match the schema", DataStorageError.DoesNotMatchJsonSchema);
                    }
                }
                else
                {
                    results.AddError("The Xml does not match the schema", DataStorageError.DoesNotMatchXMlSchema);
                }
            }
            else
            {
                results.AddError("Record not found.", DataStorageError.RecordNotFound);
            }

            return results;
        }
        public DTOConfigurationSaveResults Put([FromBody] DTOConfigurationUpdate dtoUpdate)
        {
            var update = new ConfigurationUpdate()
            {
                Id    = dtoUpdate.Id,
                Value = dtoUpdate.Value
            };
            var format = dtoUpdate.Format;

            if (format == FormatTypeId.Json)
            {
                return(_convertSaveResults(_dataStore.UpdateConfigurationSectionJson(update)));
            }
            else if (format == FormatTypeId.Xml)
            {
                return(_convertSaveResults(_dataStore.UpdateConfigurationSectionXml(update)));
            }
            else
            {
                //TODO log or handle the exception
                throw new InvalidOperationException("Invalid format type specified");
            }
        }
Esempio n. 5
0
 public ConfigurationSaveResults UpdateConfigurationSectionXml(ConfigurationUpdate update)
 {
     throw new NotImplementedException();
 }
 internal void OnConfigurationUpdated(ConfigurationObject configurationObject) => ConfigurationUpdate?.Invoke(configurationObject, null);