示例#1
0
        public GpioPinValue GetPinValue(DeviceSensor pin)
        {
            var val = Controller.Read(pin.Pin);

            pin.Value = val;
            return(val);
        }
示例#2
0
 public IActionResult UpdateDeviceSensor(int deviceId, [FromBody] DeviceSensor sensor)
 {
     try
     {
         _logger.LogInformation($"POST /v1/Device/{deviceId}/Sensor/Update called");
         sensor = _aquariumService.UpdateDeviceSensor(sensor);
         return(new OkObjectResult(sensor));
     }
     catch (Exception ex)
     {
         _logger.LogError($"POST /v1/Device/{deviceId}/Sensor/Update: { ex.Message } Details: { ex.ToString() }");
         return(BadRequest());
     }
 }
示例#3
0
 public IActionResult RemoveDeviceSensor(int deviceId, [FromBody] DeviceSensor sensor)
 {
     try
     {
         _logger.LogInformation($"POST /v1/Device/{deviceId}/Sensor/Remove called");
         _aquariumService.DeleteDeviceSensor(deviceId, sensor.Id);
         return(new OkResult());
     }
     catch (Exception ex)
     {
         _logger.LogError($"POST /v1/Device/{deviceId}/Sensor/Remove: { ex.Message } Details: { ex.ToString() }");
         return(BadRequest());
     }
 }
示例#4
0
        public DeviceSensor UpdateDeviceSensor(DeviceSensor deviceSensor)
        {
            var updated = _aquariumDao.UpdateDeviceSensor(deviceSensor);

            //tell the pi
            try
            {
                ApplyUpdatedDevice(updated.DeviceId);
            }
            catch
            {
                _logger.LogError("Could not send update to devices.");
            }
            return(updated);
        }
示例#5
0
 public void RegisterDevicePin(DeviceSensor deviceSensor)
 {
     CreateController();
     Pins.Add(deviceSensor);
     try
     {
         PreparePins();
     }
     catch (Exception e)
     {
         Pins.Remove(deviceSensor);
         _logger.LogError($"Could not register device sensor ({deviceSensor.Name} Pin Number: {deviceSensor.Pin} Polarity: {deviceSensor.Polarity})");
         _logger.LogError(e.Message);
     }
 }
示例#6
0
        /* Device Sensors */
        public DeviceSensor CreateDeviceSensor(int deviceId, DeviceSensor deviceSensor)
        {
            deviceSensor.DeviceId = deviceId;
            deviceSensor          = _aquariumDao.AddDeviceSensor(deviceSensor);

            //tell the pi
            try
            {
                ApplyUpdatedDevice(deviceId);
            }
            catch
            {
                _logger.LogError("Could not send update to devices.");
            }
            return(deviceSensor);
        }
示例#7
0
        public async Task <DeviceConfig> SaveDeviceAsync(DeviceConfig model)
        {
            Device dbDevice = null;

            var transaction = await context.Database.BeginTransactionAsync();

            if (string.IsNullOrEmpty(model.DeviceId))
            {
                dbDevice = new Device
                {
                    Name      = model.Name,
                    IsEnabled = model.IsEnabled
                };

                await context.Device.AddAsync(dbDevice);
            }
            else
            {
                dbDevice = await context.Device.FirstOrDefaultAsync(d => d.DeviceId == Guid.Parse(model.DeviceId));

                if (dbDevice == null)
                {
                    throw new ApplicationException($"Device with id {model.DeviceId.ToString()} was not found.");
                }

                dbDevice.Name      = model.Name;
                dbDevice.IsEnabled = model.IsEnabled;

                context.Device.Update(dbDevice);
            }

            try
            {
                await context.SaveChangesAsync();
            }
            catch (System.Exception)
            {
                transaction.Rollback();
                throw;
            }

            context.DeviceSensor.RemoveRange(context.DeviceSensor.Where(ds => !model.Sensors.Any(s => s.DeviceSensorId == ds.DeviceSensorId)));
            await context.SaveChangesAsync();

            foreach (var sensor in model.Sensors)
            {
                DeviceSensor dbDeviceSensor = null;
                if (sensor.DeviceSensorId.HasValue)
                {
                    dbDeviceSensor = await context.DeviceSensor.FirstOrDefaultAsync(ds => ds.DeviceSensorId == sensor.DeviceSensorId);

                    dbDeviceSensor.IsEnabled = sensor.IsEnabled;
                    context.DeviceSensor.Update(dbDeviceSensor);
                }
                else
                {
                    dbDeviceSensor           = new DeviceSensor();
                    dbDeviceSensor.DeviceId  = dbDevice.DeviceId;
                    dbDeviceSensor.SensorId  = sensor.SensorId;
                    dbDeviceSensor.IsEnabled = sensor.IsEnabled;
                    await context.DeviceSensor.AddAsync(dbDeviceSensor);
                }
            }

            context.DeviceEventType.RemoveRange(context.DeviceEventType.Where(de => !model.Events.Any(e => e.DeviceEventTypeId == de.DeviceEventTypeId)));
            await context.SaveChangesAsync();

            foreach (var eventType in model.Events)
            {
                DeviceEventType dbDeviceEventType = null;
                if (eventType.DeviceEventTypeId.HasValue)
                {
                    dbDeviceEventType = await context.DeviceEventType.FirstOrDefaultAsync(de => de.DeviceEventTypeId == eventType.DeviceEventTypeId);

                    dbDeviceEventType.IsEnabled = eventType.IsEnabled;
                    context.DeviceEventType.Update(dbDeviceEventType);
                }
                else
                {
                    dbDeviceEventType             = new DeviceEventType();
                    dbDeviceEventType.DeviceId    = dbDevice.DeviceId;
                    dbDeviceEventType.EventTypeId = eventType.EventTypeId;
                    dbDeviceEventType.IsEnabled   = eventType.IsEnabled;
                    await context.DeviceEventType.AddAsync(dbDeviceEventType);
                }
            }

            context.DeviceState.RemoveRange(context.DeviceState.Where(ds => !model.States.Any(e => e.DeviceStateId == ds.DeviceStateId)));
            await context.SaveChangesAsync();

            foreach (var state in model.States)
            {
                DeviceState dbDeviceState = null;
                if (state.DeviceStateId.HasValue)
                {
                    dbDeviceState = await context.DeviceState.FirstOrDefaultAsync(ds => ds.DeviceStateId == state.DeviceStateId);

                    dbDeviceState.IsEnabled = state.IsEnabled;
                    context.DeviceState.Update(dbDeviceState);
                }
                else
                {
                    dbDeviceState           = new DeviceState();
                    dbDeviceState.DeviceId  = dbDevice.DeviceId;
                    dbDeviceState.StateId   = state.StateId;
                    dbDeviceState.IsEnabled = state.IsEnabled;
                    await context.DeviceState.AddAsync(dbDeviceState);
                }
            }

            try
            {
                await context.SaveChangesAsync();
            }
            catch (Exception)
            {
                transaction.Rollback();
                throw;
            }

            transaction.Commit();

            return(await GetDeviceByIdAsync(dbDevice.DeviceId.ToString()));
        }
示例#8
0
 public void SetPinValue(DeviceSensor pin, PinValue pinValue)
 {
     Controller.Write(pin.Pin, pinValue);
 }
