mirror of
https://github.com/eliasrenman/gardentron.git
synced 2026-03-17 04:56:06 +01:00
feat: outline of processor
This commit is contained in:
@@ -34,6 +34,8 @@ function readMoistureLevels() {
|
||||
});
|
||||
}
|
||||
|
||||
export type MositureRow = Awaited<ReturnType<typeof insertRows>>[number];
|
||||
|
||||
async function insertRows(data: MoistureResponse | undefined) {
|
||||
if (!data) {
|
||||
return [];
|
||||
|
||||
30
server/src/processors/moisture.processor.ts
Normal file
30
server/src/processors/moisture.processor.ts
Normal 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 }));
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user