public async Task ImportSources() { var json = LoadFile(); if (json == null) { return; } try { var client = await authenticator.GetAuthenticatedClient(); JArray array = JArray.Parse(json); await ImportSources(client, array); } catch (Exception e) { Console.WriteLine($"Failed to deserialize the file"); Console.WriteLine($"Please addapt your file based on the following example"); ImportSource sampleSite = new ImportSource { Id = 12345, Name = "My Source", Description = "Source description", SourceTypeId = 72, TimeZoneId = "Romance Standard Time", EnergyTypeId = 1, Variables = new List <VariableImport> { new VariableImport { VariableTypeId = 0, UnitId = 8, Divider = 1, Granularity = 10, GranularityTimeBase = TimePeriod.Minute }, new VariableImport { VariableTypeId = 25, UnitId = 8, Divider = 1, Granularity = 10, GranularityTimeBase = TimePeriod.Minute } } }; Console.WriteLine(JsonConvert.SerializeObject(new[] { sampleSite }, Formatting.Indented)); } }
private async Task ImportVariables(JToken sourceToken, HttpClient client, ImportSource source) { var variables = sourceToken[nameof(ImportSource.Variables)]; if (variables != null) { var variableArray = variables.ToObject <JArray>(); foreach (var variableToken in variableArray) { var importVariable = variableToken.ToObject <VariableImport>(); if (importVariable.Id.HasValue) { var variablePatch = new JsonPatchDocument(); WalkNode(variableToken, "", (n, path) => { foreach (var property in n.Properties()) { if (property.Value.Type != JTokenType.Array && property.Value.Type != JTokenType.Object && property.Value.Type != JTokenType.Property && !string.Equals(property.Name, nameof(VariableImport.Id), StringComparison.InvariantCultureIgnoreCase)) { variablePatch.Replace($"{path}{property.Name}", property.ToObject(GetPropertyType(property.Value.Type))); } } }); await DataCreator.PatchVariable(client, source.Id.Value, importVariable.Id.Value, variablePatch); Console.WriteLine($"update variable - id <{importVariable.Id}> of type <{importVariable.VariableTypeId}> source <{source.Name}>"); } else { var variable = await DataCreator.CreateVariable(client, source.Id.Value, importVariable); Console.WriteLine($"Created variable - id <{variable.Id}> of type <{importVariable.VariableTypeId}> source <{source.Name}>"); } } } }