State -> NightLightStateMachine

This commit is contained in:
Sven van Heugten 2026-01-04 10:24:55 +01:00
parent 285874777a
commit c7c5de9e21
4 changed files with 8 additions and 8 deletions

View file

@ -34,7 +34,7 @@ type FakeLight(light: Light) =
brightness <- newBrightness brightness <- newBrightness
type FakeHome(now: DateTime) = type FakeHome(now: DateTime) =
let mutable nightLightStateMachine = State now let mutable nightLightStateMachine = NightLightStateMachine now
let assertIsOkAndGet result = let assertIsOkAndGet result =
match result with match result with

View file

@ -11,7 +11,7 @@
<Compile Include="Moods.fs" /> <Compile Include="Moods.fs" />
<Compile Include="ZigbeeEvents.fs" /> <Compile Include="ZigbeeEvents.fs" />
<Compile Include="ZigbeeCommands.fs" /> <Compile Include="ZigbeeCommands.fs" />
<Compile Include="Core.fs" /> <Compile Include="NightLightStateMachine.fs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -17,8 +17,8 @@ let internal generateZigbeeCommandToFixLight partOfDay light =
generateZigbeeCommand light.FriendlyName color brightness generateZigbeeCommand light.FriendlyName color brightness
type State(time: DateTime) = type NightLightStateMachine(time: DateTime) =
member this.OnEventReceived(event: Event) : Result<State * Message seq, ParseEventError> = member this.OnEventReceived(event: Event) : Result<NightLightStateMachine * Message seq, ParseEventError> =
result { result {
let partOfDay = getPartOfDay time let partOfDay = getPartOfDay time
@ -36,7 +36,7 @@ type State(time: DateTime) =
| Some light -> generateZigbeeCommandToFixLight partOfDay light |> Seq.singleton | Some light -> generateZigbeeCommandToFixLight partOfDay light |> Seq.singleton
| None -> Seq.empty | None -> Seq.empty
| TimeChanged newTime -> | TimeChanged newTime ->
let newState = State newTime let newState = NightLightStateMachine newTime
let newPartOfDay = getPartOfDay newTime let newPartOfDay = getPartOfDay newTime
return return

View file

@ -29,12 +29,12 @@ let private publishZigbeeCommands (mqttClient: IMqttClient) (logger: ILogger) (c
|> Async.Ignore |> Async.Ignore
} }
let private handleEvent (mqttClient: IMqttClient) (logger: ILogger) (state: State) (event: Event) = let private handleEvent (mqttClient: IMqttClient) (logger: ILogger) (state: NightLightStateMachine) (event: Event) =
match event with match event with
| ReceivedZigbeeEvent payload -> logger.LogInformation("Received message with payload {Payload}", payload) | ReceivedZigbeeEvent payload -> logger.LogInformation("Received message with payload {Payload}", payload)
| _ -> () | _ -> ()
let result = event |> onEventReceived state let result = event |> state.OnEventReceived
match result with match result with
| Ok(newState, commands) -> | Ok(newState, commands) ->
@ -77,7 +77,7 @@ let mainAsync _ =
let mqttClientOptions = MqttClientOptionsBuilder().WithTcpServer(server).Build() let mqttClientOptions = MqttClientOptionsBuilder().WithTcpServer(server).Build()
let stateLock = new SemaphoreSlim(1, 1) let stateLock = new SemaphoreSlim(1, 1)
let mutable state = State DateTime.Now let mutable state = NightLightStateMachine DateTime.Now
mqttClient.add_ApplicationMessageReceivedAsync (fun e -> mqttClient.add_ApplicationMessageReceivedAsync (fun e ->
async { async {