summaryrefslogtreecommitdiff
path: root/fig-monitor-bullfrog/src
diff options
context:
space:
mode:
authorLLLL Colonq <llll@colonq>2023-11-16 19:06:43 -0500
committerLLLL Colonq <llll@colonq>2023-11-16 19:06:43 -0500
commitdcef0b65069fb38fd0f6c4382353167f603ebff1 (patch)
tree45954ffe308c3dd056e6af4f734e6d2af89e5856 /fig-monitor-bullfrog/src
Initial commit
Diffstat (limited to 'fig-monitor-bullfrog/src')
-rw-r--r--fig-monitor-bullfrog/src/Fig/Monitor/Bullfrog.hs36
-rw-r--r--fig-monitor-bullfrog/src/Fig/Monitor/Bullfrog/Utils.hs29
2 files changed, 65 insertions, 0 deletions
diff --git a/fig-monitor-bullfrog/src/Fig/Monitor/Bullfrog.hs b/fig-monitor-bullfrog/src/Fig/Monitor/Bullfrog.hs
new file mode 100644
index 0000000..1a267f8
--- /dev/null
+++ b/fig-monitor-bullfrog/src/Fig/Monitor/Bullfrog.hs
@@ -0,0 +1,36 @@
+{-# Language QuasiQuotes #-}
+{-# Language RecordWildCards #-}
+{-# Language ApplicativeDo #-}
+
+module Fig.Monitor.Bullfrog
+ ( bullfrogClient
+ ) where
+
+import Fig.Prelude
+
+import qualified Data.Text as Text
+
+import qualified Wuss as WS
+import qualified Network.WebSockets.Connection as WS
+
+import Fig.Utils.SExpr
+import Fig.Bus.Client
+import Fig.Monitor.Bullfrog.Utils
+
+bullfrogClient :: Config -> (Text, Text) -> IO ()
+bullfrogClient cfg busAddr = do
+ WS.runSecureClient "colonq.computer" 443 ("/bullfrog/api/channel/broadcast?token=" <> Text.unpack cfg.authToken) \conn -> do
+ busClient busAddr
+ (\cmds -> do
+ log "Connected to bus and broadcast server"
+ cmds.subscribe [sexp|(monitor bullfrog broadcast)|]
+ )
+ (\_cmds d -> do
+ case d of
+ SExprList [ev, SExprString msg]
+ | ev == [sexp|(monitor bullfrog broadcast)|] -> do
+ log $ "Broadcasting message: " <> msg
+ WS.sendTextData conn msg
+ _ -> log $ "Invalid incoming message: " <> tshow d
+ )
+ (pure ())
diff --git a/fig-monitor-bullfrog/src/Fig/Monitor/Bullfrog/Utils.hs b/fig-monitor-bullfrog/src/Fig/Monitor/Bullfrog/Utils.hs
new file mode 100644
index 0000000..b0ae02b
--- /dev/null
+++ b/fig-monitor-bullfrog/src/Fig/Monitor/Bullfrog/Utils.hs
@@ -0,0 +1,29 @@
+{-# Language ApplicativeDo #-}
+
+module Fig.Monitor.Bullfrog.Utils
+ ( FigMonitorBullfrogException(..)
+ , Config(..)
+ , loadConfig
+ ) where
+
+import Fig.Prelude
+
+import qualified Toml
+
+newtype FigMonitorBullfrogException = FigMonitorBullfrogException Text
+ deriving (Show, Eq, Ord)
+instance Exception FigMonitorBullfrogException
+
+newtype Config = Config
+ { authToken :: Text
+ } deriving (Show, Eq, Ord)
+
+configCodec :: Toml.TomlCodec Config
+configCodec = do
+ authToken <- Toml.text "auth_token" Toml..= (\a -> a.authToken)
+ pure $ Config{..}
+
+loadConfig :: FilePath -> IO Config
+loadConfig path = Toml.decodeFileEither configCodec path >>= \case
+ Left err -> throwM . FigMonitorBullfrogException $ tshow err
+ Right config -> pure config