/// <inheritdoc />
        public void Refresh()
        {
            lock (_writeLock)
            {
                if (File.Exists(_deviceConfigFile))
                {
                    var deviceConfigurationString = File.ReadAllText(_deviceConfigFile);
                    _devices = new ConcurrentDictionary <string, Device>(JsonConvert.DeserializeObject <Dictionary <string, Device> >(deviceConfigurationString));

                    // Validate the config
                    foreach (var device in _devices)
                    {
                        var errors = DeviceValidator.Validate(device.Value);
                        if (errors.Any())
                        {
                            _logger.LogWarning("GoogleDevices.json issues detected for device {Device}: {DeviceConfigIssues}", device.Key, errors);
                        }
                    }

                    _logger.LogInformation($"Loaded device configuration from {_deviceConfigFile}");
                }
                else
                {
                    _devices = new ConcurrentDictionary <string, Device>();
                    _logger.LogInformation($"Device configuration file {_deviceConfigFile} not found, starting with empty set");
                }
            }
        }
        public void CanValidate()
        {
            // Arrange
            if (File.Exists(_testFilePath))
            {
                var deviceConfigurationString = File.ReadAllText(_testFilePath);
                var devices = new ConcurrentDictionary <string, Device>(JsonConvert.DeserializeObject <Dictionary <string, Device> >(deviceConfigurationString));

                // Act
                var errors = devices.Select(x => DeviceValidator.Validate(x.Value));

                // Assert
                Assert.All(errors, result => Assert.Empty(result));
            }
        }
        public IActionResult InsertDevice([Microsoft.AspNetCore.Mvc.FromBody] MedicalDevice device)
        {
            if (!DeviceValidator.ValidateDevice(device))
            {
                return(BadRequest("Please enter valid input"));
            }
            try
            {
                _deviceDataRepository.InsertDevice(device);
            }
            catch (Exception)
            {
                return(StatusCode(500, "unable to insert device information"));
            }

            return(Ok("Insertion successful"));
        }
Esempio n. 4
0
        public IActionResult Create(DeviceViewModel viewModel)
        {
            if (_deviceRepository.Contains(viewModel.Id))
            {
                ModelState.AddModelError("Id", "Device Id already exists");
            }

            // Set new values
            var device = new Models.State.Device
            {
                Id              = viewModel.Id,
                Type            = viewModel.Type,
                Disabled        = viewModel.Disabled,
                WillReportState = viewModel.WillReportState,
                RoomHint        = viewModel.RoomHint,
                Name            = new NameInfo
                {
                    Name = viewModel.Name
                },
                Traits = new List <DeviceTrait>()
            };

            // Default names
            if (!string.IsNullOrEmpty(viewModel.DefaultNames))
            {
                device.Name.DefaultNames = viewModel.DefaultNames.Split(',').Select(x => x.Trim()).ToList();
            }
            else
            {
                device.Name.DefaultNames = new List <string>();
            }

            // Nicknames
            if (!string.IsNullOrEmpty(viewModel.Nicknames))
            {
                device.Name.Nicknames = viewModel.Nicknames.Split(',').Select(x => x.Trim()).ToList();
            }
            else
            {
                device.Name.Nicknames = new List <string>();
            }

            // Device Info
            if (!string.IsNullOrEmpty(viewModel.Manufacturer) ||
                !string.IsNullOrEmpty(viewModel.Model) ||
                !string.IsNullOrEmpty(viewModel.HwVersion) ||
                !string.IsNullOrEmpty(viewModel.SwVersion))
            {
                if (device.DeviceInfo == null)
                {
                    device.DeviceInfo = new DeviceInfo();
                }

                device.DeviceInfo.Manufacturer = !string.IsNullOrEmpty(viewModel.Manufacturer) ? viewModel.Manufacturer : null;
                device.DeviceInfo.Model        = !string.IsNullOrEmpty(viewModel.Model) ? viewModel.Model : null;
                device.DeviceInfo.HwVersion    = !string.IsNullOrEmpty(viewModel.HwVersion) ? viewModel.HwVersion : null;
                device.DeviceInfo.SwVersion    = !string.IsNullOrEmpty(viewModel.SwVersion) ? viewModel.SwVersion : null;
            }
            else
            {
                device.DeviceInfo = null;
            }

            // Final validation
            foreach (var error in DeviceValidator.Validate(device))
            {
                ModelState.AddModelError(string.Empty, error);
            }

            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Create"));
            }

            // Save changes
            _deviceRepository.Add(device);

            return(RedirectToAction("Index"));
        }
Esempio n. 5
0
        public IActionResult Edit([Required] string deviceId, DeviceViewModel viewModel)
        {
            if (!_deviceRepository.Contains(deviceId))
            {
                return(NotFound());
            }

            // Set new values
            var device = _deviceRepository.GetDetached(deviceId);

            device.Id              = viewModel.Id;
            device.Type            = viewModel.Type;
            device.Disabled        = viewModel.Disabled;
            device.WillReportState = viewModel.WillReportState;
            device.RoomHint        = viewModel.RoomHint;
            device.Name.Name       = viewModel.Name;

            // Default names
            if (!string.IsNullOrEmpty(viewModel.DefaultNames))
            {
                device.Name.DefaultNames = viewModel.DefaultNames.Split(',').Select(x => x.Trim()).ToList();
            }
            else
            {
                device.Name.DefaultNames = new List <string>();
            }

            // Nicknames
            if (!string.IsNullOrEmpty(viewModel.Nicknames))
            {
                device.Name.Nicknames = viewModel.Nicknames.Split(',').Select(x => x.Trim()).ToList();
            }
            else
            {
                device.Name.Nicknames = new List <string>();
            }

            // Device Info
            if (!string.IsNullOrEmpty(viewModel.Manufacturer) ||
                !string.IsNullOrEmpty(viewModel.Model) ||
                !string.IsNullOrEmpty(viewModel.HwVersion) ||
                !string.IsNullOrEmpty(viewModel.SwVersion))
            {
                if (device.DeviceInfo == null)
                {
                    device.DeviceInfo = new DeviceInfo();
                }

                device.DeviceInfo.Manufacturer = !string.IsNullOrEmpty(viewModel.Manufacturer) ? viewModel.Manufacturer : null;
                device.DeviceInfo.Model        = !string.IsNullOrEmpty(viewModel.Model) ? viewModel.Model : null;
                device.DeviceInfo.HwVersion    = !string.IsNullOrEmpty(viewModel.HwVersion) ? viewModel.HwVersion : null;
                device.DeviceInfo.SwVersion    = !string.IsNullOrEmpty(viewModel.SwVersion) ? viewModel.SwVersion : null;
            }
            else
            {
                device.DeviceInfo = null;
            }

            // Final validation
            foreach (var error in DeviceValidator.Validate(device))
            {
                ModelState.AddModelError(string.Empty, error);
            }

            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Edit", new { deviceId }));
            }

            // Save changes
            _deviceRepository.Update(deviceId, device);

            return(RedirectToAction("Index"));
        }