Skip to content

Instantly share code, notes, and snippets.

@ga2arch
Created August 3, 2014 14:58
Show Gist options
  • Save ga2arch/56404fdd8366f0da8683 to your computer and use it in GitHub Desktop.
Save ga2arch/56404fdd8366f0da8683 to your computer and use it in GitHub Desktop.
Simple alarm, "sbatty in 10s", set an alarm in 10s
{-# LANGUAGE DeriveGeneric, OverloadedStrings #-}
module Main where
import Control.Applicative
import Control.Concurrent
import Data.Default ( def )
import Data.Serialize
import System.Environment ( getArgs )
import System.Daemon
import System.Process
import GHC.Generics
data Command = Add Int | Error
deriving (Generic, Show)
data Response = Ok | Failed
deriving (Generic, Show)
instance Serialize Command
instance Serialize Response
beep = sequence_ [b, b, b]
where
b = system "/usr/bin/afplay /Users/ga2arch/Music/beep.wav"
handleCommand :: Command -> IO Response
handleCommand (Add t) = do
forkIO $ do
threadDelay t
beep
return Ok
handleCommand Error = return Failed
microseconds time =
case (last time) of
's' -> 1 * 10^6
'm' -> 6 * 10^7
'h' -> 36 * 10^8
parseCmd :: [String] -> Command
parseCmd ("in":time:_) = Add $ (read $ init time) * (microseconds time)
parseCmd _ = Error
main :: IO ()
main = do
ensureDaemonRunning "sbatty" def { daemonPort = 6666 } handleCommand
cmd <- parseCmd <$> getArgs :: IO Command
res <- runClient "localhost" 6666 cmd
print (res :: Maybe Response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment