Skip to content

Commit

Permalink
Removes Resource* modules; centralizes Device and Group message handl…
Browse files Browse the repository at this point in the history
…ing in DeviceView update; adds Groups page
  • Loading branch information
ddellacosta committed Feb 22, 2025
1 parent 0126349 commit 7e55770
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 227 deletions.
5 changes: 5 additions & 0 deletions ui/src/AutomationService/DeviceMessage.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Prelude (class Show)

import AutomationService.Device (Device, DeviceId)
import AutomationService.DeviceState (DeviceState)
import AutomationService.Group (Group)
import Data.Generic.Rep (class Generic)
import Data.Show.Generic (genericShow)

Expand All @@ -18,6 +19,10 @@ data Message
| DeviceSelected DeviceId
| NoDeviceSelected
| PublishDeviceMsg String
-- group messages
| LoadGroupsFailed String
| LoadGroups (Array Group)
| PublishGroupMsg String

derive instance Generic Message _

Expand Down
41 changes: 41 additions & 0 deletions ui/src/AutomationService/DeviceView.purs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import AutomationService.Device (Device(..), DeviceDetails, DeviceId, Devices,
import AutomationService.DeviceMessage (Message(..))
import AutomationService.DeviceState (DeviceState, DeviceStates, getDeviceState)
import AutomationService.Exposes (SubProps(..), canGet, canSet, enumValues, isOn, isPublished)
import AutomationService.Group (Group)
import AutomationService.Lighting (ColorSetter(..), getColorSetter, getNumericCap,
getOnOffSwitch, getPreset)
import AutomationService.Logging (debug)
Expand Down Expand Up @@ -48,6 +49,7 @@ type State =
, deviceStates :: DeviceStates
, selectedDeviceId :: Maybe DeviceId
, deviceStateUpdateTimers :: Ref DeviceStateUpdateTimers
, groups :: Array Group
}

initState :: Ref DeviceStateUpdateTimers -> State
Expand All @@ -56,6 +58,7 @@ initState dsUpdateTimers =
, deviceStates: M.empty
, selectedDeviceId: Nothing
, deviceStateUpdateTimers: dsUpdateTimers
, groups: []
}

init :: Ref DeviceStateUpdateTimers -> Transition Message State
Expand Down Expand Up @@ -120,12 +123,50 @@ update ws s = case _ of
pure $ s { selectedDeviceId = Nothing }

PublishDeviceMsg msg -> do
forkVoid $ liftEffect $ do
debug $ "Publishing with '" <> msg -- <> "' to topic: " <> topic
for_ ws $ \ws' ->
sendString ws' msg
pure s

LoadGroups newGroups -> do
-- _ <- traceM newGroups

forkVoid $ liftEffect $ debug $ "loaded groups: " <> show newGroups

forkVoid $ do
liftEffect $ for_ newGroups $ \g -> do
let
-- this needs to get passed in from parent state as config, or something
subscribeMsg =
MQTT.subscribe (deviceTopic g.name) "HTTP 8080"
pingStateMsg =
MQTT.publish (getTopic g.name) $ MQTT.state ""

debug $ "Groups: subscribing with: " <> (stringify $ encodeJson subscribeMsg)
debug $
"Groups: pinging to get initial state: " <> (stringify $ encodeJson pingStateMsg)

for_ ws $ \ws' -> do
sendJson ws' <<< encodeJson $ subscribeMsg
sendJson ws' <<< encodeJson $ pingStateMsg

pure s { groups = newGroups }

LoadGroupsFailed _msg -> do
-- forkVoid $
-- liftEffect $ debug $ "LoadGroupsFailed with msg: " <> msg) *> pure s
-- _ <- traceM msg
pure s

PublishGroupMsg msg -> do
forkVoid $ liftEffect $ do
debug $ "Publishing with '" <> msg -- <> "' to topic: " <> topic
for_ ws $ \ws' ->
sendString ws' msg
pure s


view :: State -> Dispatch Message -> ReactElement
view { devices, deviceStates } dispatch =
H.div "all-devices" -- "container mx-auto mt-5 d-flex flex-column justify-content-between"
Expand Down
11 changes: 0 additions & 11 deletions ui/src/AutomationService/GroupMessage.purs

This file was deleted.

