public async Task <IActionResult> SaveEvent([FromRoute] string id, [FromBody] RecyclingProcess data)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                using (DbWaterRecyclingContext db = new DbWaterRecyclingContext())
                {
                    var device = db.Devices.Where(i => i.Code == id).FirstOrDefault();
                    data.From = device ?? throw new ArgumentNullException("id", "Dispositivo no identificado");

                    data.CaptureDate = DateTime.Now;

                    data.Process = !string.IsNullOrEmpty(data.Process) ? data.Process : GetnewProcess(device);

                    var result = await db.AddAsync(data);

                    await db.SaveChangesAsync();

                    return(Ok(data.Process));
                }
            }
            catch (System.Exception ex)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError,
                                  ex));
            }
        }
Beispiel #2
0
        public async Task <IActionResult> GetLiteById(string id)
        {
            try
            {
                using (DbWaterRecyclingContext db = new DbWaterRecyclingContext())
                {
                    var processes = await db.RecyclingProcesses
                                    .Where(i => string.IsNullOrEmpty(id) || i.From.Code.Equals(id))
                                    .Include(i => i.From)
                                    .ToListAsync();


                    return(Ok(new
                    {
                        LastProcessId = processes.LastOrDefault()?.Process,
                        recyclingProcessCount = processes.Count
                    }));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError,
                                  ex));
            }
        }
        public async Task <IActionResult> GetEvents([FromQuery] string id)
        {
            try
            {
                using (DbWaterRecyclingContext db = new DbWaterRecyclingContext())
                {
                    var events = await db.RecyclingProcesses
                                 .Where(i => i.Process.Equals(id))
                                 .GroupBy(i => new { i.Distance, i.Turbidity })
                                 .Select(i => new
                    {
                        i.Key.Distance,
                        i.Key.Turbidity,
                        i.First().CaptureDate
                    })
                                 .OrderByDescending(i => i.CaptureDate)
                                 .ToListAsync();

                    return(Ok(events.Take(5)));
                }
            }
            catch (System.Exception ex)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError,
                                  ex));
            }
        }
Beispiel #4
0
        public async Task <IActionResult> RemoveCode(string id)
        {
            try
            {
                using (DbWaterRecyclingContext db = new DbWaterRecyclingContext())
                {
                    var code = await db.Devices.Where(i => i.Code.Equals(id)).FirstOrDefaultAsync();

                    if (code == null)
                    {
                        return(BadRequest("Código Incorrecto"));
                    }
                    if (code.State)
                    {
                        code.State = false;

                        db.Update(code);

                        await db.SaveChangesAsync();

                        return(Ok("Dispositivo Desasociado correctamente"));
                    }

                    return(BadRequest("El código no está asociado a ningun dispositivo"));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, ex));
            }
        }
Beispiel #5
0
        public async Task <IActionResult> NotificationsDevice(string id, [FromQuery] string token)
        {
            try
            {
                using (DbWaterRecyclingContext db = new DbWaterRecyclingContext())
                {
                    var code = await db.Devices.Where(i => i.Code.Equals(id)).FirstOrDefaultAsync();

                    if (code == null)
                    {
                        return(BadRequest());
                    }

                    code.DeviceToken = token;
                    db.Update(code);

                    await db.SaveChangesAsync();

                    Notifications.Notify notify = new Notifications.Notify();
                    notify.NotifyNewDevice(code);
                    return(Ok(10));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError,
                                  ex));
            }
        }
Beispiel #6
0
        public async Task <IActionResult> Get([FromQuery] string id)
        {
            try
            {
                using (DbWaterRecyclingContext db = new DbWaterRecyclingContext())
                {
                    var devices = await db.Devices.Where(i => string.IsNullOrEmpty(id) || i.Code.Equals(id))
                                  .Include(i => i.RecyclingProcessList)
                                  .ToListAsync();


                    return(Ok(devices.Select(i => new
                    {
                        i.Code,
                        i.Ip,
                        i.Created,
                        i.State,
                        recyclingProcessList = i.RecyclingProcessList.Count,
                        recyclingProcessCount = LocalGroupBy(i.RecyclingProcessList)
                    })));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError,
                                  ex));
            }
        }
