packages feed

WL500gPLib-0.3.1: src/Main.hs

module Main where

import qualified Network.Asus.WL500gP as R
import System.Environment (getArgs)
import Control.Monad (liftM, sequence_)
import Data.Either (either)
import Control.Concurrent (threadDelay)
import Control.Monad.Trans (liftIO)

main :: IO ()
main = do
  args <- getArgs
  runWith args

defaultConnection = R.Connection {
                      R.user = "admin",
                      R.password = "admin",
                      R.hostname = "192.168.1.1"
                    }

withConn = R.withConnection defaultConnection

brace sb cmd sa = sequence_ [putStrLn sb, cmd, putStrLn sa]

runWith :: [String] -> IO ()
runWith args 
    | head args == "status" = 
          either putStrLn print =<< withConn R.readStatus

    | head args == "log" =
        either putStrLn (putStr . unlines . reverse . take 10 . reverse) 
            =<< withConn R.readLog 

    | head args == "disconnect" =
        brace "Disconnecting from WAN..." 
              (withConn R.disconnectWan)
              "Disconnected"
    | head args == "connect" =
        brace "Connecting to WAN..."
              (withConn R.connectWan)
              "Signal sent. Check the status"
    | head args == "reconnect" =
        brace "Reconnecting WAN..."
              (withConn $ sequence_ [R.disconnectWan, 
                                     liftIO $ threadDelay 5000, 
                                     R.connectWan])
              "Done. Check the status"
    | head args == "clear" =
        brace "Clearing log..."
              (withConn R.clearLog)
              "Done"
    | otherwise = undefined