Пример #1
0
        public async Task Report(Reading reading)
        {
            if (reading == null)
            {
                throw new ArgumentNullException(nameof(reading));
            }

            await context.Entry(reading).Reference(r => r.Sensor).LoadAsync();

            await context.Entry(reading.Sensor).Reference(s => s.Alert).LoadAsync();

            Alert alert = reading.Sensor.Alert;

            if (alert == null)
            {
                return;
            }

            bool isAlert = alert.Check(reading.Value);

            if (isAlert != alert.IsNotified)
            {
                alert.IsNotified = !alert.IsNotified;
                await context.SaveChangesAsync();

                string subject = FormatSubject(isAlert);
                string message = FormatMessage(isAlert, reading);
                await emailSender.SendEmailAsync(alert.Email, subject, message);
            }
        }
Пример #2
0
        public IActionResult Show([FromRoute] int devId)
        {
            Device device = context.Devices.Find(devId);

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

            if (!IsAuthorized(device))
            {
                return(Forbid());
            }

            context.Entry(device).Collection(d => d.Sensors).Load();
            ViewData["Title"] = device.NameOrId();
            var cards = device.Sensors.Select(s => new Card()
            {
                Title = s.NameOrId(),
                Id    = s.SensorId,
                Url   = $"/{s.DeviceId}/{s.SensorId}",
                Value = lastValues.GetSensorLastValue(s.DeviceId, s.SensorId),
                Unit  = s.Unit
            });

            return(View(model: cards));
        }
Пример #3
0
        public IActionResult Settings([FromRoute] int devId, [FromRoute] int senId)
        {
            Sensor sensor = context.Sensors.Find(devId, senId);

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

            if (!IsAuthorized(sensor))
            {
                return(Forbid());
            }

            context.Entry(sensor).Reference(s => s.Alert).Load();
            return(View(model: sensor));
        }