diff options
author | Paul Oliver <contact@pauloliver.dev> | 2025-01-17 14:37:20 -0800 |
---|---|---|
committer | Paul Oliver <contact@pauloliver.dev> | 2025-01-17 19:16:43 -0800 |
commit | ebb88408c1d0884b5ca9b7d68bf76d31c33d2e5b (patch) | |
tree | c7c2c6b636e8eb89f2d4c6accf77a8c671b8ab9f /hsm-dummy-pulser/Main.hs | |
parent | dc6bf1472c930ff1448c419d3205148bce1b787e (diff) |
Diffstat (limited to 'hsm-dummy-pulser/Main.hs')
-rw-r--r-- | hsm-dummy-pulser/Main.hs | 30 |
1 files changed, 13 insertions, 17 deletions
diff --git a/hsm-dummy-pulser/Main.hs b/hsm-dummy-pulser/Main.hs index d15b616..cc16cd4 100644 --- a/hsm-dummy-pulser/Main.hs +++ b/hsm-dummy-pulser/Main.hs @@ -2,6 +2,7 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TemplateHaskell #-} +import Data.ByteString (ByteString) import Data.Function ((&)) import Data.Text (Text, pack) import Effectful (Eff, (:>), runEff) @@ -13,6 +14,7 @@ import Effectful.State.Static.Local (evalState) import Hsm.Core.App (launch) import Hsm.Core.Env (deriveFromYaml) import Hsm.Core.Fsm qualified as F +import Hsm.Core.Message (message) import Hsm.Core.Zmq.Server (runServer, send) import Streamly.Data.Stream (Stream, repeatM) import System.IO.Echo (withoutInputEcho) @@ -29,29 +31,23 @@ $(deriveFromYaml ''Env) pulse :: (Concurrent :> es, Reader Env :> es) => Stream (Eff es) () pulse = repeatM $ asks period >>= threadDelay -stateRun :: F.FsmState () Int Env Int -stateRun = F.FsmState "run" action - where - action _ env sta = - if sta < env.pulses - then next - else exit - where - next = - F.FsmOutput - (Just $ F.FsmResult sta (succ sta) stateRun) - [(LogInfo, "Sending pulse #" <> pack (show sta))] - exit = - F.FsmOutput - Nothing - [(LogAttention, "Reached " <> pack (show env.pulses) <> " pulses")] +stateRun :: F.FsmState () ByteString Env Int +stateRun = + F.FsmState "run" $ \_ env sta -> + if sta < env.pulses + then F.FsmOutput + (Just $ F.FsmResult (message env.name sta) (succ sta) stateRun) + [(LogInfo, "Sending pulse #" <> pack (show sta))] + else F.FsmOutput + Nothing + [(LogAttention, "Sent " <> pack (show env.pulses) <> " pulses")] -- Dummy pulser service: -- Proof of concept. Publishes a "pulse" through ZMQ at a set interval. main :: IO () main = launch @Env "dummy-pulser" withoutInputEcho $ \env logger level -> - (pulse & F.fsm @_ @_ @Env @Int & send @Env @_ @Int) + (pulse & F.fsm @_ @_ @Env @Int & send) & runServer @Env & evalState @Int 1 & evalState stateRun |