private async Task Update()
        {
            Console.WriteLine("Enter source id");
            var sourceId = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter variable id");
            var variableId = Convert.ToInt32(Console.ReadLine());
            var client     = await authenticator.GetAuthenticatedClient();

            var response = await client.GetAsync($"{OpisenseApi}variables?displayLevel=verbose&id={variableId}");

            response.EnsureSuccessStatusCode();

            var calculatedVariable = (await response.Content.ReadAsAsync <CalculatedVariable[]>()).FirstOrDefault();

            if (calculatedVariable != null)
            {
                var updateResponse = await client.PutAsJsonAsync($"{OpisenseApi}sources/{sourceId}/variables/{variableId}", calculatedVariable);

                updateResponse.EnsureSuccessStatusCode();
            }
        }
        public async Task LoadFileFromStream(string fileName, Stream stream, int?siteId = null)
        {
            var client = await authenticator.GetAuthenticatedClient();

            using (var content = new MultipartFormDataContent())
            {
                stream.Seek(0, SeekOrigin.Begin);

                content.Add(new StreamContent(stream), fileName, fileName);

                using (var response = await client.PostAsync($"{OpisenseApi}storage?parameters.filename={fileName}&parameters.siteId={siteId}", content))
                {
                    response.EnsureSuccessStatusCode();
                    Console.WriteLine();
                    Console.WriteLine("File successfully uploaded...");
                }
            }
        }
示例#3
0
        public async Task DeleteSource()
        {
            var client = await authenticator.GetAuthenticatedClient();

            var sources = await sourceSelector.DisplaySources(true);

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Enter the Id of the source you want to delete: ");

            var sourceId = Convert.ToInt32(Console.ReadLine());
            var source   = sources.Single(x => x.Id == sourceId);

            Console.WriteLine("Chosen source: ");
            ConsoleTable
            .From(new List <Source> {
                source
            })
            .Write();

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Are you sure you want to delete this source?");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Type YES if you confirm:");
            var response = Console.ReadLine();

            if (response != null && response.Equals("YES", StringComparison.InvariantCultureIgnoreCase))
            {
                Console.WriteLine();
                Console.WriteLine("WARNING: This action cannot be undone, please enter the Source Id to validate:");
                var confirmId = Convert.ToInt32(Console.ReadLine());
                if (confirmId == sourceId)
                {
                    await InternalDeleteSource(client, sourceId);
                }
                else
                {
                    Console.WriteLine("The source id you typed is different. Abording the request.");
                }
            }
        }
        public async Task DisplayData()
        {
            var client = await authenticator.GetAuthenticatedClient();

            var variable = await variableSelector.SelectVariable(client);

            if (variable == null)
            {
                return;
            }

            var variableTypes = await GetVariableTypes(client);

            var variableType = variableTypes.Single(x => x.Id == variable.VariableTypeId);

            Console.WriteLine();
            Console.WriteLine("1. Get RAW Data");
            Console.WriteLine("2. Get Data grouped by hour");

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Select how to get the data: ");
            var display = Convert.ToInt32(Console.ReadLine());

            Stopwatch sw = new Stopwatch();

            sw.Start();
            var data = await GetData(client, variable, display == 1? 0 : 2, variableType.Aggregate);

            var getDataElapsedTime = sw.Elapsed;

            sw.Restart();
            ConsoleTable
            .From(data.Select(x => new { x.VariableId, Value = x.GetValue(), x.Date }))
            .Write();
            var displayDataElapsedTime = sw.Elapsed;

            sw.Stop();
            Console.WriteLine($"Data retrieval took {getDataElapsedTime:g}");
            Console.WriteLine($"Data display took {displayDataElapsedTime:g}");
        }
示例#5
0
        public async Task <Source[]> DisplaySources(bool fromSite = false)
        {
            var client = await authenticator.GetAuthenticatedClient();

            return(await InternalDisplaySource(client, fromSite));
        }
