Fix warnings
This commit is contained in:
+1
-1
@@ -21,7 +21,7 @@ module AFRP
|
|||||||
|
|
||||||
import Control.Category (Category(..), (>>>))
|
import Control.Category (Category(..), (>>>))
|
||||||
import Prelude hiding ((.), id)
|
import Prelude hiding ((.), id)
|
||||||
import Control.Arrow (Arrow(..), ArrowChoice(..), ArrowLoop(..), returnA)
|
import Control.Arrow (Arrow(..), ArrowChoice(..), ArrowLoop(..))
|
||||||
import Data.Time (UTCTime)
|
import Data.Time (UTCTime)
|
||||||
import Control.Monad.Fix (MonadFix (mfix))
|
import Control.Monad.Fix (MonadFix (mfix))
|
||||||
import Data.Either (fromLeft)
|
import Data.Either (fromLeft)
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ module HomeAssistant.Controller
|
|||||||
, lightController
|
, lightController
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import AFRP (Mealy, eff, Event(..), hold, events, changes, mapAccum, filterA, (>>|), toEvent)
|
import AFRP (Mealy, eff, Event(..), hold, events, changes, filterA, (>>|), toEvent)
|
||||||
import Control.Arrow (Arrow(..), ArrowChoice(..), returnA)
|
import Control.Arrow (Arrow(..), returnA)
|
||||||
import Control.Category ((>>>))
|
import Control.Category ((>>>))
|
||||||
import Data.Aeson (Value)
|
import Data.Aeson (Value)
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
@@ -63,15 +63,6 @@ data Ruuvi = Ruuvi { ruuviTemperature :: Double, ruuviPressure :: Double }
|
|||||||
ruuvi :: Mealy eff (Event Value) (Event Ruuvi)
|
ruuvi :: Mealy eff (Event Value) (Event Ruuvi)
|
||||||
ruuvi = (Ruuvi <$> ruuviTemperatures <*> ruuviPressures) >>> changes
|
ruuvi = (Ruuvi <$> ruuviTemperatures <*> ruuviPressures) >>> changes
|
||||||
|
|
||||||
data Direction = Increase | Decrease | Steady
|
|
||||||
deriving (Show, Eq)
|
|
||||||
|
|
||||||
numericDirection = mapAccum go (Nothing, Nothing) extract
|
|
||||||
where
|
|
||||||
go (_, old) new = (old, new)
|
|
||||||
extract :: (Maybe Double, Maybe Double) -> Direction
|
|
||||||
extract (old, new) = maybe Steady (\x -> if x > 0 then Increase else Decrease) $ (-) <$> old <*> new
|
|
||||||
|
|
||||||
data DoorState = Open | Closed
|
data DoorState = Open | Closed
|
||||||
deriving (Show, Eq)
|
deriving (Show, Eq)
|
||||||
|
|
||||||
@@ -101,9 +92,6 @@ lightController = proc ev -> do
|
|||||||
|
|
||||||
entityChangeEvent :: T.Text -> Mealy eff (Event Value) (Event Value)
|
entityChangeEvent :: T.Text -> Mealy eff (Event Value) (Event Value)
|
||||||
entityChangeEvent entityId = entityChangeEvent' entityId >>> toEvent
|
entityChangeEvent entityId = entityChangeEvent' entityId >>> toEvent
|
||||||
where
|
|
||||||
isEntity :: Value -> Bool
|
|
||||||
isEntity = has (key "event" . key "data" . key "entity_id" . _String . only entityId)
|
|
||||||
|
|
||||||
entityChangeEvent' :: T.Text -> Mealy eff (Event Value) (Either () Value)
|
entityChangeEvent' :: T.Text -> Mealy eff (Event Value) (Either () Value)
|
||||||
entityChangeEvent' entityId = events >>| filterA isEntity
|
entityChangeEvent' entityId = events >>| filterA isEntity
|
||||||
@@ -123,13 +111,10 @@ entityBool' entityId = entityChangeEvent' entityId >>| (arr state >>> arr (maybe
|
|||||||
toBool = \case
|
toBool = \case
|
||||||
"on" -> True
|
"on" -> True
|
||||||
"off" -> False
|
"off" -> False
|
||||||
|
_ -> False
|
||||||
|
|
||||||
entityRead :: (Read a) => T.Text -> Mealy eff (Event Value) (Event a)
|
entityRead :: (Read a) => T.Text -> Mealy eff (Event Value) (Event a)
|
||||||
entityRead entityId = entityRead' entityId >>> toEvent
|
entityRead entityId = entityRead' entityId >>> toEvent
|
||||||
where
|
|
||||||
state v = v ^? key "event" . key "data" . key "new_state" . key "state" . _String . TL.unpacked . to read
|
|
||||||
|
|
||||||
entityBool :: T.Text -> Mealy eff (Event Value) (Event Bool)
|
entityBool :: T.Text -> Mealy eff (Event Value) (Event Bool)
|
||||||
entityBool entityId = entityBool' entityId >>> toEvent
|
entityBool entityId = entityBool' entityId >>> toEvent
|
||||||
where
|
|
||||||
state v = v ^? key "event" . key "data" . key "new_state" . key "state" . _String . TL.unpacked . to read
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ module HomeAssistant.Runtime
|
|||||||
, CallIdGen
|
, CallIdGen
|
||||||
, mkCallIdGen
|
, mkCallIdGen
|
||||||
, hassEval
|
, hassEval
|
||||||
|
, dryRunHassEval
|
||||||
, receiveJSON
|
, receiveJSON
|
||||||
, wsCallService
|
, wsCallService
|
||||||
) where
|
) where
|
||||||
@@ -21,7 +22,7 @@ import qualified Data.Text as T
|
|||||||
import qualified Network.WebSockets as WS
|
import qualified Network.WebSockets as WS
|
||||||
import Network.Socket (withSocketsDo)
|
import Network.Socket (withSocketsDo)
|
||||||
import System.Environment (getEnv)
|
import System.Environment (getEnv)
|
||||||
import Data.Time (UTCTime, getCurrentTime)
|
import Data.Time (getCurrentTime)
|
||||||
import Data.IORef (newIORef, atomicModifyIORef')
|
import Data.IORef (newIORef, atomicModifyIORef')
|
||||||
|
|
||||||
step :: (forall x. eff x -> IO x) -> Mealy eff a b -> a -> IO (b, Mealy eff a b)
|
step :: (forall x. eff x -> IO x) -> Mealy eff a b -> a -> IO (b, Mealy eff a b)
|
||||||
@@ -72,7 +73,7 @@ app gen token conn = do
|
|||||||
go f = do
|
go f = do
|
||||||
msg <- WS.receiveData conn :: IO BL.ByteString
|
msg <- WS.receiveData conn :: IO BL.ByteString
|
||||||
let decoded = Event $ either (const Null) id $ eitherDecode @Value msg
|
let decoded = Event $ either (const Null) id $ eitherDecode @Value msg
|
||||||
(x, f') <- step (hassEval gen conn) f decoded
|
(x, f') <- step (dryRunHassEval gen) f decoded
|
||||||
mapM_ print x
|
mapM_ print x
|
||||||
go f'
|
go f'
|
||||||
|
|
||||||
@@ -112,6 +113,12 @@ hassEval :: CallIdGen -> WS.Connection -> HASSEff a -> IO a
|
|||||||
hassEval gen conn = \case
|
hassEval gen conn = \case
|
||||||
CallService x -> do
|
CallService x -> do
|
||||||
callId <- generateCallId gen
|
callId <- generateCallId gen
|
||||||
|
wsCallService conn callId (serviceDomain x) (serviceName x) (serviceTarget x)
|
||||||
|
Pure a -> pure a
|
||||||
|
|
||||||
|
dryRunHassEval :: CallIdGen -> HASSEff a -> IO a
|
||||||
|
dryRunHassEval gen = \case
|
||||||
|
CallService x -> do
|
||||||
|
callId <- generateCallId gen
|
||||||
print (callId, x)
|
print (callId, x)
|
||||||
-- wsCallService conn callId (serviceDomain x) (serviceName x) (serviceTarget x)
|
|
||||||
Pure a -> pure a
|
Pure a -> pure a
|
||||||
|
|||||||
Reference in New Issue
Block a user