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> 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));
            }
        }