示例#6
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));
            }
        }
        public async Task DemoSetup()
        {
            var client = await authenticator.GetAuthenticatedClient();

            var form = new Form
            {
                Name       = "SOURCE_FORM",
                EntityType = "source",
                Groups     = new List <Group>
                {
                    new Group
                    {
                        Name   = "Group1",
                        Fields = new List <Field>
                        {
                            new Field {
                                Name = "STRING_FIELD", Type = FieldType.String
                            },
                            new Field {
                                Name = "DOUBLE_FIELD", Type = FieldType.Double
                            },
                        }
                    },
                    new Group
                    {
                        Name   = "Group2",
                        Fields = new List <Field>
                        {
                            new Field {
                                Name = "INT_LIST_FIELD", Type = FieldType.Int, IsList = true, Items = new List <ListItem>
                                {
                                    new ListItem {
                                        Name = "One", Value = 1
                                    },
                                    new ListItem {
                                        Name = "Two", Value = 2
                                    }
                                }
                            },
                            new Field {
                                Name = "DATE_FIELD", Type = FieldType.Date
                            },
                        }
                    }
                }
            };

            Console.WriteLine("Checking if source form exists");
            if (await GetSourceForm(client, form.Name) == null)
            {
                Console.WriteLine("Creating source form");
                await CreateSourceForm(client, form);
            }



            Console.WriteLine("Creating 3 sites in Opisense");
            Console.WriteLine("Creating site1");
            var siteId1 = await CreateSite(client, "site1");

            Console.WriteLine("Creating site2");
            var siteId2 = await CreateSite(client, "site2");

            Console.WriteLine("Creating site3");
            var siteId3 = await CreateSite(client, "site3");

            Console.WriteLine("Creating source1 and variables");
            var source1 = await CreateSource(client, siteId1, "source1");

            // Add
            var source1Variable1 = await CreateVariable(client, source1.Id, 0);

            // Ajout de la variable d'index
            var source1Variable2 = await CreateVariable(client, source1.Id, 25);

            Console.WriteLine("Creating source2 and variables");
            var source2 = await CreateSource(client, siteId1, "source2");

            // Ajout de la variable de consommation
            var source2Variable1 = await CreateVariable(client, source2.Id, 0);

            // Ajout de la variable d'index
            var source2Variable2 = await CreateVariable(client, source2.Id, 25);

            Console.WriteLine("Creating source3 and variables");
            var source3 = await CreateSource(client, siteId2, "source3");

            // Ajout de la variable de consommation
            var source3Variable1 = await CreateVariable(client, source3.Id, 0);

            // Ajout de la variable d'index
            var source3Variable2 = await CreateVariable(client, source3.Id, 25);

            Console.WriteLine("Creating source4 and variables");
            var source4 = await CreateSource(client, siteId2, "source4");

            // Ajout de la variable de consommation
            var source4Variable1 = await CreateVariable(client, source4.Id, 0);

            // Ajout de la variable d'index
            var source4Variable2 = await CreateVariable(client, source4.Id, 25);

            Console.WriteLine("Creating source5 and variables");
            var source5 = await CreateSource(client, siteId3, "source5");

            // Ajout de la variable de consommation
            var source5Variable1 = await CreateVariable(client, source5.Id, 0);

            // Ajout de la variable d'index
            var source5Variable2 = await CreateVariable(client, source5.Id, 25);


            //    Ajouter dans chaque source précédemment créé 2 variables(type double et type int) avec 1 ans d'historique des valeurs au points de 10mins (=6*24*365*2 points), sauf pour le site3 ou il faut avoir un "trou" pour tous les 10 valeurs

            Console.WriteLine("Adding data in source 1");
            await CreateData(client, source1Variable1, source1Variable2);

            Console.WriteLine("Adding data in source 2");
            await CreateData(client, source2Variable1, source2Variable2);

            Console.WriteLine("Adding data in source 3");
            await CreateData(client, source3Variable1, source3Variable2);

            Console.WriteLine("Adding data in source 4");
            await CreateData(client, source4Variable1, source4Variable2);

            Console.WriteLine("Adding data in source 5");
            await CreateData(client, source5Variable1, source5Variable2, holesEveryXDatapoint : 10);


            // ATTENTION: LORS DE L'INGESTION, LES DONNEES PASSENT PAS PLUSIEURS ETAPES (ENRICHISSEMENT, VARIABLE CALCULEES, ALERTES)
            // IL Y A DONC UN DELAIS POUVANT ALLER JUSQU'A 10 MINUTES AVANT QU'ELLES SOIENT ACCESSIBLE VIA L'API
        }