Logging and really not working light restoration

This commit is contained in:
MasseR committed 2026-09-30 13:46:16 +03:00
1 parent 1098be227d
commit c75fc1a7b9
10 files changed
+180 -151

No files matched your search

+25 -24
View File
@@ -17,9 +17,7 @@ import Control.Concurrent.STM
, writeTChan
, writeTVar
)
import Control.Concurrent.Async (race)
import Control.Concurrent (threadDelay)
import Control.Exception (onException)
import Control.Exception.Annotated (throw)
import Control.Lens ((^?))
import Control.Monad (forever, forM_, when)
@@ -33,25 +31,28 @@ import HomeAssistant.Runtime.Bus
import HomeAssistant.Runtime.Flags (isEnabled)
import HomeAssistant.Runtime.Supervisor (Fatal (..))
import qualified Network.WebSockets as WS
import Katip (sl, logFM, Severity (..), ls, Namespace (Namespace), KatipContext, katipAddNamespace, katipAddContext)
import Katip (sl, logFM, Severity (..), ls, Namespace (Namespace), KatipContext, katipAddNamespace, katipAddContext, logLocM)
import Data.UUID (toText)
import AFRP (Request(..), Event(..))
import Control.Monad.IO.Class (liftIO, MonadIO)
import HomeAssistant.Runtime.RateLimit (RateLimited, RateLimiter, runRateLimited)
import Control.Monad.Catch (MonadCatch)
import GHC.Stack (HasCallStack)
import UnliftIO (MonadUnliftIO, race, withRunInIO)
import Control.Monad.Catch (onException)
-- | Connect, authenticate, subscribe, then receive and broadcast forever.
-- Restarting this action reconnects. All setup sends happen before the
-- connection is published in the bus, so only the writer sends afterwards.
readerAction :: MonadIO m => String -> Int -> String -> S.Set T.Text -> Bus -> m Void
readerAction host port token ents bus = liftIO $
WS.runClient host port "/api/websocket" $ \conn -> do
handshake conn token
readerAction :: (MonadCatch m, HasCallStack, KatipContext m, MonadUnliftIO m, MonadIO m) => String -> Int -> String -> S.Set T.Text -> Bus -> m Void
readerAction host port token ents bus = katipAddNamespace "receiver" $ withRunInIO $ \rio ->
WS.runClient host port "/api/websocket" $ \conn -> rio $ do
liftIO $ handshake conn token
subscribe bus conn ents
atomically $ writeTVar (busConn bus) (Just conn)
putStrLn "[reader] connected"
liftIO $ atomically $ writeTVar (busConn bus) (Just conn)
logLocM InfoS "connected"
-- Unpublish on exit so the writer blocks and the backlog survives the outage.
receiveLoop bus conn `onException` atomically (writeTVar (busConn bus) Nothing)
receiveLoop bus conn `onException` liftIO (atomically (writeTVar (busConn bus) Nothing))
handshake :: WS.Connection -> String -> IO ()
handshake conn token = do
@@ -73,12 +74,12 @@ expectType expected msg =
isTriggerEvent :: Value -> Bool
isTriggerEvent v = v ^? key "type" . _String == Just "event"
subscribe :: Bus -> WS.Connection -> S.Set T.Text -> IO ()
subscribe :: HasCallStack => KatipContext m => Bus -> WS.Connection -> S.Set T.Text -> m ()
subscribe bus conn ents =
forM_ (S.toList ents) $ \entityId -> do
print entityId
sid <- generateCallId (busGen bus)
WS.sendTextData conn $ encode $ object
logLocM InfoS $ ls entityId
sid <- liftIO $ generateCallId (busGen bus)
liftIO $ WS.sendTextData conn $ encode $ object
[ "id" .= sid
, "type" .= ("subscribe_trigger" :: T.Text)
, "trigger" .= object
@@ -93,14 +94,14 @@ subscribe bus conn ents =
-- Each read races a one-second timeout: a timeout broadcasts 'Tick' so
-- time-based primitives (debounce, rollup, fixed, ...) keep advancing
-- even when no state changes arrive.
receiveLoop :: Bus -> WS.Connection -> IO Void
receiveLoop :: (HasCallStack, MonadUnliftIO m, KatipContext m) => Bus -> WS.Connection -> m Void
receiveLoop bus conn = forever $ do
winner <- race (threadDelay 1_000_000) (WS.receiveData conn)
winner <- race (liftIO $ threadDelay 1_000_000) (liftIO $ WS.receiveData conn)
case winner of
Left () -> atomically $ writeTChan (busInbound bus) Tick
Left () -> liftIO $ atomically $ writeTChan (busInbound bus) Tick
Right msg -> case eitherDecode msg of
Left err -> putStrLn $ "[reader] skipping undecodable message: " <> err
Right v -> do
Left err -> logLocM WarningS $ ls $ "skipping undecodable message: " <> err
Right v -> liftIO $ do
when (isTriggerEvent v) (recordInbound bus)
atomically $ writeTChan (busInbound bus) (Event v)
@@ -134,11 +135,11 @@ dispatchService
=> RateLimiter -> Bus -> Request -> Service -> m ()
dispatchService rateLimiter bus request svc = do
enabled <- isEnabled (busFlags bus) (requestHandler request)
if enabled
then do
conn <- liftIO $ atomically $ readTVar (busConn bus) >>= maybe retry pure
runRateLimited rateLimiter $ sendWithId bus conn svc
else logFM InfoS (ls $ "skipped disabled handler: " <> show svc)
let logPrefix = if enabled then "Calling: " else "Skipping: "
logFM InfoS (ls $ logPrefix <> show svc)
when enabled $ do
conn <- liftIO $ atomically $ readTVar (busConn bus) >>= maybe retry pure
runRateLimited rateLimiter $ sendWithId bus conn svc
encodeService :: Int -> Service -> Value
encodeService callId Service{..} = object $