Пример #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, SensorTrackingDbContext dbContext)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                ServiceCollectionExtension.FillDatabase(dbContext);
            }


            app.UseRouting();

            app.UseAuthorization();

            app.UseCors(option => option.AllowAnyOrigin());

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
Пример #2
0
        public static void FillDatabase(SensorTrackingDbContext context)
        {
            bool createdDb = context.Database.EnsureCreated();

            if (createdDb)
            {
                string done = "criado";
            }

            if (context.Sensores.Any())
            {
                return;
            }

            DateTime startDt         = DateTime.UtcNow;
            double   timestampInicio = startDt.Subtract(DateTime.UnixEpoch).TotalMilliseconds;
            double   timestampFim    = startDt.AddMinutes(2).Subtract(DateTime.UnixEpoch).TotalMilliseconds;

            int idTemp = 1;

            // 10 regioes.
            ICollection <string> regiaoList = new List <string>()
            {
                "rio_de_janeiro", "sao_paulo", "curitiba", "joinvile", "maranhao", "paraiba",
                "brasilia", "sergipe", "belo_horizonte", "fortaleza"
            };

            // de 10 a 15 sensores por regiao
            string base_name = "sensor_0";

            // 5min de dados: 60x5 = 300/sensor
            // valor: string e numero
            // 10 reg x 15 sensor_name x 300 sensor_input = 45.000

            ICollection <SensorModel> novosModels = new List <SensorModel>();

            foreach (string reg in regiaoList)
            {
                int qtddSensorReg            = GetRandomInt(15);
                ICollection <int> intChoised = new HashSet <int>();
                while (intChoised.Count != qtddSensorReg)
                {
                    intChoised.Add(GetRandomInt(15));
                }

                foreach (int num in intChoised)
                {
                    int    oneSecond    = 1000;
                    double timeStampCur = timestampInicio;
                    while (timeStampCur < timestampFim)
                    {
                        string valor     = GetValue();
                        string tag       = "brasil." + reg + "." + base_name + num;
                        long   timestamp = Convert.ToInt64(Math.Round(timeStampCur));

                        SensorModel model = SensorModel.Build(tag, timestamp, valor);
                        //model.Id = idTemp;
                        novosModels.Add(model);

                        idTemp       += 1;
                        timeStampCur += oneSecond;
                    }
                }
            }

            // https://docs.microsoft.com/pt-br/ef/core/modeling/data-seeding#custom-initialization-logic
            context.Sensores.AddRangeAsync(novosModels);
            context.SaveChangesAsync();
        }