diff --git a/src/Cdl/Codegen.hs b/src/Cdl/Codegen.hs new file mode 100644 index 0000000..258ae38 --- /dev/null +++ b/src/Cdl/Codegen.hs @@ -0,0 +1,35 @@ +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])