diff --git a/actions/data4map.js b/actions/data4map.js index d21aeda..7911d50 100644 --- a/actions/data4map.js +++ b/actions/data4map.js @@ -67,7 +67,7 @@ const readLastDates = async (typ) => { return await fetchFromInflux(ret, query) } -export const getData4map = async (params) => { +export var getData4map = async (params) => { let start = DateTime.now() let ret = {err: null} diff --git a/actions/getsensorData.js b/actions/getsensorData.js index cdd59d3..45142b5 100644 --- a/actions/getsensorData.js +++ b/actions/getsensorData.js @@ -104,7 +104,8 @@ export const calcRange = (opts) => { // return: // Returns from the special routines // ********************************************* -export const getSensorData = async (params) => { +// export const getSensorData = async (params) => { +export async function getSensorData(params) { let ret = {err: null} let {opts, err} = checkParams(params, { // check for sensorid mandatory: [{name: 'sensorid', type: 'int'}], @@ -116,7 +117,7 @@ export const getSensorData = async (params) => { // with the sensorid get the type of this sensor let erg = await getOneProperty({sensorid: opts.sensorid}) if (erg.err) { - return returnOnError(ret, err1, getSensorData.name) + return returnOnError(ret, erg.err, getSensorData.name) } // distribute to the right routine @@ -132,7 +133,8 @@ export const getSensorData = async (params) => { - export const getActData = async (opts) => { +// export const getActData = async (opts) => { +export async function getActData(opts) { let ret = {err: null, values: []} let sorting = '' if(opts.sort) { @@ -156,7 +158,7 @@ ${sorting} // ..../api/getavgdata?sensorid=123&span=2&avg=10&moving=true&datetime=2022-04-12T13:14:15Z -export const getAvgData = async (params) => { +export var getAvgData = async (params) => { let ret = {data: {count: 0, values: []}, err: null} let {opts, err} = checkParams(params, { @@ -200,7 +202,7 @@ export const getAvgData = async (params) => { } // ..../api/getlongavg?sensorid=123&span=2 -export const getLongAvg = async (params) => { +export var getLongAvg = async (params) => { let ret = {data: {count: 0, values: []}, err: null} let {opts, err} = checkParams(params, { diff --git a/app.js b/app.js index b0ad79b..eb2e1d6 100644 --- a/app.js +++ b/app.js @@ -5,8 +5,8 @@ import cookieParser from 'cookie-parser' import cors from 'cors' import indexRouter from './routes/index.js' -import { apiRouter } from './routes/api.js' -import chartRouter from './routes/chartapi.js' +import { apiRouter, chartapiRouter } from './routes/api.js' +// import chartRouter from './routes/chartapi.js' const app = express() @@ -18,7 +18,7 @@ app.use(cookieParser()) app.use('/', indexRouter) app.use('/api', apiRouter) -app.use('/chart', chartRouter) +app.use('/chart', chartapiRouter) // catch 404 and forward to error handler diff --git a/routes/api.js b/routes/api.js index c79601a..62e0520 100644 --- a/routes/api.js +++ b/routes/api.js @@ -1,16 +1,33 @@ import express from 'express' import {getData4map} from "../actions/data4map.js" -import * as cTable from "../utilities/commands.js" +import * as tables from "../utilities/tables.js" import * as ERR from "../utilities/errortexts.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(); +export const chartapiRouter = express.Router(); -export const dispatchCommand = async (cmd, table, query, res) => { +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: 'getsensordata', func: getData.getSensorData}, + {cmd: 'getmapdata', func: getData4map} +] + + +export const dispatchCommand = async (cmd, table, params, res) => { for (let c of table) { if (c.cmd === cmd) { - let erg = await c.func(query) + let erg = await c.func(params) if (typeof erg === 'string') { res.type('text') res.send(erg) @@ -25,6 +42,14 @@ export const dispatchCommand = async (cmd, table, query, res) => { // normal routes called from javascript client apiRouter.get('/:cmd', async (req, res) => { - await dispatchCommand(req.params.cmd, cTable.cmdTable, req.query, res) + const params = req.query + params.chart = false + await dispatchCommand(req.params.cmd, cmdTable, params, res) }) +// normal routes called from javascript client +chartapiRouter.get('/:cmd', async (req, res) => { + const params = req.query + params.chart = true + await dispatchCommand(req.params.cmd, cmdTable, params, res) +}) diff --git a/routes/chartapi.js b/routes/chartapi.js index 2c75055..2571039 100644 --- a/routes/chartapi.js +++ b/routes/chartapi.js @@ -1,40 +1,14 @@ import express from 'express' +import * as tables from "../utilities/tables.js" +import { dispatchCommand } from "./api.js" -import {getData4map} from "../actions/data4map.js" -import * as cTable from "../utilities/commands.js" -import * as ERR from "../utilities/errortexts.js" - -const router = express.Router(); - -/* GET home page. */ +const chartapiRouter = express.Router(); // normal routes called from javascript client -router.get('/:cmd', async (req, res) => { - const cmd = req.params.cmd - let table - for (let c of cTable.chartcmdTable) { - if (c.cmd === cmd) { - let erg = await c.func(req.query) - if (typeof erg === 'string') { - res.type('text') - res.send(erg) - } else { - res.json(erg) - } - return - } - } - res.json({err: ERR.CMNDUNKOWN}) +chartapiRouter.get('/:cmd', async (req, res) => { + const params = req.query + params.kind = 'chart' + await dispatchCommand(req.params.cmd, tables.cmdTable, params, res) }) -router.post('/:cmd', async (req, res) => { - const cmd = req.params.cmd - if (cmd === 'getdata4maps') { - const erg = await getData4map(req.body) - res.json(erg) - } else { - res.json({err: ERR.CMNDUNKOWN}) - } -}) - -export default router +export default chartapiRouter \ No newline at end of file diff --git a/routes/index.js b/routes/index.js index af56114..5a6a40b 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,7 +1,4 @@ import express from 'express' - -import {getData4map} from "../actions/data4map.js" -import * as cTable from "../utilities/commands.js" import * as ERR from "../utilities/errortexts.js" const router = express.Router(); diff --git a/sensorspecials/noise.js b/sensorspecials/noise.js index e52aeee..5143f4f 100644 --- a/sensorspecials/noise.js +++ b/sensorspecials/noise.js @@ -4,11 +4,9 @@ import {returnOnError} from "../utilities/reporterror.js"; import { getActData, getAvgData, getLongAvg, fetchFromInflux, calcRange} from "../actions/getsensorData.js" import checkParams from "../utilities/checkparams.js"; -import {getOneProperty} from "../actions/getproperties.js"; import * as ERR from "../utilities/errortexts.js" import {DateTime} from 'luxon' -import {getData4map} from "../actions/data4map.js"; -import {NODATA} from "../utilities/errortexts.js"; +import * as noiseChart from "./noiseChart.js"; const setoptionfromtable = (opt,tabval) => { let ret = opt @@ -25,6 +23,7 @@ export const getNoiseData = async (params, possibles, props) => { let {opts, err} = checkParams(params, { mandatory:[ {name:'sensorid', type: 'int'}, + {name:'chart', type: 'bool'}, ], optional: possibles }) @@ -44,6 +43,9 @@ export const getNoiseData = async (params, possibles, props) => { opts.start = start opts.stop = stop let erg = await x.func(opts) // get the data + if (opts.chart) { + return erg + } ret = { err: erg.err, sid: opts.sensorid, @@ -92,8 +94,12 @@ export const getNoiseData = async (params, possibles, props) => { // .... // // ********************************************* -const getLiveData = async (opt) => { - return await getActData(opt) +const getLiveData = async (opts) => { + const erg = await getActData(opts) + if (!opts.chart || erg.err) { + return erg + } + return noiseChart.liveData(opts, erg.values) } @@ -135,9 +141,10 @@ const gethavgData = async (opts, props) => { } } return csvStr - } else { + } else if (!opts.chart || erg.err) { return {err: erg.err, values: erg.values} - + } else { + return noiseChart.havgData(opts, erg.values) } } @@ -413,16 +420,6 @@ const getAPIprops = (opt) => { } -const whatTable = [ - {'what':'live', 'span': 1, 'daystart': false, peak: false, 'func': getLiveData}, - {'what':'havg', 'span': 7, 'daystart': true, peak: true, 'func': gethavgData}, - {'what':'davg', 'span': 30, 'daystart': true, peak: true, 'func': getdavgData}, - {'what':'daynight', 'span': 30, 'daystart': true, peak: false, 'func': getdaynightData}, - {'what':'lden', 'span': 30, 'daystart': true, peak: false, 'func': getLdenData}, - {'what':'props', 'span': 0, 'daystart': true, peak:false, 'func': getAPIprops}, -]; - - const getNoiseAVGData = async (opts) => { let ret = {err: null, values: []} let emptyValues = {n_AVG:-1} @@ -506,3 +503,12 @@ peak = data return { err: ret.err, values: hoursArr} } +const whatTable = [ + {'what':'live', 'span': 1, 'daystart': false, peak: false, 'func': getLiveData}, + {'what':'havg', 'span': 7, 'daystart': true, peak: true, 'func': gethavgData}, + {'what':'davg', 'span': 30, 'daystart': true, peak: true, 'func': getdavgData}, + {'what':'daynight', 'span': 30, 'daystart': true, peak: false, 'func': getdaynightData}, + {'what':'lden', 'span': 30, 'daystart': true, peak: false, 'func': getLdenData}, + {'what':'props', 'span': 0, 'daystart': true, peak:false, 'func': getAPIprops}, +]; + diff --git a/sensorspecials/noiseChart.js b/sensorspecials/noiseChart.js index 40ba3d4..b992111 100644 --- a/sensorspecials/noiseChart.js +++ b/sensorspecials/noiseChart.js @@ -1,2 +1,273 @@ -// Prepare all data to plt the charts +// Prepare all data to plot the charts +import * as utils from '../utilities/chartoptions.js' +export const liveData = (params, values) => { + let series1 = [] + let series2 = [] + let series3 = [] + + // save the current (actual) values here + let aktVal = {} + + + // Put values into the arrays + let cnt = 0 + values.forEach((x) => { + let dat = new Date(x.datetime).getTime() + series1.push([dat, x.noise_LAeq]) + series2.push([dat, x.noise_LA_max]) + series3.push([dat, x.noise_LA_min]) // put data and value into series array + }) + + if (values.length != 0) { + const lastidx = values.length - 1 + // Aktuelle Werte speichern + aktVal['LAeq'] = values[lastidx].noise_LAeq + aktVal['LAMax'] = values[lastidx].noise_LA_max + aktVal['LAMin'] = values[lastidx].noise_LA_min + + // InfoTafel füllen + let infoTafel = + '
| Aktuelle Werte | ' + + '||
|---|---|---|
| LAeq | ' + (aktVal.LAeq).toFixed(1) + ' | dbA | ' + infoTafel += + '
| LAmin | ' + (aktVal.LAMin).toFixed(0) + ' | dbA | ' + infoTafel += + '
| LAmax | ' + (aktVal.LAMax).toFixed(0) + ' | dbA | ' + infoTafel += + '