mirror of
https://github.com/eliasrenman/gardentron.git
synced 2026-03-16 20:46:07 +01:00
fix: read moisture level
This commit is contained in:
Binary file not shown.
@@ -1,10 +1,18 @@
|
|||||||
/**
|
export type MoistureReading = {
|
||||||
* A number between 0 and 100 to represent precentages
|
/**
|
||||||
*/
|
* A number between 0 and 100 to represent precentages
|
||||||
export type moisturePrecentage = number;
|
*/
|
||||||
|
precentage: number;
|
||||||
export type moistureResponse = {
|
/**
|
||||||
sensor_0: moisturePrecentage;
|
* Raw read
|
||||||
sensor_1: moisturePrecentage;
|
*/
|
||||||
sensor_2: moisturePrecentage;
|
read: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type MoistureResponse = {
|
||||||
|
data: {
|
||||||
|
sensor_0: MoistureReading;
|
||||||
|
sensor_1: MoistureReading;
|
||||||
|
sensor_2: MoistureReading;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,30 +1,54 @@
|
|||||||
import cron from "node-cron";
|
import cron from "node-cron";
|
||||||
import { iotClient } from "../axios/iot.axios";
|
import { iotClient } from "../axios/iot.axios";
|
||||||
import { moistureResponse } from "../axios/iot.types";
|
import { MoistureResponse } from "../axios/iot.types";
|
||||||
import { prisma } from "../prisma";
|
import { prisma } from "../prisma";
|
||||||
import { emitter } from "../eventemitter";
|
import { emitter } from "../eventemitter";
|
||||||
|
import { AxiosError } from "axios";
|
||||||
|
|
||||||
export function registerCronjobs() {
|
export function registerCronjobs() {
|
||||||
cron.schedule("* * * * *", async () => {
|
cron.schedule("* * * * *", async () => {
|
||||||
console.log("Checking moisture levels");
|
console.log("Checking moisture levels");
|
||||||
|
|
||||||
// Read moisture sensors
|
// Read moisture sensors
|
||||||
const { data } = await iotClient.post<moistureResponse>("moisture");
|
try {
|
||||||
const rows = [];
|
const { data } = await readMoistureLevels();
|
||||||
for (const [key, value] of Object.entries(data)) {
|
const rows = await insertRows(data);
|
||||||
const row = await prisma.moistureValue.create({
|
|
||||||
|
console.log("Successfully checked moisture levels");
|
||||||
|
if (rows.length === 0) return;
|
||||||
|
// Emit event
|
||||||
|
emitter.emit("moisture.updated", ...rows);
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function readMoistureLevels() {
|
||||||
|
return iotClient
|
||||||
|
.get<MoistureResponse | undefined>("moisture/read")
|
||||||
|
.catch((err: AxiosError) => {
|
||||||
|
return {
|
||||||
|
data: undefined,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function insertRows(data: MoistureResponse | undefined) {
|
||||||
|
if (!data) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.all(
|
||||||
|
Object.entries(data.data).map(([key, value]) =>
|
||||||
|
prisma.moistureValue.create({
|
||||||
data: {
|
data: {
|
||||||
name: key,
|
name: key,
|
||||||
value,
|
value: value.precentage,
|
||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
},
|
},
|
||||||
select: { createdAt: true, id: true, name: true, value: true },
|
select: { createdAt: true, id: true, name: true, value: true },
|
||||||
});
|
})
|
||||||
|
)
|
||||||
rows.push(row);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
// Emit event
|
|
||||||
emitter.emit("moisture.updated", rows);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { prisma } from "./prisma";
|
|||||||
|
|
||||||
startServer()
|
startServer()
|
||||||
.then(async () => {
|
.then(async () => {
|
||||||
await prisma.$disconnect();
|
await prisma.$connect();
|
||||||
})
|
})
|
||||||
.catch(async (e) => {
|
.catch(async (e) => {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
|
|||||||
@@ -4,7 +4,11 @@ import { registerCronjobs } from "./cron";
|
|||||||
|
|
||||||
export async function startServer() {
|
export async function startServer() {
|
||||||
const port = 3000 || process.env.HTTP_PORT;
|
const port = 3000 || process.env.HTTP_PORT;
|
||||||
const io = new Server({});
|
const io = new Server({
|
||||||
|
cors: {
|
||||||
|
origin: "*",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
registerSockets(io);
|
registerSockets(io);
|
||||||
registerCronjobs();
|
registerCronjobs();
|
||||||
|
|||||||
@@ -7,7 +7,12 @@ import { prisma } from "../prisma";
|
|||||||
export class MoistureSocket extends SocketHandler {
|
export class MoistureSocket extends SocketHandler {
|
||||||
constructor(server: Server) {
|
constructor(server: Server) {
|
||||||
super(server);
|
super(server);
|
||||||
|
|
||||||
|
this.server.on("getMoisture", this.getMoisture);
|
||||||
|
this.server.on("getAllMoistures", this.getAllMoistures);
|
||||||
|
|
||||||
emitter.on("moisture.updated", (...rows: any) => {
|
emitter.on("moisture.updated", (...rows: any) => {
|
||||||
|
console.log("Moisture updated event listener called");
|
||||||
server.emit("moisture.updated", rows);
|
server.emit("moisture.updated", rows);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,24 @@
|
|||||||
import io, { Server, Socket } from "socket.io";
|
import { Server, Socket } from "socket.io";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers methods to socket.io server passed in constructors.
|
||||||
|
* All methods will be registered unless it is prefixed with "_".
|
||||||
|
*/
|
||||||
export abstract class SocketHandler {
|
export abstract class SocketHandler {
|
||||||
constructor(private readonly server: Server) {
|
constructor(protected readonly server: Server) {
|
||||||
const ignoredMethods = ["connection"];
|
const ignoredMethods: string[] = ["constructor"];
|
||||||
|
|
||||||
for (const [key, value] of Object.entries(this)) {
|
const prototype = Object.getPrototypeOf(this);
|
||||||
if (typeof value === "function" && !ignoredMethods.includes(key)) {
|
const methodNames = Object.getOwnPropertyNames(prototype).filter(
|
||||||
this.server.on(key, value);
|
(name) => typeof prototype[name] === "function"
|
||||||
}
|
);
|
||||||
}
|
|
||||||
|
methodNames.forEach((methodName) => {
|
||||||
|
if (methodName.startsWith("_")) return;
|
||||||
|
if (ignoredMethods.includes(methodName)) return;
|
||||||
|
|
||||||
|
this.server.on(methodName, prototype[methodName]);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract connection(socket: Socket): void;
|
abstract connection(socket: Socket): void;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user