{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Tesla.Car.Command.Climate (
hvacOn, hvacOff, heatSeat, Seat(..),
setTemps, wheelHeater, wheelHeaterOff, wheelHeaterOn,
maxDefrost,
wakeUp,
bioweaponMode,
Sometimes(..), OffPeakConfig(..), Preconditioning,
scheduledDepartureOff, scheduleDeparture
) where
import Control.Monad.IO.Class (MonadIO (..))
import Network.Wreq (FormParam (..))
import Tesla.Car.Command
wheelHeater :: MonadIO m => Bool -> Car m CommandResponse
wheelHeater :: Bool -> Car m CommandResponse
wheelHeater Bool
on = String -> [FormParam] -> Car m CommandResponse
forall (m :: * -> *) p.
(MonadIO m, Postable p) =>
String -> p -> Car m CommandResponse
runCmd String
"remote_steering_wheel_heater_request" [ByteString
"on" ByteString -> Bool -> FormParam
forall v. FormValue v => ByteString -> v -> FormParam
:= Bool
on]
wheelHeaterOn :: MonadIO m => Car m CommandResponse
wheelHeaterOn :: Car m CommandResponse
wheelHeaterOn = Bool -> Car m CommandResponse
forall (m :: * -> *). MonadIO m => Bool -> Car m CommandResponse
wheelHeater Bool
True
wheelHeaterOff :: MonadIO m => Car m CommandResponse
wheelHeaterOff :: Car m CommandResponse
wheelHeaterOff = Bool -> Car m CommandResponse
forall (m :: * -> *). MonadIO m => Bool -> Car m CommandResponse
wheelHeater Bool
False
bioweaponMode :: MonadIO m => Bool -> Car m CommandResponse
bioweaponMode :: Bool -> Car m CommandResponse
bioweaponMode Bool
on = String -> [FormParam] -> Car m CommandResponse
forall (m :: * -> *) p.
(MonadIO m, Postable p) =>
String -> p -> Car m CommandResponse
runCmd String
"set_bioweapon_mode" [ByteString
"on" ByteString -> Bool -> FormParam
forall v. FormValue v => ByteString -> v -> FormParam
:= Bool
on]
data Seat = DriverSeat | PassengerSeat | RearLeftSeat | RearCenterSeat | RearRightSeat
heatSeat :: MonadIO m => Seat -> Int -> Car m CommandResponse
heatSeat :: Seat -> Int -> Car m CommandResponse
heatSeat Seat
seat Int
level = String -> [FormParam] -> Car m CommandResponse
forall (m :: * -> *) p.
(MonadIO m, Postable p) =>
String -> p -> Car m CommandResponse
runCmd String
"remote_seat_heater_request" [ByteString
"heater" ByteString -> Int -> FormParam
forall v. FormValue v => ByteString -> v -> FormParam
:= Seat -> Int
seatNum Seat
seat, ByteString
"level" ByteString -> Int -> FormParam
forall v. FormValue v => ByteString -> v -> FormParam
:= Int
level]
where
seatNum :: Seat -> Int
seatNum :: Seat -> Int
seatNum Seat
DriverSeat = Int
0
seatNum Seat
PassengerSeat = Int
1
seatNum Seat
RearLeftSeat = Int
2
seatNum Seat
RearCenterSeat = Int
4
seatNum Seat
RearRightSeat = Int
5
setTemps :: MonadIO m => (Double, Double) -> Car m CommandResponse
setTemps :: (Double, Double) -> Car m CommandResponse
setTemps (Double
driver, Double
passenger) = String -> [FormParam] -> Car m CommandResponse
forall (m :: * -> *) p.
(MonadIO m, Postable p) =>
String -> p -> Car m CommandResponse
runCmd String
"set_temps" [ByteString
"driver_temp" ByteString -> Double -> FormParam
forall v. FormValue v => ByteString -> v -> FormParam
:= Double
driver, ByteString
"passenger_temp" ByteString -> Double -> FormParam
forall v. FormValue v => ByteString -> v -> FormParam
:= Double
passenger]
maxDefrost :: MonadIO m => Bool -> Car m CommandResponse
maxDefrost :: Bool -> Car m CommandResponse
maxDefrost Bool
on = String -> [FormParam] -> Car m CommandResponse
forall (m :: * -> *) p.
(MonadIO m, Postable p) =>
String -> p -> Car m CommandResponse
runCmd String
"set_preconditioning_max" [ByteString
"on" ByteString -> Bool -> FormParam
forall v. FormValue v => ByteString -> v -> FormParam
:= Bool
on]
scheduledDepartureOff :: MonadIO m => Car m CommandResponse
scheduledDepartureOff :: Car m CommandResponse
scheduledDepartureOff = String -> [FormParam] -> Car m CommandResponse
forall (m :: * -> *) p.
(MonadIO m, Postable p) =>
String -> p -> Car m CommandResponse
runCmd String
"set_scheduled_departure" [ ByteString
"enable" ByteString -> Bool -> FormParam
forall v. FormValue v => ByteString -> v -> FormParam
:= Bool
False ]
data Sometimes = Never | Always | WeekdaysOnly
type Preconditioning = Sometimes
data OffPeakConfig = OffPeakConfig {
OffPeakConfig -> Sometimes
_offPeakEnabled :: Sometimes,
OffPeakConfig -> Time
_offPeakEndTime :: Time
}
scheduleDeparture :: MonadIO m => Time -> Preconditioning -> Maybe OffPeakConfig -> Car m CommandResponse
scheduleDeparture :: Time -> Sometimes -> Maybe OffPeakConfig -> Car m CommandResponse
scheduleDeparture Time
t Sometimes
p Maybe OffPeakConfig
o = String -> [FormParam] -> Car m CommandResponse
forall (m :: * -> *) p.
(MonadIO m, Postable p) =>
String -> p -> Car m CommandResponse
runCmd String
"set_scheduled_departure" ([ByteString
"enable" ByteString -> Bool -> FormParam
forall v. FormValue v => ByteString -> v -> FormParam
:= Bool
True, ByteString
"departure_time" ByteString -> Time -> FormParam
forall v. FormValue v => ByteString -> v -> FormParam
:= Time
t] [FormParam] -> [FormParam] -> [FormParam]
forall a. Semigroup a => a -> a -> a
<> [FormParam]
pp [FormParam] -> [FormParam] -> [FormParam]
forall a. Semigroup a => a -> a -> a
<> Maybe OffPeakConfig -> [FormParam]
op Maybe OffPeakConfig
o)
where
pp :: [FormParam]
pp = ByteString -> ByteString -> Sometimes -> [FormParam]
s ByteString
"preconditioning_enabled" ByteString
"preconditioning_weekdays_only" Sometimes
p
op :: Maybe OffPeakConfig -> [FormParam]
op Maybe OffPeakConfig
Nothing = OffPeakConfig -> [FormParam]
opp (Sometimes -> Time -> OffPeakConfig
OffPeakConfig Sometimes
Never (Finite 1440 -> Time
Time Finite 1440
0))
op (Just OffPeakConfig
x) = OffPeakConfig -> [FormParam]
opp OffPeakConfig
x
opp :: OffPeakConfig -> [FormParam]
opp OffPeakConfig{Time
Sometimes
_offPeakEndTime :: Time
_offPeakEnabled :: Sometimes
_offPeakEndTime :: OffPeakConfig -> Time
_offPeakEnabled :: OffPeakConfig -> Sometimes
..} = (ByteString
"end_off_peak_time" ByteString -> Time -> FormParam
forall v. FormValue v => ByteString -> v -> FormParam
:= Time
_offPeakEndTime) FormParam -> [FormParam] -> [FormParam]
forall a. a -> [a] -> [a]
: ByteString -> ByteString -> Sometimes -> [FormParam]
s ByteString
"off_peak_charging_enabled" ByteString
"off_peak_charging_weekdays_only" Sometimes
_offPeakEnabled
s :: ByteString -> ByteString -> Sometimes -> [FormParam]
s ByteString
e ByteString
w = \case
Sometimes
Never -> [ByteString
e ByteString -> Bool -> FormParam
forall v. FormValue v => ByteString -> v -> FormParam
:= Bool
False, ByteString
w ByteString -> Bool -> FormParam
forall v. FormValue v => ByteString -> v -> FormParam
:= Bool
False]
Sometimes
Always -> [ByteString
e ByteString -> Bool -> FormParam
forall v. FormValue v => ByteString -> v -> FormParam
:= Bool
True, ByteString
w ByteString -> Bool -> FormParam
forall v. FormValue v => ByteString -> v -> FormParam
:= Bool
False]
Sometimes
WeekdaysOnly -> [ByteString
e ByteString -> Bool -> FormParam
forall v. FormValue v => ByteString -> v -> FormParam
:= Bool
True, ByteString
w ByteString -> Bool -> FormParam
forall v. FormValue v => ByteString -> v -> FormParam
:= Bool
True]
mkNamedCommands [("hvacOn", "auto_conditioning_start"),
("hvacOff", "auto_conditioning_stop"),
("wakeUp", "wake_up")]