36 lines
999 B
Haskell
36 lines
999 B
Haskell
module Cdl.Codegen where
|
|
import Cdl.Cow hiding (dataPoints)
|
|
import Data.Fixed
|
|
import Data.List
|
|
|
|
standard =
|
|
[ "from server.fgs.model import *"
|
|
, "from server.fgs import db"
|
|
, "import datetime"
|
|
]
|
|
|
|
dataPoint :: DataPoint -> String
|
|
dataPoint (id, Coord{longitude=lo,latitude=la}) =
|
|
"DataPoint(longitude=" ++ show lo ++
|
|
", latitude=" ++ show la ++
|
|
", datetime=datetime.now())"
|
|
|
|
dataPointLine :: DataPoint -> String
|
|
dataPointLine dp = "db.session.add(" ++ dataPoint dp ++ ")"
|
|
|
|
commitLine :: String
|
|
commitLine = "db.session.commit()"
|
|
|
|
sleepLine :: Float -> String
|
|
sleepLine f = "sleep(" ++ show f ++ ")"
|
|
|
|
dataPoints :: [DataPoint] -> [String]
|
|
dataPoints dps = map dataPointLine dps ++ [commitLine]
|
|
|
|
dataBatches :: Float -> [[DataPoint]] -> [String]
|
|
dataBatches f dbs = intercalate [sleepLine f] $ map dataPoints dbs
|
|
|
|
outputCow :: String -> Float -> Collar -> IO ()
|
|
outputCow filename f c =
|
|
writeFile filename $ intercalate "\n" $ standard ++ (dataBatches f $ runCows f [c])
|