feat: added moisture checker

This commit is contained in:
Elias Renman
2023-05-21 16:33:44 +02:00
parent bb9407c2fa
commit 028e825541
20 changed files with 454 additions and 756 deletions

30
server/src/cron/index.ts Normal file
View File

@@ -0,0 +1,30 @@
import cron from "node-cron";
import { iotClient } from "../axios/iot.axios";
import { moistureResponse } from "../axios/iot.types";
import { prisma } from "../prisma";
import { emitter } from "../eventemitter";
export function registerCronjobs() {
cron.schedule("* * * * *", async () => {
console.log("Checking moisture levels");
// Read moisture sensors
const { data } = await iotClient.post<moistureResponse>("moisture");
const rows = [];
for (const [key, value] of Object.entries(data)) {
const row = await prisma.moistureValue.create({
data: {
name: key,
value,
createdAt: new Date(),
},
select: { createdAt: true, id: true, name: true, value: true },
});
rows.push(row);
}
// Emit event
emitter.emit("moisture.updated", rows);
});
}