More preparation for fading alarm
This commit is contained in:
parent
9104fac638
commit
953abc5998
1 changed files with 46 additions and 43 deletions
|
|
@ -11,25 +11,51 @@ open FsToolkit.ErrorHandling
|
||||||
let internal tryFindLight friendlyName =
|
let internal tryFindLight friendlyName =
|
||||||
Seq.tryFind (fun light -> light.FriendlyName = friendlyName) lights
|
Seq.tryFind (fun light -> light.FriendlyName = friendlyName) lights
|
||||||
|
|
||||||
type internal NightLightState =
|
let internal generateZigbeeCommandsToFixLight (light: Light) (desiredLightState: LightState) =
|
||||||
{ Time: DateTime
|
|
||||||
Alarm: bool
|
|
||||||
LightToState: Map<Light, LightState> }
|
|
||||||
|
|
||||||
let internal generateZigbeeCommandsToFixLight (nightLightState: NightLightState) (light: Light) =
|
|
||||||
seq {
|
seq {
|
||||||
let desiredLightState = nightLightState.LightToState[light]
|
|
||||||
|
|
||||||
match light.ControlledWithRemote, desiredLightState.State with
|
match light.ControlledWithRemote, desiredLightState.State with
|
||||||
| NonRemote, On -> ()
|
| NonRemote, On -> ()
|
||||||
| NonRemote, Off -> failwith $"Unexpectly trying to turn off {light}. It's not remote-controlled."
|
| NonRemote, Off -> failwith $"Unexpectly trying to turn off {light}. It's not remote-controlled."
|
||||||
| _, _ -> yield generateStateCommand nightLightState.LightToState[light].State light
|
| _, _ -> yield generateStateCommand desiredLightState.State light
|
||||||
|
|
||||||
if desiredLightState.State = On then
|
if desiredLightState.State = On then
|
||||||
yield generateColorCommand light desiredLightState.Color
|
yield generateColorCommand light desiredLightState.Color
|
||||||
yield generateBrightnessCommand light desiredLightState.Brightness
|
yield generateBrightnessCommand light desiredLightState.Brightness
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type internal NightLightState =
|
||||||
|
{ Time: DateTime
|
||||||
|
Alarm: bool
|
||||||
|
LightToState: Map<Light, LightState> }
|
||||||
|
|
||||||
|
let internal createOrUpdateNightLightState
|
||||||
|
(time: DateTime)
|
||||||
|
(alarm: bool)
|
||||||
|
(maybeOldLightToState: Map<Light, LightState> option)
|
||||||
|
=
|
||||||
|
let partOfDay = getPartOfDay time
|
||||||
|
|
||||||
|
let lightToState =
|
||||||
|
lights
|
||||||
|
|> Seq.map (fun light ->
|
||||||
|
let color, brightness =
|
||||||
|
getDesiredMood light.Room partOfDay |> getDesiredColorAndBrightness light.Bulb
|
||||||
|
|
||||||
|
let previousState =
|
||||||
|
maybeOldLightToState
|
||||||
|
|> Option.map (fun lightToState -> lightToState[light].State)
|
||||||
|
|> Option.defaultValue On
|
||||||
|
|
||||||
|
light,
|
||||||
|
{ Color = color
|
||||||
|
Brightness = brightness
|
||||||
|
State = if alarm then On else previousState })
|
||||||
|
|> Map.ofSeq
|
||||||
|
|
||||||
|
{ Time = time
|
||||||
|
Alarm = alarm
|
||||||
|
LightToState = lightToState }
|
||||||
|
|
||||||
let internal withStateFor (light: Light) (state: State) (oldNightLightState: NightLightState) =
|
let internal withStateFor (light: Light) (state: State) (oldNightLightState: NightLightState) =
|
||||||
let oldState = oldNightLightState.LightToState[light]
|
let oldState = oldNightLightState.LightToState[light]
|
||||||
|
|
||||||
|
|
@ -42,8 +68,7 @@ let internal withStateForRemoteControlledLights (state: State) (oldNightLightSta
|
||||||
|> Seq.fold (fun acc light -> acc |> withStateFor light state) oldNightLightState
|
|> Seq.fold (fun acc light -> acc |> withStateFor light state) oldNightLightState
|
||||||
|
|
||||||
let internal withAlarmOff (oldNightLightState: NightLightState) =
|
let internal withAlarmOff (oldNightLightState: NightLightState) =
|
||||||
{ oldNightLightState with
|
createOrUpdateNightLightState oldNightLightState.Time false (Some oldNightLightState.LightToState)
|
||||||
Alarm = false }
|
|
||||||
|
|
||||||
let internal generateZigbeeCommandsForDifference (maybeBefore: NightLightState option) (after: NightLightState) =
|
let internal generateZigbeeCommandsForDifference (maybeBefore: NightLightState option) (after: NightLightState) =
|
||||||
after.LightToState
|
after.LightToState
|
||||||
|
|
@ -51,7 +76,7 @@ let internal generateZigbeeCommandsForDifference (maybeBefore: NightLightState o
|
||||||
let oldState = maybeBefore |> Option.map _.LightToState[light]
|
let oldState = maybeBefore |> Option.map _.LightToState[light]
|
||||||
|
|
||||||
if oldState <> Some newState then
|
if oldState <> Some newState then
|
||||||
generateZigbeeCommandsToFixLight after light
|
generateZigbeeCommandsToFixLight light after.LightToState[light]
|
||||||
else
|
else
|
||||||
Seq.empty)
|
Seq.empty)
|
||||||
|
|
||||||
|
|
@ -71,7 +96,7 @@ type NightLightStateMachine private (maybeState: NightLightState option) =
|
||||||
|
|
||||||
this,
|
this,
|
||||||
match maybeLight with
|
match maybeLight with
|
||||||
| Some light -> generateZigbeeCommandsToFixLight currentState light
|
| Some light -> generateZigbeeCommandsToFixLight light currentState.LightToState[light]
|
||||||
| None -> Seq.empty
|
| None -> Seq.empty
|
||||||
| ButtonPress action ->
|
| ButtonPress action ->
|
||||||
let newNightLightState =
|
let newNightLightState =
|
||||||
|
|
@ -90,42 +115,20 @@ type NightLightStateMachine private (maybeState: NightLightState option) =
|
||||||
NightLightStateMachine(Some newNightLightState),
|
NightLightStateMachine(Some newNightLightState),
|
||||||
generateZigbeeCommandsForDifference (Some currentState) newNightLightState
|
generateZigbeeCommandsForDifference (Some currentState) newNightLightState
|
||||||
| TimeChanged newTime, maybeCurrentState ->
|
| TimeChanged newTime, maybeCurrentState ->
|
||||||
let newPartOfDay = getPartOfDay newTime
|
|
||||||
|
|
||||||
let newDayStarted =
|
|
||||||
let maybePreviousPartOfDay =
|
|
||||||
maybeCurrentState |> Option.map _.Time |> Option.map getPartOfDay
|
|
||||||
|
|
||||||
maybePreviousPartOfDay <> Some Day && newPartOfDay = Day
|
|
||||||
|
|
||||||
let alarm =
|
let alarm =
|
||||||
maybeCurrentState |> Option.map _.Alarm |> Option.defaultValue false
|
let newDayStarted =
|
||||||
|| newDayStarted
|
let newPartOfDay = getPartOfDay newTime
|
||||||
|
|
||||||
let newLightToState =
|
let maybePreviousPartOfDay =
|
||||||
lights
|
maybeCurrentState |> Option.map _.Time |> Option.map getPartOfDay
|
||||||
|> Seq.map (fun light ->
|
|
||||||
let color, brightness =
|
|
||||||
getDesiredMood light.Room newPartOfDay
|
|
||||||
|> getDesiredColorAndBrightness light.Bulb
|
|
||||||
|
|
||||||
let previousState =
|
maybePreviousPartOfDay <> Some Day && newPartOfDay = Day
|
||||||
maybeCurrentState
|
|
||||||
|> Option.map _.LightToState[light].State
|
|
||||||
|> Option.defaultValue On
|
|
||||||
|
|
||||||
let newState = if alarm then On else previousState
|
newDayStarted
|
||||||
|
|| maybeCurrentState |> Option.map _.Alarm |> Option.defaultValue false
|
||||||
light,
|
|
||||||
{ Color = color
|
|
||||||
Brightness = brightness
|
|
||||||
State = newState })
|
|
||||||
|> Map.ofSeq
|
|
||||||
|
|
||||||
let newNightLightState =
|
let newNightLightState =
|
||||||
{ Time = newTime
|
createOrUpdateNightLightState newTime alarm (maybeCurrentState |> Option.map _.LightToState)
|
||||||
Alarm = alarm
|
|
||||||
LightToState = newLightToState }
|
|
||||||
|
|
||||||
return
|
return
|
||||||
NightLightStateMachine(Some newNightLightState),
|
NightLightStateMachine(Some newNightLightState),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue