feat(csms): add system settings management for OCPP 1.6J heartbeat interval

This commit is contained in:
2026-03-17 01:42:29 +08:00
parent 4d940e2cd4
commit dee947ce3e
12 changed files with 2308 additions and 5 deletions

View File

@@ -1,3 +1,4 @@
export * from './auth-schema.ts'
export * from './ocpp-schema.ts'
export * from './tariff-schema.ts'
export * from './settings-schema.ts'

View File

@@ -0,0 +1,15 @@
import { jsonb, pgTable, timestamp, varchar } from "drizzle-orm/pg-core";
/**
* 系统参数配置(按模块 key 存储)
* 例如key=ocpp16j, value={ heartbeatInterval: 60 }
*/
export const systemSetting = pgTable("system_setting", {
key: varchar("key", { length: 64 }).primaryKey(),
value: jsonb("value").notNull().$type<Record<string, unknown>>(),
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
updatedAt: timestamp("updated_at", { withTimezone: true })
.notNull()
.defaultNow()
.$onUpdate(() => new Date()),
});

View File

@@ -20,6 +20,7 @@ import idTagRoutes from './routes/id-tags.ts'
import userRoutes from './routes/users.ts'
import setupRoutes from './routes/setup.ts'
import tariffRoutes from './routes/tariff.ts'
import settingsRoutes from './routes/settings.ts'
import type { HonoEnv } from './types/hono.ts'
@@ -64,6 +65,7 @@ app.route('/api/id-tags', idTagRoutes)
app.route('/api/users', userRoutes)
app.route('/api/setup', setupRoutes)
app.route('/api/tariff', tariffRoutes)
app.route('/api/settings', settingsRoutes)
app.get('/api', (c) => {
const user = c.get('user')

View File

@@ -0,0 +1,45 @@
import { eq } from "drizzle-orm";
import { systemSetting } from "@/db/schema.js";
import { useDrizzle } from "@/lib/db.js";
export const SETTINGS_KEY_OCPP16J = "ocpp16j";
const DEFAULT_HEARTBEAT_INTERVAL = 60;
const MIN_HEARTBEAT_INTERVAL = 10;
const MAX_HEARTBEAT_INTERVAL = 86400;
export type Ocpp16jSettings = {
heartbeatInterval: number;
};
export type SettingsPayload = {
ocpp16j: Ocpp16jSettings;
};
export function sanitizeHeartbeatInterval(raw: unknown): number {
if (typeof raw !== "number" || !Number.isFinite(raw)) {
return DEFAULT_HEARTBEAT_INTERVAL;
}
const n = Math.round(raw);
if (n < MIN_HEARTBEAT_INTERVAL) return MIN_HEARTBEAT_INTERVAL;
if (n > MAX_HEARTBEAT_INTERVAL) return MAX_HEARTBEAT_INTERVAL;
return n;
}
export function getDefaultHeartbeatInterval(): number {
return DEFAULT_HEARTBEAT_INTERVAL;
}
export async function getOcpp16jSettings(): Promise<Ocpp16jSettings> {
const db = useDrizzle();
const [ocpp16jRow] = await db
.select()
.from(systemSetting)
.where(eq(systemSetting.key, SETTINGS_KEY_OCPP16J))
.limit(1);
const ocpp16jRaw = (ocpp16jRow?.value ?? {}) as Record<string, unknown>;
return {
heartbeatInterval: sanitizeHeartbeatInterval(ocpp16jRaw.heartbeatInterval),
};
}

View File

@@ -1,19 +1,19 @@
import { useDrizzle } from '@/lib/db.js'
import dayjs from 'dayjs'
import { chargePoint } from '@/db/schema.js'
import { getOcpp16jSettings } from '@/lib/system-settings.js'
import type {
BootNotificationRequest,
BootNotificationResponse,
OcppConnectionContext,
} from '../types.ts'
const DEFAULT_HEARTBEAT_INTERVAL = 60
export async function handleBootNotification(
payload: BootNotificationRequest,
ctx: OcppConnectionContext,
): Promise<BootNotificationResponse> {
const db = useDrizzle()
const { heartbeatInterval } = await getOcpp16jSettings()
const [cp] = await db
.insert(chargePoint)
@@ -30,7 +30,7 @@ export async function handleBootNotification(
meterSerialNumber: payload.meterSerialNumber ?? null,
// New, unknown devices start as Pending — admin must manually accept them
registrationStatus: 'Pending',
heartbeatInterval: DEFAULT_HEARTBEAT_INTERVAL,
heartbeatInterval,
lastBootNotificationAt: dayjs().toDate(),
})
.onConflictDoUpdate({
@@ -45,7 +45,7 @@ export async function handleBootNotification(
meterType: payload.meterType ?? null,
meterSerialNumber: payload.meterSerialNumber ?? null,
// Do NOT override registrationStatus — preserve whatever the admin set
heartbeatInterval: DEFAULT_HEARTBEAT_INTERVAL,
heartbeatInterval,
lastBootNotificationAt: dayjs().toDate(),
updatedAt: dayjs().toDate(),
},
@@ -59,7 +59,7 @@ export async function handleBootNotification(
return {
currentTime: dayjs().toISOString(),
interval: DEFAULT_HEARTBEAT_INTERVAL,
interval: heartbeatInterval,
status,
}
}

View File

@@ -0,0 +1,65 @@
import { Hono } from "hono";
import { useDrizzle } from "@/lib/db.js";
import { systemSetting } from "@/db/schema.js";
import type { HonoEnv } from "@/types/hono.ts";
import {
SETTINGS_KEY_OCPP16J,
getOcpp16jSettings,
sanitizeHeartbeatInterval,
type SettingsPayload,
} from "@/lib/system-settings.js";
const app = new Hono<HonoEnv>();
app.get("/", async (c) => {
const payload: SettingsPayload = {
ocpp16j: await getOcpp16jSettings(),
};
return c.json(payload);
});
app.put("/", async (c) => {
const currentUser = c.get("user");
if (currentUser?.role !== "admin") {
return c.json({ error: "Forbidden" }, 403);
}
let body: Partial<SettingsPayload>;
try {
body = await c.req.json<Partial<SettingsPayload>>();
} catch {
return c.json({ error: "Invalid JSON" }, 400);
}
if (!body.ocpp16j) {
return c.json({ error: "Missing ocpp16j settings" }, 400);
}
const heartbeatInterval = sanitizeHeartbeatInterval(body.ocpp16j.heartbeatInterval);
const db = useDrizzle();
await db
.insert(systemSetting)
.values({
key: SETTINGS_KEY_OCPP16J,
value: { heartbeatInterval },
})
.onConflictDoUpdate({
target: systemSetting.key,
set: {
value: { heartbeatInterval },
updatedAt: new Date(),
},
});
const payload: SettingsPayload = {
ocpp16j: {
heartbeatInterval,
},
};
return c.json(payload);
});
export default app;