mirror of
https://github.com/eliasrenman/gardentron.git
synced 2026-03-16 20:46:07 +01:00
feat: added config
This commit is contained in:
18
server/config.json
Normal file
18
server/config.json
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
18
server/config.json.example
Normal file
18
server/config.json.example
Normal 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
25
server/src/config.ts
Normal 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"))
|
||||
);
|
||||
14
server/src/config.types.ts
Normal file
14
server/src/config.types.ts
Normal 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 };
|
||||
};
|
||||
Reference in New Issue
Block a user