You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
361 lines
11 KiB
361 lines
11 KiB
package main |
|
|
|
import ( |
|
"context" |
|
"fmt" |
|
"strconv" |
|
"strings" |
|
|
|
"github.com/google/uuid" |
|
"samhofi.us/x/keybase/v2/types/chat1" |
|
) |
|
|
|
func reset(m chat1.MsgSummary) { |
|
_, err := k.KVDelete(&m.Channel.Name, "teslabot", "authtok") |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error resetting your authentication. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
_, err = k.KVDelete(&m.Channel.Name, "teslabot", "startPass") |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error resetting your authentication. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
} |
|
|
|
func authenticate(m chat1.MsgSummary) { |
|
defer log.PanicSafe() |
|
if isAuthenticated(m) { |
|
k.SendMessageByConvID(m.ConvID, "You have already authenticated (please use !reset to reset.") |
|
return |
|
} |
|
if !m.IsEphemeral { |
|
k.SendMessageByConvID(m.ConvID, "Please remember to delete your message after we have authenticated!") |
|
} |
|
parts := strings.Split(m.Content.Text.Body, " ") |
|
if len(parts) != 3 { |
|
k.SendMessageByConvID(m.ConvID, "Invalid input for command authenticate. Requires username and password. This information is not stored in keybase, or logged.") |
|
return |
|
} |
|
username := parts[1] |
|
password := parts[2] |
|
t, err := login(context.Background(), username, password) |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error logging in. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
log.LogDebug("Token created for %+v", m.Sender.Username) |
|
_, err = k.KVPut(&m.Channel.Name, "teslabot", "authtok", t) |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error storing your auth token. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
k.ReactByConvID(m.ConvID, m.Id, ":car:") |
|
k.DeleteByConvID(m.ConvID, m.Id) |
|
k.SendMessageByConvID(m.ConvID, "You're all set!") |
|
} |
|
|
|
func listVehicles(m chat1.MsgSummary) { |
|
c := getTeslaClient(m) |
|
if c == nil { |
|
return |
|
} |
|
v, err := c.Vehicles() |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error listing vehicles. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
ret := "Detected vehicles for account: ```" |
|
for _, v := range v { |
|
ret += fmt.Sprintf("VIN: %s\n", v.Vin) |
|
ret += fmt.Sprintf("Name: %s\n\n", v.DisplayName) |
|
} |
|
ret += "```" |
|
k.SendMessageByConvID(m.ConvID, ret) |
|
} |
|
|
|
func honk(m chat1.MsgSummary) { |
|
v := getVehicle(m) |
|
if v == nil { |
|
return |
|
} |
|
err := v.HonkHorn() |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error honking your horn. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
k.SendMessageByConvID(m.ConvID, "I've honked your horn!") |
|
} |
|
|
|
func chargeStatus(m chat1.MsgSummary) { |
|
v := getVehicle(m) |
|
if v == nil { |
|
return |
|
} |
|
state, err := v.ChargeState() |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error getting charge state. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
ret := fmt.Sprintf("Status for %+v: ```", v.DisplayName) |
|
ret += fmt.Sprintf("\nCurrent Charge: %+v (%+vmi)", state.BatteryLevel, state.BatteryRange) |
|
ret += fmt.Sprintf("\nCharging State: %+v", state.ChargingState) |
|
ret += fmt.Sprintf("\nTime to full: %+vmin", state.MinutesToFullCharge) |
|
ret += fmt.Sprintf("\nConnected Cable: %+v", state.ConnChargeCable) |
|
if state.FastChargerPresent { |
|
ret += fmt.Sprintf("\nFast Charger: %+v %+v", state.FastChargerBrand, state.FastChargerType) |
|
} |
|
ret += "```\n" |
|
if state.BatteryHeaterOn { |
|
ret += "The battery heater is on. " |
|
} |
|
if state.ChargePortDoorOpen { |
|
ret += "The charge port is open. " |
|
} |
|
k.SendMessageByConvID(m.ConvID, ret) |
|
} |
|
|
|
func flashLights(m chat1.MsgSummary) { |
|
v := getVehicle(m) |
|
if v == nil { |
|
return |
|
} |
|
err := v.FlashLights() |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error flashing your lights. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
|
|
k.SendMessageByConvID(m.ConvID, "I've flashed your lights!") |
|
} |
|
|
|
func currentTemp(m chat1.MsgSummary) { |
|
v := getVehicle(m) |
|
if v == nil { |
|
return |
|
} |
|
guiSettings, err := v.GuiSettings() |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error getting your preferences. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
climateState, err := v.ClimateState() |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error getting your Climate State. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
tempSetting := climateState.DriverTempSetting |
|
insideTemp := climateState.InsideTemp |
|
if guiSettings.GuiTemperatureUnits == "F" { |
|
tempSetting = (climateState.DriverTempSetting * 1.8) + 32 |
|
insideTemp = (climateState.InsideTemp * 1.8) + 32 |
|
} |
|
if climateState.IsClimateOn { |
|
k.SendMessageByConvID(m.ConvID, "Your climate on and set to %+v, current temp is: %+v inside %+v", |
|
tempSetting, insideTemp, v.DisplayName) |
|
} else { |
|
k.SendMessageByConvID(m.ConvID, "Your climate off but set to %+v, current temp is %+v inside %+v", |
|
tempSetting, insideTemp, v.DisplayName) |
|
} |
|
} |
|
|
|
func setClimate(m chat1.MsgSummary) { |
|
v := getVehicle(m) |
|
if v == nil { |
|
return |
|
} |
|
guiSettings, err := v.GuiSettings() |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error getting your preferences. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
parts := strings.Split(m.Content.Text.Body, " ") |
|
for _, val := range parts { |
|
if val == "on" { |
|
err := v.StartAirConditioning() |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error starting your Climate. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
} |
|
if val == "off" { |
|
err := v.StopAirConditioning() |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error turning off your Climate. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
} |
|
if temp, err := strconv.Atoi(val); err == nil { |
|
if guiSettings.GuiTemperatureUnits == "F" { |
|
temp = (temp - 32) * 5 / 9 |
|
} |
|
err = v.SetTemperature(float64(temp), float64(temp)) |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error setting your Climate. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
err = v.StartAirConditioning() |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error starting your Climate. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
} |
|
} |
|
climateState, err := v.ClimateState() |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error getting your Climate State. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
tempSetting := climateState.DriverTempSetting |
|
insideTemp := climateState.InsideTemp |
|
if guiSettings.GuiTemperatureUnits == "F" { |
|
tempSetting = (climateState.DriverTempSetting * 1.8) + 32 |
|
insideTemp = (climateState.InsideTemp * 1.8) + 32 |
|
} |
|
if climateState.IsClimateOn { |
|
k.SendMessageByConvID(m.ConvID, "Your climate on and set to %+v, current temp is: %+v", tempSetting, insideTemp) |
|
} else { |
|
k.SendMessageByConvID(m.ConvID, "Your climate off but set to %+v", tempSetting) |
|
} |
|
|
|
} |
|
|
|
func lockVehicle(m chat1.MsgSummary) { |
|
v := getVehicle(m) |
|
if v == nil { |
|
return |
|
} |
|
err := v.LockDoors() |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error locking your doors. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
} |
|
|
|
func unlockVehicle(m chat1.MsgSummary) { |
|
v := getVehicle(m) |
|
if v == nil { |
|
return |
|
} |
|
err := v.UnlockDoors() |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error unlocking your doors. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
} |
|
|
|
func enableStart(m chat1.MsgSummary) { |
|
parts := strings.Split(m.Content.Text.Body, " ") |
|
if len(parts) != 3 { |
|
k.SendMessageByConvID(m.ConvID, "You must 'accept' this command and supply your password. This command is not as safe, as it stores your password in KVStore.") |
|
return |
|
} |
|
if !strings.Contains(parts[1], "accept") { |
|
k.SendMessageByConvID(m.ConvID, "You must 'accept' this command and supply your password. This command is not as safe, as it stores your password in KVStore.") |
|
return |
|
} |
|
_, err := k.KVPut(&m.Channel.Name, "teslabot", "startPass", parts[2]) |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error storing your password. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
|
|
} |
|
|
|
func disableStart(m chat1.MsgSummary) { |
|
_, err := k.KVDelete(&m.Channel.Name, "teslabot", "startPass") |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error deleting your password. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
} |
|
|
|
func startVehicle(m chat1.MsgSummary) { |
|
v := getVehicle(m) |
|
if v == nil { |
|
return |
|
} |
|
test, _ := k.KVGet(&m.Channel.Name, "teslabot", "startPass") |
|
if test.EntryValue == "" { |
|
k.SendMessageByConvID(m.ConvID, "You must first !enablestart to use this command.") |
|
return |
|
} |
|
err := v.Start(test.EntryValue) |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error starting your vehicle. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
k.SendMessageByConvID(m.ConvID, "Your vehicle has been started!") |
|
|
|
} |
|
|
|
func openTrunk(m chat1.MsgSummary) { |
|
v := getVehicle(m) |
|
|
|
parts := strings.Split(m.Content.Text.Body, " ") |
|
|
|
if len(parts) != 2 { |
|
k.SendMessageByConvID(m.ConvID, "You must supply front or rear.") |
|
return |
|
} |
|
|
|
trunk := parts[1] |
|
|
|
switch trunk { |
|
case "rear": |
|
case "front": |
|
err := v.OpenTrunk(trunk) |
|
if err != nil { |
|
tracker := uuid.NewString() |
|
k.SendMessageByConvID(m.ConvID, "There was an error opening your trunk. Contact @rudi9719 for more information with code %+v", tracker) |
|
log.LogError("%+v: %+v", tracker, err) |
|
return |
|
} |
|
default: |
|
k.SendMessageByConvID(m.ConvID, "You must supply front or rear.") |
|
return |
|
} |
|
|
|
}
|
|
|