Stop requiring the initial time in NightLightStateMachine
We're only fooling ourselves if we think that it's ready *right* after construction anyway. After all, the initial state of the lights won't be updated when the state machine is constructed.
This commit is contained in:
parent
db4434cd20
commit
9531dee52b
5 changed files with 31 additions and 19 deletions
|
|
@ -43,8 +43,8 @@ type FakeLight(light: Light) =
|
|||
if hasPower then
|
||||
color <- newColor
|
||||
|
||||
type FakeHome(now: DateTime) =
|
||||
let mutable nightLightStateMachine = NightLightStateMachine now
|
||||
type FakeHome() =
|
||||
let mutable nightLightStateMachine = NightLightStateMachine()
|
||||
|
||||
let assertIsOkAndGet result =
|
||||
match result with
|
||||
|
|
|
|||
|
|
@ -27,22 +27,22 @@ module InteractionsHelpers =
|
|||
[<Properties(Arbitrary = [| typeof<Arbitraries> |])>]
|
||||
type NightLightTests() =
|
||||
[<Property>]
|
||||
let ``Brightness should always be under 255`` (now: DateTime) (interactions: Interaction list) =
|
||||
let fakeHome = FakeHome now
|
||||
let ``Brightness should always be under 255`` (interactions: Interaction list) =
|
||||
let fakeHome = FakeHome()
|
||||
fakeHome.Interact interactions
|
||||
fakeHome.ForAllLightsThatAreOn(fun (_, brightness, _) -> brightness < 255uy)
|
||||
|
||||
[<Property>]
|
||||
let ``Lights should be red during the night`` (now: DateTime) (interactions: Interaction list) =
|
||||
let fakeHome = FakeHome now
|
||||
let ``Lights should be red during the night`` (interactions: Interaction list) =
|
||||
let fakeHome = FakeHome()
|
||||
fakeHome.Interact interactions
|
||||
|
||||
InteractionsHelpers.isNightAfter interactions
|
||||
==> fakeHome.ForAllLightsThatAreOn(fun (_, _, color) -> color = Red)
|
||||
|
||||
[<Property>]
|
||||
let ``Lights should be white or yellow during the day`` (now: DateTime) (interactions: Interaction list) =
|
||||
let fakeHome = FakeHome now
|
||||
let ``Lights should be white or yellow during the day`` (interactions: Interaction list) =
|
||||
let fakeHome = FakeHome()
|
||||
fakeHome.Interact interactions
|
||||
|
||||
InteractionsHelpers.isDayAfter interactions
|
||||
|
|
|
|||
|
|
@ -17,7 +17,9 @@ type ParseZigbeeEventError =
|
|||
| InvalidFriendlyNameField
|
||||
| UnknownType
|
||||
|
||||
type ParseEventError = ParseZigbeeEventError of ParseZigbeeEventError
|
||||
type OnEventReceivedError =
|
||||
| ParseZigbeeEventError of ParseZigbeeEventError
|
||||
| TimeIsUnknown
|
||||
|
||||
type Room =
|
||||
| Bathroom
|
||||
|
|
|
|||
|
|
@ -17,13 +17,15 @@ let internal generateZigbeeCommandToFixLight partOfDay light =
|
|||
|
||||
generateZigbeeCommand light.FriendlyName color brightness
|
||||
|
||||
type NightLightStateMachine(time: DateTime) =
|
||||
member this.OnEventReceived(event: Event) : Result<NightLightStateMachine * Message seq, ParseEventError> =
|
||||
result {
|
||||
let partOfDay = getPartOfDay time
|
||||
type NightLightStateMachine private (maybeTime: DateTime option) =
|
||||
new() = NightLightStateMachine None
|
||||
|
||||
match event with
|
||||
| ReceivedZigbeeEvent payload ->
|
||||
member this.OnEventReceived(event: Event) : Result<NightLightStateMachine * Message seq, OnEventReceivedError> =
|
||||
result {
|
||||
let maybePartOfDay = maybeTime |> Option.map getPartOfDay
|
||||
|
||||
match event, maybePartOfDay with
|
||||
| ReceivedZigbeeEvent payload, Some partOfDay ->
|
||||
let! zigbeeEvent = parseZigbeeEvent payload |> Result.mapError ParseZigbeeEventError
|
||||
|
||||
return
|
||||
|
|
@ -35,14 +37,15 @@ type NightLightStateMachine(time: DateTime) =
|
|||
match maybeLight with
|
||||
| Some light -> generateZigbeeCommandToFixLight partOfDay light |> Seq.singleton
|
||||
| None -> Seq.empty
|
||||
| TimeChanged newTime ->
|
||||
let newState = NightLightStateMachine newTime
|
||||
| TimeChanged newTime, maybePartOfDay ->
|
||||
let newState = NightLightStateMachine(Some newTime)
|
||||
let newPartOfDay = getPartOfDay newTime
|
||||
|
||||
return
|
||||
newState,
|
||||
if partOfDay <> newPartOfDay then
|
||||
if maybePartOfDay <> Some newPartOfDay then
|
||||
lights |> Seq.map (generateZigbeeCommandToFixLight newPartOfDay)
|
||||
else
|
||||
Seq.empty
|
||||
| _, None -> return! Error TimeIsUnknown
|
||||
}
|
||||
|
|
|
|||
|
|
@ -77,7 +77,14 @@ let mainAsync _ =
|
|||
let mqttClientOptions = MqttClientOptionsBuilder().WithTcpServer(server).Build()
|
||||
|
||||
let stateLock = new SemaphoreSlim(1, 1)
|
||||
let mutable state = NightLightStateMachine DateTime.Now
|
||||
|
||||
let! initialState =
|
||||
let emptyNightLightStateMachine = NightLightStateMachine()
|
||||
|
||||
TimeChanged DateTime.Now
|
||||
|> handleEvent mqttClient logger emptyNightLightStateMachine
|
||||
|
||||
let mutable state = initialState
|
||||
|
||||
mqttClient.add_ApplicationMessageReceivedAsync (fun e ->
|
||||
async {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue