110 lines
3.4 KiB
JavaScript
110 lines
3.4 KiB
JavaScript
import { db } from "$lib/server/db";
|
|
import * as table from "$lib/server/db/schema";
|
|
import { getCurrentDevices } from "$lib/server/mqtt-devices.js";
|
|
import { fail } from "@sveltejs/kit";
|
|
import { eq } from "drizzle-orm";
|
|
|
|
export const load = async (event) => {
|
|
const floors = await db
|
|
.select({ floor: table.floors.floor })
|
|
.from(table.floors)
|
|
.orderBy(table.floors.floor);
|
|
|
|
let devices = getCurrentDevices();
|
|
|
|
if (devices.length === 0) {
|
|
devices = [
|
|
{
|
|
id: "no-devices",
|
|
name: "No ESP8266 devices connected",
|
|
type: "esp8266",
|
|
status: "offline",
|
|
},
|
|
];
|
|
}
|
|
|
|
return {
|
|
floors: floors.map((f) => f.floor),
|
|
devices: devices,
|
|
};
|
|
};
|
|
|
|
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)
|
|
.where(eq(table.floors.floor, n));
|
|
|
|
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!" });
|
|
|
|
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!" });
|
|
|
|
await db.delete(table.floors).where(eq(table.floors.floor, n));
|
|
},
|
|
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);
|
|
deviceData.forEach(async (dev) => {
|
|
await db.insert(table.sensors).values({ id: dev.id, user: event.locals.session.userId });
|
|
});
|
|
|
|
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.` });
|
|
}
|
|
|
|
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!" });
|
|
}
|
|
},
|
|
};
|