Beispiel #7
0
        public async Task <IActionResult> ActivateCode(string id)
        {
            try
            {
                using (DbWaterRecyclingContext db = new DbWaterRecyclingContext())
                {
                    var code = await db.Devices.Where(i => i.Code.Equals(id)).FirstOrDefaultAsync();

                    if (code == null)
                    {
                        return(BadRequest());
                    }
                    if (code.State)
                    {
                        return(Ok(99));
                    }

                    code.State = true;

                    db.Update(code);

                    await db.SaveChangesAsync();

                    return(Ok(10));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError,
                                  ex));
            }
        }
        public IActionResult SimulateEvent([FromRoute] string id, [FromBody] RecyclingProcess data)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                using (DbWaterRecyclingContext db = new DbWaterRecyclingContext())
                {
                    var device  = db.Devices.Where(i => i.Code == id).FirstOrDefault();
                    var from    = device ?? throw new ArgumentNullException("id", "Dispositivo no identificado");
                    var process = !string.IsNullOrEmpty(data.Process) ? data.Process : GetnewProcess(device);


                    for (int i = 0; i < data.Distance; i++)
                    {
                        int milliseconds = 2000;
                        Thread.Sleep(milliseconds);

                        data.Distance = data.Distance - 1;
                        if (data.Distance <= 2)
                        {
                            break;
                        }
                        db.Add(new RecyclingProcess
                        {
                            From        = from,
                            CaptureDate = DateTime.Now,
                            Process     = process,
                            Distance    = data.Distance,
                            Turbidity   = data.Turbidity,
                        });
                        db.SaveChanges();
                    }

                    return(Ok(process));
                }
            }
            catch (System.Exception ex)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError,
                                  ex));
            }
        }
Beispiel #9
0
        public async Task <IActionResult> GetFull([FromQuery] string id)
        {
            try
            {
                using (DbWaterRecyclingContext db = new DbWaterRecyclingContext())
                {
                    var devices = await db.Devices.Where(i => string.IsNullOrEmpty(id) || i.Code.Equals(id))
                                  .Include(i => i.RecyclingProcessList)
                                  .ToListAsync();


                    return(Ok(devices));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError,
                                  ex));
            }
        }
Beispiel #10
0
        public async Task <IActionResult> NewDevice()
        {
            try
            {
                using (DbWaterRecyclingContext db = new DbWaterRecyclingContext())
                {
                    var    exist = false;
                    string code  = "";
                    while (!exist)
                    {
                        code = GenerateUniqueCode();

                        var existCode = await db.Devices.Where(i => i.Code.Equals(code))
                                        .FirstOrDefaultAsync();

                        if (existCode == null)
                        {
                            exist = true;
                        }
                    }

                    Device device = new Device();
                    device.Code    = code;
                    device.Created = DateTime.Now;
                    device.Ip      = this.HttpContext.Connection.RemoteIpAddress.ToString();

                    var result = await db.AddAsync(device);

                    await db.SaveChangesAsync();

                    return(Ok(code));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError,
                                  ex));
            }
        }
Beispiel #11
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(
            IApplicationBuilder app,
            IHostingEnvironment env,
            DbWaterRecyclingContext dbContext
            )
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
            });
            // ===== Use Authentication ======
            app.UseAuthentication();
            app.UseMvc();

            app.Use(async(context, next) => {
                await next();
                if (context.Response.StatusCode == 404 &&
                    !Path.HasExtension(context.Request.Path.Value) &&
                    !context.Request.Path.Value.StartsWith("/api/", StringComparison.Ordinal))
                {
                    context.Request.Path = "/index.html";
                    await next();
                }
            });

            app.UseMvcWithDefaultRoute();
            app.UseDefaultFiles();
            app.UseStaticFiles();

            // ===== Create tables ======
            dbContext.Database.EnsureCreated();
        }
 private static void SaveEvent(RecyclingProcess data, Device from, string process)
 {
     using (DbWaterRecyclingContext db = new DbWaterRecyclingContext())
     {
     }
 }