Simplify even more

This commit is contained in:
Sven van Heugten 2026-01-17 15:48:47 +01:00
parent 9de9aefcdf
commit 1e6788ac48

View file

@ -32,18 +32,15 @@ type NightLightStateMachine private (maybeState: NightLightState option) =
member this.OnEventReceived(event: Event) : Result<NightLightStateMachine * Message seq, OnEventReceivedError> = member this.OnEventReceived(event: Event) : Result<NightLightStateMachine * Message seq, OnEventReceivedError> =
result { result {
let updateLightStateForRemoteControlledLights (oldLightToState: Map<Light, LightState>) desiredLightState = let withUpdatedStateFor (light: Light) (state: State) (oldNightLightState: NightLightState) =
remoteControlledLights let oldState = oldNightLightState.LightToState[light]
|> Seq.fold
(fun acc key ->
let oldState = oldLightToState[key]
Map.add { oldNightLightState with
key LightToState = Map.add light { oldState with State = state } oldNightLightState.LightToState }
{ oldState with
State = desiredLightState } let withUpdatedStateForRemoteControlledLights (state: State) (oldNightLightState: NightLightState) =
acc) remoteControlledLights
oldLightToState |> Seq.fold (fun acc light -> acc |> withUpdatedStateFor light state) oldNightLightState
match event, maybeState with match event, maybeState with
| ReceivedZigbeeEvent payload, Some state -> | ReceivedZigbeeEvent payload, Some state ->
@ -59,27 +56,23 @@ type NightLightStateMachine private (maybeState: NightLightState option) =
| Some light -> generateZigbeeCommandsToFixLight state.LightToState[light] light | Some light -> generateZigbeeCommandsToFixLight state.LightToState[light] light
| None -> Seq.empty | None -> Seq.empty
| ButtonPress action -> | ButtonPress action ->
let newLightToState = let newState =
match action with match action with
| PressedOn -> updateLightStateForRemoteControlledLights state.LightToState On | PressedOn -> state |> withUpdatedStateForRemoteControlledLights On
| PressedOff -> updateLightStateForRemoteControlledLights state.LightToState Off | PressedOff -> state |> withUpdatedStateForRemoteControlledLights Off
| PressedLeft -> | PressedLeft ->
let lightThatShouldBeOn = let lightThatShouldBeOn =
remoteControlledLights remoteControlledLights
|> Seq.find (fun light -> light.ControlledWithRemote = RemoteLeft) |> Seq.find (fun light -> light.ControlledWithRemote = RemoteLeft)
let oldState = state.LightToState[lightThatShouldBeOn] state
|> withUpdatedStateForRemoteControlledLights Off
|> withUpdatedStateFor lightThatShouldBeOn On
updateLightStateForRemoteControlledLights state.LightToState Off NightLightStateMachine(Some newState),
|> Map.add lightThatShouldBeOn { oldState with State = On }
NightLightStateMachine(
Some
{ state with
LightToState = newLightToState }
),
remoteControlledLights remoteControlledLights
|> Seq.collect (fun light -> generateZigbeeCommandsToFixLight newLightToState[light] light) |> Seq.collect (fun light ->
generateZigbeeCommandsToFixLight newState.LightToState[light] light)
| TimeChanged newTime, maybeState -> | TimeChanged newTime, maybeState ->
let newPartOfDay = getPartOfDay newTime let newPartOfDay = getPartOfDay newTime