This commit is contained in:
rxf
2023-03-08 16:17:02 +01:00
commit 0b6959d4f6
35 changed files with 5804 additions and 0 deletions
+55
View File
@@ -0,0 +1,55 @@
// parse the params from http call
import * as ERR from '../utilities/errortexts.js'
import {returnOnError} from "./reporterror.js"
const checkParams = (params, mo) => {
let o = {opts: {}, err: null}
if ((mo.mandatory.length !== 0) && (params === undefined)) {
return returnOnError(o, ERR.NOPARAMETER, checkParams.name )
}
for (let p of mo.mandatory) {
if (!(p.name in params)) {
return returnOnError(o, ERR.NOMANDPARAM.replace('xx', p.name), checkParams.name )
}
if (p.type === 'int') {
let x = parseInt(params[p.name])
if (isNaN(x)) {
return returnOnError(o, ERR.PARAMNONUM, checkParams.name )
} else {
o.opts[p.name] = x
continue
}
} else if (p.type === 'float') {
let x = parseFloat(params[p.name])
if (isNaN(x)) {
return returnOnError(o, ERR.PARAMNONUM, checkParams.name )
} else {
o.opts[p.name] = x
continue
}
}
o.opts[p.name] = params[p.name]
}
for(let p of mo.optional) {
if (!(p.name in params)) {
o.opts[p.name] = p.default
} else {
if (p.type === 'int') {
let x = parseInt(params[p.name])
if (isNaN(x)) {
o.opts[p.name] = p.default
} else {
o.opts[p.name] = x
}
} else if (p.type === 'bool') {
o.opts[p.name] = params[p.name] === 'true'
} else {
o.opts[p.name] = params[p.name]
}
}
}
return o
}
export default checkParams
+22
View File
@@ -0,0 +1,22 @@
// Command table for http calls
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"
import * as radioact from "../sensorspecials/radioact.js"
import * as noise from "../sensorspecials/noise.js"
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: 'getradiodata', func: radioact.getRadioData},
{cmd: 'getnoisedata', func: noise.getNoiseData},
{cmd: 'getsensordata', func: getData.getSensorData}
]
export default cmdTable
+35
View File
@@ -0,0 +1,35 @@
// convert influx csv output to JSON
export function csv2Json(str, delimiter = ",") {
// slice from start of text to the first \n index
// use split to create an array from string by delimiter
const headers = str.slice(0, str.indexOf("\r\n")).split(delimiter).slice(3);
let x = headers.findIndex((x) => (x === '_time') || (x === '_stop'))
if (x != -1) {
headers[x] = 'datetime'
}
// slice from \n index + 1 to the end of the text
// use split to create an array of each csv value row
const rows = str.slice(str.indexOf("\r\n") + 2).split("\r\n").slice(0,-2)
// Map the rows
// split values from each row into an array
// use headers.reduce to create an object
// object properties derived from headers:values
// the object passed as an element of the array
const arr = rows.map(function (row) {
const values = row.split(delimiter).slice(3);
const el = headers.reduce(function (object, header, index) {
if(header !== 'datetime') {
object[header] = parseFloat(values[index]);
} else {
object[header] = values[index];
}
return object;
}, {});
return el;
});
// return the array
return arr;
}
+14
View File
@@ -0,0 +1,14 @@
// 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'}`
+16
View File
@@ -0,0 +1,16 @@
import { DateTime} from 'luxon'
const MOCHA_TEST = process.env.MOCHA_TEST || false
export function logit(str) {
if(MOCHA_TEST) return
let s = `${DateTime.now().toISO()} => ${str}`;
console.log(s);
}
export function logerror(str) {
if(MOCHA_TEST) return
let s = `${DateTime.utc().toISO()} => *** ERROR *** ${str}`;
console.log(s);
}
+14
View File
@@ -0,0 +1,14 @@
import {logit} from "./logit.js";
export const reportError = (message, errortext) => {
message.error = true
message.errortext = errortext
return message
}
export const returnOnError = (pr, error, name) => {
pr.err = error
logit(`${name}: ${error}`)
return pr
}