2020-06-15 20:24:56 +02:00
|
|
|
const express = require('express');
|
|
|
|
|
const compression = require('compression');
|
|
|
|
|
const util = require('util');
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
const path = require('path');
|
|
|
|
|
const os = require('os');
|
2020-06-16 01:25:22 +02:00
|
|
|
const http = require("http");
|
2020-06-15 20:24:56 +02:00
|
|
|
|
2020-06-16 00:34:38 +02:00
|
|
|
const exec = util.promisify(require('child_process').exec);
|
|
|
|
|
const readFileAsync = util.promisify(fs.readFile);
|
2020-06-15 20:24:56 +02:00
|
|
|
|
|
|
|
|
const app = express(); // app is an instance of express
|
|
|
|
|
|
|
|
|
|
app.use(compression()); // enables gzip compression
|
2020-06-16 00:34:38 +02:00
|
|
|
|
|
|
|
|
app.options('*', (req, res) => {
|
2020-06-15 20:24:56 +02:00
|
|
|
res.set({
|
|
|
|
|
'Access-Control-Allow-Origin': '*',
|
2020-06-16 00:34:38 +02:00
|
|
|
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
2020-06-15 20:24:56 +02:00
|
|
|
'Access-Control-Allow-Headers': 'Content-Type',
|
|
|
|
|
'Access-Control-Max-Age': 600
|
|
|
|
|
})
|
|
|
|
|
res.status(200).end();
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
app.get('/dvbStatus', async (req, res) => {
|
|
|
|
|
res.set({
|
|
|
|
|
'Access-Control-Allow-Origin': '*'
|
|
|
|
|
});
|
|
|
|
|
const { stdout, stderr } = await exec('DTV');
|
2020-06-16 00:34:38 +02:00
|
|
|
const i = parseInt(stdout) * 1e-6; // i is in Hz; divide by 1M to have MHz
|
2020-06-15 20:24:56 +02:00
|
|
|
res.send({
|
|
|
|
|
"id": "dvb-t-status",
|
|
|
|
|
"data": `Frontend Frequency: ${i} Mhz`,
|
|
|
|
|
"stderr": stderr
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('/dvbChannel', async (req, res) => {
|
|
|
|
|
res.set({
|
|
|
|
|
'Access-Control-Allow-Origin': '*'
|
|
|
|
|
});
|
|
|
|
|
const { stdout, stderr } = await exec('DTV');
|
2020-06-16 00:34:38 +02:00
|
|
|
const i = parseInt(stdout) * 1e-6; // i is in Hz; divide by 1M to have MHz
|
2020-06-15 20:24:56 +02:00
|
|
|
readFileAsync(path.join(__dirname,"frequencies.json"))
|
|
|
|
|
.then(res2 => {
|
|
|
|
|
const db = JSON.parse(res2);
|
|
|
|
|
res.send({
|
2020-06-16 00:34:38 +02:00
|
|
|
"id": `dvb-t-channel`,
|
|
|
|
|
"name": db[i]["name"].toString(),
|
2020-06-15 20:24:56 +02:00
|
|
|
"data": db[i]["channels"].toString(),
|
|
|
|
|
"stderr": stderr
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.catch(err => console.error(err))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('/internetCheck', async (req, res) => {
|
|
|
|
|
res.set({
|
|
|
|
|
'Access-Control-Allow-Origin': '*'
|
|
|
|
|
});
|
2020-06-16 01:25:22 +02:00
|
|
|
http.get('http://icanhazip.com', (httpRes) => {
|
|
|
|
|
httpRes.setEncoding('utf8');
|
|
|
|
|
httpRes.on('data', (body) => {
|
|
|
|
|
res.send(
|
|
|
|
|
{
|
|
|
|
|
"id": "internet-check",
|
|
|
|
|
"data": body,
|
|
|
|
|
"stderr": ""
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
});
|
|
|
|
|
});
|
2020-06-15 20:24:56 +02:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2020-06-16 01:25:22 +02:00
|
|
|
app.get('/sysStatus', (req, res) => {
|
2020-06-15 20:24:56 +02:00
|
|
|
res.set({
|
|
|
|
|
'Access-Control-Allow-Origin': '*'
|
|
|
|
|
});
|
|
|
|
|
readFileAsync('/sys/class/thermal/thermal_zone0/temp')
|
|
|
|
|
.then(res2=>
|
|
|
|
|
{
|
|
|
|
|
res.send(
|
|
|
|
|
{
|
|
|
|
|
"id": "sys-stats",
|
2020-06-16 00:34:38 +02:00
|
|
|
"data": `Temp: ${(parseFloat(res2)*1e-3).toFixed(2)} °C Load Avg: ${os.loadavg().map(x=>x.toFixed(2)).toString()}`,
|
2020-06-15 20:24:56 +02:00
|
|
|
"stderr": ""
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.catch(err=> {
|
|
|
|
|
console.error(err)
|
|
|
|
|
res.send(
|
|
|
|
|
{
|
|
|
|
|
"id": "sys-stats",
|
|
|
|
|
"data": `Load Avg: ${os.loadavg().map(x=>x.toFixed(2)).toString()}`,
|
|
|
|
|
"stderr": err
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.use(express.static('/var/www/default')); // serve static build site
|
|
|
|
|
|
|
|
|
|
// app.get('*', (req, res) => {
|
|
|
|
|
// res.sendFile(__dirname + '/public/index.html');
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
//app.listen(8000,"192.168.10.14", () => console.log('listen on 8000'));
|
|
|
|
|
app.listen(process.env.NODE_PORT,'127.0.0.80', () => console.log('listen on 3000'));
|