94 changes: 11 additions & 83 deletions ui/src/AutomationService/GroupView.purs
Original file line number Diff line number Diff line change
@@ -1,94 +1,22 @@
module AutomationService.GroupView
( State
, init
, initState
, update
, view
( view
)
where

import AutomationService.Device as Device
import AutomationService.DeviceView as Devices
import AutomationService.Group (Group)
import AutomationService.GroupMessage (Message(..))
import AutomationService.Logging (debug)
import AutomationService.MQTT as MQTT
import AutomationService.DeviceView (State) as Devices
import AutomationService.DeviceMessage (Message) as Devices
-- import AutomationService.MQTT as MQTT
import AutomationService.React.Bootstrap as Bootstrap
import AutomationService.WebSocket (class WebSocket, sendJson, sendString)
import Data.Argonaut.Core (stringify)
import Data.Argonaut.Encode.Class (encodeJson)
import Data.Array as A
import Data.Foldable (for_)
-- import Data.Map as M
import Data.Maybe (Maybe)
import Effect.Class (liftEffect)
import Elmish (Transition, Dispatch, ReactElement, forkVoid) -- , (<|), (<?|))
import Data.Array as Array
-- import Data.Maybe (Maybe)
import Elmish (Dispatch, ReactElement) -- , (<|), (<?|))
import Elmish.HTML.Styled as H
import Prelude (($), (<<<), (<>), (<#>), discard, pure, show)
import Prelude (($), (<<<), (<>), (<#>), show)

-- import Debug (traceM)


type State =
{ groups :: Array Group
}

initState :: State
initState =
{ groups: []
}

init :: Transition Message State
init = pure initState

update
:: forall ws. WebSocket ws
=> Maybe ws
-> State
-> Message
-> Transition Message State
update ws s = case _ of
LoadGroups newGroups -> do
-- _ <- traceM newGroups

forkVoid $ liftEffect $ debug $ "loaded groups: " <> show newGroups

forkVoid $ do
liftEffect $ for_ newGroups $ \g -> do
let
-- this needs to get passed in from parent state as config, or something
subscribeMsg =
MQTT.subscribe (Device.deviceTopic g.name) "HTTP 8080"
pingStateMsg =
MQTT.publish (Device.getTopic g.name) $ MQTT.state ""

debug $ "Groups: subscribing with: " <> (stringify $ encodeJson subscribeMsg)
debug $
"Groups: pinging to get initial state: " <> (stringify $ encodeJson pingStateMsg)

for_ ws $ \ws' -> do
sendJson ws' <<< encodeJson $ subscribeMsg
sendJson ws' <<< encodeJson $ pingStateMsg

pure s { groups = newGroups }

LoadGroupsFailed _msg -> do
-- forkVoid $
-- liftEffect $ debug $ "LoadGroupsFailed with msg: " <> msg) *> pure s
-- _ <- traceM msg
pure s

PublishGroupMsg msg -> do
forkVoid $ liftEffect $ do
debug $ "Publishing with '" <> msg -- <> "' to topic: " <> topic
for_ ws $ \ws' ->
sendString ws' msg
pure s

view :: Devices.State -> State -> Dispatch Message -> ReactElement
view _state { groups } _dispatch =
view :: Devices.State -> Dispatch Devices.Message -> ReactElement
view _state@{ groups } _dispatch =
H.div ""
[ H.div "" $ H.text $ (show $ A.length groups) <> " Groups"
[ H.div "" $ H.text $ (show $ Array.length groups) <> " Groups"
, H.ul "" $ groups <#> \g ->
Bootstrap.accordion {} $
Bootstrap.accordionItem { eventKey: 0 }
Expand Down
12 changes: 7 additions & 5 deletions ui/src/AutomationService/Message.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@ where

import Prelude

import AutomationService.ResourceMessage as Resources
import AutomationService.DeviceMessage as Devices
import Data.Generic.Rep (class Generic)
import Data.Show.Generic (genericShow)
import Data.String.Common as S
import Data.String.Pattern (Pattern(..), Replacement(..))

data Message ws
= SetPage Page
| DeviceMsg Resources.Message
| GroupMsg Resources.Message
| UpdateCnt Resources.Message
| DeviceMsg Devices.Message
| InitWS ws
| PublishMsgChanged String
| Publish

data Page = Devices | PublishMQTT
data Page
= Devices
| Groups
| PublishMQTT

derive instance Generic Page _

Expand All @@ -33,6 +34,7 @@ instance Show Page where
pageName :: Page -> String
pageName = case _ of
Devices -> "Devices"
Groups -> "Groups"
PublishMQTT -> "Publish MQTT"

pageNameClass :: Page -> String
Expand Down
12 changes: 0 additions & 12 deletions ui/src/AutomationService/ResourceMessage.purs

This file was deleted.

74 changes: 0 additions & 74 deletions ui/src/AutomationService/ResourceView.purs

This file was deleted.

Loading

0 comments on commit 7e55770

Please sign in to comment.