Many changes to have Noise Live Chart

This commit is contained in:
rxf
2023-03-20 20:01:22 +01:00
parent acad6d8276
commit 088005c040
8 changed files with 282 additions and 97 deletions
+86 -17
View File
@@ -2,13 +2,14 @@
import {DateTime} from "luxon"
import * as influx from "../databases/influx.js"
import * as mongo from "../databases/mongo.js"
import {returnOnError} from "../utilities/reporterror.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 {getNoiseData} from "../sensorspecials/noise.js";
import {getRadioData} from "../sensorspecials/radioact.js";
import {getOneProperty} from "./getproperties.js"
import {getNoiseData} from "../sensorspecials/noise.js"
import {getRadioData} from "../sensorspecials/radioact.js"
// Possible params for the different sensor types
const noiseParams = [
@@ -94,14 +95,14 @@ export const calcRange = (opts) => {
// *********************************************
// getSensorData
//
// Depending on the parameter 'sensorid', ditribute to the speciell routines for
// Depending on the parameter 'sensorid', distribute to the special routines for
// the sensors
//
// params:
// params: all parameters from the url
//
// return:
// Retirns from the special routines
// Returns from the special routines
// *********************************************
export const getSensorData = async (params) => {
let ret = {err: null}
@@ -110,18 +111,18 @@ export const getSensorData = async (params) => {
optional: []
})
if (err) {
return returnOnError(ret, err, getSensordata.name)
return returnOnError(ret, err, getSensorData.name)
}
// with the sensorid get the type of this sensor
let {props, err1} = await getOneProperty({sensorid: opts.sensorid})
if (err1) {
let erg = await getOneProperty({sensorid: opts.sensorid})
if (erg.err) {
return returnOnError(ret, err1, getSensorData.name)
}
// distribute to the right routine
for(let item of sensorTypeTable) {
if(item.typ === props.type) {
ret = item.func(params, item.possibleParams, props) // get the values from database
if(item.typ === erg.props.type) {
ret = item.func(params, item.possibleParams, erg.props) // get the values from database
return ret
}
}
@@ -131,8 +132,8 @@ export const getSensorData = async (params) => {
export const getActData = async (opts) => {
let ret = {data: {count: 0, values: []}, err: null}
export const getActData = async (opts, props) => {
let ret = {values: [], props: props, err: null}
let sorting = ''
if(opts.sort) {
if (opts.sort === 1) {
@@ -225,12 +226,11 @@ export const getLongAvg = async (params) => {
|> drop(columns: ["_start","_measurement", "sid"])
|> pivot(rowKey:["_stop"], columnKey: ["_field"], valueColumn: "_value")
`
return fetchFromInflux(ret, query)
return await fetchFromInflux(ret, query)
}
export const fetchFromInflux = async (data, query) => {
let ret = {err: null}
export const fetchFromInflux = async (ret, query) => {
let { values, err} = await influx.influxRead(query)
if(err) {
if(err.toString().includes('400')) {
@@ -240,8 +240,77 @@ export const fetchFromInflux = async (data, query) => {
}
}
if (values.length === 0) {
return returnOnError(data, ERR.NODATA, fetchFromInflux.name)
return returnOnError(ret, ERR.NODATA, fetchFromInflux.name)
}
ret.values = csv2Json(values)
return ret
}
// *********************************************
// function getMAPaktData() {
//
// Get the actual data from the database within the bounds for the given sensor typ
//
// params:
// opt: opt.box => region for which to find sensor data
// opt.typ: type of sensor
//
// return
// JSON
// { avgs: [
// { location: [ 9.00, 48.80 ], id: 29174, lastSeen: "2019-10-23T12:03:00.000Z", noise_max: "65" }
// { location: [ 9.10, 49.80 ], id: 28194, lastSeen: "2019-10-22T16:03:00.000Z", noise_max: "45" }
// .........
// ], lastDate: "2019-10-29T15:05:59.000Z" }
//
// *********************************************
export const getMAPaktData = async (opt) => {
let box = opt.box
if ((box == "") || (box == undefined)) {
return {"avgs": [], "lastDate": null}
}
let south = parseFloat(box[0][1])
let north = parseFloat(box[1][1])
let east = parseFloat(box[1][0])
let west = parseFloat(box[0][0])
let aktData = []
let lastDate = 0
let loc = {
location: {
$geoWithin: {
$box: [
[west, south],
[east, north]
]
}
},
typ: opt.typ,
}
let docs = await mongo.readMapdata(loc,0)
// console.log(docs)
for (let i = 0;i < docs.length; i++) {
let item = docs[i]
let oneAktData = {}
oneAktData['location'] = item.location.coordinates
oneAktData['id'] = item._id // ID des Sensors holen
oneAktData['lastSeen'] = item.values.datetime
oneAktData['indoor'] = item.indoor
let dati = item.values.datetime
let dt = new Date(dati)
if ((now - dt) >= 31 * 24 * 3600 * 1000) { // älter als 1 Monat ->
oneAktData['noise_max'] = -2 // -2 zurückgeben
} else if ((now - dt) >= 3600 * 1000) { // älter als 1 Stunde ->
oneAktData['noise_max'] = -1 // -1 zurückgeben
} else {
oneAktData['noise_max'] = -5 // bedutet -> nicht anzeigen
if (item.values.hasOwnProperty('noise_LA_max')) {
oneAktData['noise_max'] = item.values.noise_LA_max.toFixed(0) // und merken
}
if (dati > lastDate) {
lastDate = dati
}
}
aktData.push(oneAktData) // dies ganzen Werte nun in das Array
}
return {"avgs": aktData, "lastDate": lastDate} // alles bearbeitet _> Array senden
}