Skip to content
This repository has been archived by the owner on Mar 2, 2022. It is now read-only.

Commit

Permalink
Send intitial positions in putState
Browse files Browse the repository at this point in the history
See #53
  • Loading branch information
jaspervdj committed Mar 21, 2013
1 parent efd717b commit 585d1e7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
27 changes: 19 additions & 8 deletions count-von-count/src/CountVonCount/Boxxy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ putState boxxies target = do
-- Get info
stations <- P.getAllStations database
teams <- P.getAllTeams database
teams' <- forM teams $ \team -> do
cstate <- Counter.counterStateForTeam team (boxxiesCounter boxxies)
return (team, cstate)
laps <- P.getLatestLaps database Nothing 10
laps' <- forM laps $ \lap -> do
team <- P.getTeam database $ P.lapTeam lap
Expand All @@ -157,7 +160,7 @@ putState boxxies target = do
Just t -> forM_ [t]

withTarget $ \c -> makeRequest c "/state" $
stateJson circuitLength stations teams laps'
stateJson circuitLength stations teams' laps'
where
circuitLength = configCircuitLength config
config = boxxiesConfig boxxies
Expand Down Expand Up @@ -186,26 +189,34 @@ boxxiesToList boxxies = forM (boxxiesState boxxies) $ \(c, rs) -> do


--------------------------------------------------------------------------------
stateJson :: Double -> [P.Station] -> [P.Team] -> [(P.Lap, P.Team)] -> A.Value
stateJson :: Double -> [P.Station] -> [(P.Team, Counter.CounterState)]
-> [(P.Lap, P.Team)] -> A.Value
stateJson circuitLength stations teams laps = A.object
[ "circuitLength" .= circuitLength
, "stations" .= A.object
[ P.refToText (P.stationId s) .= stationJson s
| s <- stations
]
, "teams" .= A.object
[P.refToText (P.teamId t) .= teamJson t | t <- teams]
[P.refToText (P.teamId t) .= teamJson t cs | (t, cs) <- teams]
, "laps" .= map (uncurry lapJson) laps
]


--------------------------------------------------------------------------------
teamJson :: P.Team -> A.Value
teamJson t = A.object
[ "id" .= P.refToText (P.teamId t)
, "laps" .= P.teamLaps t
, "name" .= P.teamName t
teamJson :: P.Team -> Counter.CounterState -> A.Value
teamJson t cstate = A.object
[ "id" .= P.refToText (P.teamId t)
, "laps" .= P.teamLaps t
, "name" .= P.teamName t
, "position" .= pos
, "lastUpdate" .= lu
]
where
(pos, lu) = case cstate of
Counter.NoCounterState -> (Nothing, Nothing)
Counter.CounterState _ se _ lu' ->
(Just (P.refToText $ P.stationId $ sensorStation se), Just lu')


--------------------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions count-von-count/src/CountVonCount/Counter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module CountVonCount.Counter

, resetCounterFor
, counterStateFor
, counterStateForTeam

, findDeadBatons
, watchdog
Expand Down Expand Up @@ -135,6 +136,13 @@ counterStateFor baton counter =
lookupCounterState baton <$> readIORef (counterMap counter)


--------------------------------------------------------------------------------
counterStateForTeam :: P.Team -> Counter -> IO CounterState
counterStateForTeam team counter = case P.teamBaton team of
Nothing -> return NoCounterState
Just bref -> counterStateFor bref counter


--------------------------------------------------------------------------------
findDeadBatons :: Int -> Counter -> IO [P.Ref P.Baton]
findDeadBatons lifespan counter = do
Expand Down

0 comments on commit 585d1e7

Please sign in to comment.