-
-
Save julian-klode/8e6c4fe64d7aadd0f5e04316e7a7f96b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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