Ejemplo n.º 1
0
        public IActionResult Create([Required] string deviceId, TraitViewModel viewModel)
        {
            if (!_deviceRepository.Contains(deviceId))
            {
                return(NotFound());
            }

            var device = _deviceRepository.GetDetached(deviceId);

            if (device.Traits.Any(x => x.Trait == viewModel.Trait))
            {
                ModelState.AddModelError("Trait", "Device already contains trait");
            }

            // Set values
            var trait = new DeviceTrait
            {
                Trait      = viewModel.Trait,
                Attributes = !string.IsNullOrEmpty(viewModel.Attributes) ? JsonConvert.DeserializeObject <Dictionary <string, object> >(viewModel.Attributes, new ObjectDictionaryConverter()) : null,
                Commands   = !string.IsNullOrEmpty(viewModel.Commands) ? JsonConvert.DeserializeObject <Dictionary <string, IDictionary <string, string> > >(viewModel.Commands) : new Dictionary <string, IDictionary <string, string> >(),
                State      = !string.IsNullOrEmpty(viewModel.State) ? JsonConvert.DeserializeObject <Dictionary <string, DeviceState> >(viewModel.State) : null
            };

            // Handle any challenges
            switch (viewModel.ChallengeType)
            {
            case ChallengeType.Acknowledge:
                trait.Challenge = new AcknowledgeChallenge();
                break;

            case ChallengeType.Pin:
                trait.Challenge = new PinChallenge
                {
                    Pin = viewModel.ChallengePin
                };
                break;

            case ChallengeType.None:
            default:
                trait.Challenge = null;
                break;
            }

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

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

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

            return(RedirectToAction("Edit", "GoogleDevice", new { deviceId }));
        }
Ejemplo n.º 2
0
        public IActionResult Edit([Required] string deviceId, [Required] string traitId, TraitViewModel viewModel)
        {
            if (!_deviceRepository.Contains(deviceId))
            {
                return(NotFound());
            }

            var device = _deviceRepository.GetDetached(deviceId);

            var traitEnumId = traitId.ToEnum <TraitType>();

            if (!device.Traits.Any(x => x.Trait == traitEnumId))
            {
                return(NotFound());
            }

            // Lock the trait type just in case
            viewModel.Trait = traitEnumId;

            // Set new values
            var trait = device.Traits.FirstOrDefault(x => x.Trait == traitEnumId);

            trait.Attributes = !string.IsNullOrEmpty(viewModel.Attributes) ? JsonConvert.DeserializeObject <Dictionary <string, object> >(viewModel.Attributes, new ObjectDictionaryConverter()) : null;
            trait.Commands   = !string.IsNullOrEmpty(viewModel.Commands) ? JsonConvert.DeserializeObject <Dictionary <string, IDictionary <string, string> > >(viewModel.Commands) : null;
            trait.State      = !string.IsNullOrEmpty(viewModel.State) ? JsonConvert.DeserializeObject <Dictionary <string, DeviceState> >(viewModel.State) : null;

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

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

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

            return(RedirectToAction("Edit", "GoogleDevice", new { deviceId }));
        }
Ejemplo n.º 3
0
        public IActionResult Edit([Required] string deviceId, [Required] string traitId, TraitViewModel viewModel)
        {
            if (!_deviceRepository.Contains(deviceId))
            {
                return(NotFound());
            }

            var device = _deviceRepository.GetDetached(deviceId);

            var traitEnumId = traitId.ToEnum <TraitType>();

            if (!device.Traits.Any(x => x.Trait == traitEnumId))
            {
                return(NotFound());
            }

            // Lock the trait type just in case
            viewModel.Trait = traitEnumId;

            if (DeviceMetadata.SupportedTraits.TryGetValue(device.Type, out IList <TraitType> supportedTraits) && !supportedTraits.Contains(viewModel.Trait))
            {
                ModelState.AddModelError("Trait", "Device does not support trait type");
            }

            // Set new values
            var trait = device.Traits.FirstOrDefault(x => x.Trait == traitEnumId);

            trait.Attributes = !string.IsNullOrEmpty(viewModel.Attributes) ? JsonConvert.DeserializeObject <Dictionary <string, object> >(viewModel.Attributes, new ObjectDictionaryConverter()) : null;
            trait.Commands   = !string.IsNullOrEmpty(viewModel.Commands) ? JsonConvert.DeserializeObject <Dictionary <string, IDictionary <string, string> > >(viewModel.Commands) : null;
            trait.State      = !string.IsNullOrEmpty(viewModel.State) ? JsonConvert.DeserializeObject <Dictionary <string, DeviceState> >(viewModel.State) : null;

            // Handle any challenges
            switch (viewModel.ChallengeType)
            {
            case ChallengeType.Acknowledge:
                trait.Challenge = new AcknowledgeChallenge();
                break;

            case ChallengeType.Pin:
                trait.Challenge = new PinChallenge
                {
                    Pin = viewModel.ChallengePin
                };
                break;

            case ChallengeType.None:
            default:
                trait.Challenge = null;
                break;
            }

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

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

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

            return(RedirectToAction("Edit", "GoogleDevice", new { deviceId }));
        }