feat(api): 添加 MeterValue 展示支持,重置 OCPP 认证密钥二次确认
This commit is contained in:
@@ -2,7 +2,8 @@ import { Hono } from "hono";
|
||||
import { desc, eq, sql } from "drizzle-orm";
|
||||
import dayjs from "dayjs";
|
||||
import { useDrizzle } from "@/lib/db.js";
|
||||
import { chargePoint, connector } from "@/db/schema.js";
|
||||
import { chargePoint, connector, meterValue } from "@/db/schema.js";
|
||||
import type { SampledValue } from "@/db/schema.js";
|
||||
import { ocppConnections } from "@/ocpp/handler.js";
|
||||
import { generateOcppPassword, hashOcppPassword } from "@/lib/ocpp-auth.js";
|
||||
import type { HonoEnv } from "@/types/hono.ts";
|
||||
@@ -122,6 +123,7 @@ app.get("/connections", (c) => {
|
||||
app.get("/:id", async (c) => {
|
||||
const db = useDrizzle();
|
||||
const id = c.req.param("id");
|
||||
const isAdmin = c.get("user")?.role === "admin";
|
||||
|
||||
const [cp] = await db.select().from(chargePoint).where(eq(chargePoint.id, id)).limit(1);
|
||||
|
||||
@@ -130,12 +132,39 @@ app.get("/:id", async (c) => {
|
||||
const allConnectors = await db.select().from(connector).where(eq(connector.chargePointId, id));
|
||||
const cpStatus = allConnectors.find((conn) => conn.connectorId === 0);
|
||||
const displayConnectors = allConnectors.filter((conn) => conn.connectorId > 0);
|
||||
const [latestMeter] = await db
|
||||
.select({
|
||||
timestamp: meterValue.timestamp,
|
||||
sampledValues: meterValue.sampledValues,
|
||||
})
|
||||
.from(meterValue)
|
||||
.where(eq(meterValue.chargePointId, id))
|
||||
.orderBy(desc(meterValue.timestamp), desc(meterValue.receivedAt))
|
||||
.limit(1);
|
||||
|
||||
const meterHistory = isAdmin
|
||||
? (
|
||||
await db
|
||||
.select({
|
||||
connectorNumber: meterValue.connectorNumber,
|
||||
timestamp: meterValue.timestamp,
|
||||
sampledValues: meterValue.sampledValues,
|
||||
})
|
||||
.from(meterValue)
|
||||
.where(eq(meterValue.chargePointId, id))
|
||||
.orderBy(desc(meterValue.timestamp), desc(meterValue.receivedAt))
|
||||
.limit(24)
|
||||
).reverse()
|
||||
: [];
|
||||
|
||||
return c.json({
|
||||
...cp,
|
||||
connectors: displayConnectors,
|
||||
chargePointStatus: cpStatus?.status ?? null,
|
||||
chargePointErrorCode: cpStatus?.errorCode ?? null,
|
||||
latestMeterTimestamp: latestMeter?.timestamp ?? null,
|
||||
latestMeterValues: ((latestMeter?.sampledValues as SampledValue[] | undefined) ?? []),
|
||||
meterHistory,
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user