Implement fading alarm

This commit is contained in:
Sven van Heugten 2026-01-17 18:01:14 +01:00
parent 3e33e489aa
commit 6b73e32cda
3 changed files with 26 additions and 7 deletions

View file

@ -79,7 +79,12 @@ type internal State =
| On | On
| Off | Off
type internal Brightness = Brightness of int type internal Brightness =
| Brightness of int
member this.Scale(b: float) =
match this with
| Brightness brightness -> Brightness <| int (float brightness * b)
type internal Color = type internal Color =
| ColorByCoordinates of float * float | ColorByCoordinates of float * float

View file

@ -48,7 +48,11 @@ let internal createOrUpdateNightLightState
light, light,
{ Color = color { Color = color
Brightness = brightness Brightness =
if alarm && light.ControlledWithRemote <> NonRemote then
brightness.Scale(getAlarmWeight time)
else
brightness
State = if alarm then On else previousState }) State = if alarm then On else previousState })
|> Map.ofSeq |> Map.ofSeq

View file

@ -6,11 +6,21 @@ type PartOfDay =
| Day | Day
| Night | Night
let private startOfDay = TimeSpan.FromHours 6
let private endOfDay = TimeSpan.FromHours 20.5
let getPartOfDay (dateTime: DateTime) = let getPartOfDay (dateTime: DateTime) =
match dateTime with match dateTime with
| _ when | _ when dateTime.TimeOfDay >= startOfDay && dateTime.TimeOfDay < endOfDay -> Day
dateTime.TimeOfDay >= TimeSpan.FromHours 6
&& dateTime.TimeOfDay < TimeSpan.FromHours 20.5
->
Day
| _ -> Night | _ -> Night
let getAlarmWeight (dateTime: DateTime) =
let currentTime = dateTime.TimeOfDay
let alarmEnd = startOfDay + TimeSpan(0, 15, 0)
let totalSeconds = (alarmEnd - startOfDay).TotalSeconds
if startOfDay <= currentTime && currentTime <= alarmEnd then
let elapsedSeconds = (currentTime - startOfDay).TotalSeconds
elapsedSeconds / totalSeconds
else
1.0