示例#9
0
        public static void SeedDevicesAndSensors(this ModelBuilder modelBuilder)
        {
            // create sensors
            var sensors = new Sensor[]
            {
                //0. no2 - µg/m³
                new Sensor()
                {
                    Id              = new Guid("0e4309da-f578-4f0e-b2e1-181c5273ca5a"),
                    Label           = "no2",
                    FriendlyLabel   = "Nitrogen dioxide",
                    MeasurementUnit = "µg/m³",
                    Description     = "Nitrogen dioxide (NO2) at high concentrations causes inflammation of the airways. Breathing in high levels of NO2 can increase the likelihood of respiratory problems: wheezing, coughing, colds, flu and bronchitis.",
                    ValueType       = ValueTypeEnum.Float
                },

                //1. co2 - µg/m³
                new Sensor()
                {
                    Id              = new Guid("a652d3d5-7467-4577-902d-c10a9b36760a"),
                    Label           = "co2",
                    FriendlyLabel   = "Carbon Dioxide",
                    MeasurementUnit = "µg/m³",
                    Description     = "Carbon dioxide is a natural gas found in our atmosphere. It is colorless, odorless, and tasteless - indistinguishable by individuals.",
                    ValueType       = ValueTypeEnum.Float
                },

                //2. o3 - µg/m³
                new Sensor()
                {
                    Id              = new Guid("0aa2301c-3cd1-4d51-be90-ae772b72936c"),
                    Label           = "o3",
                    FriendlyLabel   = "Ozone",
                    MeasurementUnit = "µg/m³",
                    Description     = "Ozone is unstable and highly reactive. Ozone is used as a bleach, a deodorizing agent, and a sterilization agent for air and drinking water. At low concentrations, it is toxic.",
                    ValueType       = ValueTypeEnum.Float
                },

                //3. so2 - µg/m³
                new Sensor()
                {
                    Id              = new Guid("c8d95eef-3891-40ff-8397-750156fdc448"),
                    Label           = "so2",
                    FriendlyLabel   = "Sulfur dioxide",
                    MeasurementUnit = "µg/m³",
                    Description     = "Sulfur dioxide is a colourless gas with a sharp, irritating odour. It is produced by burning fossil fuels and by the smelting of mineral ores that contain sulfur.Erupting volcanoes can be a significant natural source of sulfur dioxide emissions.",
                    ValueType       = ValueTypeEnum.Float
                },

                //4. temp - °C
                new Sensor()
                {
                    Id              = new Guid("e2fd8491-0b3a-41f1-bf14-e29cbab7ada4"),
                    Label           = "temp",
                    FriendlyLabel   = "Temperature",
                    MeasurementUnit = "°C",
                    Description     = "Temperature is a physical quantity expressing hot and cold.",
                    ValueType       = ValueTypeEnum.Float
                },

                //5. open/close - open / close
                new Sensor()
                {
                    Id              = new Guid("99234723-7492-4eb3-8e44-83468263080b"),
                    Label           = "openclose",
                    FriendlyLabel   = "Open/Close Sensor",
                    MeasurementUnit = "",
                    Description     = "It can be attached to any door and window.",
                    ValueType       = ValueTypeEnum.Boolean
                },

                //6. humidity - %
                new Sensor()
                {
                    Id              = new Guid("7529a87a-3bd2-44ad-9624-2d0ee3f40519"),
                    Label           = "hum",
                    FriendlyLabel   = "Humidity",
                    MeasurementUnit = "%",
                    Description     = "Humidity is the amount of water vapour present in air.",
                    ValueType       = ValueTypeEnum.Float
                },

                //7. sound - dBA
                new Sensor()
                {
                    Id              = new Guid("b8f4a465-6e36-4eaa-b0cd-ef6826f1b42f"),
                    Label           = "sound",
                    FriendlyLabel   = "Sound Sensor",
                    MeasurementUnit = "dBA",
                    Description     = "The Sound Sensor records noise levels, due to its integrated microphone.",
                    ValueType       = ValueTypeEnum.Float
                },
            };

            // create devices

            var devices = new Device[]
            {
                new Device()
                {
                    Id                = new Guid("c06c5c27-eddc-499a-815a-44dc4b866735"),
                    DeviceName        = "device001",
                    IntervalInSeconds = 3,
                    IsActivated       = true,
                    IsPublic          = false,
                    IsDeleted         = false,
                    UserId            = new Guid("c0961b4f-466c-4844-bd95-9f7bfa62cc7d")
                },

                new Device()
                {
                    Id                = new Guid("b5e6b650-011f-42ea-8d14-153fa819cf98"),
                    DeviceName        = "device002",
                    IntervalInSeconds = 2,
                    IsActivated       = true,
                    IsPublic          = true,
                    IsDeleted         = false,
                    UserId            = new Guid("c0961b4f-466c-4844-bd95-9f7bfa62cc7d")
                },

                new Device()
                {
                    Id                = new Guid("77f2aa97-f5a0-40e0-a811-5aaf59626356"),
                    DeviceName        = "device003",
                    IntervalInSeconds = 5,
                    IsActivated       = true,
                    IsPublic          = false,
                    IsDeleted         = false,
                    UserId            = new Guid("c0961b4f-466c-4844-bd95-9f7bfa62cc7d")
                },
            };

            var deviceSensors = new DeviceSensor[]
            {
                new DeviceSensor()
                {
                    DeviceId = devices[0].Id,
                    SensorId = sensors[0].Id,
                    MinValue = 5,
                    MaxValue = 50
                },
                new DeviceSensor()
                {
                    DeviceId = devices[0].Id, SensorId = sensors[1].Id
                },

                new DeviceSensor()
                {
                    DeviceId = devices[1].Id, SensorId = sensors[5].Id
                },
                new DeviceSensor()
                {
                    DeviceId = devices[1].Id, SensorId = sensors[6].Id
                },
                new DeviceSensor()
                {
                    DeviceId = devices[1].Id, SensorId = sensors[7].Id
                },

                new DeviceSensor()
                {
                    DeviceId = devices[2].Id, SensorId = sensors[4].Id
                },
            };

            modelBuilder.Entity <Device>()
            .HasData(devices);

            modelBuilder.Entity <Sensor>()
            .HasData(sensors);

            modelBuilder.Entity <DeviceSensor>()
            .HasData(deviceSensors);
        }