Supervise workers and make auth failures fatal
This commit is contained in:
@@ -15,6 +15,7 @@ import Control.Concurrent.STM
|
||||
, writeTChan
|
||||
, writeTVar
|
||||
)
|
||||
import Control.Exception.Annotated (throw)
|
||||
import Control.Lens ((^?))
|
||||
import Control.Monad (forever)
|
||||
import Data.Aeson (Value, eitherDecode, encode, object, (.=))
|
||||
@@ -24,6 +25,7 @@ import qualified Data.Text as T
|
||||
import Data.Void (Void)
|
||||
import HomeAssistant.Controller (Service (..))
|
||||
import HomeAssistant.Runtime.Bus
|
||||
import HomeAssistant.Runtime.Supervisor (Fatal (..))
|
||||
import qualified Network.WebSockets as WS
|
||||
|
||||
-- | Connect, authenticate, subscribe, then receive and broadcast forever.
|
||||
@@ -53,7 +55,7 @@ expectType :: T.Text -> Value -> IO ()
|
||||
expectType expected msg =
|
||||
case msg ^? key "type" . _String of
|
||||
Just t | t == expected -> pure ()
|
||||
_ -> fail $ "expected " <> T.unpack expected <> ", got: " <> show msg
|
||||
_ -> throw (Fatal $ "expected " <> expected <> ", got: " <> T.pack (show msg))
|
||||
|
||||
subscribe :: Bus -> WS.Connection -> IO ()
|
||||
subscribe bus conn = do
|
||||
@@ -77,7 +79,7 @@ receiveJSON :: WS.Connection -> IO Value
|
||||
receiveJSON conn = do
|
||||
msg <- WS.receiveData conn
|
||||
case eitherDecode msg of
|
||||
Left err -> fail $ "Invalid JSON from Home Assistant: " ++ err
|
||||
Left err -> throw (Fatal $ "Invalid JSON from Home Assistant: " <> T.pack err)
|
||||
Right x -> pure x
|
||||
|
||||
writerAction :: Bus -> IO Void
|
||||
|
||||
Reference in New Issue
Block a user