Exemplo n.º 1
0
        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}>");
                    }
                }
            }
        }
Exemplo n.º 2
0
        private async Task ImportSources(HttpClient client, JToken sources, int?siteId = null)
        {
            var sourceArray = sources.ToObject <JArray>();

            foreach (var sourceToken in sourceArray)
            {
                var source = sourceToken.ToObject <ImportSource>();

                if (source.Id.HasValue)
                {
                    var sourcePatch = new JsonPatchDocument();
                    WalkNode(sourceToken, "", (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(ImportSource.Id), StringComparison.InvariantCultureIgnoreCase))
                            {
                                sourcePatch.Replace($"{path}{property.Name}", property.ToObject(GetPropertyType(property.Value.Type)));
                            }
                        }
                    });
                    await DataCreator.PatchSource(client, source.Id.Value, sourcePatch);

                    Console.WriteLine($"Updated source - id <{source.Id}> named <{source.Name}>");
                }
                else
                {
                    if (siteId.HasValue)
                    {
                        source.SiteId = siteId.Value;
                    }
                    source.Id = (await DataCreator.CreateSource(client, source)).Id;
                    Console.WriteLine($"Created source - id <{source.Id}> named <{source.Name}> in site <{source.SiteId}>");
                }
                await ImportVariables(sourceToken, client, source);
            }
        }
Exemplo n.º 3
0
        public async Task ImportSites()
        {
            var json = LoadFile();

            if (json == null)
            {
                return;
            }
            try
            {
                var client = await authenticator.GetAuthenticatedClient();

                JArray array = JArray.Parse(json);
                foreach (var jtoken in array)
                {
                    var importSite = jtoken.ToObject <ImportSite>();

                    if (importSite.Id.HasValue)
                    {
                        var sitePatch = new JsonPatchDocument();
                        WalkNode(jtoken, "", (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(ImportSite.Id), StringComparison.InvariantCultureIgnoreCase))
                                {
                                    sitePatch.Replace($"{path}{property.Name}", property.ToObject(GetPropertyType(property.Value.Type)));
                                }
                            }
                        });
                        await DataCreator.PatchSite(client, importSite.Id.Value, sitePatch);

                        Console.WriteLine($"Updated site - id <{importSite.Id}> named <{importSite.Name}>");
                    }
                    else
                    {
                        importSite.Id = await DataCreator.CreateSite(client, importSite);

                        Console.WriteLine($"Created site - id <{importSite.Id}> named <{importSite.Name}>");
                    }
                    var sources = jtoken[nameof(ImportSite.Sources)];
                    if (sources != null)
                    {
                        await ImportSources(client, sources, importSite.Id);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Failed to deserialize the file");
                Console.WriteLine($"Please addapt your file based on the following example");
                var        clientData = JsonConvert.DeserializeObject <dynamic>(@"
                        {
                           ""MyForm1"":
                                {
                                ""MyGroup1"":
                                    {
                                    ""MyField1"":10,
                                    ""MyField2"": ""something""
                                    }
                                },
                            ""MyForm2"": { }
                        }");
                ImportSite sampleSite = new ImportSite
                {
                    Name       = "My site",
                    Street     = "Albert 1er",
                    City       = "Bruxelles",
                    PostalCode = "1000",
                    Country    = "Belgium",
                    TimeZoneId = "Romance Standard Time",
                    TypeId     = 1,
                    ClientData = clientData,
                    Sources    = new List <ImportSource>
                    {
                        new ImportSource
                        {
                            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 static void Main()
        {
            using (var authenticator = new Authenticator())
            {
                var siteSelector              = new SiteSelector();
                var sourceSelector            = new SourceSelector(siteSelector, authenticator);
                var variableSelector          = new VariableSelector(sourceSelector);
                var dataCreator               = new DataCreator(variableSelector, authenticator);
                var reporter                  = new Reporter(variableSelector, authenticator);
                var siteDeletor               = new SiteDeletor(siteSelector, authenticator);
                var sourceDeletor             = new SourceDeletor(sourceSelector, authenticator);
                var importer                  = new Importer(authenticator);
                var storageLoader             = new StorageLoader(authenticator);
                var calculatedVariableManager = new CalculatedVariableManager(authenticator, sourceSelector);

                var exitCode = 99;
                int userInput;
                do
                {
                    userInput = DisplayMenu(exitCode);
                    try
                    {
                        switch (userInput)
                        {
                        case 1:
                            sourceSelector.DisplaySources().Wait();
                            break;

                        case 2:
                            dataCreator.DemoSetup().Wait();
                            break;

                        case 3:
                            dataCreator.UpdateData().Wait();
                            break;

                        case 4:
                            reporter.DisplayData().Wait();
                            break;

                        case 5:
                            siteDeletor.DeleteSite().Wait();
                            break;

                        case 6:
                            sourceDeletor.DeleteSource().Wait();
                            break;

                        case 7:
                            importer.ImportSites().Wait();
                            break;

                        case 8:
                            importer.ImportSources().Wait();
                            break;

                        case 9:
                            sourceSelector.SearchSources().Wait();
                            break;

                        case 10:
                            storageLoader.LoadFileFromString("test_upload_console.txt", "This is a simple test").Wait();
                            break;

                        case 11:
                            authenticator.RefreshToken().Wait();
                            break;

                        case 12:
                            calculatedVariableManager.CrudCalculatedVariables().Wait();
                            break;
                        }
                    }
                    catch (Exception exc)
                    {
                        Console.WriteLine(exc.Message);
                        Console.WriteLine(exc.StackTrace);
                        Console.WriteLine("Something bad happened. Please try again...");
                    }
                } while (userInput != exitCode);
                authenticator.Dispose();
            }
        }