Adds application
This commit is contained in:
commit
b2dc69142a
12 changed files with 1683 additions and 0 deletions
44
source/main.ts
Normal file
44
source/main.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import express from "express";
|
||||
import dotenv from 'dotenv';
|
||||
import * as fs from "node:fs";
|
||||
import {Agent} from "undici";
|
||||
|
||||
if (fs.existsSync(".env.dist")) {
|
||||
dotenv.config({ path: '.env.dist'});
|
||||
}
|
||||
|
||||
const URI = `${process.env.DOMAIN}/api/states`;
|
||||
|
||||
async function getSensorValue(sensor: string): Promise<number> {
|
||||
const result = await fetch(`${URI}/${sensor}`, {
|
||||
dispatcher: new Agent({
|
||||
connect: {
|
||||
rejectUnauthorized: false
|
||||
}
|
||||
}),
|
||||
headers: [
|
||||
['Authorization', `Bearer ${process.env.TOKEN}`]
|
||||
]
|
||||
});
|
||||
if (!result.ok) {
|
||||
console.error(result)
|
||||
return -1;
|
||||
}
|
||||
const data: unknown = await result.json();
|
||||
return parseInt(data.state);
|
||||
}
|
||||
|
||||
const server = express()
|
||||
|
||||
server.use("/", express.static("public"));
|
||||
|
||||
server.get('/get', async (req, res) => {
|
||||
const result = {
|
||||
temperature: await getSensorValue(process.env.TEMPERATURE_SENSOR ?? ''),
|
||||
moisture: await getSensorValue(process.env.MOISTURE_SENSOR ?? '')
|
||||
}
|
||||
|
||||
res.json(result)
|
||||
})
|
||||
|
||||
server.listen(8080);
|
||||
Loading…
Add table
Add a link
Reference in a new issue