{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell   #-}

module Tesla.Car.Command.Charging (
  startCharging, stopCharging, setLimit, openChargePort, closeChargePort,
  setAmps,
  scheduledChargingOff, scheduleCharging
  ) where

import           Control.Monad.IO.Class (MonadIO (..))

import           Tesla.Car.Command

-- | Set the desired charge level (percent).
setLimit :: MonadIO m => Percent -> Car m CommandResponse
setLimit :: forall (m :: * -> *). MonadIO m => Percent -> Car m CommandResponse
setLimit Percent
to = forall (m :: * -> *).
MonadIO m =>
String -> [Pair] -> Car m CommandResponse
runCmd String
"set_charge_limit" [Key
"percent" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Percent
to]

-- | Set the charge current.
setAmps :: MonadIO m => Int -> Car m CommandResponse
setAmps :: forall (m :: * -> *). MonadIO m => Int -> Car m CommandResponse
setAmps Int
to = forall (m :: * -> *).
MonadIO m =>
String -> [Pair] -> Car m CommandResponse
runCmd String
"set_charging_amps" [Key
"charging_amps" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Int
to]

-- | Disable scheduled charging.
scheduledChargingOff :: MonadIO m => Car m CommandResponse
scheduledChargingOff :: forall (m :: * -> *). MonadIO m => Car m CommandResponse
scheduledChargingOff = forall (m :: * -> *).
MonadIO m =>
String -> [Pair] -> Car m CommandResponse
runCmd String
"set_scheduled_charging" [ Key
"enable" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Bool
False ]

-- | Schedule charging for the given number of minutes after midnight (local time).
scheduleCharging :: MonadIO m => Time -> Car m CommandResponse
scheduleCharging :: forall (m :: * -> *). MonadIO m => Time -> Car m CommandResponse
scheduleCharging Time
mins = forall (m :: * -> *).
MonadIO m =>
String -> [Pair] -> Car m CommandResponse
runCmd String
"set_scheduled_charging" [ Key
"enable" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Bool
True, Key
"time" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Time
mins ]

mkNamedCommands [("startCharging", "charge_start"),
                 ("stopCharging", "charge_stop"),
                 ("openChargePort", "charge_port_door_open"),
                 ("closeChargePort", "charge_port_door_close")]