blob: 06ffc6f46b95f78773b7dafb7dd771a85dda6d1b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
module Main(main) where
import Control.Exception
import Data.Attoparsec.Enumerator (iterParser, ParseError, errorContexts)
import Data.ByteString (ByteString,pack)
import Data.Enumerator (Enumerator, Iteratee, enumLists, run, ($$))
import Data.Enumerator.Binary (enumHandle)
import System.Exit
import System.IO (hSetBinaryMode,stdin)
import Network.EBus.Layer2
main :: IO ()
main = do
-- * Select binary mode (True) or text mode (False) on a open handle. (See also openBinaryFile.)
hSetBinaryMode stdin True
loop 900
where loop :: Int -> IO()
loop 0 = do
print "finished"
loop n = do
readAndDumpPacket
loop $ n - 1
-- | Read one eBus Packet and dump layer2 to stdout
readAndDumpPacket :: IO()
readAndDumpPacket = do
-- * run
-- Run an iteratee until it finishes, and return either the final value (if it succeeded) or the error (if it failed).
-- * run_
-- Like run, except errors are converted to exceptions and thrown. Primarily useful for small scripts or other simple cases.
maybePacket <- run( stdinEnumerator $$ parserIteratee)
case maybePacket of
Right result -> do { print "Result"; print $ result}
Left exc -> case fromException (exc::SomeException) of
Just e
| any (\ctx -> ctx == "demandInput") (errorContexts (e::ParseError)) -> do
putStrLn "EOF reached"
exitWith $ ExitFailure 1
| otherwise -> do putStr "Other Error: "; print e
_ -> putStr ""
stdinEnumerator :: Enumerator ByteString IO a
stdinEnumerator = enumHandle 1 stdin
parserIteratee :: Iteratee ByteString IO EbusPacket
parserIteratee = do
p <- iterParser ebusParserLayer2
return p
|