Skip to content

Commit

Permalink
Adopt to teslasolarcharger api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasfaerber committed May 30, 2024
1 parent 2358b63 commit f02fc75
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 101 deletions.
12 changes: 6 additions & 6 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@ func isCharging(carid int) bool {
}
return false // better panic?
}
if TSCSettings.CarsToManage[sCarId].CarState.PluggedIn == false {
if TSCSettings.CarsToManage[sCarId].PluggedIn == false {
if Cfg.Debug {
logger.Printf("isCharging, returning false\n")
}
return false
}
if Cfg.Debug {
logger.Printf("isCharging, returning %t\n",
TSCSettings.CarsToManage[sCarId].CarState.ChargerActualCurrent > 0)
TSCSettings.CarsToManage[sCarId].ChargerActualCurrent > 0)
}
return TSCSettings.CarsToManage[sCarId].CarState.ChargerActualCurrent > 0
return TSCSettings.CarsToManage[sCarId].ChargerActualCurrent > 0
}

func shouldCharge(carid int) (string, bool) {
var sCarId, success = getSettingsCarId(carid)
if !success {
return "Error getting car settings", false
}
if TSCSettings.CarsToManage[sCarId].CarState.PluggedIn == false {
if TSCSettings.CarsToManage[sCarId].PluggedIn == false {
return "Not plugged in.", false
}
if TSCSettings.CarsToManage[sCarId].CarState.SoC >= Cfg.ChargeSocLimit {
if TSCSettings.CarsToManage[sCarId].SoC >= Cfg.ChargeSocLimit {
return "SoC Limit below Car SoC. Not charging (anymore)", false
}
return "", true
Expand All @@ -47,7 +47,7 @@ func isSpotCharge(carid int) bool {
}
//timeStampLayout := "2006-01-02T15:04:05"
timeStampLayout := time.RFC3339
timeLatestTimeToReachSoC := TSCSettings.CarsToManage[sCarId].CarConfiguration.LatestTimeToReachSoC
timeLatestTimeToReachSoC := TSCSettings.CarsToManage[sCarId].LatestTimeToReachSoC
latestTimeToReachSoC, err := time.Parse(timeStampLayout, timeLatestTimeToReachSoC)
if err != nil {
fmt.Printf("isSpotCharge, Error: %+v\n", err)
Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ func checkPriceLoop(carid int, spotchargeprice float64) {
}
message := fmt.Sprintf("Car %d, PluggedIn: %t, SoC: %d, SocLimit (SpotCharge): %d, SoCLimit (Car): %d,"+
" ChargerActualCurrent: %d", carid,
TSCSettings.CarsToManage[settingsCarid].CarState.PluggedIn,
TSCSettings.CarsToManage[settingsCarid].CarState.SoC,
TSCSettings.CarsToManage[settingsCarid].PluggedIn,
TSCSettings.CarsToManage[settingsCarid].SoC,
Cfg.ChargeSocLimit,
TSCSettings.CarsToManage[settingsCarid].CarState.SocLimit,
TSCSettings.CarsToManage[settingsCarid].CarState.ChargerActualCurrent)
TSCSettings.CarsToManage[settingsCarid].SocLimit,
TSCSettings.CarsToManage[settingsCarid].ChargerActualCurrent)
message += ", SpotPrice NOW: %f"
if SpotPrice > spotchargeprice {
message += " - above charge price of %f."
Expand Down
186 changes: 95 additions & 91 deletions tscsettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,48 @@ type TSCSettingsEntry struct {
Overage interface{} `json:"overage"`
PowerBuffer interface{} `json:"powerBuffer"`
CarsToManage []struct {
Id int `json:"id"`
Vin string `json:"vin"`
CarConfiguration struct {
ChargeMode int `json:"chargeMode"`
MinimumSoC int `json:"minimumSoC"`
LatestTimeToReachSoC string `json:"latestTimeToReachSoC"`
IgnoreLatestTimeToReachSocDate bool `json:"ignoreLatestTimeToReachSocDate"`
MaximumAmpere int `json:"maximumAmpere"`
MinimumAmpere int `json:"minimumAmpere"`
UsableEnergy int `json:"usableEnergy"`
ShouldBeManaged bool `json:"shouldBeManaged"`
ShouldSetChargeStartTimes bool `json:"shouldSetChargeStartTimes"`
ChargingPriority int `json:"chargingPriority"`
} `json:"carConfiguration"`
CarState struct {
Name string `json:"name"`
ShouldStartChargingSince interface{} `json:"shouldStartChargingSince"`
EarliestSwitchOn interface{} `json:"earliestSwitchOn"`
ShouldStopChargingSince time.Time `json:"shouldStopChargingSince"`
EarliestSwitchOff time.Time `json:"earliestSwitchOff"`
ScheduledChargingStartTime interface{} `json:"scheduledChargingStartTime"`
SoC int `json:"soC"`
SocLimit int `json:"socLimit"`
IsHomeGeofence bool `json:"isHomeGeofence"`
TimeUntilFullCharge string `json:"timeUntilFullCharge"`
ReachingMinSocAtFullSpeedCharge time.Time `json:"reachingMinSocAtFullSpeedCharge"`
AutoFullSpeedCharge bool `json:"autoFullSpeedCharge"`
LastSetAmp int `json:"lastSetAmp"`
ChargerPhases int `json:"chargerPhases"`
ActualPhases int `json:"actualPhases"`
ChargerVoltage int `json:"chargerVoltage"`
ChargerActualCurrent int `json:"chargerActualCurrent"`
ChargerPilotCurrent int `json:"chargerPilotCurrent"`
ChargerRequestedCurrent int `json:"chargerRequestedCurrent"`
PluggedIn bool `json:"pluggedIn"`
ClimateOn bool `json:"climateOn"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
DistanceToHomeGeofence int `json:"distanceToHomeGeofence"`
ChargingPowerAtHome int `json:"chargingPowerAtHome"`
State int `json:"state"`
Healthy bool `json:"healthy"`
ReducedChargeSpeedWarning bool `json:"reducedChargeSpeedWarning"`
PlannedChargingSlots []interface{} `json:"plannedChargingSlots"`
} `json:"carState"`
Id int `json:"id"`
Vin string `json:"vin"`
TeslaMateCarId int `json:teslaMateCarId`
ChargeMode int `json:"chargeMode"`
MinimumSoC int `json:"minimumSoC"`
LatestTimeToReachSoC string `json:"latestTimeToReachSoC"`
IgnoreLatestTimeToReachSocDate bool `json:"ignoreLatestTimeToReachSocDate"`
MaximumAmpere int `json:"maximumAmpere"`
MinimumAmpere int `json:"minimumAmpere"`
UsableEnergy int `json:"usableEnergy"`
ShouldBeManaged bool `json:"shouldBeManaged"`
ShouldSetChargeStartTimes bool `json:"shouldSetChargeStartTimes"`
ChargingPriority int `json:"chargingPriority"`
Name string `json:"name"`
ShouldStartChargingSince interface{} `json:"shouldStartChargingSince"`
EarliestSwitchOn interface{} `json:"earliestSwitchOn"`
ShouldStopChargingSince time.Time `json:"shouldStopChargingSince"`
EarliestSwitchOff time.Time `json:"earliestSwitchOff"`
ScheduledChargingStartTime interface{} `json:"scheduledChargingStartTime"`
SoC int `json:"soC"`
SocLimit int `json:"socLimit"`
IsHomeGeofence bool `json:"isHomeGeofence"`
TimeUntilFullCharge string `json:"timeUntilFullCharge"`
ReachingMinSocAtFullSpeedCharge time.Time `json:"reachingMinSocAtFullSpeedCharge"`
AutoFullSpeedCharge bool `json:"autoFullSpeedCharge"`
LastSetAmp int `json:"lastSetAmp"`
ChargerPhases int `json:"chargerPhases"`
ActualPhases int `json:"actualPhases"`
ChargerVoltage int `json:"chargerVoltage"`
ChargerActualCurrent int `json:"chargerActualCurrent"`
ChargerPilotCurrent int `json:"chargerPilotCurrent"`
ChargerRequestedCurrent int `json:"chargerRequestedCurrent"`
PluggedIn bool `json:"pluggedIn"`
ClimateOn bool `json:"climateOn"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
DistanceToHomeGeofence int `json:"distanceToHomeGeofence"`
ChargingPowerAtHome int `json:"chargingPowerAtHome"`
State int `json:"state"`
Healthy bool `json:"healthy"`
ReducedChargeSpeedWarning bool `json:"reducedChargeSpeedWarning"`
PlannedChargingSlots []interface{} `json:"plannedChargingSlots"`
} `json:"carsToManage"`
HomeBatterySoc interface{} `json:"homeBatterySoc"`
HomeBatteryPower interface{} `json:"homeBatteryPower"`
Expand All @@ -71,51 +68,48 @@ type TSCSettingsEntry struct {
AllowUnlimitedFleetApiRequests bool `json:"allowUnlimitedFleetApiRequests"`
LastFleetApiRequestAllowedCheck time.Time `json:"lastFleetApiRequestAllowedCheck"`
Cars []struct {
Id int `json:"id"`
Vin string `json:"vin"`
CarConfiguration struct {
ChargeMode int `json:"chargeMode"`
MinimumSoC int `json:"minimumSoC"`
LatestTimeToReachSoC string `json:"latestTimeToReachSoC"`
IgnoreLatestTimeToReachSocDate bool `json:"ignoreLatestTimeToReachSocDate"`
MaximumAmpere int `json:"maximumAmpere"`
MinimumAmpere int `json:"minimumAmpere"`
UsableEnergy int `json:"usableEnergy"`
ShouldBeManaged bool `json:"shouldBeManaged"`
ShouldSetChargeStartTimes bool `json:"shouldSetChargeStartTimes"`
ChargingPriority int `json:"chargingPriority"`
} `json:"carConfiguration"`
CarState struct {
Name string `json:"name"`
ShouldStartChargingSince interface{} `json:"shouldStartChargingSince"`
EarliestSwitchOn interface{} `json:"earliestSwitchOn"`
ShouldStopChargingSince time.Time `json:"shouldStopChargingSince"`
EarliestSwitchOff time.Time `json:"earliestSwitchOff"`
ScheduledChargingStartTime interface{} `json:"scheduledChargingStartTime"`
SoC int `json:"soC"`
SocLimit int `json:"socLimit"`
IsHomeGeofence bool `json:"isHomeGeofence"`
TimeUntilFullCharge string `json:"timeUntilFullCharge"`
ReachingMinSocAtFullSpeedCharge time.Time `json:"reachingMinSocAtFullSpeedCharge"`
AutoFullSpeedCharge bool `json:"autoFullSpeedCharge"`
LastSetAmp int `json:"lastSetAmp"`
ChargerPhases int `json:"chargerPhases"`
ActualPhases int `json:"actualPhases"`
ChargerVoltage int `json:"chargerVoltage"`
ChargerActualCurrent int `json:"chargerActualCurrent"`
ChargerPilotCurrent int `json:"chargerPilotCurrent"`
ChargerRequestedCurrent int `json:"chargerRequestedCurrent"`
PluggedIn bool `json:"pluggedIn"`
ClimateOn bool `json:"climateOn"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
DistanceToHomeGeofence int `json:"distanceToHomeGeofence"`
ChargingPowerAtHome int `json:"chargingPowerAtHome"`
State int `json:"state"`
Healthy bool `json:"healthy"`
ReducedChargeSpeedWarning bool `json:"reducedChargeSpeedWarning"`
PlannedChargingSlots []interface{} `json:"plannedChargingSlots"`
} `json:"carState"`
Id int `json:"id"`
Vin string `json:"vin"`
TeslaMateCardId int `json:teslaMateCarId`
ChargeMode int `json:"chargeMode"`
MinimumSoC int `json:"minimumSoC"`
LatestTimeToReachSoC string `json:"latestTimeToReachSoC"`
IgnoreLatestTimeToReachSocDate bool `json:"ignoreLatestTimeToReachSocDate"`
MaximumAmpere int `json:"maximumAmpere"`
MinimumAmpere int `json:"minimumAmpere"`
UsableEnergy int `json:"usableEnergy"`
ShouldBeManaged bool `json:"shouldBeManaged"`
ShouldSetChargeStartTimes bool `json:"shouldSetChargeStartTimes"`
ChargingPriority int `json:"chargingPriority"`
Name string `json:"name"`
ShouldStartChargingSince interface{} `json:"shouldStartChargingSince"`
EarliestSwitchOn interface{} `json:"earliestSwitchOn"`
ShouldStopChargingSince time.Time `json:"shouldStopChargingSince"`
EarliestSwitchOff time.Time `json:"earliestSwitchOff"`
ScheduledChargingStartTime interface{} `json:"scheduledChargingStartTime"`
SoC int `json:"soC"`
SocLimit int `json:"socLimit"`
IsHomeGeofence bool `json:"isHomeGeofence"`
TimeUntilFullCharge string `json:"timeUntilFullCharge"`
ReachingMinSocAtFullSpeedCharge time.Time `json:"reachingMinSocAtFullSpeedCharge"`
AutoFullSpeedCharge bool `json:"autoFullSpeedCharge"`
LastSetAmp int `json:"lastSetAmp"`
ChargerPhases int `json:"chargerPhases"`
ActualPhases int `json:"actualPhases"`
ChargerVoltage int `json:"chargerVoltage"`
ChargerActualCurrent int `json:"chargerActualCurrent"`
ChargerPilotCurrent int `json:"chargerPilotCurrent"`
ChargerRequestedCurrent int `json:"chargerRequestedCurrent"`
PluggedIn bool `json:"pluggedIn"`
ClimateOn bool `json:"climateOn"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
DistanceToHomeGeofence int `json:"distanceToHomeGeofence"`
ChargingPowerAtHome int `json:"chargingPowerAtHome"`
State int `json:"state"`
Healthy bool `json:"healthy"`
ReducedChargeSpeedWarning bool `json:"reducedChargeSpeedWarning"`
PlannedChargingSlots []interface{} `json:"plannedChargingSlots"`
} `json:"cars"`
}

Expand All @@ -142,7 +136,17 @@ func GetCarConfiguration(carid int, init bool) (TSCSettingsEntry, bool) {
//fmt.Printf("element.vin: %s\n", element.Vin)
if element.Id == carid {
if init == true {
PreviousCarSettings = element.CarConfiguration
PreviousCarSettings.ChargeMode = element.ChargeMode
PreviousCarSettings.MinimumSoC = element.MinimumSoC
PreviousCarSettings.LatestTimeToReachSoC = element.LatestTimeToReachSoC
PreviousCarSettings.IgnoreLatestTimeToReachSocDate = element.IgnoreLatestTimeToReachSocDate
PreviousCarSettings.MaximumAmpere = element.MaximumAmpere
PreviousCarSettings.MinimumAmpere = element.MinimumAmpere
PreviousCarSettings.UsableEnergy = element.UsableEnergy
PreviousCarSettings.ShouldBeManaged = element.ShouldBeManaged
PreviousCarSettings.ShouldSetChargeStartTimes = element.ShouldSetChargeStartTimes
PreviousCarSettings.ChargingPriority = element.ChargingPriority

}
}
}
Expand Down

0 comments on commit f02fc75

Please sign in to comment.