Files
mg4/server/model.go
T
nilsandClaude Opus 5.5 a5db5604d3 Code review fixes: app 2.9.0, server 2.2.0
Telemetry no longer loses queued batches. The server drops a bad value,
or a sample without a plausible time, instead of rejecting the batch,
and answers 200 with what it dropped. The app sends only values inside
the server's ranges (speed as a magnitude, so reversing is fine) and no
samples before the head unit's clock is set.

Server:
- Pairing hands out and accepts a token only after the device is saved;
  a failed save leaves the request pending.
- A paired token serves only the installation id it was paired with
  (403 otherwise); the shared INGEST_TOKEN still serves any.
- Wrong-password lockouts and open pairing requests are limited per
  client address (last X-Forwarded-For entry), so a stranger cannot
  lock the owner out; a car starting again replaces its own request.
- ABRP forwarding skips samples stamped ahead of the server's clock,
  which used to stop it until a restart.

App:
- Settings written to plaintext during a keystore failure move back
  into encrypted storage; the stored installation id wins.
- The installation id is derived from ANDROID_ID on first start, so a
  reinstall finds its history; a stored or imported id still wins.
- The scanned config file is deleted after import (it holds the token).
- GPS stops while live telemetry is off or the server unconfigured, and
  pauses while parked and idle, keeping the fix the car parked with.
- Range estimates use the monotonic clock.
- One HTTP helper (ServerHttp) for telemetry, sync and pairing.
- Last Turkish strings translated; mise logs shows the NilsMG4 tags.

Grafana reads InfluxDB with its own read-only token (new grafana-influx
Secret, created by hand as described in k8s/README.md).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QLJfZQZeAKAwSqViTmLzki
2026-10-18 00:36:19 +02:00

111 lines
3.5 KiB
Go

package main
import "time"
// Totals are one day's (or one chunk's) consumption counters.
type Totals struct {
Km float64 `json:"km"`
Kwh float64 `json:"kwh"`
Hours float64 `json:"hours"`
SocDrop float64 `json:"socDrop"`
}
func (t *Totals) add(o Totals) {
t.Km += o.Km
t.Kwh += o.Kwh
t.Hours += o.Hours
t.SocDrop += o.SocDrop
}
// ConsumptionChunk is one 2-minute delta of a day's consumption, as the car sends it.
// Days are built from these when history is read.
type ConsumptionChunk struct {
InstallationID string
Date string // the car's local date, YYYY-MM-DD
RecordedAt time.Time
Day Totals
SohPercent *float64
}
// DailyConsumption is a day folded from its chunks.
type DailyConsumption struct {
InstallationID string `json:"installationId"`
Date string `json:"date"`
RecordedAt time.Time `json:"recordedAt"`
Day Totals `json:"day"`
SohPercent *float64 `json:"sohPercent,omitempty"`
// Lifetime is the running total up to and including this day; not part of the
// car's history response, which carries one lifetime for all days.
Lifetime Totals `json:"-"`
}
type CurvePoint struct {
Timestamp time.Time
PowerKw float64
SocPercent *float64
}
type ChargingSession struct {
InstallationID string
SessionID string
StartedAt time.Time
EndedAt time.Time
DurationSeconds float64
EnergyKwh float64
ChargingLossPercent float64
GridEnergyKwh float64
PricePerKwh float64
TotalCost float64
IsDemo bool
StartSocPercent *float64
EndSocPercent *float64
Curve []CurvePoint
}
// telemetryNumbers are the numeric live fields, named and unit-ed as ABRP's tlm/send
// expects them, with the range each one must fall in.
var telemetryNumbers = map[string][2]float64{
"soc": {0, 100}, // %
"speed": {0, 400}, // km/h
"power": {-1000, 1000}, // kW, positive leaving the battery
"est_battery_range": {0, 2000}, // km
"capacity": {0, 500}, // kWh
"soe": {0, 500}, // kWh
"soh": {0, 100}, // %
"kwh_charged": {0, 1000}, // kWh, this charge session
"odometer": {0, 10_000_000}, // km
"lat": {-90, 90}, // °
"lon": {-180, 180}, // °
"elevation": {-1000, 10_000}, // m
"heading": {0, 360}, // °
"batt_temp": {-60, 100}, // °C
"cabin_temp": {-60, 100}, // °C
"ext_temp": {-60, 70}, // °C
"hvac_setpoint": {0, 40}, // °C
"hvac_power": {0, 50}, // kW
"voltage": {0, 1500}, // V
"current": {-2000, 2000}, // A
"tire_pressure_fl": {0, 1000}, // kPa
"tire_pressure_fr": {0, 1000}, // kPa
"tire_pressure_rl": {0, 1000}, // kPa
"tire_pressure_rr": {0, 1000}, // kPa
}
// telemetryFlags are the boolean live fields; ABRP takes them as 0/1.
var telemetryFlags = []string{"is_charging", "is_dcfc", "is_parked"}
// TelemetrySample is one live sample. Only UTC is always present; an unread signal is
// absent from Numbers and Flags, never 0.
type TelemetrySample struct {
UTC int64 // epoch seconds
Numbers map[string]float64
Flags map[string]bool
}
type TelemetryBatch struct {
InstallationID string
Samples []TelemetrySample
// Dropped names the values and samples validation left out, e.g. "samples[3].speed".
Dropped []string
}