feat(mileage): add OneOS source priority controls

This commit is contained in:
kkfluous
2026-07-23 14:11:52 +08:00
parent becacecc67
commit d8773ff0a0
12 changed files with 448 additions and 44 deletions

View File

@@ -1,5 +1,10 @@
import { Hono } from 'hono';
import { fetchOneOsMileageDates } from './oneos-api.js';
import {
parseMileageSourcePriority,
protocolsForSourcePriority,
sourceCategoryFromProtocol,
} from './source-policy.js';
const app = new Hono();
@@ -23,6 +28,8 @@ const MAX_DAYS = 366;
app.get('/:plate/recent', async (c) => {
const plate = c.req.param('plate');
if (!plate) return c.json({ plate: '', days: [] }, 400);
const sourcePriority = parseMileageSourcePriority(c.req.query('sourcePriority'));
const protocolPriority = protocolsForSourcePriority(sourcePriority);
const today = new Date();
today.setHours(0, 0, 0, 0);
@@ -58,10 +65,16 @@ app.get('/:plate/recent', async (c) => {
dates.push(fmt(dateCursor));
dateCursor.setDate(dateCursor.getDate() + 1);
}
const rowsByDate = await fetchOneOsMileageDates(dates, [plate]);
const rowsByDate = await fetchOneOsMileageDates(dates, [plate], protocolPriority);
// 补全:从 start 到 end 每天一条
const result: { date: string; dailyKm: number; isDataSynced: boolean }[] = [];
const result: {
date: string;
dailyKm: number;
isDataSynced: boolean;
sourceProtocol: 'GB32960' | 'MQTT' | 'JT808' | null;
sourceCategory: 'INSTRUMENT' | 'GPS' | null;
}[] = [];
const cursor = new Date(start);
while (cursor <= end) {
const key = fmt(cursor);
@@ -70,11 +83,19 @@ app.get('/:plate/recent', async (c) => {
date: key,
dailyKm: hit?.status === 'NORMAL' ? (hit.dailyMileageKm || 0) : 0,
isDataSynced: hit?.status === 'NORMAL',
sourceProtocol: hit?.sourceProtocol || null,
sourceCategory: sourceCategoryFromProtocol(hit?.sourceProtocol || null),
});
cursor.setDate(cursor.getDate() + 1);
}
return c.json({ plate, start: fmt(start), end: fmt(end), days: result });
return c.json({
plate,
start: fmt(start),
end: fmt(end),
sourcePriority,
days: result,
});
} catch (e: unknown) {
console.error('vehicle recent error:', e);
return c.json({ plate, days: [] }, 500);