Skip to content

Instantly share code, notes, and snippets.

Last active Sep 9, 2021
What would you like to do?
import Control.Monad
main = do topic <- getLine
payload <- getLine
let msg = Message (Topic topic) payload
mapM_ (runAction topic payload) rules
runAction topic payload (Action (Topic t) cb)
= when (t == topic) $ cb payload
rules :: [Action]
rules = [
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