Initial commit
This commit is contained in:
commit
284fdc1261
16 changed files with 448 additions and 0 deletions
33
NightLight.Core/ZigbeeEvents.fs
Normal file
33
NightLight.Core/ZigbeeEvents.fs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
module NightLight.ZigbeeEvents
|
||||
|
||||
open FsToolkit.ErrorHandling
|
||||
open FSharp.Data
|
||||
|
||||
type ZigbeeEvent = DeviceAnnounce of FriendlyName: string
|
||||
|
||||
type ParseZigbeeEventError =
|
||||
| InvalidJson
|
||||
| MissingTypeField
|
||||
| MissingDataField
|
||||
| MissingFriendlyNameField
|
||||
| InvalidTypeField
|
||||
| InvalidFriendlyNameField
|
||||
| UnknownType
|
||||
|
||||
let internal parseZigbeeEvent str =
|
||||
result {
|
||||
let! jsonValue = JsonValue.TryParse str |> Result.requireSome InvalidJson
|
||||
|
||||
let! messageType = jsonValue.TryGetProperty "type" |> Result.requireSome MissingTypeField
|
||||
let! messageData = jsonValue.TryGetProperty "data" |> Result.requireSome MissingDataField
|
||||
|
||||
return!
|
||||
match messageType with
|
||||
| JsonValue.String "device_announce" ->
|
||||
match messageData.TryGetProperty "friendly_name" with
|
||||
| Some(JsonValue.String friendlyName) -> Ok(DeviceAnnounce friendlyName)
|
||||
| Some _ -> Error InvalidFriendlyNameField
|
||||
| None -> Error MissingFriendlyNameField
|
||||
| JsonValue.String _ -> Error UnknownType
|
||||
| _ -> Error InvalidTypeField
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue