Store more state

This commit is contained in:
Sven van Heugten 2026-01-17 15:33:02 +01:00
parent 78b57258c1
commit 437e5bf25d
2 changed files with 36 additions and 12 deletions

View file

@ -87,3 +87,8 @@ type internal Brightness = Brightness of int
type internal Color = type internal Color =
| ColorByCoordinates of float * float | ColorByCoordinates of float * float
| ColorByTemperature of int | ColorByTemperature of int
type internal LightState =
{ State: State
Brightness: Brightness
Color: Color }

View file

@ -28,16 +28,25 @@ let internal generateZigbeeCommandsToFixLight state partOfDay (light: Light) =
type internal NightLightState = type internal NightLightState =
{ Time: DateTime { Time: DateTime
LightToState: Map<Light, State> } LightToState: Map<Light, LightState> }
type NightLightStateMachine private (maybeState: NightLightState option) = type NightLightStateMachine private (maybeState: NightLightState option) =
new() = NightLightStateMachine None new() = NightLightStateMachine None
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 desiredLightState = let updateLightStateForRemoteControlledLights (oldLightToState: Map<Light, LightState>) desiredLightState =
remoteControlledLights remoteControlledLights
|> Seq.fold (fun acc key -> Map.add key desiredLightState acc) oldLightToState |> Seq.fold
(fun acc key ->
let oldState = oldLightToState[key]
Map.add
key
{ oldState with
State = desiredLightState }
acc)
oldLightToState
match event, maybeState with match event, maybeState with
| ReceivedZigbeeEvent payload, | ReceivedZigbeeEvent payload,
@ -53,7 +62,7 @@ type NightLightStateMachine private (maybeState: NightLightState option) =
this, this,
match maybeLight with match maybeLight with
| Some light -> generateZigbeeCommandsToFixLight lightToState[light] partOfDay light | Some light -> generateZigbeeCommandsToFixLight lightToState[light].State partOfDay light
| None -> Seq.empty | None -> Seq.empty
| ButtonPress action -> | ButtonPress action ->
let newLightToState = let newLightToState =
@ -61,11 +70,14 @@ type NightLightStateMachine private (maybeState: NightLightState option) =
| PressedOn -> updateLightStateForRemoteControlledLights lightToState On | PressedOn -> updateLightStateForRemoteControlledLights lightToState On
| PressedOff -> updateLightStateForRemoteControlledLights lightToState Off | PressedOff -> updateLightStateForRemoteControlledLights lightToState Off
| PressedLeft -> | PressedLeft ->
let lightThatShouldBeOn =
remoteControlledLights
|> Seq.find (fun light -> light.ControlledWithRemote = RemoteLeft)
let oldState = lightToState[lightThatShouldBeOn]
updateLightStateForRemoteControlledLights lightToState Off updateLightStateForRemoteControlledLights lightToState Off
|> Map.add |> Map.add lightThatShouldBeOn { oldState with State = On }
(remoteControlledLights
|> Seq.find (fun light -> light.ControlledWithRemote = RemoteLeft))
On
NightLightStateMachine( NightLightStateMachine(
Some Some
@ -74,7 +86,7 @@ type NightLightStateMachine private (maybeState: NightLightState option) =
), ),
remoteControlledLights remoteControlledLights
|> Seq.collect (fun light -> |> Seq.collect (fun light ->
generateZigbeeCommandsToFixLight newLightToState[light] partOfDay light) generateZigbeeCommandsToFixLight newLightToState[light].State partOfDay light)
| TimeChanged newTime, maybeState -> | TimeChanged newTime, maybeState ->
let newPartOfDay = getPartOfDay newTime let newPartOfDay = getPartOfDay newTime
@ -87,8 +99,12 @@ type NightLightStateMachine private (maybeState: NightLightState option) =
let newLightToState = let newLightToState =
lights lights
|> Seq.map (fun light -> |> Seq.map (fun light ->
let color, brightness =
getDesiredMood light.Room newPartOfDay
|> getDesiredColorAndBrightness light.Bulb
let previousState = let previousState =
maybeState |> Option.map _.LightToState[light] |> Option.defaultValue On maybeState |> Option.map _.LightToState[light].State |> Option.defaultValue On
let newState = let newState =
if partOfDayChanged && newPartOfDay = Day then if partOfDayChanged && newPartOfDay = Day then
@ -96,7 +112,10 @@ type NightLightStateMachine private (maybeState: NightLightState option) =
else else
previousState previousState
light, newState) light,
{ Color = color
Brightness = brightness
State = newState })
|> Map.ofSeq |> Map.ofSeq
let newState = let newState =
@ -111,7 +130,7 @@ type NightLightStateMachine private (maybeState: NightLightState option) =
if partOfDayChanged then if partOfDayChanged then
lights lights
|> Seq.collect (fun light -> |> Seq.collect (fun light ->
generateZigbeeCommandsToFixLight newLightToState[light] newPartOfDay light) generateZigbeeCommandsToFixLight newLightToState[light].State newPartOfDay light)
else else
Seq.empty Seq.empty
| _, None -> return! Error TimeIsUnknown | _, None -> return! Error TimeIsUnknown