Parse color commands in FakeHome
This commit is contained in:
parent
6baf725765
commit
7125fee28e
2 changed files with 26 additions and 3 deletions
|
|
@ -15,15 +15,21 @@ type Interaction =
|
||||||
| HumanInteraction of HumanInteraction
|
| HumanInteraction of HumanInteraction
|
||||||
| TimeChanged of DateTime
|
| TimeChanged of DateTime
|
||||||
|
|
||||||
|
type Color =
|
||||||
|
| White
|
||||||
|
| Yellow
|
||||||
|
| Red
|
||||||
|
|
||||||
type LightState =
|
type LightState =
|
||||||
| Off
|
| Off
|
||||||
| On of Brightness: byte
|
| On of Brightness: byte * Color: Color
|
||||||
|
|
||||||
type FakeLight(light: Light) =
|
type FakeLight(light: Light) =
|
||||||
let mutable hasPower = false
|
let mutable hasPower = false
|
||||||
let mutable brightness: byte = 255uy
|
let mutable brightness: byte = 255uy
|
||||||
|
let mutable color: Color = White
|
||||||
|
|
||||||
member _.LightWithState = light, if hasPower then On brightness else Off
|
member _.LightWithState = light, if hasPower then On(brightness, color) else Off
|
||||||
|
|
||||||
member _.TurnOn() = hasPower <- true
|
member _.TurnOn() = hasPower <- true
|
||||||
|
|
||||||
|
|
@ -33,6 +39,10 @@ type FakeLight(light: Light) =
|
||||||
if hasPower then
|
if hasPower then
|
||||||
brightness <- newBrightness
|
brightness <- newBrightness
|
||||||
|
|
||||||
|
member _.SetColor(newColor: Color) =
|
||||||
|
if hasPower then
|
||||||
|
color <- newColor
|
||||||
|
|
||||||
type FakeHome(now: DateTime) =
|
type FakeHome(now: DateTime) =
|
||||||
let mutable nightLightStateMachine = NightLightStateMachine now
|
let mutable nightLightStateMachine = NightLightStateMachine now
|
||||||
|
|
||||||
|
|
@ -60,6 +70,19 @@ type FakeHome(now: DateTime) =
|
||||||
| Some(JsonValue.Number newBrightness) -> fakeLight.SetBrightness(byte newBrightness)
|
| Some(JsonValue.Number newBrightness) -> fakeLight.SetBrightness(byte newBrightness)
|
||||||
| None -> ()
|
| None -> ()
|
||||||
| value -> failwith $"Unexpected brightness value {value}"
|
| value -> failwith $"Unexpected brightness value {value}"
|
||||||
|
|
||||||
|
match parsedPayload.TryGetProperty "color" with
|
||||||
|
| Some color ->
|
||||||
|
match color.TryGetProperty "x", color.TryGetProperty "y" with
|
||||||
|
| Some(JsonValue.Number 0.3227M), Some(JsonValue.Number 0.329M) -> fakeLight.SetColor White
|
||||||
|
| Some(JsonValue.Number 0.6942M), Some(JsonValue.Number 0.2963M) -> fakeLight.SetColor Red
|
||||||
|
| _ -> failwith $"Unexpected color value {color}"
|
||||||
|
| None -> ()
|
||||||
|
|
||||||
|
match parsedPayload.TryGetProperty "color_temp" with
|
||||||
|
| Some(JsonValue.Number temperature) when temperature = 454M -> fakeLight.SetColor Yellow
|
||||||
|
| None -> ()
|
||||||
|
| value -> failwith $"Unexpected color temperature value {value}"
|
||||||
}
|
}
|
||||||
|> ignore
|
|> ignore
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,6 @@ type NightLightTests() =
|
||||||
fakeHome.LightStates
|
fakeHome.LightStates
|
||||||
|> Seq.choose (fun (_, state) ->
|
|> Seq.choose (fun (_, state) ->
|
||||||
match state with
|
match state with
|
||||||
| On brightness -> Some brightness
|
| On(brightness, _) -> Some brightness
|
||||||
| Off -> None)
|
| Off -> None)
|
||||||
|> Seq.forall (fun brightness -> brightness < 255uy)
|
|> Seq.forall (fun brightness -> brightness < 255uy)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue