Example #1
0
        public async Task <IActionResult> GetT_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 <T_sensor> t_Sensors = GetUserElements.GetUserT_sensors(user.Id, _context);

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

            var t_sensor = await _context.T_sensors.FindAsync(id);

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

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

            if (id != t_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 <T_sensor> t_Sensors = GetUserElements.GetUserT_sensors(user.Id, _context);

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

            //Alarm
            T_sensor sensor = _context.T_sensors.Find(t_sensor.Id);

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

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

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

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

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

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

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

            return(NoContent());
        }