Beispiel #1
0
        public async Task <ActionResult> ChangeState([FromBody] HueLightStatePutDto statePutDto)
        {
            if (statePutDto != null && await this._caller.ChangeLightState(statePutDto))
            {
                return(this.Ok());
            }

            else
            {
                return(this.BadRequest("Lampen Status konnte nicht geändert werden, bitte Logs ansehen"));
            }
        }
        public Task <bool> ChangeLightState(HueLightStatePutDto statePutDto)
        {
            bool result = false;

            return(Task.Run <bool>(async() =>
            {
                try
                {
                    var currentState = await this.GetInfo(statePutDto.Id) ?? throw new Exception("Lampe mit der ID {statePutDto.Id} konnte nicht gefunden werden !! Ist diese Registriert ? ");

                    var content = new LightStateHandler(this._logger, this._mapper).GetSwitchStateContent(currentState, statePutDto);

                    if (content.IsNecessary.HasValue && content.IsNecessary.Value)
                    {
                        var response = await this._configuration["BridgeIP"]
                                       .AppendPathSegments("api", this._configuration["DefaultApiUser"], "lights", statePutDto.Id, "state")
                                       .ToUri()
                                       .PutAsJsonAsync(content.content);

                        return this._responseHandler.ResponseContainsErrors(response);
                    }

                    else
                    {
                        if (content.IsNecessary.HasValue)
                        {
                            this._logger?.LogInformation("Lampe hat einen ungültigen Status und kann nicht geändert werden");

                            result = true;
                        }

                        else
                        {
                            this._logger?.LogCritical("Serializierung hat nicht funktioniert bitte, Log ansehen");

                            result = false;
                        }
                    }

                    return result;
                }

                catch (Exception ex)
                {
                    this._logger.LogError(ex, "Generelle Exception während ChangeLightState");
                    return false;
                }
            }));
        }
Beispiel #3
0
        private object MapToType(EHueDeviceType deviceType, HueLightStatePutDto state, bool withoutOn)
        {
            object contentDto = null;

            switch (deviceType)
            {
            case EHueDeviceType.Extended_Color_Light:
                contentDto = withoutOn ? (object)this._mapper.Map <ExtendedColorLightWithoutOnPutDto>(state) : this._mapper.Map <ExtendedColorLightStatePutDto>(state);
                break;

            case EHueDeviceType.Temperature_Light:
                contentDto = withoutOn ? (object)this._mapper.Map <ColorTemperaturWithoutOnPutDto>(state) :  this._mapper.Map <ColorTemperatureLightPutStateDto>(state);
                break;

            default:
                contentDto = null;
                break;
            }

            return(contentDto);
        }
Beispiel #4
0
        public (bool?IsNecessary, string content) GetSwitchStateContent(HueDeviceDto currentDevice,
                                                                        HueLightStatePutDto state)
        {
            if (state.On != currentDevice.State.On)
            {
                return(this.GetContent(currentDevice, state, false));
            }

            else
            {
                if (currentDevice.State.On) // Soll laut Api nicht immer mit On auf True schicken wenn dieser bereits True ist
                {
                    return(this.GetContent(currentDevice, state, true));
                }

                else
                {
                    this._logger?.LogDebug($"Aktueller Status von Light {state.Id} " +
                                           $"hat bereits den gewünschten Status {state.On}");

                    return(false, string.Empty);
                }
            }
        }
Beispiel #5
0
        private (bool?IsNecessary, string content) GetContent(HueDeviceDto currentDevice, HueLightStatePutDto state, bool withoutOn)
        {
            if (TryParseHueDeviceType(currentDevice, out var deviceType))
            {
                var contentDto = this.MapToType(deviceType, state, withoutOn);

                if (contentDto == null)
                {
                    this._logger?.LogError($"{nameof(LightStateHandler)} hat den DeviceType {currentDevice.Type} nicht implementiert");

                    return(false, string.Empty);
                }

                else
                {
                    try
                    {
                        return(true, LowerCaseSerializer.SerializeObject(contentDto));
                    }

                    catch (Exception ex)
                    {
                        this._logger?.LogError(ex, "Während der Serialisierung trat ein Fehler auf");

                        return(null, string.Empty);
                    }
                }
            }

            else
            {
                this._logger?.LogError($"Light {currentDevice.Name} hat einen ungültigen Type {currentDevice.Type}");
                return(null, string.Empty);
            }
        }