Cleanup values into a separate function to be called for refreshing
This commit is contained in:
218
main.go
218
main.go
@ -26,14 +26,18 @@ var (
|
|||||||
climateOn *widgets.QCheckBox
|
climateOn *widgets.QCheckBox
|
||||||
lockedDoors *widgets.QCheckBox
|
lockedDoors *widgets.QCheckBox
|
||||||
sentryMode *widgets.QCheckBox
|
sentryMode *widgets.QCheckBox
|
||||||
startStopCharge *widgets.QRadioButton
|
startStopCharge *widgets.QCheckBox
|
||||||
|
|
||||||
honk *widgets.QPushButton
|
honk *widgets.QPushButton
|
||||||
flashLights *widgets.QPushButton
|
flashLights *widgets.QPushButton
|
||||||
trunk *widgets.QPushButton
|
trunk *widgets.QPushButton
|
||||||
frunk *widgets.QPushButton
|
frunk *widgets.QPushButton
|
||||||
|
|
||||||
vehicle *tesla.Vehicle
|
vehicle *tesla.Vehicle
|
||||||
|
vehicleState *tesla.VehicleState
|
||||||
|
chargeStats *tesla.ChargeState
|
||||||
|
climateState *tesla.ClimateState
|
||||||
|
guiSettings *tesla.GuiSettings
|
||||||
|
|
||||||
window *widgets.QMainWindow
|
window *widgets.QMainWindow
|
||||||
mainApp *widgets.QApplication
|
mainApp *widgets.QApplication
|
||||||
@ -43,97 +47,39 @@ var (
|
|||||||
func main() {
|
func main() {
|
||||||
mainApp = widgets.NewQApplication(len(os.Args), os.Args)
|
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()
|
vboxLayout := widgets.NewQVBoxLayout()
|
||||||
statusLayout := widgets.NewQFormLayout(nil)
|
statusLayout := widgets.NewQFormLayout(nil)
|
||||||
|
|
||||||
batteryLevel = widgets.NewQLineEdit(nil)
|
batteryLevel = widgets.NewQLineEdit(nil)
|
||||||
batteryLevel.SetText(fmt.Sprintf("%+v", chargeStats.BatteryLevel))
|
|
||||||
batteryLevel.SetReadOnly(true)
|
batteryLevel.SetReadOnly(true)
|
||||||
batteryLevel.SetFixedWidth(30)
|
batteryLevel.SetFixedWidth(30)
|
||||||
batteryRange = widgets.NewQLineEdit(nil)
|
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)
|
batteryRange.SetReadOnly(true)
|
||||||
chargingState = widgets.NewQLineEdit(nil)
|
chargingState = widgets.NewQLineEdit(nil)
|
||||||
chargingState.SetText(chargeStats.ChargingState)
|
|
||||||
chargingState.SetReadOnly(true)
|
chargingState.SetReadOnly(true)
|
||||||
minutesToFull = widgets.NewQLineEdit(nil)
|
minutesToFull = widgets.NewQLineEdit(nil)
|
||||||
minutesToFull.SetText(fmt.Sprintf("%+v", chargeStats.MinutesToFullCharge))
|
|
||||||
minutesToFull.SetReadOnly(true)
|
minutesToFull.SetReadOnly(true)
|
||||||
fastChargerInd = widgets.NewQLineEdit(nil)
|
fastChargerInd = widgets.NewQLineEdit(nil)
|
||||||
fastChargerInd.SetText(chargeStats.FastChargerBrand)
|
|
||||||
fastChargerInd.SetReadOnly(true)
|
fastChargerInd.SetReadOnly(true)
|
||||||
batteryHeaterInd = widgets.NewQLineEdit(nil)
|
batteryHeaterInd = widgets.NewQLineEdit(nil)
|
||||||
if chargeStats.BatteryHeaterOn {
|
|
||||||
batteryHeaterInd.SetText("On")
|
|
||||||
}
|
|
||||||
batteryHeaterInd.SetReadOnly(true)
|
batteryHeaterInd.SetReadOnly(true)
|
||||||
chargeDoorOpenInd = widgets.NewQLineEdit(nil)
|
chargeDoorOpenInd = widgets.NewQLineEdit(nil)
|
||||||
if chargeStats.ChargePortDoorOpen {
|
|
||||||
chargeDoorOpenInd.SetText("Open")
|
|
||||||
} else {
|
|
||||||
chargeDoorOpenInd.SetText("Closed")
|
|
||||||
}
|
|
||||||
chargeDoorOpenInd.SetReadOnly(true)
|
chargeDoorOpenInd.SetReadOnly(true)
|
||||||
insideTemp = widgets.NewQLineEdit(nil)
|
insideTemp = widgets.NewQLineEdit(nil)
|
||||||
insideTemp.SetText(fmt.Sprintf("%.0f", insideTempVal))
|
|
||||||
insideTemp.SetReadOnly(true)
|
insideTemp.SetReadOnly(true)
|
||||||
|
insideTemp.SetFixedWidth(25)
|
||||||
outsideTemp = widgets.NewQLineEdit(nil)
|
outsideTemp = widgets.NewQLineEdit(nil)
|
||||||
outsideTemp.SetText(fmt.Sprintf("%.0f", outsideTempVal))
|
|
||||||
outsideTemp.SetReadOnly(true)
|
outsideTemp.SetReadOnly(true)
|
||||||
|
outsideTemp.SetFixedWidth(25)
|
||||||
tempSetting = widgets.NewQLineEdit(nil)
|
tempSetting = widgets.NewQLineEdit(nil)
|
||||||
tempSetting.SetText(fmt.Sprintf("%.0f", tempSettingVal))
|
tempSetting.SetFixedWidth(25)
|
||||||
tempSetting.SetReadOnly(true)
|
|
||||||
|
|
||||||
climateOn = widgets.NewQCheckBox(nil)
|
climateOn = widgets.NewQCheckBox(nil)
|
||||||
climateOn.SetChecked(climateState.IsClimateOn)
|
|
||||||
lockedDoors = widgets.NewQCheckBox(nil)
|
lockedDoors = widgets.NewQCheckBox(nil)
|
||||||
sentryMode = widgets.NewQCheckBox(nil)
|
sentryMode = widgets.NewQCheckBox(nil)
|
||||||
if vehicleState != nil {
|
|
||||||
lockedDoors.SetChecked(vehicleState.Locked)
|
startStopCharge = widgets.NewQCheckBox(nil)
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
honk = widgets.NewQPushButton(nil)
|
honk = widgets.NewQPushButton(nil)
|
||||||
flashLights = widgets.NewQPushButton(nil)
|
flashLights = widgets.NewQPushButton(nil)
|
||||||
@ -151,6 +97,7 @@ func main() {
|
|||||||
chargeHbox.AddWidget(batteryRange, 0, 0)
|
chargeHbox.AddWidget(batteryRange, 0, 0)
|
||||||
statusLayout.AddRow2(currentChargeLabel, chargeHbox)
|
statusLayout.AddRow2(currentChargeLabel, chargeHbox)
|
||||||
|
|
||||||
|
setValues()
|
||||||
statusLayout.AddRow3("Charging State: ", chargingState)
|
statusLayout.AddRow3("Charging State: ", chargingState)
|
||||||
if chargeStats.ChargingState != "Disconnected" {
|
if chargeStats.ChargingState != "Disconnected" {
|
||||||
statusLayout.AddRow3("Minutes to Full: ", minutesToFull)
|
statusLayout.AddRow3("Minutes to Full: ", minutesToFull)
|
||||||
@ -163,6 +110,30 @@ func main() {
|
|||||||
}
|
}
|
||||||
statusLayout.AddRow3("Charge Port: ", chargeDoorOpenInd)
|
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)
|
doorLockLabel := widgets.NewQLabel(nil, 0)
|
||||||
sentryModeLabel := widgets.NewQLabel(nil, 0)
|
sentryModeLabel := widgets.NewQLabel(nil, 0)
|
||||||
chargingStateLabel := widgets.NewQLabel(nil, 0)
|
chargingStateLabel := widgets.NewQLabel(nil, 0)
|
||||||
@ -180,6 +151,11 @@ func main() {
|
|||||||
securityHbox.AddWidget(chargingStateLabel, 0, 0)
|
securityHbox.AddWidget(chargingStateLabel, 0, 0)
|
||||||
securityHbox.AddWidget(startStopCharge, 0, 0)
|
securityHbox.AddWidget(startStopCharge, 0, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lockedDoors.ConnectStateChanged(lockDoors)
|
||||||
|
sentryMode.ConnectStateChanged(sentryModeEnable)
|
||||||
|
startStopCharge.ConnectStateChanged(enableCharging)
|
||||||
|
climateOn.ConnectStateChanged(enableClimate)
|
||||||
statusLayout.AddRow2(doorLockLabel, securityHbox)
|
statusLayout.AddRow2(doorLockLabel, securityHbox)
|
||||||
|
|
||||||
honk = widgets.NewQPushButton(nil)
|
honk = widgets.NewQPushButton(nil)
|
||||||
@ -215,7 +191,6 @@ func main() {
|
|||||||
|
|
||||||
window = widgets.NewQMainWindow(nil, 0)
|
window = widgets.NewQMainWindow(nil, 0)
|
||||||
window.SetWindowTitle(fmt.Sprintf("%+v: %+v", vehicle.DisplayName, vehicle.Vin))
|
window.SetWindowTitle(fmt.Sprintf("%+v: %+v", vehicle.DisplayName, vehicle.Vin))
|
||||||
|
|
||||||
centralWidget := widgets.NewQWidget(window, 0)
|
centralWidget := widgets.NewQWidget(window, 0)
|
||||||
centralWidget.SetLayout(vboxLayout)
|
centralWidget.SetLayout(vboxLayout)
|
||||||
window.SetCentralWidget(centralWidget)
|
window.SetCentralWidget(centralWidget)
|
||||||
@ -225,6 +200,113 @@ func main() {
|
|||||||
widgets.QApplication_Exec()
|
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) {
|
func honkHorn(c bool) {
|
||||||
err := vehicle.HonkHorn()
|
err := vehicle.HonkHorn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -271,6 +353,8 @@ func showDialogue(msg string, a ...interface{}) {
|
|||||||
window.Show()
|
window.Show()
|
||||||
popup = false
|
popup = false
|
||||||
dialogue.Close()
|
dialogue.Close()
|
||||||
|
|
||||||
|
go setValues()
|
||||||
})
|
})
|
||||||
quitBtn.ConnectClicked(func(checked bool) {
|
quitBtn.ConnectClicked(func(checked bool) {
|
||||||
mainApp.Quit()
|
mainApp.Quit()
|
||||||
|
|||||||
Reference in New Issue
Block a user