Server: InfluxDB 2 storage, live telemetry endpoint, ABRP forwarder
- MongoDB replaced by InfluxDB 2 (one bucket, installationId tag). Consumption is stored per chunk at its recordedAt and summed on read, so a chunk the car re-sends from its cache overwrites itself instead of being counted twice ($inc did count it twice). Lifetime is a running total derived on read. - POST /v1/telemetry takes batches of samples in ABRP's tlm fields; every field is bounded, unknown fields are dropped, unread signals stay absent. - After a successful write the newest fresh sample is forwarded to ABRP with ABRP_API_KEY / ABRP_USER_TOKEN from the server environment; rate limited, never queued. The car no longer needs ABRP credentials. - Self-hosting: standalone output, Dockerfile (tests run in the build, runs as UID 10001), .env.example and README for the new setup. - Dev shell gains Node 22 and InfluxDB 2. Verified locally against InfluxDB 2.7.12: all endpoints, retry idempotency, and the dashboard pages. Unit tests: 17 pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { aggregateDays } from "../src/lib/days.ts";
|
||||
|
||||
const ID = "6f1a2b3c-4d5e-4f60-8a9b-0c1d2e3f4a5b";
|
||||
const chunk = (date: string, at: number, km: number, kwh: number, sohPercent?: number) => ({
|
||||
installationId: ID,
|
||||
date,
|
||||
recordedAt: new Date(at),
|
||||
day: { km, kwh, hours: km / 50, socDrop: kwh / 0.6 },
|
||||
...(sohPercent === undefined ? {} : { sohPercent }),
|
||||
});
|
||||
|
||||
test("adds chunks up per day, newest day first, with a running lifetime", () => {
|
||||
const days = aggregateDays([
|
||||
chunk("2026-10-16", 1_000, 10, 2),
|
||||
chunk("2026-10-17", 3_000, 5, 1),
|
||||
chunk("2026-10-16", 2_000, 20, 3),
|
||||
]);
|
||||
assert.deepEqual(days.map((d) => d.date), ["2026-10-17", "2026-10-16"]);
|
||||
assert.equal(days[1].day.km, 30);
|
||||
assert.equal(days[1].day.kwh, 5);
|
||||
assert.equal(days[1].recordedAt.getTime(), 2_000);
|
||||
assert.equal(days[1].lifetime?.km, 30);
|
||||
assert.equal(days[0].lifetime?.km, 35);
|
||||
assert.equal(days[0].lifetime?.kwh, 6);
|
||||
});
|
||||
|
||||
test("the newest manual SoH of a day wins, and a day without one has none", () => {
|
||||
const days = aggregateDays([
|
||||
chunk("2026-10-16", 2_000, 1, 1, 96),
|
||||
chunk("2026-10-16", 1_000, 1, 1, 98),
|
||||
chunk("2026-10-17", 3_000, 1, 1),
|
||||
]);
|
||||
assert.equal(days[1].sohPercent, 96);
|
||||
assert.equal("sohPercent" in days[0], false);
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { pickForForward, resetAbrpState, toTlm } from "../src/lib/abrp.ts";
|
||||
import { ValidationError, validateTelemetryBatch } from "../src/lib/validate.ts";
|
||||
|
||||
const INSTALLATION = "6f1a2b3c-4d5e-4f60-8a9b-0c1d2e3f4a5b";
|
||||
const NOW_S = 1_790_000_000;
|
||||
|
||||
test("accepts a sample in ABRP's field names and keeps only what was sent", () => {
|
||||
const batch = validateTelemetryBatch({
|
||||
installationId: INSTALLATION,
|
||||
samples: [{ utc: NOW_S, soc: 81, power: -12.5, speed: 0, lat: 52.1, lon: 7.6, is_charging: 1, is_dcfc: false }],
|
||||
});
|
||||
assert.deepEqual(batch.samples[0], {
|
||||
utc: NOW_S, soc: 81, power: -12.5, speed: 0, lat: 52.1, lon: 7.6, is_charging: true, is_dcfc: false,
|
||||
});
|
||||
// An unread signal stays absent: a missing SOC must never become 0.
|
||||
assert.equal("est_battery_range" in batch.samples[0], false);
|
||||
});
|
||||
|
||||
test("drops unknown fields instead of rejecting the batch", () => {
|
||||
const batch = validateTelemetryBatch({ installationId: INSTALLATION, samples: [{ utc: NOW_S, future_field: 3 }] });
|
||||
assert.deepEqual(batch.samples[0], { utc: NOW_S });
|
||||
});
|
||||
|
||||
test("rejects bad timestamps, out-of-range values, bad flags and half positions", () => {
|
||||
const send = (sample: object) => validateTelemetryBatch({ installationId: INSTALLATION, samples: [sample] });
|
||||
assert.throws(() => send({ utc: NOW_S * 1000 }), ValidationError); // milliseconds, not seconds
|
||||
assert.throws(() => send({ utc: 1.5 }), ValidationError);
|
||||
assert.throws(() => send({ utc: NOW_S, soc: 101 }), ValidationError);
|
||||
assert.throws(() => send({ utc: NOW_S, speed: "fast" }), ValidationError);
|
||||
assert.throws(() => send({ utc: NOW_S, is_parked: 2 }), ValidationError);
|
||||
assert.throws(() => send({ utc: NOW_S, lat: 52.1 }), ValidationError);
|
||||
});
|
||||
|
||||
test("rejects empty and oversized batches and bad installation ids", () => {
|
||||
assert.throws(() => validateTelemetryBatch({ installationId: INSTALLATION, samples: [] }), ValidationError);
|
||||
const many = Array.from({ length: 501 }, (_, i) => ({ utc: NOW_S + i }));
|
||||
assert.throws(() => validateTelemetryBatch({ installationId: INSTALLATION, samples: many }), ValidationError);
|
||||
assert.throws(() => validateTelemetryBatch({ installationId: "nope", samples: [{ utc: NOW_S }] }), ValidationError);
|
||||
});
|
||||
|
||||
test("ABRP gets flags as 0/1", () => {
|
||||
assert.deepEqual(toTlm({ utc: NOW_S, soc: 50, is_charging: true, is_parked: false }), {
|
||||
utc: NOW_S, soc: 50, is_charging: 1, is_parked: 0,
|
||||
});
|
||||
});
|
||||
|
||||
test("forwards only the newest fresh sample, at most once per interval", () => {
|
||||
resetAbrpState();
|
||||
const nowMs = NOW_S * 1000;
|
||||
const picked = pickForForward([{ utc: NOW_S - 10 }, { utc: NOW_S - 2 }, { utc: NOW_S - 5 }], nowMs);
|
||||
assert.equal(picked?.utc, NOW_S - 2);
|
||||
// A sample from ten minutes ago describes a car that has moved on.
|
||||
assert.equal(pickForForward([{ utc: NOW_S - 600 }], nowMs), null);
|
||||
});
|
||||
Reference in New Issue
Block a user