|
| 1 | +package nextcharge |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/ev-dev-labs/teslasync/internal/api/chargeautopilot" |
| 9 | +) |
| 10 | + |
| 11 | +// Verdicts returned by Decide. The frontend maps these to i18n copy. |
| 12 | +const ( |
| 13 | + VerdictEnough = "enough" |
| 14 | + VerdictWait = "wait" |
| 15 | + VerdictChargeHomeNow = "charge_home_now" |
| 16 | + VerdictSupercharger = "supercharger" |
| 17 | + VerdictSkipDC = "skip_dc" |
| 18 | +) |
| 19 | + |
| 20 | +// Reason keys for i18n interpolation (nextCharge.reason.*). |
| 21 | +const ( |
| 22 | + ReasonEnough = "enough" |
| 23 | + ReasonWaitOffpeak = "wait_offpeak" |
| 24 | + ReasonSuperchargerFaster = "supercharger_faster" |
| 25 | + ReasonSuperchargerCheaper = "supercharger_cheaper" |
| 26 | + ReasonSkipDC = "skip_dc" |
| 27 | + ReasonChargeHomeNow = "charge_home_now" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + defaultHorizon = 12 * time.Hour |
| 32 | + minWaitLead = 15 * time.Minute |
| 33 | + minWaitSavingsUSD = 0.50 |
| 34 | + scCheaperRatio = 0.90 // Supercharger wins if < 90% of the cheaper home option |
| 35 | + scPremiumRatio = 1.10 // skip DC if Supercharger is ≥10% more than charge-now |
| 36 | +) |
| 37 | + |
| 38 | +// Quote is the cheapest billed Supercharger/DC site for this VIN. |
| 39 | +type Quote struct { |
| 40 | + Site string |
| 41 | + AvgPerKWh float64 // USD per kWh from Tesla invoices |
| 42 | +} |
| 43 | + |
| 44 | +// Input seeds a 12-hour energy verdict. |
| 45 | +type Input struct { |
| 46 | + Profile chargeautopilot.Profile |
| 47 | + CurrentSOC int |
| 48 | + Now time.Time |
| 49 | + Horizon time.Duration |
| 50 | + Quote *Quote |
| 51 | +} |
| 52 | + |
| 53 | +// Decision is the GET /charge-autopilot/decision wire shape. |
| 54 | +type Decision struct { |
| 55 | + Verdict string `json:"verdict"` |
| 56 | + ReasonKey string `json:"reason_key"` |
| 57 | + Reason string `json:"reason"` |
| 58 | + CurrentSOC int `json:"current_soc"` |
| 59 | + TargetSOC int `json:"target_soc"` |
| 60 | + KWhNeeded float64 `json:"kwh_needed"` |
| 61 | + HorizonHours float64 `json:"horizon_hours"` |
| 62 | + HomeNowCost *float64 `json:"home_now_cost,omitempty"` |
| 63 | + HomeWaitCost *float64 `json:"home_wait_cost,omitempty"` |
| 64 | + HomeWaitStart *time.Time `json:"home_wait_start,omitempty"` |
| 65 | + HomeSavings *float64 `json:"home_savings,omitempty"` |
| 66 | + SuperchargerSite *string `json:"supercharger_site,omitempty"` |
| 67 | + SuperchargerPerKWh *float64 `json:"supercharger_per_kwh,omitempty"` |
| 68 | + SuperchargerCost *float64 `json:"supercharger_cost,omitempty"` |
| 69 | + ReadyBy time.Time `json:"ready_by"` |
| 70 | + CappedByHealth bool `json:"capped_by_health_guardrail"` |
| 71 | +} |
| 72 | + |
| 73 | +// Decide returns the next-charge verdict. Pure: no I/O. |
| 74 | +func Decide(in Input) Decision { |
| 75 | + now := in.Now |
| 76 | + horizon := in.Horizon |
| 77 | + if horizon <= 0 { |
| 78 | + horizon = defaultHorizon |
| 79 | + } |
| 80 | + p := in.Profile |
| 81 | + target, capped := chargeautopilot.EffectiveTarget(p.TargetSOC, p.DailyCapSOC, p.TripOverride) |
| 82 | + readyBy, err := chargeautopilot.NextReadyBy(p.ReadyBy, now) |
| 83 | + if err != nil { |
| 84 | + readyBy = now.Add(24 * time.Hour) |
| 85 | + } |
| 86 | + |
| 87 | + d := Decision{ |
| 88 | + CurrentSOC: in.CurrentSOC, |
| 89 | + TargetSOC: target, |
| 90 | + HorizonHours: horizon.Hours(), |
| 91 | + ReadyBy: readyBy, |
| 92 | + CappedByHealth: capped, |
| 93 | + } |
| 94 | + attachQuote(&d, in.Quote, 0) |
| 95 | + |
| 96 | + if in.CurrentSOC >= target { |
| 97 | + d.KWhNeeded = 0 |
| 98 | + d.Verdict = VerdictEnough |
| 99 | + d.ReasonKey = ReasonEnough |
| 100 | + d.Reason = fmt.Sprintf("Battery is at %d%%, already at the %d%% target.", in.CurrentSOC, target) |
| 101 | + return d |
| 102 | + } |
| 103 | + |
| 104 | + kwhNeeded := round2(float64(target-in.CurrentSOC) / 100.0 * p.BatteryCapacityKWh) |
| 105 | + d.KWhNeeded = kwhNeeded |
| 106 | + attachQuote(&d, in.Quote, kwhNeeded) |
| 107 | + |
| 108 | + preview, previewErr := chargeautopilot.Preview(chargeautopilot.PreviewInput{ |
| 109 | + Profile: p, |
| 110 | + CurrentSOC: in.CurrentSOC, |
| 111 | + Now: now, |
| 112 | + }) |
| 113 | + if previewErr != nil || preview == nil { |
| 114 | + if in.Quote != nil && in.Quote.AvgPerKWh > 0 { |
| 115 | + d.Verdict = VerdictSupercharger |
| 116 | + d.ReasonKey = ReasonSuperchargerFaster |
| 117 | + d.Reason = fmt.Sprintf( |
| 118 | + "Home charging cannot finish before ready-by; %s is the cheapest billed Supercharger at $%.2f/kWh.", |
| 119 | + in.Quote.Site, in.Quote.AvgPerKWh, |
| 120 | + ) |
| 121 | + return d |
| 122 | + } |
| 123 | + d.Verdict = VerdictChargeHomeNow |
| 124 | + d.ReasonKey = ReasonChargeHomeNow |
| 125 | + d.Reason = "Start charging at home now — no cheaper Supercharger quote and no feasible off-peak window." |
| 126 | + return d |
| 127 | + } |
| 128 | + |
| 129 | + d.HomeNowCost = ptrf(round2(preview.ChargeNowCost)) |
| 130 | + d.HomeWaitCost = ptrf(round2(preview.OptimizedCost)) |
| 131 | + d.HomeSavings = ptrf(round2(preview.Savings)) |
| 132 | + waitStart := preview.Window.StartTime |
| 133 | + d.HomeWaitStart = &waitStart |
| 134 | + d.KWhNeeded = round2(preview.KWhNeeded) |
| 135 | + attachQuote(&d, in.Quote, preview.KWhNeeded) |
| 136 | + |
| 137 | + homeFloor := preview.OptimizedCost |
| 138 | + if preview.ChargeNowCost < homeFloor { |
| 139 | + homeFloor = preview.ChargeNowCost |
| 140 | + } |
| 141 | + scCost := 0.0 |
| 142 | + if in.Quote != nil && in.Quote.AvgPerKWh > 0 { |
| 143 | + scCost = round2(in.Quote.AvgPerKWh * preview.KWhNeeded) |
| 144 | + } |
| 145 | + |
| 146 | + if in.Quote != nil && scCost > 0 && homeFloor > 0 && scCost < homeFloor*scCheaperRatio { |
| 147 | + d.Verdict = VerdictSupercharger |
| 148 | + d.ReasonKey = ReasonSuperchargerCheaper |
| 149 | + d.Reason = fmt.Sprintf( |
| 150 | + "%s is cheaper ($%.2f vs $%.2f at home) for the %.1f kWh you still need.", |
| 151 | + in.Quote.Site, scCost, homeFloor, preview.KWhNeeded, |
| 152 | + ) |
| 153 | + return d |
| 154 | + } |
| 155 | + |
| 156 | + waitOK := waitStart.After(now.Add(minWaitLead)) && |
| 157 | + !waitStart.After(now.Add(horizon)) && |
| 158 | + preview.Savings >= minWaitSavingsUSD |
| 159 | + if waitOK { |
| 160 | + d.Verdict = VerdictWait |
| 161 | + d.ReasonKey = ReasonWaitOffpeak |
| 162 | + d.Reason = fmt.Sprintf( |
| 163 | + "Wait for off-peak at %s — save $%.2f versus charging now.", |
| 164 | + waitStart.Format(time.Kitchen), preview.Savings, |
| 165 | + ) |
| 166 | + return d |
| 167 | + } |
| 168 | + |
| 169 | + if in.Quote != nil && scCost > 0 && preview.ChargeNowCost > 0 && scCost > preview.ChargeNowCost*scPremiumRatio { |
| 170 | + d.Verdict = VerdictSkipDC |
| 171 | + d.ReasonKey = ReasonSkipDC |
| 172 | + d.Reason = fmt.Sprintf( |
| 173 | + "Skip %s ($%.2f) — home now is $%.2f for the same energy.", |
| 174 | + in.Quote.Site, scCost, preview.ChargeNowCost, |
| 175 | + ) |
| 176 | + return d |
| 177 | + } |
| 178 | + |
| 179 | + d.Verdict = VerdictChargeHomeNow |
| 180 | + d.ReasonKey = ReasonChargeHomeNow |
| 181 | + d.Reason = "Charge at home now — the cheapest window is already open (or too far out to wait)." |
| 182 | + return d |
| 183 | +} |
| 184 | + |
| 185 | +func attachQuote(d *Decision, q *Quote, kwh float64) { |
| 186 | + if q == nil || q.AvgPerKWh <= 0 { |
| 187 | + return |
| 188 | + } |
| 189 | + d.SuperchargerSite = ptrs(q.Site) |
| 190 | + d.SuperchargerPerKWh = ptrf(round4(q.AvgPerKWh)) |
| 191 | + if kwh > 0 { |
| 192 | + d.SuperchargerCost = ptrf(round2(q.AvgPerKWh * kwh)) |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +func ptrf(v float64) *float64 { return &v } |
| 197 | +func ptrs(v string) *string { return &v } |
| 198 | + |
| 199 | +func round2(f float64) float64 { return math.Round(f*100) / 100 } |
| 200 | +func round4(f float64) float64 { return math.Round(f*10000) / 10000 } |
0 commit comments