From d9cdcfa7f40bc0ed138b6b5f86031ae4714cede9 Mon Sep 17 00:00:00 2001 From: Elias Renman Date: Sat, 17 Jun 2023 13:27:26 +0200 Subject: [PATCH] feat: enqueueing of moisture sensor --- server/src/processors/moisture.processor.ts | 26 ++++++++++++++------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/server/src/processors/moisture.processor.ts b/server/src/processors/moisture.processor.ts index 65c1673..bc0ed09 100644 --- a/server/src/processors/moisture.processor.ts +++ b/server/src/processors/moisture.processor.ts @@ -1,18 +1,28 @@ -// import Queue from "queue"; - +import Queue from "queue"; import { Prisma } from "@prisma/client"; import { iotClient } from "../axios/iot.axios"; import { MositureRow } from "../cron"; +import { config } from "../config"; + +const queue = new Queue({ results: [] }); + +queue.start().then((result) => console.log("Successfully started queue")); -// const q = new Queue({ results: [] }); export function checkReadingAndEnqueue(rows: MositureRow[]) { - // Read config for threshold values - // Conclude which sensors need watering. - // Enqueue relevant + rows.forEach((row) => { + // Read config for threshold values + const threshold = config.config.moisture.thresholds.lower[row.name]; + // Conclude which sensors need watering. + if (row.value.toNumber() >= threshold) { + console.log(`Enqueing ${row.name}`); + // Enqueue relevant sensors + queue.push(() => process(row)); + } + }); } -function enqueue() {} -function process() { +function process(row: MositureRow) { + console.log("Started processing of", row.name); // 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