Example #1
0
        public async Task <IActionResult> GetH_sensor([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (User.Claims.Single(x => x.Type == ClaimTypes.Role).Value == "User")
            {
                User user = _context.Users.Find(Convert.ToInt32(User.FindFirstValue("Sub")));

                List <H_sensor> h_Sensors = GetUserElements.GetUserH_sensors(user.Id, _context);

                if (h_Sensors == null || h_Sensors.Count == 0 || h_Sensors.Where(x => x.Id == id).ToList().Count == 0)
                {
                    return(NotFound());
                }
            }

            var h_sensor = await _context.H_sensors.FindAsync(id);

            if (h_sensor == null)
            {
                return(NotFound());
            }

            return(Ok(h_sensor));
        }
Example #2
0
        public async Task <IActionResult> PutH_sensor([FromRoute] int id, [FromBody] H_sensor h_sensor)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != h_sensor.Id)
            {
                return(BadRequest());
            }

            if (User.Claims.Single(x => x.Type == ClaimTypes.Role).Value == "User")
            {
                User user = _context.Users.Find(Convert.ToInt32(User.FindFirstValue("Sub")));

                List <H_sensor> h_Sensors = GetUserElements.GetUserH_sensors(user.Id, _context);

                if (h_Sensors == null || h_Sensors.Count == 0 || h_Sensors.Where(x => x.Id == id).ToList().Count == 0)
                {
                    return(NotFound());
                }
            }

            //Alarm
            H_sensor sensor = _context.H_sensors.Find(h_sensor.Id);

            if (h_sensor.Value < sensor.Min_value)
            {
                _context.Rooms.Find(h_sensor.RoomId).Alarm = true;

                Enterprise enterprise = _context.Enterprises.Find(_context.Rooms.Find(h_sensor.RoomId).EnterpriseId);
                User       user       = _context.Users.Find(enterprise.UserId);

                SendMail.sendEmail(user.Mail, "ALARM");
            }
            else if (h_sensor.Value > sensor.Max_value)
            {
                _context.Rooms.Find(h_sensor.RoomId).Alarm = true;

                Enterprise enterprise = _context.Enterprises.Find(_context.Rooms.Find(h_sensor.RoomId).EnterpriseId);
                User       user       = _context.Users.Find(enterprise.UserId);

                SendMail.sendEmail(user.Mail, "ALARM");
            }

            _context.Entry(h_sensor).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!H_sensorExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }