diff --git a/NightLight.Core/Models.fs b/NightLight.Core/Models.fs index d0206f7..b7a31ad 100644 --- a/NightLight.Core/Models.fs +++ b/NightLight.Core/Models.fs @@ -98,7 +98,7 @@ type internal Color = | ColorByCoordinates of float * float | ColorByTemperature of int -type internal LightState = +type internal LightSettings = { State: State Brightness: Brightness Color: Color } diff --git a/NightLight.Core/NightLightStateMachine.fs b/NightLight.Core/NightLightStateMachine.fs index 59fdbc5..5fd91a5 100644 --- a/NightLight.Core/NightLightStateMachine.fs +++ b/NightLight.Core/NightLightStateMachine.fs @@ -11,29 +11,29 @@ open FsToolkit.ErrorHandling let internal tryFindLight friendlyName = Seq.tryFind (fun light -> (lightProps light).FriendlyName = friendlyName) lights -let internal generateZigbeeCommandsToFixLight (light: Light) (desiredLightState: LightState) = +let internal generateZigbeeCommandsToFixLight (light: Light) (desiredLightSettings: LightSettings) = seq { - if desiredLightState.State = Off then - yield generateStateCommand desiredLightState.State light + if desiredLightSettings.State = Off then + yield generateStateCommand desiredLightSettings.State light - if desiredLightState.State = On then - yield generateBrightnessCommand light desiredLightState.Brightness - yield generateColorCommand light desiredLightState.Color + if desiredLightSettings.State = On then + yield generateBrightnessCommand light desiredLightSettings.Brightness + yield generateColorCommand light desiredLightSettings.Color } type internal NightLightState = { Time: DateTime Alarm: bool - LightToState: Map } + LightToLightSettings: Map } let internal createOrUpdateNightLightState (time: DateTime) (alarm: bool) - (maybeOldLightToState: Map option) + (maybeOldLightToLightSettings: Map option) = let partOfDay = getPartOfDay time - let lightToState = + let lightToLightSettings = lights |> Seq.map (fun light -> let color, brightness = @@ -41,8 +41,8 @@ let internal createOrUpdateNightLightState |> getDesiredColorAndBrightness (lightProps light).Bulb let previousState = - maybeOldLightToState - |> Option.map (fun lightToState -> lightToState[light].State) + maybeOldLightToLightSettings + |> Option.map (fun lightToLightSettings -> lightToLightSettings[light].State) |> Option.defaultValue On light, @@ -61,27 +61,27 @@ let internal createOrUpdateNightLightState { Time = time Alarm = alarm - LightToState = lightToState } + LightToLightSettings = lightToLightSettings } let internal withStateFor (light: Light) (state: State) (oldNightLightState: NightLightState) = - let oldState = oldNightLightState.LightToState[light] + let oldState = oldNightLightState.LightToLightSettings[light] createOrUpdateNightLightState oldNightLightState.Time oldNightLightState.Alarm - (Map.add light { oldState with State = state } oldNightLightState.LightToState + (Map.add light { oldState with State = state } oldNightLightState.LightToLightSettings |> Some) let internal withAlarmOff (oldNightLightState: NightLightState) = - createOrUpdateNightLightState oldNightLightState.Time false (Some oldNightLightState.LightToState) + createOrUpdateNightLightState oldNightLightState.Time false (Some oldNightLightState.LightToLightSettings) let internal generateZigbeeCommandsForDifference (maybeBefore: NightLightState option) (after: NightLightState) = - after.LightToState + after.LightToLightSettings |> Seq.collect (fun (KeyValue(light, newState)) -> - let oldState = maybeBefore |> Option.map _.LightToState[light] + let oldState = maybeBefore |> Option.map _.LightToLightSettings[light] if oldState <> Some newState then - generateZigbeeCommandsToFixLight light after.LightToState[light] + generateZigbeeCommandsToFixLight light after.LightToLightSettings[light] else Seq.empty) @@ -101,7 +101,7 @@ type NightLightStateMachine private (maybeState: NightLightState option) = this, match maybeLight with - | Some light -> generateZigbeeCommandsToFixLight light currentState.LightToState[light] + | Some light -> generateZigbeeCommandsToFixLight light currentState.LightToLightSettings[light] | None -> Seq.empty | ButtonPress action -> let newNightLightState = @@ -143,7 +143,7 @@ type NightLightStateMachine private (maybeState: NightLightState option) = || maybeCurrentState |> Option.map _.Alarm |> Option.defaultValue false let newNightLightState = - createOrUpdateNightLightState newTime alarm (maybeCurrentState |> Option.map _.LightToState) + createOrUpdateNightLightState newTime alarm (maybeCurrentState |> Option.map _.LightToLightSettings) return NightLightStateMachine(Some newNightLightState),