feat: outline of processor

This commit is contained in:
Elias Renman
2023-06-17 12:58:16 +02:00
parent 90c1d1fb5f
commit 668524832e
8 changed files with 226 additions and 182 deletions

View File

@@ -34,6 +34,8 @@ function readMoistureLevels() {
});
}
export type MositureRow = Awaited<ReturnType<typeof insertRows>>[number];
async function insertRows(data: MoistureResponse | undefined) {
if (!data) {
return [];

View File

@@ -0,0 +1,30 @@
// import Queue from "queue";
import { Prisma } from "@prisma/client";
import { iotClient } from "../axios/iot.axios";
import { MositureRow } from "../cron";
// const q = new Queue({ results: [] });
export function checkReadingAndEnqueue(rows: MositureRow[]) {
// Read config for threshold values
// Conclude which sensors need watering.
// Enqueue relevant
}
function enqueue() {}
function process() {
// Enable water gate
// Check moisture level once per second for configured time
// If the moisture level does not increase a significant amount trigger water-empty alarm and clear queue
// If moisture level reaches upper threshold then cancel out of the checking loop
// Finally turn of the water gate
}
function timeoutCb() {}
function toggleWater(index: number, on: boolean) {
return iotClient
.post(`relay/${on ? "activate" : "deactivate"}`, {
relay: index,
})
.then((item) => ({ on: true }));
}

View File

@@ -3,6 +3,8 @@ import { DefaultEventsMap } from "socket.io/dist/typed-events";
import { SocketHandler } from "./sockets";
import { emitter } from "../eventemitter";
import { prisma } from "../prisma";
import { MositureRow } from "../cron";
import { checkReadingAndEnqueue } from "../processors/moisture.processor";
export class MoistureSocket extends SocketHandler {
constructor(server: Server) {
@@ -11,9 +13,10 @@ export class MoistureSocket extends SocketHandler {
this.server.on("getMoisture", this.getMoisture);
this.server.on("getAllMoistures", this.getAllMoistures);
emitter.on("moisture.updated", (...rows: any) => {
emitter.on("moisture.updated", (...rows: MositureRow[]) => {
console.log("Moisture updated event listener called");
server.emit("moisture.updated", rows);
checkReadingAndEnqueue(rows);
});
}