73 lines
2.1 KiB
JavaScript
73 lines
2.1 KiB
JavaScript
import express from 'express'
|
|
import axios from 'axios'
|
|
import * as prep from '../charts/preparecharts.js'
|
|
import { getLanguage } from '../charts/utilities.js'
|
|
import { translate as trans } from '../routes/index.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:3005'
|
|
const API_KEY = process.env.API_KEY
|
|
|
|
|
|
/* GET home page. */
|
|
router.get('/:cmd', async function(req, res, next) {
|
|
// req.i18n.changeLanguage(req.query.lng)
|
|
const calledAs = req.baseUrl
|
|
const cmd = req.params.cmd
|
|
const pretty = (req.query.pretty !== undefined)
|
|
const lng = getLanguage()
|
|
|
|
let url = APIHOST + '/api' + req.originalUrl.slice(4) + `&lng=${lng}`
|
|
try {
|
|
const response = await axios.get(encodeURI(url) , {
|
|
headers: {'X-API-Key': API_KEY}
|
|
});
|
|
if (response.status !== 200) {
|
|
res.json({err: `${trans('ESYSCALL')} status=${response.status}`})
|
|
}
|
|
if(response.data.err) {
|
|
res.json(response.data)
|
|
return
|
|
}
|
|
// if called as '/api' then directly return the data
|
|
if ((calledAs === '/api') || (cmd === 'getoneproperty') || (cmd === 'getaddress') || (cmd === 'getcitycoords')) {
|
|
if(pretty) {
|
|
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
|
res.send(JSON.stringify(response.data,null,2))
|
|
} else {
|
|
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
|
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, req)
|
|
res.json(ret)
|
|
break;
|
|
}
|
|
}
|
|
} catch (e) {
|
|
res.json({err: `${trans('ESYSCALL')} ${e}`})
|
|
}
|
|
})
|
|
|
|
export default router
|
|
|
|
|
|
|
|
// ToDo
|
|
// Hier : getsensordata und getmapdata unterbringen und damit den externen Server aufrufen
|
|
|