Cleanup values into a separate function to be called for refreshing
This commit is contained in:
216
main.go
216
main.go
@ -26,7 +26,7 @@ var (
|
||||
climateOn *widgets.QCheckBox
|
||||
lockedDoors *widgets.QCheckBox
|
||||
sentryMode *widgets.QCheckBox
|
||||
startStopCharge *widgets.QRadioButton
|
||||
startStopCharge *widgets.QCheckBox
|
||||
|
||||
honk *widgets.QPushButton
|
||||
flashLights *widgets.QPushButton
|
||||
@ -34,6 +34,10 @@ var (
|
||||
frunk *widgets.QPushButton
|
||||
|
||||
vehicle *tesla.Vehicle
|
||||
vehicleState *tesla.VehicleState
|
||||
chargeStats *tesla.ChargeState
|
||||
climateState *tesla.ClimateState
|
||||
guiSettings *tesla.GuiSettings
|
||||
|
||||
window *widgets.QMainWindow
|
||||
mainApp *widgets.QApplication
|
||||
@ -43,97 +47,39 @@ var (
|
||||
func main() {
|
||||
mainApp = widgets.NewQApplication(len(os.Args), os.Args)
|
||||
|
||||
vehicle = getVehicle("")
|
||||
if vehicle == nil {
|
||||
showDialogue("Unable to get vehicle")
|
||||
return
|
||||
}
|
||||
vehicleState, err := vehicle.VehicleState()
|
||||
if err != nil {
|
||||
showDialogue("Unable to get Vehicle State")
|
||||
}
|
||||
chargeStats, err := vehicle.ChargeState()
|
||||
if err != nil {
|
||||
|
||||
showDialogue("Unable to get Vehicle Charge State")
|
||||
}
|
||||
climateState, err := vehicle.ClimateState()
|
||||
if err != nil {
|
||||
showDialogue("Unable to get Vehicle Climate")
|
||||
}
|
||||
guiSettings, err := vehicle.GuiSettings()
|
||||
if err != nil {
|
||||
|
||||
showDialogue("Unable to get Gui Settings")
|
||||
}
|
||||
tempSettingVal := climateState.DriverTempSetting
|
||||
insideTempVal := climateState.InsideTemp
|
||||
outsideTempVal := climateState.OutsideTemp
|
||||
if guiSettings.GuiTemperatureUnits == "F" {
|
||||
tempSettingVal = (climateState.DriverTempSetting * 1.8) + 32
|
||||
insideTempVal = (climateState.InsideTemp * 1.8) + 32
|
||||
outsideTempVal = (climateState.OutsideTemp * 1.8) + 32
|
||||
}
|
||||
|
||||
vboxLayout := widgets.NewQVBoxLayout()
|
||||
statusLayout := widgets.NewQFormLayout(nil)
|
||||
|
||||
batteryLevel = widgets.NewQLineEdit(nil)
|
||||
batteryLevel.SetText(fmt.Sprintf("%+v", chargeStats.BatteryLevel))
|
||||
batteryLevel.SetReadOnly(true)
|
||||
batteryLevel.SetFixedWidth(30)
|
||||
batteryRange = widgets.NewQLineEdit(nil)
|
||||
batteryRange.SetText(fmt.Sprintf("%.2f%+v", chargeStats.BatteryRange,
|
||||
strings.Replace(guiSettings.GuiDistanceUnits, "/hr", "", -1)))
|
||||
batteryRange.SetFixedWidth(10 * len(batteryRange.Text()))
|
||||
batteryRange.SetReadOnly(true)
|
||||
chargingState = widgets.NewQLineEdit(nil)
|
||||
chargingState.SetText(chargeStats.ChargingState)
|
||||
chargingState.SetReadOnly(true)
|
||||
minutesToFull = widgets.NewQLineEdit(nil)
|
||||
minutesToFull.SetText(fmt.Sprintf("%+v", chargeStats.MinutesToFullCharge))
|
||||
minutesToFull.SetReadOnly(true)
|
||||
fastChargerInd = widgets.NewQLineEdit(nil)
|
||||
fastChargerInd.SetText(chargeStats.FastChargerBrand)
|
||||
fastChargerInd.SetReadOnly(true)
|
||||
batteryHeaterInd = widgets.NewQLineEdit(nil)
|
||||
if chargeStats.BatteryHeaterOn {
|
||||
batteryHeaterInd.SetText("On")
|
||||
}
|
||||
|
||||
batteryHeaterInd.SetReadOnly(true)
|
||||
chargeDoorOpenInd = widgets.NewQLineEdit(nil)
|
||||
if chargeStats.ChargePortDoorOpen {
|
||||
chargeDoorOpenInd.SetText("Open")
|
||||
} else {
|
||||
chargeDoorOpenInd.SetText("Closed")
|
||||
}
|
||||
chargeDoorOpenInd.SetReadOnly(true)
|
||||
insideTemp = widgets.NewQLineEdit(nil)
|
||||
insideTemp.SetText(fmt.Sprintf("%.0f", insideTempVal))
|
||||
insideTemp.SetReadOnly(true)
|
||||
insideTemp.SetFixedWidth(25)
|
||||
outsideTemp = widgets.NewQLineEdit(nil)
|
||||
outsideTemp.SetText(fmt.Sprintf("%.0f", outsideTempVal))
|
||||
outsideTemp.SetReadOnly(true)
|
||||
outsideTemp.SetFixedWidth(25)
|
||||
tempSetting = widgets.NewQLineEdit(nil)
|
||||
tempSetting.SetText(fmt.Sprintf("%.0f", tempSettingVal))
|
||||
tempSetting.SetReadOnly(true)
|
||||
tempSetting.SetFixedWidth(25)
|
||||
|
||||
climateOn = widgets.NewQCheckBox(nil)
|
||||
climateOn.SetChecked(climateState.IsClimateOn)
|
||||
lockedDoors = widgets.NewQCheckBox(nil)
|
||||
sentryMode = widgets.NewQCheckBox(nil)
|
||||
if vehicleState != nil {
|
||||
lockedDoors.SetChecked(vehicleState.Locked)
|
||||
sentryMode.SetChecked(vehicleState.SentryMode)
|
||||
} else {
|
||||
lockedDoors.SetCheckable(false)
|
||||
sentryMode.SetCheckable(false)
|
||||
}
|
||||
startStopCharge = widgets.NewQRadioButton(nil)
|
||||
startStopCharge.SetChecked(chargeStats.ChargingState == "Charging")
|
||||
if chargeStats.ChargingState == "Disconnected" {
|
||||
startStopCharge.SetCheckable(false)
|
||||
}
|
||||
|
||||
startStopCharge = widgets.NewQCheckBox(nil)
|
||||
|
||||
honk = widgets.NewQPushButton(nil)
|
||||
flashLights = widgets.NewQPushButton(nil)
|
||||
@ -151,6 +97,7 @@ func main() {
|
||||
chargeHbox.AddWidget(batteryRange, 0, 0)
|
||||
statusLayout.AddRow2(currentChargeLabel, chargeHbox)
|
||||
|
||||
setValues()
|
||||
statusLayout.AddRow3("Charging State: ", chargingState)
|
||||
if chargeStats.ChargingState != "Disconnected" {
|
||||
statusLayout.AddRow3("Minutes to Full: ", minutesToFull)
|
||||
@ -163,6 +110,30 @@ func main() {
|
||||
}
|
||||
statusLayout.AddRow3("Charge Port: ", chargeDoorOpenInd)
|
||||
|
||||
insideTempLabel := widgets.NewQLabel(nil, 0)
|
||||
outsideTempLabel := widgets.NewQLabel(nil, 0)
|
||||
|
||||
insideTempLabel.SetText("Inside Temp: ")
|
||||
outsideTempLabel.SetText("Outside Temp: ")
|
||||
tempHbox := widgets.NewQHBoxLayout()
|
||||
tempHbox.AddWidget(insideTemp, 0, 0)
|
||||
tempHbox.AddItem(widgets.NewQSpacerItem(10, 10, widgets.QSizePolicy__Fixed, widgets.QSizePolicy__Fixed))
|
||||
tempHbox.AddWidget(outsideTempLabel, 0, 0)
|
||||
tempHbox.AddWidget(outsideTemp, 0, 0)
|
||||
statusLayout.AddRow2(insideTempLabel, tempHbox)
|
||||
|
||||
climateEnabledLabel := widgets.NewQLabel(nil, 0)
|
||||
climateEnabledLabel.SetText("Climate On: ")
|
||||
climateSettingLabel := widgets.NewQLabel(nil, 0)
|
||||
climateSettingLabel.SetText("Climate Setting: ")
|
||||
|
||||
climateHbox := widgets.NewQHBoxLayout()
|
||||
climateHbox.AddWidget(climateOn, 0, 0)
|
||||
climateHbox.AddItem(widgets.NewQSpacerItem(10, 10, widgets.QSizePolicy__Fixed, widgets.QSizePolicy__Fixed))
|
||||
climateHbox.AddWidget(climateSettingLabel, 0, 0)
|
||||
climateHbox.AddWidget(tempSetting, 0, 0)
|
||||
statusLayout.AddRow2(climateEnabledLabel, climateHbox)
|
||||
|
||||
doorLockLabel := widgets.NewQLabel(nil, 0)
|
||||
sentryModeLabel := widgets.NewQLabel(nil, 0)
|
||||
chargingStateLabel := widgets.NewQLabel(nil, 0)
|
||||
@ -180,6 +151,11 @@ func main() {
|
||||
securityHbox.AddWidget(chargingStateLabel, 0, 0)
|
||||
securityHbox.AddWidget(startStopCharge, 0, 0)
|
||||
}
|
||||
|
||||
lockedDoors.ConnectStateChanged(lockDoors)
|
||||
sentryMode.ConnectStateChanged(sentryModeEnable)
|
||||
startStopCharge.ConnectStateChanged(enableCharging)
|
||||
climateOn.ConnectStateChanged(enableClimate)
|
||||
statusLayout.AddRow2(doorLockLabel, securityHbox)
|
||||
|
||||
honk = widgets.NewQPushButton(nil)
|
||||
@ -215,7 +191,6 @@ func main() {
|
||||
|
||||
window = widgets.NewQMainWindow(nil, 0)
|
||||
window.SetWindowTitle(fmt.Sprintf("%+v: %+v", vehicle.DisplayName, vehicle.Vin))
|
||||
|
||||
centralWidget := widgets.NewQWidget(window, 0)
|
||||
centralWidget.SetLayout(vboxLayout)
|
||||
window.SetCentralWidget(centralWidget)
|
||||
@ -225,6 +200,113 @@ func main() {
|
||||
widgets.QApplication_Exec()
|
||||
}
|
||||
|
||||
func setValues() {
|
||||
vehicle = getVehicle("")
|
||||
var err error
|
||||
if vehicle == nil {
|
||||
showDialogue("Unable to get vehicle")
|
||||
return
|
||||
}
|
||||
vehicleState, err = vehicle.VehicleState()
|
||||
if err != nil {
|
||||
showDialogue("Unable to get Vehicle State")
|
||||
}
|
||||
chargeStats, err = vehicle.ChargeState()
|
||||
if err != nil {
|
||||
|
||||
showDialogue("Unable to get Vehicle Charge State")
|
||||
}
|
||||
climateState, err = vehicle.ClimateState()
|
||||
if err != nil {
|
||||
showDialogue("Unable to get Vehicle Climate")
|
||||
}
|
||||
guiSettings, err = vehicle.GuiSettings()
|
||||
if err != nil {
|
||||
|
||||
showDialogue("Unable to get Gui Settings")
|
||||
}
|
||||
tempSettingVal := climateState.DriverTempSetting
|
||||
insideTempVal := climateState.InsideTemp
|
||||
outsideTempVal := climateState.OutsideTemp
|
||||
if guiSettings.GuiTemperatureUnits == "F" {
|
||||
tempSettingVal = (climateState.DriverTempSetting * 1.8) + 32
|
||||
insideTempVal = (climateState.InsideTemp * 1.8) + 32
|
||||
outsideTempVal = (climateState.OutsideTemp * 1.8) + 32
|
||||
}
|
||||
batteryLevel.SetText(fmt.Sprintf("%+v", chargeStats.BatteryLevel))
|
||||
batteryRange.SetText(fmt.Sprintf("%.2f%+v", chargeStats.BatteryRange,
|
||||
strings.Replace(guiSettings.GuiDistanceUnits, "/hr", "", -1)))
|
||||
batteryRange.SetFixedWidth(10 * len(batteryRange.Text()))
|
||||
chargingState.SetText(chargeStats.ChargingState)
|
||||
|
||||
minutesToFull.SetText(fmt.Sprintf("%+v", chargeStats.MinutesToFullCharge))
|
||||
|
||||
fastChargerInd.SetText(chargeStats.FastChargerBrand)
|
||||
if chargeStats.BatteryHeaterOn {
|
||||
batteryHeaterInd.SetText("On")
|
||||
}
|
||||
|
||||
if chargeStats.ChargePortDoorOpen {
|
||||
chargeDoorOpenInd.SetText("Open")
|
||||
} else {
|
||||
chargeDoorOpenInd.SetText("Closed")
|
||||
}
|
||||
|
||||
insideTemp.SetText(fmt.Sprintf("%.0f", insideTempVal))
|
||||
outsideTemp.SetText(fmt.Sprintf("%.0f", outsideTempVal))
|
||||
tempSetting.SetText(fmt.Sprintf("%.0f", tempSettingVal))
|
||||
|
||||
climateOn.SetChecked(climateState.IsClimateOn)
|
||||
if vehicleState != nil {
|
||||
lockedDoors.SetChecked(vehicleState.Locked)
|
||||
sentryMode.SetChecked(vehicleState.SentryMode)
|
||||
} else {
|
||||
lockedDoors.SetCheckable(false)
|
||||
sentryMode.SetCheckable(false)
|
||||
}
|
||||
startStopCharge.SetChecked(chargeStats.ChargingState == "Charging")
|
||||
if chargeStats.ChargingState == "Disconnected" {
|
||||
startStopCharge.SetCheckable(false)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func enableClimate(i int) {
|
||||
if i == 0 {
|
||||
vehicle.StartAirConditioning()
|
||||
} else {
|
||||
vehicle.StopAirConditioning()
|
||||
}
|
||||
go setValues()
|
||||
}
|
||||
|
||||
func lockDoors(i int) {
|
||||
if i == 0 {
|
||||
vehicle.UnlockDoors()
|
||||
} else {
|
||||
vehicle.LockDoors()
|
||||
}
|
||||
go setValues()
|
||||
}
|
||||
|
||||
func sentryModeEnable(i int) {
|
||||
if i == 0 {
|
||||
showDialogue("Unable to disable sentry mode.\nUnsupported.")
|
||||
} else {
|
||||
vehicle.EnableSentry()
|
||||
}
|
||||
go setValues()
|
||||
}
|
||||
|
||||
func enableCharging(i int) {
|
||||
if i == 0 {
|
||||
vehicle.StartCharging()
|
||||
} else {
|
||||
vehicle.StopCharging()
|
||||
}
|
||||
go setValues()
|
||||
}
|
||||
|
||||
func honkHorn(c bool) {
|
||||
err := vehicle.HonkHorn()
|
||||
if err != nil {
|
||||
@ -271,6 +353,8 @@ func showDialogue(msg string, a ...interface{}) {
|
||||
window.Show()
|
||||
popup = false
|
||||
dialogue.Close()
|
||||
|
||||
go setValues()
|
||||
})
|
||||
quitBtn.ConnectClicked(func(checked bool) {
|
||||
mainApp.Quit()
|
||||
|
||||
Reference in New Issue
Block a user