summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYves Fischer <yvesf-git@xapek.org>2013-06-05 02:16:43 +0200
committerYves Fischer <yvesf-git@xapek.org>2013-06-05 02:16:43 +0200
commit041d3b4b87cde7634b37b9dd7428a8ef8b0d7397 (patch)
tree5e25414ae057b0594d3fc0934b0be51b7331ecac
parenta1988622da1e4b09df5b994d07ab3d6ee999f0d5 (diff)
downloadebus-alt-041d3b4b87cde7634b37b9dd7428a8ef8b0d7397.tar.gz
ebus-alt-041d3b4b87cde7634b37b9dd7428a8ef8b0d7397.zip
serialparser
-rw-r--r--datasources/.gitignore3
-rw-r--r--datasources/serialparser.hs45
2 files changed, 48 insertions, 0 deletions
diff --git a/datasources/.gitignore b/datasources/.gitignore
new file mode 100644
index 0000000..6478d14
--- /dev/null
+++ b/datasources/.gitignore
@@ -0,0 +1,3 @@
+*.o
+*.hi
+
diff --git a/datasources/serialparser.hs b/datasources/serialparser.hs
new file mode 100644
index 0000000..24686f5
--- /dev/null
+++ b/datasources/serialparser.hs
@@ -0,0 +1,45 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+import Control.Applicative((<*))
+import Data.Attoparsec(Parser,takeWhile1)
+import Data.Attoparsec.Char8 (char8, endOfLine, isEndOfLine)
+import Data.Attoparsec.Enumerator(iterParser)
+import Data.Enumerator(Iteratee,Enumerator,run_,($$))
+import Data.Enumerator.Binary (enumHandle)
+import System.Environment(getArgs)
+import System.IO (Handle,stdin)
+import Data.ByteString.Char8(ByteString,unpack)
+import Network.HTTP(simpleHTTP,getRequest)
+import Network.HTTP.Base(urlEncode)
+import System.Hardware.Serialport(hOpenSerial,defaultSerialSettings,commSpeed,CommSpeed(CS115200))
+data Message = Measurement ByteString ByteString deriving (Show)
+
+parser :: Parser Message
+parser = do
+ sensor <- takeWhile1 (/=61) <* char8 '='
+ value <- takeWhile1 (not . isEndOfLine) <* endOfLine
+ return $! Measurement sensor value
+
+parserIteratee :: Handle -> Iteratee ByteString IO (Message)
+parserIteratee handle = stdinEnumerator $$ iteratee
+ where stdinEnumerator :: Enumerator ByteString IO b
+ stdinEnumerator = enumHandle 1 handle
+ iteratee :: Iteratee ByteString IO (Message)
+ iteratee = iterParser parser
+
+work :: Handle -> IO ()
+work port = do (Measurement sensor value) <- run_ (parserIteratee port)
+ let url = "http://localhost:8080/api/value/" ++ (urlEncode $ unpack sensor) ++ "/" ++ (urlEncode $ unpack value) in
+ simpleHTTP (getRequest url)
+ putStrLn $ (unpack sensor) ++ "=" ++ (unpack value)
+ work port
+
+main :: IO ()
+main = do
+ args <- getArgs
+ port <- case args of
+ [path] -> hOpenSerial path defaultSerialSettings { commSpeed = CS115200 }
+ _ -> return stdin
+ work port
+
+