144 lines
3.5 KiB
JavaScript
144 lines
3.5 KiB
JavaScript
const express = require('express');
|
|||
|
|
const compression = require('compression');
|
||
|
|
const util = require('util');
|
||
|
|
const fs = require('fs');
|
||
|
|
const path = require('path');
|
||
|
|
const os = require('os');
|
||
|
|
const exec = util.promisify(require('child_process').exec);
|
||
|
|
const readFileAsync = util.promisify(fs.readFile)
|
||
|
|
|
||
|
|
|
||
|
|
const app = express(); // app is an instance of express
|
||
|
|
|
||
|
|
app.use(compression()); // enables gzip compression
|
||
|
|
app.options('/dvbStatus', (req, res) => {
|
||
|
|
res.set({
|
||
|
|
'Access-Control-Allow-Origin': '*',
|
||
|
|
'Access-Control-Allow-Methods': ' GET, OPTIONS',
|
||
|
|
'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('/media/hd/data/projects/dvb-t-status');
|
||
|
|
const { stdout, stderr } = await exec('DTV');
|
||
|
|
let i = parseInt(stdout);
|
||
|
|
i = i*1e-6; // i is in Hz; divide by 1M to have MHz
|
||
|
|
res.send({
|
||
|
|
"id": "dvb-t-status",
|
||
|
|
"data": `Frontend Frequency: ${i} Mhz`,
|
||
|
|
"stderr": stderr
|
||
|
|
});
|
||
|
|
|
||
|
|
});
|
||
|
|
|
||
|
|
|
||
|
|
app.options('/dvbChannel', (req, res) => {
|
||
|
|
res.set({
|
||
|
|
'Access-Control-Allow-Origin': '*',
|
||
|
|
'Access-Control-Allow-Methods': ' GET, OPTIONS',
|
||
|
|
'Access-Control-Allow-Headers': 'Content-Type',
|
||
|
|
'Access-Control-Max-Age': 600
|
||
|
|
})
|
||
|
|
res.status(200).end();
|
||
|
|
})
|
||
|
|
|
||
|
|
app.get('/dvbChannel', async (req, res) => {
|
||
|
|
res.set({
|
||
|
|
'Access-Control-Allow-Origin': '*'
|
||
|
|
});
|
||
|
|
const { stdout, stderr } = await exec('DTV');
|
||
|
|
let i = parseInt(stdout);
|
||
|
|
i = i*1e-6; // i is in Hz; divide by 1M to have MHz
|
||
|
|
readFileAsync(path.join(__dirname,"frequencies.json"))
|
||
|
|
.then(res2 => {
|
||
|
|
const db = JSON.parse(res2);
|
||
|
|
//console.log(db[i]);
|
||
|
|
res.send({
|
||
|
|
"id": `dvb-t-channel ${db[i]["name"]}`,
|
||
|
|
"data": db[i]["channels"].toString(),
|
||
|
|
"stderr": stderr
|
||
|
|
});
|
||
|
|
})
|
||
|
|
.catch(err => console.error(err))
|
||
|
|
|
||
|
|
|
||
|
|
});
|
||
|
|
|
||
|
|
app.options('/internetCheck', async (req, res) => {
|
||
|
|
res.set({
|
||
|
|
'Access-Control-Allow-Origin': '*',
|
||
|
|
'Access-Control-Allow-Methods': ' GET, OPTIONS',
|
||
|
|
'Access-Control-Allow-Headers': 'Content-Type',
|
||
|
|
'Access-Control-Max-Age': 600
|
||
|
|
})
|
||
|
|
res.status(200).end();
|
||
|
|
})
|
||
|
|
app.get('/internetCheck', async (req, res) => {
|
||
|
|
res.set({
|
||
|
|
'Access-Control-Allow-Origin': '*'
|
||
|
|
});
|
||
|
|
const { stdout, stderr } = await exec('/media/hd/data/projects/icanhazip');
|
||
|
|
res.send(
|
||
|
|
{
|
||
|
|
"id": "internet-check",
|
||
|
|
"data": stdout,
|
||
|
|
"stderr": stderr
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
});
|
||
|
|
|
||
|
|
app.options('/sysStatus', (req, res) => {
|
||
|
|
res.set({
|
||
|
|
'Access-Control-Allow-Origin': '*',
|
||
|
|
'Access-Control-Allow-Methods': ' GET, OPTIONS',
|
||
|
|
'Access-Control-Allow-Headers': 'Content-Type',
|
||
|
|
'Access-Control-Max-Age': 600
|
||
|
|
})
|
||
|
|
res.status(200).end();
|
||
|
|
})
|
||
|
|
|
||
|
|
app.get('/sysStatus', async (req, res) => {
|
||
|
|
res.set({
|
||
|
|
'Access-Control-Allow-Origin': '*'
|
||
|
|
});
|
||
|
|
readFileAsync('/sys/class/thermal/thermal_zone0/temp')
|
||
|
|
.then(res2=>
|
||
|
|
{
|
||
|
|
res.send(
|
||
|
|
{
|
||
|
|
"id": "sys-stats",
|
||
|
|
"data": `Temp: ${parseFloat(res2)*1e-3.toFixed(2)} °C Load Avg: ${os.loadavg().map(x=>x.toFixed(2)).toString()}`,
|
||
|
|
"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'));
|