Skip to content

Instantly share code, notes, and snippets.

newtype LuminaireState = LuminaireState
{ gateway :: GatewayId
, lights :: Map LightId LightState
}
derive instance newtypeLuminaireState :: Newtype LuminaireState _
...
setLight :: forall e. LuminaireId -> LightId -> LightColor -> Maybe Milliseconds -> AppState -> Eff (ref :: REF | e) Boolean
setLight luminaireId lightId color transitionTime appState = do
-- types
newtype LightId = LightId String
instance showLightId :: Show LightId where
show (LightId id) = show id
derive instance eqLightId :: Eq LightId
derive instance ordLightId :: Ord LightId
derive instance newtypeLightId :: Newtype LightId _
newtype Light = Light
getLuminaireLight
:: forall e
. LuminaireId
-> LightId
-> Luminaires
-> Eff (ref :: REF | e) (Maybe Light)
getLuminaireLight id lid luminaires = do
luminaire <- getLuminaire id luminaires
let lights = _.lights <<< unwrap <$> luminaire
let light = lookup lid <$> lights
nextState
:: forall e
. Light
-> LightColor
-> (Maybe Milliseconds)
-> Eff (now :: NOW | e) Light
nextState light nextColor transitionTime = do
wrap { color: nextColor
, prevColor: nextColor
, transitionStart: now
setLight
:: forall e
. LuminaireId
-> LightId
-> LightColor
-> (Maybe Milliseconds)
-> Luminaires
-> Eff (ref :: REF, now :: NOW | e) Boolean
setLight id lid color transitionTime luminaires = do
light <- getLuminaireLight id lid luminaires
newtype Light = Light
{ color :: LightColor
, prevColor :: LightColor
, transitionStart :: Instant
, transitionTime :: Milliseconds
}
derive newtype instance rfLight :: ReadForeign Light
-- No type class instance was found for
-- WTF instant creates a Maybe Instant
toLight :: StupidIntermediateStrMapThingy -> Maybe Lights
toLight =
foldlWithIndex (\k m v -> do
let transitionStart = instant (Milliseconds v.transitionStart)
let transitionTime = Milliseconds v.transitionTime
insert (LightId k) (Light v { transitionStart = transitionStart, transitionTime = transitionTime }) m
) empty
newtype Light = Light
{ color :: LightColor
, prevColor :: LightColor
--, transitionStart :: Instant
, transitionStart :: Milliseconds
, transitionTime :: Milliseconds
}
type Lights = Map LightId Light
{"queue":["dQw4w9WgXcQ","dNCrpIL2XlY","RYRLEzQVqTs","d01XRSB-7dA","MGFiqGGVAuk","whScLb0aAiM","2vjPBrBU-TM","M
NyG-xu-7SQ","_DV7Lfxdp_0","_DV7Lfxdp_0","tVj0ZTS4WF4","isP-mRUSJ6k","K0a_ofU-pLE","eW6sEkTGbUc","feA64wXhbjo",
"ntuZWHd-l_Y","Lo_edXUlrT8","Qi3KQ1FN_aE","Qi3KQ1FN_aE","dQw4w9WgXcQ","d01XRSB-7dA","koGrknEchhk","8ZCysBT5Kec
","koGrknEchhk","koGrknEchhk","fbGkxcY7YFU","fiU65nQdZE8","Ly9Uw3oaIwc","FMElp1zibWc","KJbi8AaAp0g","ZZ5LpwO-A
n4&list=RDQMDsgEFzZyzxs","vTIIMJ9tUc8","eDWuhjKO8nI","2UByXynZDUw","dHDWtJpvURU","lAIGb1lfpBw","6M6samPEMpM","
CduA0TULnow","bTm1C1ip3tI","bTm1C1ip3tI","bTm1C1ip3tI","wE3sp-RQUe0","jdCyG8zxUF8","g8ZFLCG05o4","XUhVCoTsBaM"
,"9bZkp7q19f0","OTAn2M63wZI","FoR43uPoLrM","1jre6_FBBc0","IJNR2EpS0jw","8Wp9iNINHMc","ETfiUYij5UE","YCME_bIuF3
k","MtN1YnoL46Q","NyPmhjRuPjA","F8Cg572dafQ","uK3MLlTL5Ko","otCpCn0l4Wo","kffacxfA7G4","ymNFyxvIdaM","BS-CnWB4
g7M","FdDDiEkRbvQ","dP9Wp6QVbsk","3_NaaZEjCWY","EbGjxdyRdOI","RqLAfErhXvU","pCdry6OQNtw","UrlKiddMtJQ","NX6qZl
Qnb1M",
handleMessage
:: forall e
. SockJS.Connection
-> Message
-> Eff (console :: CONSOLE | e) Unit
handleMessage conn (CurrentQueue message) = do
-- how to modify state here?
log $ "queue message: " <> unsafeStringify message
handleMessage conn (PlayPause message) = do
log $ "play/pause message: " <> unsafeStringify message