scheme for saving sensor data

This commit is contained in:
David Senoner 2025-06-11 20:33:05 +02:00
parent a44284b9b1
commit 5935064606
3 changed files with 23 additions and 1 deletions

View file

@ -1,4 +1,4 @@
import { integer, json, pgTable, text, timestamp } from "drizzle-orm/pg-core";
import { integer, json, pgTable, real, text, timestamp, uuid } from "drizzle-orm/pg-core";
export const users = pgTable("users", {
id: text().primaryKey(),
@ -18,9 +18,28 @@ export const sessions = pgTable("sessions", {
export const floors = pgTable("floors", {
floor: integer().primaryKey(),
url: text().notNull(),
image: text(),
});
export const plans = pgTable("plans", {
floor: integer().primaryKey(),
plan: json().notNull(),
});
export const sensors = pgTable("sensors", {
id: text().primaryKey(),
user: text()
.references(() => users.id)
.notNull(),
});
export const sensorData = pgTable("sensor_data", {
uuid: uuid().primaryKey(),
sensor: text("sensor")
.references(() => sensors.id)
.notNull(),
temperature: real().notNull(),
humidity: real().notNull(),
pressure: real().notNull(),
altitude: real().notNull(),
});

View file

@ -2,6 +2,7 @@ import { db } from "$lib/server/db";
import * as table from "$lib/server/db/schema";
import { eq } from "drizzle-orm";
import type { PageServerLoad } from "./$types";
import { connect } from "mqtt";
export const load: PageServerLoad = async ({ params }) => {
{

View file

@ -6,6 +6,7 @@ import { writable } from "svelte/store";
// In a real application, you might want to store more data or
// use a more robust way to manage messages, but for a basic example, this works.
const latestMessage = writable("No message yet");
const devices = writable([]);
let client = null;
@ -36,6 +37,7 @@ function connectMqtt() {
const payload = message.toString();
console.log(`Received message from topic "${topic}": ${payload}`);
latestMessage.set(payload); // Update the Svelte store
console.log(topic.split("/")[1]);
});
client.on("error", (err) => {