-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathHueJSON.hs
458 lines (405 loc) · 19.7 KB
/
HueJSON.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
{-# LANGUAGE TemplateHaskell
, OverloadedStrings
, RecordWildCards
, LambdaCase
, ScopedTypeVariables
, FlexibleInstances
, TypeSynonymInstances
, GeneralizedNewtypeDeriving #-}
module HueJSON where
import Data.Aeson
import Data.Monoid
import Data.Char
import Data.Word
import Data.Time
import Data.Attoparsec.Text
import Data.Hashable
import Data.Coerce
import qualified Data.Text as T
import qualified Data.HashMap.Strict as HM
import qualified Data.HashSet as HS
import Control.Lens
import Util
-- Records, lenses and JSON instances for communication with a Hue bridge
-- TODO: Add representations for sensor data
data Light = Light { _lgtState :: !LightState
, _lgtType :: !ZLL_LightType
, _lgtName :: !String
, _lgtModelID :: !LightModel
, _lgtManufacturerName :: !String
, _lgtLuminaireUniqueID :: !(Maybe String)
, _lgtUniqueID :: !String
, _lgtSWVersion :: !String
} deriving Show
instance FromJSON Light where
parseJSON (Object o) = Light <$> o .: "state"
<*> o .: "type"
<*> o .: "name"
<*> o .: "modelid"
<*> o .: "manufacturername"
<*> o .:? "luminaireuniqueid"
<*> o .: "uniqueid"
<*> o .: "swversion"
parseJSON _ = fail "Expected object"
-- TODO: Convert the various string arguments to proper ADTs
data LightState = LightState { _lsOn :: !Bool
, _lsBrightness :: !(Maybe Word8)
, _lsHue :: !(Maybe Word16)
, _lsSaturation :: !(Maybe Word8)
, _lsEffect :: !(Maybe String)
, _lsXY :: !(Maybe [Float])
, _lsColorTemp :: !(Maybe Word16)
, _lsAlert :: !String
, _lsColorMode :: !(Maybe ColorMode)
, _lsReachable :: !Bool
} deriving Show
instance FromJSON LightState where
parseJSON (Object o) = LightState <$> o .: "on"
<*> o .:? "bri"
<*> o .:? "hue"
<*> o .:? "sat"
<*> o .:? "effect"
<*> o .:? "xy"
<*> o .:? "ct"
<*> o .: "alert"
<*> o .:? "colormode"
<*> o .: "reachable"
parseJSON _ = fail "Expected object"
data ColorMode = CMXY | CMCT | CMHS
deriving (Eq, Show, Enum)
instance FromJSON ColorMode where
parseJSON (String s) = case T.unpack s of
"xy" -> return CMXY
"ct" -> return CMCT
"hs" -> return CMHS
str -> fail $ "Invalid color mode: " <> str
parseJSON _ = fail "Expected string"
-- Light type
--
-- http://www.developers.meethue.com/documentation/supported-lights
-- http://cache.nxp.com/documents/user_manual/JN-UG-3091.pdf
data ZLL_LightType = LT_OnOffLight
| LT_OnOffPlugInUnit
| LT_DimmableLight
| LT_DimmablePlugInUnit
| LT_ColorLight
| LT_ExtendedColorLight
| LT_ColorTemperatureLight
deriving Enum
instance FromJSON ZLL_LightType where
parseJSON (String s) =
case map toLower . T.unpack $ s of
"on/off light" -> return LT_OnOffLight
"on/off plug-in unit" -> return LT_OnOffPlugInUnit
"dimmable light" -> return LT_DimmableLight
"dimmable plug-in unit" -> return LT_DimmablePlugInUnit
"color light" -> return LT_ColorLight
"extended color light" -> return LT_ExtendedColorLight
"color temperature light" -> return LT_ColorTemperatureLight
str -> fail $ "Unknown / invalid ZLL light type: " <> str
parseJSON _ = fail "Expected string"
instance Show ZLL_LightType where
show LT_OnOffLight = "On/Off Light"
show LT_OnOffPlugInUnit = "On/Off Plug-in Unit"
show LT_DimmableLight = "Dimmable Light"
show LT_DimmablePlugInUnit = "Dimmable Plug-in Unit"
show LT_ColorLight = "Color Light"
show LT_ExtendedColorLight = "Extended Color Light"
show LT_ColorTemperatureLight = "Color Temperature Light"
isColorLT :: ZLL_LightType -> Bool
isColorLT = \case LT_ColorLight -> True
LT_ExtendedColorLight -> True
_ -> False
isCTOnlyLight :: ZLL_LightType -> Bool
isCTOnlyLight = \case LT_ColorTemperatureLight -> True
_ -> False
isCTLight :: ZLL_LightType -> Bool
isCTLight = \case LT_ColorTemperatureLight -> True
LT_ExtendedColorLight -> True
_ -> False
isDimmableLT :: ZLL_LightType -> Bool
isDimmableLT = \case LT_ColorLight -> True
LT_ExtendedColorLight -> True
LT_ColorTemperatureLight -> True
LT_DimmableLight -> True
LT_DimmablePlugInUnit -> True
_ -> False
-- Light model
--
-- http://www.developers.meethue.com/documentation/supported-lights
-- https://github.com/mhop/fhem-mirror/blob/master/fhem/FHEM/31_HUEDevice.pm
data LightModel = LM_HueBulbA19
| LM_HueBulbA19V2
| LM_HueBulbA19V3
| LM_HueSpotBR30
| LM_HueSpotGU10
| LM_HueBR30
| LM_HueCandle
| LM_HueLightStrip
| LM_HueLivingColorsIris
| LM_HueLivingColorsBloom
| LM_LivingColorsGen3Iris
| LM_LivingColorsGen3BloomAura
| LM_LivingColorsAura
| LM_HueA19Lux
| LM_HueA19White
| LM_HueA19WhiteV2
| LM_ColorLightModule
| LM_ColorTemperatureModule
| LM_HueA19WhiteAmbience
| LM_HueGU10WhiteAmbience
| LM_HueCandleWhiteAmbience
| LM_HueGo
| LM_HueLightStripPlus
| LM_HueWhiteAmbienceFlexStrip
| LM_LivingWhitesPlug
| LM_LightifyFlex
| LM_LightifyClassicA60RGBW
| LM_LightifyClassicA60TW
| LM_LightifyClassicB40TW
| LM_LightifyPAR16
| LM_LightifyPlug
| LM_InnrGU10Spot
| LM_InnrBulbRB162
| LM_InnrBulbRB172W
| LM_InnrFlexLightFL110
| LM_Unknown !String
instance FromJSON LightModel where
parseJSON (String s) =
case T.unpack s of
"LCT001" -> return LM_HueBulbA19
"LCT007" -> return LM_HueBulbA19V2
"LCT010" -> return LM_HueBulbA19V3
"LCT014" -> return LM_HueBulbA19V3
"LCT002" -> return LM_HueSpotBR30
"LCT003" -> return LM_HueSpotGU10
"LCT011" -> return LM_HueBR30 -- What's the diff. to the 'Spot'?
"LCT012" -> return LM_HueCandle
"LST001" -> return LM_HueLightStrip
"LLC010" -> return LM_HueLivingColorsIris
"LLC011" -> return LM_HueLivingColorsBloom
"LLC012" -> return LM_HueLivingColorsBloom
"LLC006" -> return LM_LivingColorsGen3Iris
"LLC007" -> return LM_LivingColorsGen3BloomAura
"LLC014" -> return LM_LivingColorsAura
"LWB004" -> return LM_HueA19Lux
"LWB006" -> return LM_HueA19White
"LWB007" -> return LM_HueA19White
"LWB010" -> return LM_HueA19WhiteV2 -- 10 & 14 assumed to be Gen2
"LWB014" -> return LM_HueA19WhiteV2
"LLM001" -> return LM_ColorLightModule
"LLM010" -> return LM_ColorTemperatureModule
"LLM011" -> return LM_ColorTemperatureModule
"LLM012" -> return LM_ColorTemperatureModule
"LTW001" -> return LM_HueA19WhiteAmbience
"LTW004" -> return LM_HueA19WhiteAmbience
"LTW013" -> return LM_HueGU10WhiteAmbience
"LTW014" -> return LM_HueGU10WhiteAmbience
"LTW012" -> return LM_HueCandleWhiteAmbience
"LLC020" -> return LM_HueGo
"LST002" -> return LM_HueLightStripPlus
"LTP001" -> return LM_HueWhiteAmbienceFlexStrip
"LWL001" -> return LM_LivingWhitesPlug
"Flex RGBW" -> return LM_LightifyFlex
"Classic A60 RGBW" -> return LM_LightifyClassicA60RGBW -- Model ID Unverified
"Classic A60 TW" -> return LM_LightifyClassicA60TW -- Model ID Unverified
"Classic B40 TW - LIGHTIFY" -> return LM_LightifyClassicB40TW
"PAR16 50 TW" -> return LM_LightifyPAR16 -- Model ID Unverified
"Plug 01" -> return LM_LightifyPlug -- Also 'Plug - LIGHTIFY'?
"RS 125" -> return LM_InnrGU10Spot -- Tentative support for Innr
"RB 162" -> return LM_InnrBulbRB162 -- ..
"RB 172 W" -> return LM_InnrBulbRB172W
"FL 110" -> return LM_InnrFlexLightFL110
str -> return $ LM_Unknown str
parseJSON _ = fail "Expected string"
instance Show LightModel where
show LM_HueBulbA19 = "Hue Bulb A19"
show LM_HueBulbA19V2 = "Hue Bulb A19 V2"
show LM_HueBulbA19V3 = "Hue Bulb A19 V3"
show LM_HueSpotBR30 = "Hue Spot BR30"
show LM_HueSpotGU10 = "Hue Spot GU10"
show LM_HueBR30 = "Hue BR30"
show LM_HueCandle = "Hue Color Candle"
show LM_HueLightStrip = "Hue LightStrip"
show LM_HueLivingColorsIris = "Hue Living Colors Iris"
show LM_HueLivingColorsBloom = "Hue Living Colors Bl."
show LM_LivingColorsGen3Iris = "Living Colors G3 Iris"
show LM_LivingColorsGen3BloomAura = "Living Colors G3 B/A"
show LM_LivingColorsAura = "Living Colors Aura"
show LM_HueA19Lux = "Hue A19 Lux"
show LM_HueA19White = "Hue A19 White"
show LM_HueA19WhiteV2 = "Hue A19 White V2"
show LM_ColorLightModule = "Color Light Module"
show LM_ColorTemperatureModule = "Color Temp. Module"
show LM_HueA19WhiteAmbience = "Hue A19 White Amb."
show LM_HueGU10WhiteAmbience = "Hue GU10 White Amb."
show LM_HueCandleWhiteAmbience = "Hue White Amb. Candle"
show LM_HueGo = "Hue Go"
show LM_HueLightStripPlus = "Hue LightStrip Plus"
show LM_HueWhiteAmbienceFlexStrip = "Hue W. Amb. FlexStrip"
show LM_LivingWhitesPlug = "LivingWhites Plug"
show LM_LightifyFlex = "LIGHTIFY Flex"
show LM_LightifyClassicA60RGBW = "LIGHTIFY Cl. A60 RGBW"
show LM_LightifyClassicA60TW = "LIGHTIFY Cl. A60 TW"
show LM_LightifyClassicB40TW = "LIGHTIFY Cl. B40 TW"
show LM_LightifyPAR16 = "LIGHTIFY PAR16 TW"
show LM_LightifyPlug = "LIGHTIFY Plug"
show LM_InnrGU10Spot = "Innr GU10 Spot"
show LM_InnrBulbRB162 = "Innr Bulb RB 162"
show LM_InnrBulbRB172W = "Innr Bulb RB 172 W"
show LM_InnrFlexLightFL110 = "Innr FlexLight FL 110"
show (LM_Unknown s) = "Unknown (" <> s <> ")"
-- Scenes
--
-- http://www.developers.meethue.com/documentation/scenes-api#41_get_all_scenes
data BridgeScene = BridgeScene { _bscName :: !String
, _bscLights :: ![LightID]
, _bscActive :: !(Maybe Bool)
, _bscOwner :: !(Maybe String)
, _bscRecycle :: !(Maybe Bool)
, _bscLocked :: !(Maybe Bool)
, _bscAppData :: !(Maybe Object)
, _bscPicture :: !(Maybe String)
, _bscLastUpdated :: !(Maybe UTCTime)
, _bscVersion :: !(Maybe Int)
} deriving Show
-- Hue UTC strings miss the final Z, fails with the default parser
parseHueTimeMaybe :: Maybe String -> Maybe UTCTime
parseHueTimeMaybe Nothing = Nothing
parseHueTimeMaybe (Just t) =
case parseTimeM True defaultTimeLocale "%FT%T" t of
Just d -> Just d
Nothing -> Nothing
instance FromJSON BridgeScene where
parseJSON (Object o) = BridgeScene <$> o .: "name"
<*> o .: "lights"
<*> o .:? "active"
<*> o .:? "owner"
<*> o .:? "recycle"
<*> o .:? "locked"
<*> o .:? "appdata"
<*> o .:? "picture"
<*> (parseHueTimeMaybe <$> o .:? "lastupdated")
<*> o .:? "version"
parseJSON _ = fail "Expected object"
-- Bridge configuration obtained from the api/config endpoint without a whitelisted user
data BridgeConfigNoWhitelist = BridgeConfigNoWhitelist
{ _bcnwSWVersion :: !String
, _bcnwAPIVersion :: !APIVersion
, _bcnwName :: !String
, _bcnwMac :: !String
} deriving Show
instance FromJSON BridgeConfigNoWhitelist where
parseJSON (Object o) =
BridgeConfigNoWhitelist <$> o .: "swversion"
<*> o .: "apiversion"
<*> o .: "name"
<*> o .: "mac"
parseJSON _ = fail "Expected object"
-- API version string
data APIVersion = APIVersion { avMajor :: !Int, avMinor :: !Int, avPatch :: !Int }
instance FromJSON APIVersion where
parseJSON (String s) =
either (fail "Failed to parse version number")
return
(parseOnly parser s)
where parser = APIVersion <$> (decimal <* char '.') <*> (decimal <* char '.') <*> decimal
parseJSON _ = fail "Expected string"
instance Show APIVersion where
show APIVersion { .. } = show avMajor <> "." <> show avMinor <> "." <> show avPatch
-- Actual bridge configuration obtainable by whitelisted user. We only parse a selection
-- of potentially interesting fields
--
-- http://www.developers.meethue.com/documentation/configuration-api#72_get_configuration
data BridgeConfig = BridgeConfig
{ _bcName :: !String
, _bcZigBeeChannel :: !Int
, _bcBridgeID :: !String
, _bcMac :: !String
, _bcIPAddress :: !IPAddress
, _bcNetmask :: !String
, _bcGateway :: !String
, _bcModelID :: !String
, _bcSWVersion :: !String
, _bcAPIVersion :: !APIVersion
, _bcSWUpdate :: !(Maybe SWUpdate)
, _bcLinkButton :: !Bool
, _bcPortalServices :: !Bool
, _bcPortalConnection :: !String
, _bcPortalState :: !(Maybe PortalState)
, _bcFactoryNew :: !Bool
} deriving Show
data SWUpdate = SWUpdate
{ _swuUpdateState :: !Int
, _swuCheckForUpdate :: !Bool
, _swuURL :: !String
, _swuText :: !String
, _swuNotify :: !Bool
} deriving Show
data PortalState = PortalState
{ _psSignedOn :: !Bool
, _psIncoming :: !Bool
, _psOutgoing :: !Bool
, _psCommunication :: !String
} deriving Show
instance FromJSON BridgeConfig where
parseJSON (Object o) =
BridgeConfig <$> o .: "name"
<*> o .: "zigbeechannel"
<*> o .: "bridgeid"
<*> o .: "mac"
<*> o .: "ipaddress"
<*> o .: "netmask"
<*> o .: "gateway"
<*> o .: "modelid"
<*> o .: "swversion"
<*> o .: "apiversion"
<*> o .:? "swupdate"
<*> o .: "linkbutton"
<*> o .: "portalservices"
<*> o .: "portalconnection"
<*> o .:? "portalstate"
<*> o .: "factorynew"
parseJSON _ = fail "Expected object"
instance FromJSON SWUpdate where
parseJSON (Object o) =
SWUpdate <$> o .: "updatestate"
<*> o .: "checkforupdate"
<*> o .: "url"
<*> o .: "text"
<*> o .: "notify"
parseJSON _ = fail "Expected object"
instance FromJSON PortalState where
parseJSON (Object o) =
PortalState <$> o .: "signedon"
<*> o .: "incoming"
<*> o .: "outgoing"
<*> o .: "communication"
parseJSON _ = fail "Expected object"
-- Some helper types for receiving lists / maps of the exported objects,
-- newtype wrappers for some modicum of type safety
newtype LightID = LightID { fromLightID :: String }
deriving (Eq, Ord, Show, FromJSON, ToJSON, Hashable)
newtype BridgeSceneID = BridgeSceneID { fromBridgeSceneID :: String }
deriving (Eq, Ord, Show, FromJSON, ToJSON, Hashable)
newtype GroupName = GroupName { fromGroupName :: String }
deriving (Eq, Ord, Show, FromJSON, ToJSON, Hashable)
type Lights = HM.HashMap LightID Light -- Light ID to light
type LightGroups = HM.HashMap GroupName (HS.HashSet LightID) -- Group names to set of light IDs
type BridgeScenes = HM.HashMap BridgeSceneID BridgeScene -- Scene ID to scene
-- The newtype wrappers for the various string types give us problems with missing JSON
-- instances, just use coerce to safely reuse the ones we already got for plain String
instance FromJSON Lights where
parseJSON v = (\(a :: HM.HashMap String Light) -> coerce a) <$> parseJSON v
instance FromJSON LightGroups where
parseJSON v = (\(a :: HM.HashMap String (HS.HashSet LightID)) -> coerce a) <$> parseJSON v
instance FromJSON BridgeScenes where
parseJSON v = (\(a :: HM.HashMap String BridgeScene) -> coerce a) <$> parseJSON v
-- Lenses
makeLenses ''BridgeConfigNoWhitelist
makeLenses ''BridgeConfig
makeLenses ''SWUpdate
makeLenses ''PortalState
makeLenses ''Light
makeLenses ''LightState
makeLenses ''BridgeScene