diff --git a/helpers.go b/helpers.go index c1d743c..54f2973 100644 --- a/helpers.go +++ b/helpers.go @@ -13,7 +13,7 @@ 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") } @@ -21,9 +21,9 @@ func isCharging(carid int) bool { } 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) { @@ -31,10 +31,10 @@ func shouldCharge(carid int) (string, bool) { 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 @@ -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) diff --git a/main.go b/main.go index 9378e2f..14ece68 100644 --- a/main.go +++ b/main.go @@ -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." diff --git a/tscsettings.go b/tscsettings.go index 809270a..7207b29 100644 --- a/tscsettings.go +++ b/tscsettings.go @@ -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"` @@ -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"` } @@ -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 + } } }