chore: updated proccesing

This commit is contained in:
Elias Renman
2023-07-03 11:10:55 +02:00
parent b670057698
commit 01d3701a50
2 changed files with 26 additions and 16 deletions

View File

@@ -1,12 +1,12 @@
{ {
"water_low": false, "water_low": true,
"timeout": 120, "timeout": 120,
"check_interval": 2.5, "check_interval": 60,
"threshold": 6, "threshold": 3.5,
"moisture": { "moisture": {
"thresholds": { "thresholds": {
"lower": { "lower": {
"sensor_0": 60, "sensor_0": 67,
"sensor_1": 50, "sensor_1": 50,
"sensor_2": 50 "sensor_2": 50
}, },

View File

@@ -3,6 +3,7 @@ import { iotClient } from "../axios/iot.axios";
import { config, logger } from "../config"; import { config, logger } from "../config";
import { MositureRow, readMoistureLevels } from "../cron"; import { MositureRow, readMoistureLevels } from "../cron";
import { emitter } from "../eventemitter"; import { emitter } from "../eventemitter";
import { AxiosError } from "axios";
const queue = new Queue({ results: [], concurrency: 1 }); const queue = new Queue({ results: [], concurrency: 1 });
@@ -18,7 +19,7 @@ export function checkReadingAndEnqueue(rows: MositureRow[]) {
// Conclude which sensors need watering. // Conclude which sensors need watering.
if (row.value.toNumber() <= threshold) { if (row.value.toNumber() <= threshold) {
logger.info(`Enqueing ${row.name}`); logger.info(`Enqueing ${row.name} Value at ${row.value.toNumber()}%`);
// Enqueue relevant sensors // Enqueue relevant sensors
queue.push((cb) => queue.push((cb) =>
process(row) process(row)
@@ -40,15 +41,19 @@ async function process(row: MositureRow) {
try { try {
// Enable water gate // Enable water gate
await toggleWater(index, true); await toggleWater(index, true);
await sleep(60 * 1000); logger.info("Watering for 5 minutes before checking water level again");
await sleep(60 * 1000 * 5);
// Check moisture level once per second for configured time // Check moisture level once per second for configured time
await Promise.race([ await Promise.race([
sleep(config.config.timeout * 1000), // sleep(config.config.timeout * 1000),
timeoutCb(row.name, row.value.toNumber()), timeoutCb(row.name, row.value.toNumber()),
]); ]);
} catch (e) { } catch (e) {
logger.info(`Failed to processing of ${row.name}`); logger.info(`Failed to processing of ${row.name}`);
logger.info(e); logger.info(e);
if (e instanceof AxiosError) {
console.log(e.response?.data);
}
} finally { } finally {
// Finally turn of the water gate // Finally turn of the water gate
await toggleWater(index, false); await toggleWater(index, false);
@@ -81,7 +86,7 @@ async function timeoutCb(name: string, initalRead: number): Promise<void> {
}; };
// Emit the the event that the confighas been updated // Emit the the event that the confighas been updated
emitter.emit("config.water_low"); emitter.emit("config.water_low");
return; // return;
} }
const upperThreshold = const upperThreshold =
initalRead + initalRead +
@@ -93,21 +98,26 @@ async function timeoutCb(name: string, initalRead: number): Promise<void> {
return; return;
} }
// If moisture level reaches upper threshold then cancel out of the checking loop // If moisture level reaches upper threshold then cancel out of the checking loop
logger.info(
`Calling check water level in ${config.config.check_interval} seconds`
);
await sleep(config.config.check_interval * 1000); await sleep(config.config.check_interval * 1000);
return timeoutCb(name, initalRead); return timeoutCb(name, initalRead);
} }
function toggleWater(index: string, on: boolean) { function toggleWater(index: string, on: boolean) {
return iotClient return iotClient
.post(`/relay/${on ? "activate" : "deactivate"}`, { .post(
`/relay/${on ? "activate" : "deactivate"}`,
{ relay: +index },
{
headers: { headers: {
Accept: "application/json",
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
method: "POST", }
body: JSON.stringify({ relay: +index }), )
})
.then(async (item) => { .then(async (item) => {
console.log(item.status, item.data);
logger.info(`Successfully ${on ? "activate" : "deactivate"}d water gate`); logger.info(`Successfully ${on ? "activate" : "deactivate"}d water gate`);
return { on }; return { on };
}); });