Introduce the remote into FakeHome
This commit is contained in:
parent
cef2a20f7a
commit
52e0ec85e9
2 changed files with 34 additions and 6 deletions
|
|
@ -9,6 +9,8 @@ open FSharp.Data
|
||||||
type HumanInteraction =
|
type HumanInteraction =
|
||||||
| LightPoweredOn of Light
|
| LightPoweredOn of Light
|
||||||
| LightPoweredOff of Light
|
| LightPoweredOff of Light
|
||||||
|
| RemotePressedOnButton
|
||||||
|
| RemotePressedOffButton
|
||||||
|
|
||||||
type Interaction =
|
type Interaction =
|
||||||
| HumanInteraction of HumanInteraction
|
| HumanInteraction of HumanInteraction
|
||||||
|
|
@ -25,15 +27,21 @@ type LightState =
|
||||||
|
|
||||||
type FakeLight(light: Light) =
|
type FakeLight(light: Light) =
|
||||||
let mutable hasPower = false
|
let mutable hasPower = false
|
||||||
|
let mutable state = true
|
||||||
let mutable brightness: byte = 255uy
|
let mutable brightness: byte = 255uy
|
||||||
let mutable color: Color = White
|
let mutable color: Color = White
|
||||||
|
|
||||||
member _.LightWithState = light, if hasPower then On(brightness, color) else Off
|
member _.LightWithState =
|
||||||
|
light, if hasPower && state then On(brightness, color) else Off
|
||||||
|
|
||||||
member _.PowerOn() = hasPower <- true
|
member _.PowerOn() = hasPower <- true
|
||||||
|
|
||||||
member _.PowerOff() = hasPower <- false
|
member _.PowerOff() = hasPower <- false
|
||||||
|
|
||||||
|
member _.SetState(newState: bool) =
|
||||||
|
if hasPower then
|
||||||
|
state <- newState
|
||||||
|
|
||||||
member _.SetBrightness(newBrightness: byte) =
|
member _.SetBrightness(newBrightness: byte) =
|
||||||
if hasPower then
|
if hasPower then
|
||||||
brightness <- newBrightness
|
brightness <- newBrightness
|
||||||
|
|
@ -69,6 +77,12 @@ type FakeHome() =
|
||||||
|
|
||||||
let parsedPayload = JsonValue.Parse command.Payload
|
let parsedPayload = JsonValue.Parse command.Payload
|
||||||
|
|
||||||
|
match parsedPayload.TryGetProperty "state" with
|
||||||
|
| Some(JsonValue.String "ON") -> fakeLight.SetState true
|
||||||
|
| Some(JsonValue.String "OFF") -> fakeLight.SetState false
|
||||||
|
| None -> ()
|
||||||
|
| value -> failwith $"Unexpected state value {value}"
|
||||||
|
|
||||||
match parsedPayload.TryGetProperty "brightness" with
|
match parsedPayload.TryGetProperty "brightness" with
|
||||||
| Some(JsonValue.Number newBrightness) -> fakeLight.SetBrightness(byte newBrightness)
|
| Some(JsonValue.Number newBrightness) -> fakeLight.SetBrightness(byte newBrightness)
|
||||||
| None -> ()
|
| None -> ()
|
||||||
|
|
@ -103,6 +117,16 @@ type FakeHome() =
|
||||||
|> ReceivedZigbeeEvent
|
|> ReceivedZigbeeEvent
|
||||||
|> onEventPublished.Trigger
|
|> onEventPublished.Trigger
|
||||||
| HumanInteraction(LightPoweredOff light) -> friendlyNameToFakeLight[light.FriendlyName].PowerOff()
|
| HumanInteraction(LightPoweredOff light) -> friendlyNameToFakeLight[light.FriendlyName].PowerOff()
|
||||||
|
| HumanInteraction RemotePressedOnButton ->
|
||||||
|
{ Topic = $"zigbee2mqtt/{remoteControlFriendlyName.Get}"
|
||||||
|
Payload = @"{ ""action"": ""on"" }" }
|
||||||
|
|> ReceivedZigbeeEvent
|
||||||
|
|> onEventPublished.Trigger
|
||||||
|
| HumanInteraction RemotePressedOffButton ->
|
||||||
|
{ Topic = $"zigbee2mqtt/{remoteControlFriendlyName.Get}"
|
||||||
|
Payload = @"{ ""action"": ""off"" }" }
|
||||||
|
|> ReceivedZigbeeEvent
|
||||||
|
|> onEventPublished.Trigger
|
||||||
| TimeChanged newTime -> newTime |> Event.TimeChanged |> onEventPublished.Trigger
|
| TimeChanged newTime -> newTime |> Event.TimeChanged |> onEventPublished.Trigger
|
||||||
|
|
||||||
type FakeHome with
|
type FakeHome with
|
||||||
|
|
|
||||||
|
|
@ -8,11 +8,15 @@ let private genTimeChangedInteraction =
|
||||||
ArbMap.defaults |> ArbMap.generate<DateTime> |> Gen.map Interaction.TimeChanged
|
ArbMap.defaults |> ArbMap.generate<DateTime> |> Gen.map Interaction.TimeChanged
|
||||||
|
|
||||||
let private genHumanInteraction =
|
let private genHumanInteraction =
|
||||||
|
let genLightInteraction =
|
||||||
Gen.elements lights
|
Gen.elements lights
|
||||||
|> Gen.bind (fun light ->
|
|> Gen.bind (fun light -> Gen.elements [ LightPoweredOn light; LightPoweredOff light ])
|
||||||
[ LightPoweredOn light; LightPoweredOff light ]
|
|
||||||
|> Gen.elements
|
let genRemoteInteraction =
|
||||||
|> Gen.map Interaction.HumanInteraction)
|
Gen.elements [ RemotePressedOnButton; RemotePressedOffButton ]
|
||||||
|
|
||||||
|
Gen.oneof [ genLightInteraction; genRemoteInteraction ]
|
||||||
|
|> Gen.map Interaction.HumanInteraction
|
||||||
|
|
||||||
let private genInteraction =
|
let private genInteraction =
|
||||||
Gen.oneof [ genTimeChangedInteraction; genHumanInteraction ]
|
Gen.oneof [ genTimeChangedInteraction; genHumanInteraction ]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue