2024-07-13 19:50:43 -07:00
|
|
|
-- requires 'at' for running a command at a certain time
|
|
|
|
-- requires 'date' for converting unix time to human readable time
|
|
|
|
-- run on boot and at noon and midnight
|
|
|
|
-- don't add commands to at while this script is running (monadic purity must be preserved)
|
|
|
|
|
|
|
|
module Main where
|
|
|
|
|
2024-08-01 20:15:32 -07:00
|
|
|
import Pure (readLines)
|
|
|
|
import Cache (dumpCache, start)
|
|
|
|
import Const (query)
|
|
|
|
import Sugar (continue, destruct)
|
2024-07-22 15:39:35 -07:00
|
|
|
import Types (ResponseCode(..), ResponseMsg(..), genericErr, toResponseMsg)
|
2024-08-01 20:15:32 -07:00
|
|
|
import Getters (fetch)
|
|
|
|
import Workers (backupRunner, prepareScripts)
|
2024-07-22 14:06:18 -07:00
|
|
|
|
2024-07-13 19:50:43 -07:00
|
|
|
import Data.ByteString.Char8 (unpack)
|
2024-07-22 15:39:35 -07:00
|
|
|
import Network.HTTP.Request (Response(responseBody, responseStatus))
|
2024-07-13 19:50:43 -07:00
|
|
|
|
|
|
|
-- error handling
|
2024-08-01 20:15:32 -07:00
|
|
|
-- clean up jank (
|
|
|
|
-- create types,
|
|
|
|
-- more pure functions,
|
|
|
|
-- reduce lines in functions,
|
|
|
|
-- more atomic/functional,
|
|
|
|
-- etc
|
|
|
|
-- )
|
2024-07-13 19:50:43 -07:00
|
|
|
-- eliminate as many do blocks as possible
|
|
|
|
-- introduce liquid types and checking
|
|
|
|
-- whitepaper!
|
|
|
|
|
|
|
|
process :: Response -> (Double -> Double -> String -> IO ()) -> IO () -> IO ()
|
|
|
|
process r run err =
|
|
|
|
case info of
|
2024-07-22 15:27:06 -07:00
|
|
|
Nothing -> continue genericErr err
|
2024-07-13 19:50:43 -07:00
|
|
|
Just (msg, lat, lon, tz) ->
|
|
|
|
let status = ResponseMsg msg
|
|
|
|
runner = run lat lon tz
|
|
|
|
in destruct status runner err
|
|
|
|
where
|
|
|
|
body = responseBody r
|
|
|
|
parts = (lines . unpack) body
|
|
|
|
info = (readLines . take 4) parts
|
|
|
|
|
|
|
|
processRunner :: Response -> IO ()
|
2024-08-01 20:15:32 -07:00
|
|
|
processRunner r = process r run backupRunner
|
|
|
|
where run lat lon tz = dumpCache lat lon tz >>= prepareScripts
|
2024-07-13 19:50:43 -07:00
|
|
|
|
2024-08-01 20:15:32 -07:00
|
|
|
ping :: (Response -> IO ()) -> IO () -> IO ()
|
|
|
|
ping run err = do
|
|
|
|
res <- fetch query
|
|
|
|
case res of
|
|
|
|
Left e -> continue (toResponseMsg e) err
|
|
|
|
Right r ->
|
|
|
|
let status = (ResponseCode . responseStatus) r
|
|
|
|
runner = run r
|
|
|
|
in destruct status runner err
|
2024-07-13 19:50:43 -07:00
|
|
|
|
|
|
|
routine :: IO ()
|
|
|
|
routine = ping processRunner backupRunner
|
|
|
|
|
|
|
|
main :: IO ()
|
2024-08-01 20:15:32 -07:00
|
|
|
main = start >> routine
|