public (bool?IsNecessary, string content) GetSwtichContent(HueGroupDto currentGroup, HueSwitchPutDto switchDto) { var newPutDto = new HueGroupStatePutDto { Id = switchDto.Id, On = !currentGroup.Action.On }; return(this.GetContent(currentGroup, newPutDto, false)); }
public async Task <ActionResult> ChangeState([FromBody] HueGroupStatePutDto statePutDto) { if (statePutDto != null && await this._apiHandler.ChangeGroupState(statePutDto)) { return(this.Ok()); } else { return(this.BadRequest("Lampen Status konnte nicht geändert werden, bitte Logs ansehen")); } }
public Task <bool> ChangeGroupState(HueGroupStatePutDto hueGroupState) { return(Task.Run(async() => { try { var currentGroup = await this.GetInfo(hueGroupState.Id) ?? throw new ArgumentException($"Unter der Id {hueGroupState.Id} konnte keine Gruppe gefunden werden"); var content = new GroupStateHandler(this._logger, this._mapper).GetPostContent(currentGroup, hueGroupState); if (content.isNecessary.HasValue && content.isNecessary.Value) { var response = await this._config["BridgeIP"] .AppendPathSegments("api", this._config["DefaultApiUser"], "groups", hueGroupState.Id, "action") .ToUri().PutAsJsonAsync(content.content); return this._responseHandler.ResponseContainsErrors(response); } else { if (content.isNecessary.HasValue) // Status ist schon eingeschalten { this._logger?.LogInformation("Gruppe hat einen ungültigen Status und kann nicht geändert werden"); return true; } else { this._logger?.LogCritical("Serializierung hat nicht funktioniert bitte, Log ansehen"); return false; } } } catch (Exception ex) { this._logger?.LogError(ex, "Während Change State Error"); return false; } })); }
private (bool?IsNecessary, string content) GetContent(HueGroupDto currentGroup, HueGroupStatePutDto state, bool withoutOn) { object content = null; if (withoutOn) { content = this._mapper.Map <GroupStateWithoutOnPut>(state); } else { content = this._mapper.Map <GroupStateWithOnPut>(state); } if (content == null) { this._logger?.LogError($"{nameof(GroupStateHandler)}hat keinen gültigen Status Wert"); return(false, string.Empty); } try { return(true, LowerCaseSerializer.SerializeObjectWithoutNullProperties(content)); } catch (Exception ex) { this._logger?.LogError(ex, "Während der Serialisierung trat ein Fehler auf"); return(null, string.Empty); } }
public (bool?isNecessary, string content) GetPostContent(HueGroupDto currentGroup, HueGroupStatePutDto wantedState) { if (wantedState.On != currentGroup.Action.On) { return(this.GetContent(currentGroup, wantedState, false)); } else { if (currentGroup.Action.On) // Soll laut Api nicht immer mit On auf True schicken wenn dieser bereits True ist { return(this.GetContent(currentGroup, wantedState, true)); } else { this._logger?.LogDebug($"Aktueller Status von Gruppe {currentGroup.Name} " + $"hat bereits den gewünschten Status {currentGroup.Action.On}"); return(false, string.Empty); } } }