Ertser Commit der test-Version
This commit is contained in:
54
routes/api.js
Normal file
54
routes/api.js
Normal file
@@ -0,0 +1,54 @@
|
||||
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)
|
||||
})
|
||||
12
routes/index.js
Normal file
12
routes/index.js
Normal file
@@ -0,0 +1,12 @@
|
||||
import express from 'express'
|
||||
import { translate as trans } from '../routes/api.js'
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
/* GET home page. */
|
||||
|
||||
router.get('/', function(req, res, next) {
|
||||
res.status(200).json({message: trans('NOTHING')})
|
||||
});
|
||||
|
||||
export default router
|
||||
Reference in New Issue
Block a user