57 lines
1.4 KiB
JavaScript
57 lines
1.4 KiB
JavaScript
import express from 'express'
|
|
rimport axios from 'axios'
|
|
import * as prep from '../charts/preparecharts.js'
|
|
|
|
const router = express.Router()
|
|
|
|
const disttable = [
|
|
{type: 'live', func: prep.liveData },
|
|
{type: 'havg', func: prep.havgData },
|
|
{type: 'davg', func: prep.davgData },
|
|
{type: 'daynight', func: prep.dayNightData },
|
|
{type: 'lden', func: prep.ldenData },
|
|
{type: 'map', func: prep.mapData },
|
|
]
|
|
|
|
let APIHOST = process.env.APIHOST || 'http://localhost:3004'
|
|
|
|
/* GET home page. */
|
|
router.get('/:cmd', async function(req, res, next) {
|
|
const cmd = req.params.cmd
|
|
let url = APIHOST
|
|
if(isNaN(cmd)) {
|
|
url += req.originalUrl
|
|
} else {
|
|
// cmd is a number ==> sid
|
|
url += `getsensordata?sid=${cmd}`
|
|
}
|
|
try {
|
|
const response = await axios(encodeURI(url));
|
|
if (response.status !== 200) {
|
|
res.json({err: `Error from servercall: status = ${response.status}`})
|
|
}
|
|
if(response.data.err) {
|
|
res.json(response.data)
|
|
return
|
|
}
|
|
const options = response.data.options
|
|
for(let x of disttable) {
|
|
if (x.type === options.data) {
|
|
let ret = x.func(options, response.data.values)
|
|
res.json(ret)
|
|
break;
|
|
}
|
|
}
|
|
} catch (e) {
|
|
res.json({err: `Error from servercall: ${e}`})
|
|
}
|
|
})
|
|
|
|
export default router
|
|
|
|
|
|
|
|
// ToDo
|
|
// Hier : getsensordata und getmapdata unterbringen und damit den externen Server aufrufen
|
|
|