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));
            }
        }
Пример #2
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));
            }
        }
Пример #3
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));
            }
        }
Пример #4
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));
            }
        }
Пример #5
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));
            }
        }