dmessage :: Handle -> CommandHistory -> Int -> Float -> Float -> IO( Maybe(Command))
sendmessage h commandHistory lastMessageTime oldDirection newDirection = do
  let directionChanged = abs(oldDirection - newDirection) > 0.1
      commandsInLastSec = length $ filter (duringLastSec lastMessageTime) commandHistory
      doSend = directionChanged && (commandsInLastSec < 10)
  case doSend of 
    True -> do
      send h "changeDir" newDirection                        
      return (Just(Command lastMessageTime newDirection))                   
    False -> do 
      return (Nothing)
  where 
    duringLastSec :: Int -> Command -> Bool
    duringLastSec newest current = abs(newest - (timestamp current)) < 1100  

sendmessage :: Handle -> CommandHistory -> Int -> Float -> Float -> IO( Maybe(Command))
sendmessage h commandHistory lastMessageTime oldDirection newDirection = do
  let directionChanged = abs(oldDirection - newDirection) > 0.1
      commandsInLastSec = length $ filter (duringLastSec lastMessageTime) commandHistory
      doSend = directionChanged && (commandsInLastSec < 10)
  case doSend of 
    True -> do
      send h "changeDir" newDirection                        
      return (Just(Command lastMessageTime newDirection))                   
    False -> do 
      return (Nothing)
  where 
    duringLastSec :: Int -> Command -> Bool
    duringLastSec newest current = abs(newest - (timestamp current)) < 1100  
sendmessage h commandHistory lastMessageTime oldDirection newDirection = do
  let directionChanged = abs(oldDirection - newDirection) > 0.1
      commandsInLastSec = length $ filter (duringLastSec lastMessageTime) commandHistory
      doSend = directionChanged && (commandsInLastSec < 10)
  case doSend of 
    True -> do
      send h "changeDir" newDirection                        
      return (Just(Command lastMessageTime newDirection))                   
    False -> do 
      return (Nothing)
  where 
    duringLastSec :: Int -> Command -> Bool
    duringLastSec newest current = abs(newest - (timestamp current)) < 1100  