feat(csms): add system settings management for OCPP 1.6J heartbeat interval
This commit is contained in:
6
apps/csms/drizzle/0006_spooky_skin.sql
Normal file
6
apps/csms/drizzle/0006_spooky_skin.sql
Normal file
@@ -0,0 +1,6 @@
|
||||
CREATE TABLE "system_setting" (
|
||||
"key" varchar(64) PRIMARY KEY NOT NULL,
|
||||
"value" jsonb NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
1987
apps/csms/drizzle/meta/0006_snapshot.json
Normal file
1987
apps/csms/drizzle/meta/0006_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -43,6 +43,13 @@
|
||||
"when": 1773678571220,
|
||||
"tag": "0005_peaceful_anthem",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 6,
|
||||
"version": "7",
|
||||
"when": 1773682931777,
|
||||
"tag": "0006_spooky_skin",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from './auth-schema.ts'
|
||||
export * from './ocpp-schema.ts'
|
||||
export * from './tariff-schema.ts'
|
||||
export * from './settings-schema.ts'
|
||||
|
||||
15
apps/csms/src/db/settings-schema.ts
Normal file
15
apps/csms/src/db/settings-schema.ts
Normal 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()),
|
||||
});
|
||||
@@ -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')
|
||||
|
||||
45
apps/csms/src/lib/system-settings.ts
Normal file
45
apps/csms/src/lib/system-settings.ts
Normal 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),
|
||||
};
|
||||
}
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
65
apps/csms/src/routes/settings.ts
Normal file
65
apps/csms/src/routes/settings.ts
Normal 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;
|
||||
Reference in New Issue
Block a user