Connected cars: update the odometer automatically

The provider tab gets an "Update odometer automatically" switch and an
interval (30 minutes to a week, daily by default). The API Server does
the updating on the scheduler's ticker, so it carries on with no page
open: every five minutes it takes the provider's odometer for each due
car and stores it when it is ahead, never backwards, the same as the
Update odometer button.

The setting belongs to the car. Readings are fetched under the account
of whoever switched it on, and stop if that user can no longer edit the
car. Each attempt and its error, if any, are stored on the car and shown
under the switch. The switch also shows when the provider can't be
reached, so updates that are failing can still be switched off.

New car fields odometer_auto_sync, odometer_sync_minutes,
odometer_sync_by, odometer_synced_at and odometer_sync_error; new route
PUT /api/cars/{id}/provider/auto-sync. Unlinking leaves the setting as
it is; the sweep only looks at linked cars.

launch.json gains api-server-8081 / web-app-8081 for when 8080 is taken.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tajniak81andClaude Opus 5.5 committed 2026-11-18 12:07:02 +01:00
1 parent 914aeeee23
commit 3db12dba63
14 files changed
+512 -11

No files matched your search

+3
View File
@@ -296,6 +296,9 @@ export const api = {
request(`/cars/${carId}/provider`, { method: "POST", body: JSON.stringify(body) }),
syncCarProvider: (carId, body = {}) =>
request(`/cars/${carId}/provider/sync`, { method: "POST", body: JSON.stringify(body) }),
// Automatic odometer updates: {enabled, intervalMinutes}. Returns the car.
setCarOdometerAutoSync: (carId, body) =>
request(`/cars/${carId}/provider/auto-sync`, { method: "PUT", body: JSON.stringify(body) }),
// Charger providers — the garage's import, aimed at the wall: a charger on a
// connected service (Anker Solix, Greencell) becomes one of the caller's own
@@ -0,0 +1,79 @@
<script setup>
// Automatic odometer updates for a car linked to a connected service: on/off and
// how often. The same write as the "Update odometer" button, done by the server
// on a timer (API Server internal/api/odometersync.go), so it keeps going with
// the page closed. The setting belongs to the car, and readings are taken under
// the account of whoever switched it on — the server records that.
//
// Its own component because the provider tab shows it in two places: under the
// readings, and on the "can't reach the provider" card, where a car that has
// updates on must still be able to switch them off.
import { ref, computed } from "vue";
import { api } from "../api";
import { t } from "../i18n";
import { formatDateTime } from "../lib/format.js";
const props = defineProps({
car: { type: Object, required: true },
label: { type: String, default: "" },
});
const emit = defineEmits(["car-updated", "synced", "error"]);
const INTERVALS = [30, 60, 180, 360, 720, 1440, 10080];
const saving = ref(false);
const minutes = computed(() => props.car.odometerSyncMinutes || 1440);
async function save(enabled, intervalMinutes) {
saving.value = true;
emit("error", "");
try {
const car = await api.setCarOdometerAutoSync(props.car.id, { enabled, intervalMinutes });
emit("car-updated", car);
// Switching on runs one update straight away; let the panel show its effect.
if (enabled) emit("synced");
} catch (e) {
emit("error", e.message);
} finally {
saving.value = false;
}
}
</script>
<template>
<div>
<div class="flex flex-wrap items-center justify-between gap-3">
<label class="flex cursor-pointer items-center gap-2 text-sm font-medium text-strong">
<input
type="checkbox"
class="h-4 w-4 accent-[var(--accent)]"
:checked="car.odometerAutoSync"
:disabled="saving"
@change="save($event.target.checked, minutes)"
/>
{{ t("car.provider.autoSync") }}
</label>
<label class="flex items-center gap-2 text-sm text-muted">
{{ t("car.provider.autoSyncEvery") }}
<select
class="dh-input !w-auto !py-1"
:value="minutes"
:disabled="saving || !car.odometerAutoSync"
@change="save(true, Number($event.target.value))"
>
<option v-for="m in INTERVALS" :key="m" :value="m">
{{ t(`car.provider.autoSyncIntervals.${m}`) }}
</option>
</select>
</label>
</div>
<p class="mt-2 text-xs text-muted">
{{ t("car.provider.autoSyncHint", { label }) }}
<template v-if="car.odometerAutoSync && car.odometerSyncedAt">
· {{ t("car.provider.autoSyncLast", { time: formatDateTime(car.odometerSyncedAt) }) }}
</template>
</p>
<p v-if="car.odometerAutoSync && car.odometerSyncError" class="mt-1 text-xs text-warning">
{{ t("car.provider.autoSyncFailed", { error: car.odometerSyncError }) }}
</p>
</div>
</template>
+34 -10
View File
@@ -14,6 +14,7 @@ import { api } from "../api";
import { prefs } from "../prefs";
import { t } from "../i18n";
import { askConfirm } from "../lib/confirm.js";
import OdometerAutoSync from "./OdometerAutoSync.vue";
import { formatDateTime, formatKm } from "../lib/format.js";
const props = defineProps({
@@ -316,6 +317,18 @@ onMounted(async () => {
<div v-if="snap.unavailable" class="rounded-card border border-dashed border-default p-8 text-center">
<p class="text-sm text-warning">{{ snap.detail }}</p>
<p class="mt-2 text-xs text-muted">{{ t("car.provider.own") }}</p>
<!-- Still offered here, so updates switched on before the provider
became unreachable can be switched off; the reason they fail
shows under the switch. -->
<OdometerAutoSync
v-if="canWrite"
class="mx-auto mt-6 max-w-xl rounded-control bg-sunken px-4 py-3 text-left"
:car="car"
:label="label"
@car-updated="(c) => emit('car-updated', c)"
@synced="load"
@error="(m) => (error = m)"
/>
</div>
<template v-else>
@@ -350,16 +363,27 @@ onMounted(async () => {
<p v-if="canArrangeMetrics" class="mt-3 text-xs text-muted">{{ t("car.provider.dragHint") }}</p>
</div>
<!-- The provider's odometer is ahead of the stored one: offer to take it. -->
<div
v-if="snap.suggestedCurrentKm && canWrite"
class="mb-4 flex flex-wrap items-center justify-between gap-3 rounded-control bg-sunken px-4 py-3">
<p class="text-sm text-body">
{{ t("car.provider.odometerSuggest", { label, km: formatKm(snap.suggestedCurrentKm) }) }}
</p>
<button class="dh-btn dh-btn-primary !px-3 !py-1.5" :disabled="syncing" @click="applyOdometer">
{{ syncing ? t("car.provider.updatingOdometer") : t("car.provider.updateOdometer") }}
</button>
<!-- The odometer: take the provider's reading now, or have the server
keep taking it on a timer. -->
<div v-if="canWrite" class="mb-4 rounded-control bg-sunken px-4 py-3">
<div
v-if="snap.suggestedCurrentKm"
class="mb-3 flex flex-wrap items-center justify-between gap-3 border-b border-subtle pb-3">
<p class="text-sm text-body">
{{ t("car.provider.odometerSuggest", { label, km: formatKm(snap.suggestedCurrentKm) }) }}
</p>
<button class="dh-btn dh-btn-primary !px-3 !py-1.5" :disabled="syncing" @click="applyOdometer">
{{ syncing ? t("car.provider.updatingOdometer") : t("car.provider.updateOdometer") }}
</button>
</div>
<OdometerAutoSync
:car="car"
:label="label"
@car-updated="(c) => emit('car-updated', c)"
@synced="load"
@error="(m) => (error = m)"
/>
</div>
<!-- The vehicle record itself -->
+14
View File
@@ -809,6 +809,20 @@
"odometerSuggest": "{label} oplyser {km}, altså mere end bilens gemte kilometerstand.",
"updateOdometer": "Opdater kilometerstand",
"updatingOdometer": "Opdaterer…",
"autoSync": "Opdater kilometerstand automatisk",
"autoSyncHint": "Henter aflæsningen fra {label} i baggrunden, også når siden er lukket. Den går kun fremad.",
"autoSyncEvery": "Hver",
"autoSyncLast": "Sidst tjekket {time}",
"autoSyncFailed": "Sidste tjek mislykkedes: {error}",
"autoSyncIntervals": {
"30": "30. minut",
"60": "time",
"180": "3. time",
"360": "6. time",
"720": "12. time",
"1440": "dag",
"10080": "uge"
},
"unlink": "Afbryd",
"unlinkConfirm": "Afbryd denne bil fra {label}? Intet gemt slettes.",
"connectTitle": "Forbind denne bil til en tjeneste",
+14
View File
@@ -808,6 +808,20 @@
"odometerSuggest": "{label} reports {km}, ahead of this car's stored reading.",
"updateOdometer": "Update odometer",
"updatingOdometer": "Updating…",
"autoSync": "Update odometer automatically",
"autoSyncHint": "Takes {label}'s reading in the background, even with this page closed. It only ever moves forward.",
"autoSyncEvery": "Every",
"autoSyncLast": "Last checked {time}",
"autoSyncFailed": "Last check failed: {error}",
"autoSyncIntervals": {
"30": "30 minutes",
"60": "hour",
"180": "3 hours",
"360": "6 hours",
"720": "12 hours",
"1440": "day",
"10080": "week"
},
"unlink": "Disconnect",
"unlinkConfirm": "Disconnect this car from {label}? Nothing already saved is deleted.",
"connectTitle": "Connect this car to a service",
+14
View File
@@ -815,6 +815,20 @@
"odometerSuggest": "{label} podaje {km}, czyli więcej niż zapisany przebieg tego samochodu.",
"updateOdometer": "Zaktualizuj przebieg",
"updatingOdometer": "Aktualizowanie…",
"autoSync": "Aktualizuj przebieg automatycznie",
"autoSyncHint": "Pobiera odczyt z {label} w tle, także przy zamkniętej stronie. Przebieg nigdy się nie cofa.",
"autoSyncEvery": "Co",
"autoSyncLast": "Ostatnie sprawdzenie {time}",
"autoSyncFailed": "Ostatnie sprawdzenie nie powiodło się: {error}",
"autoSyncIntervals": {
"30": "30 minut",
"60": "godzinę",
"180": "3 godziny",
"360": "6 godzin",
"720": "12 godzin",
"1440": "dzień",
"10080": "tydzień"
},
"unlink": "Odłącz",
"unlinkConfirm": "Odłączyć ten samochód od {label}? Żadne zapisane dane nie zostaną usunięte.",
"connectTitle": "Połącz ten samochód z usługą",