feat: Implement device firmware and command APIs, add individual device administration page, and refine UI styling.
This commit is contained in:
44
src/app/api/devices/[id]/command/route.ts
Normal file
44
src/app/api/devices/[id]/command/route.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export async function POST(
|
||||
req: Request,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const { command } = await req.json();
|
||||
|
||||
// In a real implementation, this would communicate with the hardware
|
||||
// For now, we simulate success and update the database accordingly
|
||||
|
||||
const device = await prisma.device.findUnique({
|
||||
where: { id }
|
||||
});
|
||||
|
||||
if (!device) {
|
||||
return NextResponse.json({ error: "Device not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
switch (command) {
|
||||
case "REBOOT":
|
||||
// Simulate reboot logic
|
||||
console.log(`[DEVICE CMD] Rebooting device ${device.serialNumber}`);
|
||||
break;
|
||||
case "SHUTDOWN":
|
||||
// Simulate shutdown logic
|
||||
console.log(`[DEVICE CMD] Shutting down device ${device.serialNumber}`);
|
||||
await prisma.device.update({
|
||||
where: { id },
|
||||
data: { status: "OFFLINE" }
|
||||
});
|
||||
break;
|
||||
default:
|
||||
return NextResponse.json({ error: "Unknown command" }, { status: 400 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true, command });
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
39
src/app/api/devices/[id]/firmware/route.ts
Normal file
39
src/app/api/devices/[id]/firmware/route.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export async function POST(
|
||||
req: Request,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const formData = await req.formData();
|
||||
const file = formData.get("file") as File;
|
||||
const slot = formData.get("slot") as string;
|
||||
|
||||
if (!file) {
|
||||
return NextResponse.json({ error: "No file uploaded" }, { status: 400 });
|
||||
}
|
||||
|
||||
// Simulate firmware processing
|
||||
console.log(`[FIRMWARE] Uploading ${file.name} to ${id} (Slot ${slot})`);
|
||||
|
||||
// Update device record to reflect new firmware (simulated)
|
||||
const updatedDevice = await prisma.device.update({
|
||||
where: { id },
|
||||
data: {
|
||||
firmwareVersion: "1.2.0-RC", // Simulated version bump
|
||||
activeSlot: slot || undefined
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
version: "1.2.0-RC",
|
||||
slot: updatedDevice.activeSlot
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Firmware upload error:", error);
|
||||
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -7,13 +7,14 @@ export async function PATCH(
|
||||
) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const { status, name } = await req.json();
|
||||
const { status, name, activeSlot } = await req.json();
|
||||
|
||||
const device = await prisma.device.update({
|
||||
where: { id },
|
||||
data: {
|
||||
status: status || undefined,
|
||||
name: name || undefined
|
||||
name: name || undefined,
|
||||
activeSlot: activeSlot || undefined
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user