All error messages in german and in englisch

This commit is contained in:
rxf
2023-05-09 15:37:27 +02:00
parent 2cf418756d
commit 347c6bdc96
17 changed files with 1007 additions and 243 deletions
+3 -7
View File
@@ -1,12 +1,8 @@
// Fetch the actual (= newest) data out of the dbase to show it on the map // Fetch the actual (= newest) data out of the dbase to show it on the map
import {DateTime} from "luxon" import {DateTime} from "luxon"
import {logit} from "../utilities/logit.js"
import * as mongo from "../databases/mongo.js" import * as mongo from "../databases/mongo.js"
import {reportError, returnOnError} from "../utilities/reporterror.js" import { returnOnError } from "../utilities/reporterror.js"
import * as ERR from "../utilities/errortexts.js" import { fetchFromInflux } from "./getsensorData.js";
import checkParams from "../utilities/checkparams.js";
import {fetchFromInflux} from "./getsensorData.js";
import {NOPROPSFOUND} from "../utilities/errortexts.js";
// Default distance for center search ( in km) // Default distance for center search ( in km)
@@ -136,7 +132,7 @@ export var getData4map = async (params) => {
// fetch mapdata from mongodb // fetch mapdata from mongodb
let { properties, err } = await mongo.getallProperties(mongo.properties_collection, query) let { properties, err } = await mongo.getallProperties(mongo.properties_collection, query)
if(err) { if(err) {
return returnOnError(ret, ERR.NOPROPSFOUND, getData4map.name) return returnOnError(ret, 'NOPROPSFOUND', getData4map.name)
} }
let v4map = getValue4Map(typ) let v4map = getValue4Map(typ)
for (let sensor of properties) { for (let sensor of properties) {
+1 -1
View File
@@ -16,7 +16,7 @@ export const getAddress = async (params) => {
try { try {
const response = await axios(encodeURI(url)); const response = await axios(encodeURI(url));
if (response.status !== 200) { if (response.status !== 200) {
return returnOnError(ret, ERR.RESPSTATUS.replace('ss', response.status, getAddress.name)) return returnOnError(ret, 'RESPSTATUS', getAddress.name, response.status)
} }
let akt = response.data.address let akt = response.data.address
logit(JSON.stringify(akt)) logit(JSON.stringify(akt))
+2 -3
View File
@@ -3,7 +3,6 @@
import * as mongo from "../databases/mongo.js" import * as mongo from "../databases/mongo.js"
import * as mock from "../mocks/mongo_mock.js" import * as mock from "../mocks/mongo_mock.js"
import {returnOnError} from "../utilities/reporterror.js" import {returnOnError} from "../utilities/reporterror.js"
import * as ERR from "../utilities/errortexts.js"
import checkParams from "../utilities/checkparams.js" import checkParams from "../utilities/checkparams.js"
const mockdb = false const mockdb = false
@@ -26,13 +25,13 @@ export const getOneProperty = async (params) => {
try { try {
let pp = await readProperties({sid: opts.sensorid}); // read for given sensorID let pp = await readProperties({sid: opts.sensorid}); // read for given sensorID
if ((pp.properties == null) || (pp.error)) { if ((pp.properties == null) || (pp.error)) {
return returnOnError(properties, ERR.NOPROPSREAD.replace('xx', opts.sensorid), getOneProperty.name) return returnOnError(properties, 'NOPROPSREAD', getOneProperty.name, opts.sensorid)
} }
// now find sensors with same location // now find sensors with same location
let query = {"location.0.id": pp.properties.location[0].id} let query = {"location.0.id": pp.properties.location[0].id}
let others = await readProperties(query) let others = await readProperties(query)
if (others.err) { if (others.err) {
return returnOnError(properties, ERR.NOPROPSREAD.replace('xx',others.errortext), getOneProperty.name) return returnOnError(properties, 'NOPROPSREAD', getOneProperty.name, others.errortext)
} }
if (others.properties.length > 0) { if (others.properties.length > 0) {
for (const x of others.properties) { for (const x of others.properties) {
+3 -4
View File
@@ -6,7 +6,6 @@ import * as mongo from "../databases/mongo.js"
import {returnOnError} from "../utilities/reporterror.js" import {returnOnError} from "../utilities/reporterror.js"
import {csv2Json} from "../utilities/csv2json.js" import {csv2Json} from "../utilities/csv2json.js"
import checkParams from "../utilities/checkparams.js" import checkParams from "../utilities/checkparams.js"
import * as ERR from "../utilities/errortexts.js"
import {getOneProperty} from "./getproperties.js" import {getOneProperty} from "./getproperties.js"
import {getNoiseData} from "../sensorspecials/noise.js" import {getNoiseData} from "../sensorspecials/noise.js"
import {getRadioData} from "../sensorspecials/radioact.js" import {getRadioData} from "../sensorspecials/radioact.js"
@@ -127,7 +126,7 @@ export async function getSensorData(params) {
return ret return ret
} }
} }
return returnOnError(ret, ERR.CMNDUNKOWN, getActData.name) return returnOnError(ret, 'CMNDUNKNOWN', getActData.name)
} }
@@ -236,13 +235,13 @@ export const fetchFromInflux = async (ret, query) => {
let { values, err} = await influx.influxRead(query) let { values, err} = await influx.influxRead(query)
if(err) { if(err) {
if(err.toString().includes('400')) { if(err.toString().includes('400')) {
return returnOnError(ret, ERR.SYNTAXURL, fetchFromInflux.name) return returnOnError(ret, 'SYNTAXURL', fetchFromInflux.name)
} else { } else {
return returnOnError(ret, err, fetchFromInflux.name) return returnOnError(ret, err, fetchFromInflux.name)
} }
} }
if (values.length <= 2) { if (values.length <= 2) {
return returnOnError(ret, ERR.NODATA, fetchFromInflux.name) return returnOnError(ret, 'NODATA', fetchFromInflux.name)
} }
ret.values = csv2Json(values) ret.values = csv2Json(values)
return ret return ret
+24 -1
View File
@@ -3,11 +3,34 @@ import logger from 'morgan'
import express from 'express' import express from 'express'
import cookieParser from 'cookie-parser' import cookieParser from 'cookie-parser'
import cors from 'cors' import cors from 'cors'
import i18next from 'i18next'
import i18nextMiddleware from 'i18next-http-middleware'
import Backend from 'i18next-node-fs-backend'
const app = express()
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import indexRouter from './routes/index.js' import indexRouter from './routes/index.js'
import { apiRouter } from './routes/api.js' import { apiRouter } from './routes/api.js'
import {fileURLToPath} from "url";
import path from "path";
const app = express()
i18next
.use(Backend)
.use(i18nextMiddleware.LanguageDetector)
.init({
backend: {
loadPath: __dirname + '/locales/{{lng}}/{{ns}}.json'
},
fallbackLng: 'de',
debug: false,
preload: ['de', 'en']
});
app.use(i18nextMiddleware.handle(i18next));
app.use(cors()) app.use(cors())
app.use(logger('dev')) app.use(logger('dev'))
+1 -1
View File
@@ -13,7 +13,7 @@ import http from 'http'
* Get port from environment and store in Express. * Get port from environment and store in Express.
*/ */
const port = normalizePort(process.env.PORT || '3004'); const port = normalizePort(process.env.PORT || '3005');
app.set('port', port); app.set('port', port);
/** /**
+1 -2
View File
@@ -33,8 +33,7 @@ export const influxRead = async (query) => {
timeout: 10000, timeout: 10000,
}) })
if (ret.status !== 200) { if (ret.status !== 200) {
let error = ERR.RESPSTATUS.replace('xx',ret.status) return returnOnError(erg, RESPSTATUS, influxRead.name, ret.status)
return returnOnError(erg, error, influxRead.name)
} }
erg.values = ret.data erg.values = ret.data
} catch (e) { } catch (e) {
+16
View File
@@ -0,0 +1,16 @@
{
"CMNDUNKNOWN": "Unbekanntes Kommando",
"NOTHING": "Nichts anzuzeigen",
"PARAMNONUM": "Parameter \"xxx\" ist keine Zahl",
"NOTYP": "KeinTyp angegeben",
"NOSENSFOUND": "Kein passender Sensor in den Properties gefunden",
"NOMANDPARAM": "Nitewendiger Parameter xxx nicht angegeben",
"NOPROPSREAD": "Keine properties für Sensor xxx eingelesen",
"NOPARAMETER": "Keine Parameter übergene",
"RESPSTATUS": "Rückgabe-Status: xxx",
"NODATA": "keine Daten gefunden",
"SYNTAXURL": "Syntax Fehler beim Aufruf der URL",
"WRONGTYPE": "Sensor xxx ist kein yyy Sensor",
"NOLASTDATES": "Probleme bein abholen der letzten Daten",
"NOPROPSFOUND": "Properties Collection nicht gefunden"
}
+16
View File
@@ -0,0 +1,16 @@
{
"CMNDUNKNOWN": "Command not known",
"NOTHING": "Nothing to show",
"PARAMNONUM": "Parameter 'xxx' is not a number",
"NOTYP": "No type given",
"NOSENSFOUND": "No suitable sensors found in properties",
"NOMANDPARAM": "Mandatory parameter xxx not given",
"NOPROPSREAD": "No properties read for sensor xxx",
"NOPARAMETER": "No parameter given",
"RESPSTATUS": "Returned status: xxxx",
"NODATA": "No data found",
"SYNTAXURL": "Syntax error in calling url!",
"WRONGTYPE": "Sensor xxx is not of type yyy",
"NOLASTDATES": "Problems fetching last dates from database",
"NOPROPSFOUND": "Properties collection not found"
}
+906 -187
View File
File diff suppressed because it is too large Load Diff
+6 -3
View File
@@ -1,7 +1,7 @@
{ {
"name": "sensorapi", "name": "sensorapi",
"version": "1.2.2", "version": "1.3.0",
"date": "2023-04-24", "date": "2023-05-09",
"private": true, "private": true,
"scripts": { "scripts": {
"start": "node ./bin/www.js", "start": "node ./bin/www.js",
@@ -18,8 +18,11 @@
"cookie-parser": "~1.4.4", "cookie-parser": "~1.4.4",
"cors": "^2.8.5", "cors": "^2.8.5",
"debug": "~2.6.9", "debug": "~2.6.9",
"express": "~4.16.1", "express": "^4.18.2",
"http-errors": "~1.6.3", "http-errors": "~1.6.3",
"i18next": "^22.4.15",
"i18next-http-middleware": "^3.3.0",
"i18next-node-fs-backend": "^2.1.3",
"luxon": "^2.3.1", "luxon": "^2.3.1",
"mongodb": "^4.4.1", "mongodb": "^4.4.1",
"morgan": "~1.9.1", "morgan": "~1.9.1",
+7 -5
View File
@@ -1,16 +1,12 @@
import express from 'express' import express from 'express'
import {getData4map} from "../actions/data4map.js" import {getData4map} from "../actions/data4map.js"
import * as ERR from "../utilities/errortexts.js"
import * as getData from "../actions/getsensorData.js"; import * as getData from "../actions/getsensorData.js";
import * as getProps from "../actions/getproperties.js"; import * as getProps from "../actions/getproperties.js";
import * as getAKWs from "../actions/getAKWData.js"; import * as getAKWs from "../actions/getAKWData.js";
import * as holAddr from "../actions/getaddress.js"; import * as holAddr from "../actions/getaddress.js";
export const apiRouter = express.Router(); export const apiRouter = express.Router();
import { logit, logerror } from '../utilities/logit.js'
const cmdTable = [ const cmdTable = [
{cmd: 'getactdata', func: getData.getActData}, {cmd: 'getactdata', func: getData.getActData},
@@ -23,6 +19,11 @@ const cmdTable = [
{cmd: 'getmapdata', func: getData4map} {cmd: 'getmapdata', func: getData4map}
] ]
let i18n;
export const translate = (x) => {
return i18n.t(x)
}
export const dispatchCommand = async (cmd, table, params, res) => { export const dispatchCommand = async (cmd, table, params, res) => {
for (let c of table) { for (let c of table) {
@@ -37,12 +38,13 @@ export const dispatchCommand = async (cmd, table, params, res) => {
return return
} }
} }
res.json({err: ERR.CMNDUNKOWN}) res.json({err:translate('CMNDUNKNOWN')})
} }
// normal routes called from javascript client // normal routes called from javascript client
apiRouter.get('/:cmd', async (req, res) => { apiRouter.get('/:cmd', async (req, res) => {
const params = req.query const params = req.query
params.chart = false params.chart = false
i18n = req.i18n
await dispatchCommand(req.params.cmd, cmdTable, params, res) await dispatchCommand(req.params.cmd, cmdTable, params, res)
}) })
+2 -2
View File
@@ -1,12 +1,12 @@
import express from 'express' import express from 'express'
import * as ERR from "../utilities/errortexts.js" import { translate as trans } from '../routes/api.js'
const router = express.Router(); const router = express.Router();
/* GET home page. */ /* GET home page. */
router.get('/', function(req, res, next) { router.get('/', function(req, res, next) {
res.status(200).json({message: ERR.NOTHIMG}) res.status(200).json({message: trans('NOTHIMG')})
}); });
export default router export default router
+6 -5
View File
@@ -4,8 +4,9 @@
import {returnOnError} from "../utilities/reporterror.js"; import {returnOnError} from "../utilities/reporterror.js";
import { getActData, getAvgData, getLongAvg, fetchFromInflux, calcRange} from "../actions/getsensorData.js" import { getActData, getAvgData, getLongAvg, fetchFromInflux, calcRange} from "../actions/getsensorData.js"
import checkParams from "../utilities/checkparams.js"; import checkParams from "../utilities/checkparams.js";
import * as ERR from "../utilities/errortexts.js"
import {DateTime} from 'luxon' import {DateTime} from 'luxon'
import { translate as trans } from '../routes/api.js'
const setoptionfromtable = (opt,tabval) => { const setoptionfromtable = (opt,tabval) => {
let ret = opt let ret = opt
@@ -58,12 +59,12 @@ export const getNoiseData = async (params, possibles, props) => {
delete ret.options.peak delete ret.options.peak
} }
if(ret.values.length === 0) { if(ret.values.length === 0) {
ret.err = ERR.NODATA ret.err = trans('NODATA')
} }
return ret return ret
} }
} }
return returnOnError(ret, ERR.CMNDUNKOWN, getNoiseData.name) return returnOnError(ret, 'CMNDUNKNOWN', getNoiseData.name)
} }
@@ -487,7 +488,7 @@ peak = data
return returnOnError(ret, ret.err, getNoiseAVGData.name) return returnOnError(ret, ret.err, getNoiseAVGData.name)
} }
if(ret.values.length === 0) { if(ret.values.length === 0) {
return returnOnError(ret, ERR.NODATA, getNoiseAVGData.name) return returnOnError(ret, 'NODATA', getNoiseAVGData.name)
} }
// The times are always the END of the period (so: period from 00:00h to 01:00h -> time is 01:00) // The times are always the END of the period (so: period from 00:00h to 01:00h -> time is 01:00)
@@ -514,7 +515,7 @@ peak = data
const whatTable = [ const whatTable = [
{'what':'live', 'span': 1, 'daystart': false, peak: false, 'func': getLiveData}, {'what':'live', 'span': 1, 'daystart': false, peak: false, 'func': getLiveData},
{'what':'havg', 'span': 3, 'daystart': false, peak: true, 'func': gethavgData}, {'what':'havg', 'span': 7, 'daystart': false, peak: true, 'func': gethavgData},
{'what':'davg', 'span': 30, 'daystart': true, peak: true, 'func': getdavgData}, {'what':'davg', 'span': 30, 'daystart': true, peak: true, 'func': getdavgData},
{'what':'daynight', 'span': 30, 'daystart': true, peak: false, 'func': getdaynightData}, {'what':'daynight', 'span': 30, 'daystart': true, peak: false, 'func': getdaynightData},
{'what':'lden', 'span': 30, 'daystart': true, peak: false, 'func': getLdenData}, {'what':'lden', 'span': 30, 'daystart': true, peak: false, 'func': getLdenData},
+4 -5
View File
@@ -1,21 +1,20 @@
// parse the params from http call // parse the params from http call
import * as ERR from '../utilities/errortexts.js'
import {returnOnError} from "./reporterror.js" import {returnOnError} from "./reporterror.js"
const checkParams = (params, mo) => { const checkParams = (params, mo) => {
let o = {opts: {}, err: null} let o = {opts: {}, err: null}
if ((mo.mandatory.length !== 0) && (params === undefined)) { if ((mo.mandatory.length !== 0) && (params === undefined)) {
return returnOnError(o, ERR.NOPARAMETER, checkParams.name ) return returnOnError(o, 'NOPARAMETER', checkParams.name )
} }
for (let p of mo.mandatory) { for (let p of mo.mandatory) {
if (!(p.name in params)) { if (!(p.name in params)) {
return returnOnError(o, ERR.NOMANDPARAM.replace('xx', p.name), checkParams.name ) return returnOnError(o, 'NOMANDPARAM', checkParams.name, p.name)
} }
if (p.type === 'int') { if (p.type === 'int') {
let x = parseInt(params[p.name]) let x = parseInt(params[p.name])
if (isNaN(x)) { if (isNaN(x)) {
return returnOnError(o, ERR.PARAMNONUM, checkParams.name ) return returnOnError(o, 'PARAMNONUM', checkParams.name, p.name)
} else { } else {
o.opts[p.name] = x o.opts[p.name] = x
continue continue
@@ -23,7 +22,7 @@ const checkParams = (params, mo) => {
} else if (p.type === 'float') { } else if (p.type === 'float') {
let x = parseFloat(params[p.name]) let x = parseFloat(params[p.name])
if (isNaN(x)) { if (isNaN(x)) {
return returnOnError(o, ERR.PARAMNONUM, checkParams.name ) return returnOnError(o, 'PARAMNONUM', checkParams.name, p.name)
} else { } else {
o.opts[p.name] = x o.opts[p.name] = x
continue continue
-16
View File
@@ -1,16 +0,0 @@
// Errortexts
export const CMNDUNKOWN = 'Command not known'
export const NOTHIMG = 'Nothing to show'
export const PARAMNONUM = `Parameter ${'xx'} is not a number`
export const NOTYP = 'No type given'
export const NOSENSFOUND = 'No suitable sensors found in properties'
export const NOMANDPARAM = `Mandatory parameter '${'xx'}' not given`
export const NOPROPSREAD = `No properties read for sensor ${'xx'}`
export const NOPARAMETER = 'No parameter given'
export const RESPSTATUS = `Returned status = ${'xx'}`
export const NODATA = 'No data found'
export const SYNTAXURL = 'Syntax error in calling url!'
export const WRONGTYPE = `Sensor ${'xx'} is not of type ${'yy'}`
export const NOLASTDATES = `Problems fetching last dates from database`
export const NOPROPSFOUND = `Properties collection not found`
+9 -1
View File
@@ -1,4 +1,5 @@
import {logit} from "./logit.js"; import {logit} from "./logit.js";
import { translate as trans } from '../routes/api.js'
export const reportError = (message, errortext) => { export const reportError = (message, errortext) => {
message.error = true message.error = true
@@ -6,7 +7,14 @@ export const reportError = (message, errortext) => {
return message return message
} }
export const returnOnError = (pr, error, name) => { export const returnOnError = (pr, error, name, p1='', p2='') => {
error = trans(error)
if (error.indexOf('xxx') !== -1) {
error = error.replace('xxx', p1)
}
if (error.indexOf('yyy') !== -1) {
error = error.replace('yyy', p1)
}
pr.err = error pr.err = error
logit(`${name}: ${error}`) logit(`${name}: ${error}`)
return pr return pr