Skip to content

Instantly share code, notes, and snippets.

@julian-klode
Last active September 9, 2021 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julian-klode/8e6c4fe64d7aadd0f5e04316e7a7f96b to your computer and use it in GitHub Desktop.
Save julian-klode/8e6c4fe64d7aadd0f5e04316e7a7f96b to your computer and use it in GitHub Desktop.
import Control.Monad
main = do topic <- getLine
payload <- getLine
let msg = Message (Topic topic) payload
mapM_ (runAction topic payload) rules
where
runAction topic payload (Action (Topic t) cb)
= when (t == topic) $ cb payload
rules :: [Action]
rules = [
controlButton1
]
data Topic = Topic String
type Payload = String
data Message = Message Topic Payload
data Button = Button Topic
data OnOff = On | Off
data Action = Action Topic (Payload -> IO ())
-- FIXME: We actually need to attach this to mqtt
publish (Topic topic) msg = putStrLn (topic ++ ":" ++ msg)
stateChanged (Button bt) cb = Action bt listener
where listener payload = case payload of
"on" -> cb On
"off" -> cb Off
button1 = Button (Topic "button1")
-- Definition of light class
data Light = Light Topic
setLight :: Light -> OnOff -> IO ()
setLight (Light topic) On = publish topic "ON"
setLight (Light topic) Off = publish topic "OFF"
light1 = Light (Topic "light1")
light2 = Light (Topic "light2")
controlButton1 = button1 `stateChanged` toggleLight
where toggleLight onOff = mapM_ (`setLight` onOff) [light1, light2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment