2025-05-18 17:21:58 +02:00
|
|
|
import { db } from "$lib/server/db";
|
|
|
|
import * as table from "$lib/server/db/schema";
|
|
|
|
import { fail } from "@sveltejs/kit";
|
|
|
|
import { eq } from "drizzle-orm";
|
|
|
|
|
|
|
|
export const load = async (event) => {
|
2025-06-12 18:53:02 +02:00
|
|
|
// Fetch all available floors
|
|
|
|
const floors = await db
|
|
|
|
.select({ floor: table.floors.floor })
|
|
|
|
.from(table.floors)
|
|
|
|
.orderBy(table.floors.floor);
|
|
|
|
|
|
|
|
// Provide mock ESP8266 devices for now
|
|
|
|
const devices = [
|
|
|
|
{ id: "esp8266-001", name: "ESP8266 #001", type: "esp8266", status: "online" },
|
|
|
|
{ id: "esp8266-002", name: "ESP8266 #002", type: "esp8266", status: "online" },
|
|
|
|
{ id: "esp8266-003", name: "ESP8266 #003", type: "esp8266", status: "offline" },
|
|
|
|
{ id: "esp8266-004", name: "ESP8266 #004", type: "esp8266", status: "online" },
|
|
|
|
];
|
|
|
|
|
|
|
|
return {
|
|
|
|
floors: floors.map((f) => f.floor),
|
|
|
|
devices: devices,
|
|
|
|
};
|
2025-05-18 17:21:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
newfloor: async (event) => {
|
|
|
|
const formData = await event.request.formData();
|
|
|
|
const newFloorNumber = await formData.get("number");
|
|
|
|
const n = Number(newFloorNumber);
|
|
|
|
if (isNaN(n)) return fail(400, { message: "Invalid number!" });
|
|
|
|
|
|
|
|
const exists = await db
|
|
|
|
.select({ floor: table.floors.floor })
|
|
|
|
.from(table.floors)
|
2025-05-18 23:58:46 +02:00
|
|
|
.where(eq(table.floors.floor, n));
|
|
|
|
|
2025-05-18 17:21:58 +02:00
|
|
|
if (exists.length == 1) return fail(400, { message: "Floor " + n + " already exists!" });
|
|
|
|
|
|
|
|
await db.insert(table.floors).values({ floor: n, url: "/" });
|
|
|
|
},
|
|
|
|
rmfloor: async (event) => {
|
|
|
|
const formData = await event.request.formData();
|
|
|
|
const rmFloorNumber = await formData.get("number");
|
|
|
|
const n = Number(rmFloorNumber);
|
|
|
|
if (isNaN(n)) return fail(400, { message: "Invalid number!" });
|
|
|
|
|
|
|
|
const floors = await db.select().from(table.floors);
|
|
|
|
if (floors.length == 1) return fail(400, { message: "Cannot delete last floor!" });
|
|
|
|
|
2025-05-19 16:40:11 +02:00
|
|
|
const exists = await db
|
|
|
|
.select({ floor: table.floors.floor })
|
|
|
|
.from(table.floors)
|
|
|
|
.where(eq(table.floors.floor, n));
|
|
|
|
|
|
|
|
if (exists.length == 0) return fail(400, { message: "Floor " + n + " does not exist!" });
|
|
|
|
|
2025-05-18 17:21:58 +02:00
|
|
|
await db.delete(table.floors).where(eq(table.floors.floor, n));
|
|
|
|
},
|
2025-06-12 18:53:02 +02:00
|
|
|
savefloor: async (event) => {
|
|
|
|
const formData = await event.request.formData();
|
|
|
|
const floorNumber = formData.get("floorNumber");
|
|
|
|
const floorPlanImage = formData.get("floorPlanImage");
|
|
|
|
const devices = formData.get("devices");
|
|
|
|
|
|
|
|
const n = Number(floorNumber);
|
|
|
|
if (isNaN(n)) return fail(400, { message: "Invalid floor number!" });
|
|
|
|
|
|
|
|
if (!floorPlanImage || !devices) {
|
|
|
|
return fail(400, { message: "Missing floor plan or device configuration!" });
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const deviceData = JSON.parse(devices);
|
|
|
|
|
|
|
|
// Check if floor exists
|
|
|
|
const exists = await db
|
|
|
|
.select({ floor: table.floors.floor })
|
|
|
|
.from(table.floors)
|
|
|
|
.where(eq(table.floors.floor, n));
|
|
|
|
|
|
|
|
if (exists.length === 0) {
|
|
|
|
return fail(400, { message: `Floor ${n} does not exist! Please create it first.` });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update floor with configuration
|
|
|
|
// Note: In a real implementation, you would store this data properly
|
|
|
|
// For now, we'll just update the url field as a JSON string
|
|
|
|
const floorConfig = {
|
|
|
|
image: floorPlanImage,
|
|
|
|
devices: deviceData,
|
|
|
|
};
|
|
|
|
|
|
|
|
await db
|
|
|
|
.update(table.floors)
|
|
|
|
.set({ url: JSON.stringify(floorConfig) })
|
|
|
|
.where(eq(table.floors.floor, n));
|
|
|
|
|
|
|
|
return { success: true, message: `Floor ${n} configuration saved successfully!` };
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error saving floor configuration:", error);
|
|
|
|
return fail(500, { message: "Failed to save floor configuration!" });
|
|
|
|
}
|
|
|
|
},
|
2025-05-18 17:21:58 +02:00
|
|
|
};
|