Move functions to module level

This commit is contained in:
Sven van Heugten 2026-01-17 15:54:31 +01:00
parent 1e6788ac48
commit e422f03217

View file

@ -27,21 +27,21 @@ type internal NightLightState =
{ Time: DateTime { Time: DateTime
LightToState: Map<Light, LightState> } LightToState: Map<Light, LightState> }
type NightLightStateMachine private (maybeState: NightLightState option) = let internal withStateFor (light: Light) (state: State) (oldNightLightState: NightLightState) =
new() = NightLightStateMachine None
member this.OnEventReceived(event: Event) : Result<NightLightStateMachine * Message seq, OnEventReceivedError> =
result {
let withUpdatedStateFor (light: Light) (state: State) (oldNightLightState: NightLightState) =
let oldState = oldNightLightState.LightToState[light] let oldState = oldNightLightState.LightToState[light]
{ oldNightLightState with { oldNightLightState with
LightToState = Map.add light { oldState with State = state } oldNightLightState.LightToState } LightToState = Map.add light { oldState with State = state } oldNightLightState.LightToState }
let withUpdatedStateForRemoteControlledLights (state: State) (oldNightLightState: NightLightState) = let internal withStateForRemoteControlledLights (state: State) (oldNightLightState: NightLightState) =
remoteControlledLights remoteControlledLights
|> Seq.fold (fun acc light -> acc |> withUpdatedStateFor light state) oldNightLightState |> Seq.fold (fun acc light -> acc |> withStateFor light state) oldNightLightState
type NightLightStateMachine private (maybeState: NightLightState option) =
new() = NightLightStateMachine None
member this.OnEventReceived(event: Event) : Result<NightLightStateMachine * Message seq, OnEventReceivedError> =
result {
match event, maybeState with match event, maybeState with
| ReceivedZigbeeEvent payload, Some state -> | ReceivedZigbeeEvent payload, Some state ->
let! zigbeeEvent = parseZigbeeEvent payload |> Result.mapError ParseZigbeeEventError let! zigbeeEvent = parseZigbeeEvent payload |> Result.mapError ParseZigbeeEventError
@ -58,16 +58,16 @@ type NightLightStateMachine private (maybeState: NightLightState option) =
| ButtonPress action -> | ButtonPress action ->
let newState = let newState =
match action with match action with
| PressedOn -> state |> withUpdatedStateForRemoteControlledLights On | PressedOn -> state |> withStateForRemoteControlledLights On
| PressedOff -> state |> withUpdatedStateForRemoteControlledLights Off | PressedOff -> state |> withStateForRemoteControlledLights Off
| PressedLeft -> | PressedLeft ->
let lightThatShouldBeOn = let lightThatShouldBeOn =
remoteControlledLights remoteControlledLights
|> Seq.find (fun light -> light.ControlledWithRemote = RemoteLeft) |> Seq.find (fun light -> light.ControlledWithRemote = RemoteLeft)
state state
|> withUpdatedStateForRemoteControlledLights Off |> withStateForRemoteControlledLights Off
|> withUpdatedStateFor lightThatShouldBeOn On |> withStateFor lightThatShouldBeOn On
NightLightStateMachine(Some newState), NightLightStateMachine(Some newState),
remoteControlledLights remoteControlledLights