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