55 lines
1.5 KiB
JavaScript
55 lines
1.5 KiB
JavaScript
import express from 'express'
|
|
|
|
import {getData4map} from "../actions/data4map.js"
|
|
import * as getData from "../actions/getsensorData.js";
|
|
import * as getProps from "../actions/getproperties.js";
|
|
import * as getAKWs from "../actions/getAKWData.js";
|
|
import * as holAddr from "../actions/getaddress.js";
|
|
|
|
export const apiRouter = express.Router();
|
|
|
|
const cmdTable = [
|
|
{cmd: 'getactdata', func: getData.getActData},
|
|
{cmd: 'getlongavg', func: getData.getLongAvg},
|
|
{cmd: 'getavgdata', func: getData.getAvgData},
|
|
{cmd: 'getoneproperty', func: getProps.getOneProperty},
|
|
{cmd: 'getakwdata', func: getAKWs.getakwdata},
|
|
{cmd: 'getaddress', func: holAddr.getAddress},
|
|
{cmd: 'getcitycoords', func: holAddr.getCityCoords},
|
|
{cmd: 'getsensordata', func: getData.getSensorData},
|
|
{cmd: 'getmapdata', func: getData4map}
|
|
]
|
|
|
|
let i18n;
|
|
|
|
export const translate = (x) => {
|
|
return i18n.t(x)
|
|
}
|
|
|
|
export const dispatchCommand = async (cmd, table, params, res) => {
|
|
let notfound = true
|
|
for (let c of table) {
|
|
if (c.cmd === cmd) {
|
|
notfound = false
|
|
let erg = await c.func(params)
|
|
if (typeof erg === 'string') {
|
|
res.type('text')
|
|
res.send(erg)
|
|
} else {
|
|
res.json(erg)
|
|
}
|
|
}
|
|
}
|
|
if (notfound) {
|
|
res.json({err: translate('CMNDUNKNOWN')})
|
|
}
|
|
}
|
|
|
|
// normal routes called from javascript client
|
|
apiRouter.get('/:cmd', async (req, res) => {
|
|
const params = req.query
|
|
params.chart = false
|
|
i18n = req.i18n
|
|
await dispatchCommand(req.params.cmd, cmdTable, params, res)
|
|
})
|