Esempio n. 1
0
 public DetectSensorCommand(ILogger <DetectSensorCommand> logger, IDeviceCommunicationService deviceService,
                            IDeviceLockManager deviceLockManager, DatabaseContext databaseContext,
                            LogEntryHandler logEntryHandler,
                            IJobCancellationToken cancellationToken)
 {
     this.logger            = logger;
     this.deviceLockManager = deviceLockManager;
     this.databaseContext   = databaseContext;
     this.logEntryHandler   = logEntryHandler;
     this.deviceService     = deviceService;
     this.cancellationToken = cancellationToken.ShutdownToken;
 }
 public DetectDeviceCommand(ILogger <DetectDeviceCommand> logger, IDeviceLockManager deviceLockManager,
                            DatabaseContext databaseContext, IDeviceCommunicationService deviceService,
                            IOptions <JsonOptions> options, LogEntryHandler logEntryHandler,
                            IJobCancellationToken cancellationToken)
 {
     this.logger                = logger;
     this.databaseContext       = databaseContext;
     this.deviceService         = deviceService;
     this.logEntryHandler       = logEntryHandler;
     this.deviceLockManager     = deviceLockManager;
     this.jsonSerializerOptions = options.Value.JsonSerializerOptions;
     this.cancellationToken     = cancellationToken.ShutdownToken;
 }
Esempio n. 3
0
        public async Task <Expression <Func <DatabaseContext, Sensor> > > EditSensor(DatabaseContext databaseContext, EditSensorParameters model, LogEntryHandler logEntryHandler)
        {
            var sensor = await databaseContext.Sensors.GetRequiredByIdAsync(model.Id);

            await using (var logEntry = logEntryHandler.AddLogEntry(LogEntryEvent.Edit, sensor: sensor))
            {
                try
                {
                    var plant = model.PlantId.HasValue ? await databaseContext.Plants.GetRequiredByIdAsync(model.PlantId.Value) : null;

                    sensor.MACAddress = model.MACAddress;
                    sensor.Name       = model.Name;
                    sensor.Plant      = plant;
                    await databaseContext.SaveChangesAsync();

                    logEntry.Success();
                    return(ctx => ctx.Sensors.GetRequiredById(sensor.Id));
                }
                catch (Exception ex)
                {
                    logEntry.Failure(ex, "Failed to edit Sensor!");
                    throw;
                }
            }
        }
Esempio n. 4
0
        public async Task <Sensor> DeleteSensor(DatabaseContext databaseContext, DeleteSensorParameters model, LogEntryHandler logEntryHandler)
        {
            //get current user, try to convert this into a async method so we can get the user!
            var sensor = await databaseContext.Sensors.GetRequiredByIdAsync(model.Id);

            await using (var logEntry = logEntryHandler.AddLogEntry(LogEntryEvent.Delete, sensor: sensor))
            {
                try
                {
                    databaseContext.Sensors.Remove(sensor);
                    await databaseContext.SaveChangesAsync();

                    logEntry.Success();
                    return(sensor);
                }
                catch (Exception ex)
                {
                    logEntry.Failure(ex, "Failed to delete Sensor!");
                    throw;
                }
            }
        }
        public async Task <Plant> DeletePlant(DatabaseContext databaseContext, DeletePlantParameters model, LogEntryHandler logEntryHandler)
        {
            var plant = await databaseContext.Plants.GetRequiredByIdAsync(model.Id);

            await using (var logEntry = logEntryHandler.AddLogEntry(LogEntryEvent.Delete, plant: plant))
            {
                try
                {
                    databaseContext.Plants.Remove(plant);
                    await databaseContext.SaveChangesAsync();

                    logEntry.Success();
                    return(plant);
                }
                catch (Exception ex)
                {
                    logEntry.Failure(ex, "Failed to delete plant!");
                    throw;
                }
            }
        }
        public async Task <Expression <Func <DatabaseContext, Plant> > > EditPlant(DatabaseContext databaseContext, EditPlantParameters model, LogEntryHandler logEntryHandler)
        {
            var plant = await databaseContext.Plants.GetRequiredByIdAsync(model.Id);

            await using (var logEntry = logEntryHandler.AddLogEntry(LogEntryEvent.Edit, plant: plant))
            {
                try
                {
                    plant.LatinName = model.LatinName;
                    plant.Alias     = model.Alias;
                    plant.Display   = model.Display;
                    plant.ImageUrl  = model.ImageUrl;

                    plant.Basic.Blooming       = model.Blooming;
                    plant.Basic.Category       = model.Category;
                    plant.Basic.Color          = model.Color;
                    plant.Basic.FloralLanguage = model.FloralLanguage;
                    plant.Basic.Origin         = model.Origin;
                    plant.Basic.Production     = model.Production;

                    plant.Maintenance.Fertilization = model.Fertilization;
                    plant.Maintenance.Pruning       = model.Pruning;
                    plant.Maintenance.Size          = model.Size;
                    plant.Maintenance.Soil          = model.Soil;
                    plant.Maintenance.Sunlight      = model.Sunlight;
                    plant.Maintenance.Watering      = model.Watering;

                    plant.Parameters.EnvironmentHumidity = new Database.Range(model.MinEnvironmentHumidity, model.MaxEnvironmentHumidity);
                    plant.Parameters.LightLux            = new Database.Range(model.MinLightLux, model.MaxLightLux);
                    plant.Parameters.LightMmol           = new Database.Range(model.MinLightMmol, model.MaxLightMmol);
                    plant.Parameters.SoilFertility       = new Database.Range(model.MinSoilFertility, model.MaxSoilFertility);
                    plant.Parameters.SoilHumidity        = new Database.Range(model.MinSoilHumidity, model.MaxSoilHumidity);
                    plant.Parameters.Temperature         = new Database.Range(model.MinTemperature, model.MaxTemperature);

                    await databaseContext.SaveChangesAsync();

                    logEntry.Success();
                    return(ctx => ctx.Plants.GetRequiredById(plant.Id));
                }
                catch (Exception ex)
                {
                    logEntry.Failure(ex, "Failed to edit plant!");
                    throw;
                }
            }
        }
        public async Task <Expression <Func <DatabaseContext, Plant> > > AddPlant(DatabaseContext databaseContext, AddPlantParameters model, LogEntryHandler logEntryHandler)
        {
            await using (var logEntry = logEntryHandler.AddLogEntry(LogEntryEvent.Add))
            {
                try
                {
                    var plant = new Plant()
                    {
                        LatinName = model.LatinName,
                        Alias     = model.Alias,
                        Display   = model.Display,
                        ImageUrl  = model.ImageUrl,
                        Basic     = new PlantBasic
                        {
                            Blooming       = model.Blooming,
                            Category       = model.Category,
                            Color          = model.Color,
                            FloralLanguage = model.FloralLanguage,
                            Origin         = model.Origin,
                            Production     = model.Production
                        },
                        Maintenance = new PlantMaintenance
                        {
                            Fertilization = model.Fertilization,
                            Pruning       = model.Pruning,
                            Size          = model.Size,
                            Soil          = model.Soil,
                            Sunlight      = model.Sunlight,
                            Watering      = model.Watering
                        },
                        Parameters = new PlantParameters
                        {
                            EnvironmentHumidity = new Database.Range(model.MinEnvironmentHumidity, model.MaxEnvironmentHumidity),
                            LightLux            = new Database.Range(model.MinLightLux, model.MaxLightLux),
                            LightMmol           = new Database.Range(model.MinLightMmol, model.MaxLightMmol),
                            SoilFertility       = new Database.Range(model.MinSoilFertility, model.MaxSoilFertility),
                            SoilHumidity        = new Database.Range(model.MinSoilHumidity, model.MaxSoilHumidity),
                            Temperature         = new Database.Range(model.MinTemperature, model.MaxTemperature)
                        }
                    };
                    databaseContext.Plants.Add(plant);
                    await databaseContext.SaveChangesAsync();

                    logEntry.Attach(plant);
                    logEntry.Success();
                    return(ctx => ctx.Plants.GetRequiredById(plant.Id));
                }
                catch (Exception ex)
                {
                    logEntry.Failure(ex, "Failed to add plant!");
                    throw;
                }
            }
        }
