-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathHueBroker.hs
More file actions
46 lines (37 loc) · 1.4 KB
/
HueBroker.hs
File metadata and controls
46 lines (37 loc) · 1.4 KB
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
{-# LANGUAGE OverloadedStrings #-}
module HueBroker ( BrokerBridge(..)
, queryBrokerServer
) where
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Catch
import Network.HTTP.Client (isIpAddress)
import Network.HTTP.Simple
import Data.Aeson
import qualified Data.ByteString.Char8 as B8
import Util
-- Discover local Hue bridges using the broker server
--
-- http://www.developers.meethue.com/documentation/hue-bridge-discovery
-- Bridge description obtained from the broker server
data BrokerBridge = BrokerBridge
{ brID :: !String
, brInternalIPAddress :: !IPAddress
, brName :: !(Maybe String)
, brMacAddress :: !(Maybe String)
} deriving Show
instance FromJSON BrokerBridge where
parseJSON (Object o) = do
ip <- o .: "internalipaddress"
unless (isIpAddress $ B8.pack ip) $ fail "Invalid IP address"
BrokerBridge <$> o .: "id"
<*> pure (IPAddress ip)
<*> o .:? "name"
<*> o .:? "macaddress"
parseJSON _ = fail "Expected object"
queryBrokerServer :: (MonadThrow m, MonadIO m) => m [BrokerBridge]
queryBrokerServer = do
let brokerServerURL = "https://www.meethue.com/api/nupnp"
request <- parseRequest brokerServerURL
response <- httpJSON request
return (getResponseBody response :: [BrokerBridge])