feat: added config

This commit is contained in:
Elias Renman
2023-06-17 13:17:40 +02:00
parent 668524832e
commit 05da42f29f
4 changed files with 75 additions and 0 deletions

18
server/config.json Normal file
View File

@@ -0,0 +1,18 @@
{
"timeout": 30,
"threshold": 6,
"moisture": {
"thresholds": {
"lower": {
"0": 20,
"1": 20,
"2": 20
},
"upper": {
"0": 70,
"1": 70,
"2": 70
}
}
}
}

View File

@@ -0,0 +1,18 @@
{
"timeout": 30,
"threshold": 6,
"moisture": {
"thresholds": {
"lower": {
"0": 20,
"1": 20,
"2": 20
},
"upper": {
"0": 70,
"1": 70,
"2": 70
}
}
}
}

25
server/src/config.ts Normal file
View File

@@ -0,0 +1,25 @@
import { readFileSync, writeFileSync } from "fs";
import { join, resolve } from "path";
import { Configuration } from "./config.types";
export class Config<T extends Record<string, any>> {
private _json!: T;
constructor(private path: string) {
this._json = JSON.parse(readFileSync(path).toString());
}
public get config() {
return this._json;
}
public set config(val: T) {
writeFileSync(this.path, JSON.stringify(val, null, 2));
this._json = val;
}
}
export const config = new Config<Configuration>(
resolve(join(__dirname, "../config.json"))
);

View File

@@ -0,0 +1,14 @@
export type Configuration = {
timeout: number;
threshold: number;
moisture: Moisture;
};
export type Moisture = {
thresholds: Thresholds;
};
export type Thresholds = {
lower: { [key: string]: number };
upper: { [key: string]: number };
};