Esempio n. 8
0
 public UWPPlatform(LogEntryHandler handler)
 {
     _handler         = handler;
     TemporaryStorage = new TokenStore();
 }
        public async Task <Expression <Func <DatabaseContext, Device> > > EditDevice(DatabaseContext databaseContext, EditDeviceParameters model, LogEntryHandler logEntryHandler)
        {
            var device = await databaseContext.Devices.GetRequiredByIdAsync(model.Id);

            await using (var logEntry = logEntryHandler.AddLogEntry(LogEntryEvent.Edit, device: device))
            {
                try
                {
                    device.MACAddress = model.MACAddress;
                    device.IPAddress  = model.IPAddress;
                    device.Port       = model.Port;
                    device.Name       = model.Name;
                    await databaseContext.SaveChangesAsync();

                    logEntry.Success();
                    return(ctx => ctx.Devices.Single(x => x.Id == device.Id));
                }
                catch (Exception ex)
                {
                    logEntry.Failure(ex, "Failed to edit device!");
                    throw;
                }
            }
        }
        public async Task <Expression <Func <DatabaseContext, Device> > > AddDevice(DatabaseContext databaseContext, AddDeviceParameters model, LogEntryHandler logEntryHandler)
        {
            await using (var logEntry = logEntryHandler.AddLogEntry(LogEntryEvent.Add))
            {
                try
                {
                    var device = new Device()
                    {
                        MACAddress = model.MACAddress,
                        IPAddress  = model.IPAddress,
                        Port       = model.Port,
                        Name       = model.Name
                    };
                    databaseContext.Devices.Add(device);
                    await databaseContext.SaveChangesAsync();

                    logEntry.Attach(device);
                    logEntry.Success();
                    return(ctx => ctx.Devices.GetRequiredById(device.Id));
                }
                catch (Exception ex)
                {
                    logEntry.Failure(ex, "Failed to add device!");
                    throw;
                }
            }
        }
        public async Task <Device> DeleteDevice(DatabaseContext databaseContext, DeleteDeviceParameters model, LogEntryHandler logEntryHandler)
        {
            var device = await databaseContext.Devices.GetRequiredByIdAsync(model.Id);

            await using (var logEntry = logEntryHandler.AddLogEntry(LogEntryEvent.Delete, device: device))
            {
                try
                {
                    databaseContext.Devices.Remove(device);
                    await databaseContext.SaveChangesAsync();

                    logEntry.Success();
                    return(device);
                }
                catch (Exception ex)
                {
                    logEntry.Failure(ex, "Failed to delete device!");
                    throw;
                }
            }
        }