Beispiel #1
0
        public async Task <IActionResult> EditDevices(AppUserDevicesModificationViewModel model)
        {
            List <AppUserDevice> appUserDeviceAssociationsToAdd = new List <AppUserDevice>();

            if (model.DeviceIdsToAdd != null && model.DeviceIdsToAdd.Length != 0)
            {
                foreach (Guid id in model.DeviceIdsToAdd)
                {
                    appUserDeviceAssociationsToAdd.Add(new AppUserDevice(model.UserId, id));
                }
                await _dbContext.AppUserDeviceAssociation.AddRangeAsync(appUserDeviceAssociationsToAdd);
            }

            List <AppUserDevice> toDelete = new List <AppUserDevice>();

            if (model.DeviceIdsToRemove != null && model.DeviceIdsToRemove.Length != 0)
            {
                foreach (Guid id in model.DeviceIdsToRemove)
                {
                    toDelete.AddRange(_dbContext.AppUserDeviceAssociation.Where(p => p.AppUserId == model.UserId).Where(d => d.DeviceId == id));
                }

                _dbContext.AppUserDeviceAssociation.RemoveRange(toDelete);
                await _dbContext.SaveChangesAsync();
            }

            await _dbContext.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
        public IActionResult CreateDevice(Device model)
        {
            if (_dbContext.Devices.ToList().Any(elem => elem.Name == model.Name))
            {
                ModelState.AddModelError("", "A device with the same name already exists");
            }

            if (ModelState.IsValid)
            {
                Device device = new Device(model.Name, model.Lake, model.Location, model.Depth);
                _dbContext.Add(device);
                _dbContext.SaveChangesAsync();
                return(RedirectToAction("Index"));
            }
            return(View(model));
        }
        public async Task SaveValuesToDbAsync(ValueModel model)
        {
            Enums.SensorTypes sensorType = model.SensorType;

            List <ValueItemModel> valueItemsToSave = model.Items;
            IEnumerable <Value>   values           = await _dbContext.QueryValuesAsync(p => p, sensorType);

            if (values.Count() != 0)
            { //TODO: this prevents older values from different devices to be stored, check for device and time (not only for time)
                DateTime latest = values.Select(p => p.Timestamp).Max();
                valueItemsToSave = model.Items.Where(d => d.Timestamp > latest).ToList();
            }

            List <Device> deviceList = (List <Device>) await _dbContext.QueryDevicesAsync(model.DeviceName);

            Device device;

            if (deviceList.Count() == 0)
            {
                device = new Device(model.DeviceName, "not defined", "not defined", Enums.Depth.NotDefined);
                await _deviceStorage.SaveNewDeviceToDbAsync(device);
            }
            else
            {
                //TODO check for every value by its timestamp, which device is the correct one and do not just take the latest.

                device = deviceList.MaxBy(p => p.TimeOfCreation);
            }

            List <Value> valuesToSave = new List <Value>();

            foreach (var value in valueItemsToSave)
            {
                valuesToSave.Add(new Value(value.Timestamp, device, value.Data, sensorType));
            }

            await _dbContext.Values.AddRangeAsync(valuesToSave);

            await _dbContext.SaveChangesAsync();
        }
        public async Task SaveNewDeviceToDbAsync(Device device)
        {
            await _dbContext.Devices.AddAsync(device);

            await _dbContext.SaveChangesAsync();
        }