Many changes to have Noise Live Chart
This commit is contained in:
+86
-38
@@ -4,11 +4,31 @@ import {logit} from "../utilities/logit.js"
|
||||
import * as mongo from "../databases/mongo.js"
|
||||
import {reportError, returnOnError} from "../utilities/reporterror.js"
|
||||
import * as ERR from "../utilities/errortexts.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)
|
||||
const DEFAULT_DISTANCE = 10
|
||||
|
||||
// Value to use fpr map
|
||||
const value4map = [
|
||||
{ typ: 'noise', value: 'noise_LA_max'},
|
||||
{ typ: 'pm', value: 'P1'},
|
||||
{ typ: 'thp', value: 'temperature'},
|
||||
{ typ: 'radioactivity', value: 'counts_per_minute'},
|
||||
]
|
||||
|
||||
// get value for map from tabel
|
||||
const getValue4Map = (t) => {
|
||||
for(let x of value4map) {
|
||||
if(x.typ == t) {
|
||||
return x.value
|
||||
}
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
// Relations between types and value type
|
||||
const vtype2measurement = {
|
||||
@@ -19,6 +39,7 @@ const vtype2measurement = {
|
||||
};
|
||||
|
||||
|
||||
|
||||
// find first value type from measurement
|
||||
const getfieldfromtype = (typ) => {
|
||||
for (const [key, value] of Object.entries(vtype2measurement)) {
|
||||
@@ -30,27 +51,42 @@ const getfieldfromtype = (typ) => {
|
||||
}
|
||||
|
||||
|
||||
// read the last entries from the influx database
|
||||
const readLastDates = async (typ) => {
|
||||
let ret = {values: [], err: null}
|
||||
let query = `
|
||||
from(bucket: "sensor_data")
|
||||
|> range(start: -2h)
|
||||
|> filter(fn: (r) => r._measurement == "${typ}" and r._field == "${getValue4Map(typ)}")
|
||||
|> last()
|
||||
|> group()
|
||||
|> map(fn: (r) => ({r with sid: int(v: r.sid)}))
|
||||
|> sort(columns: ["sid"])
|
||||
|> keep(columns: ["_time","sid","_value"])
|
||||
`
|
||||
return await fetchFromInflux(ret, query)
|
||||
}
|
||||
|
||||
export const getData4map = async (params) => {
|
||||
let start = DateTime.now()
|
||||
let retur = { err: null, data: { params: params} }
|
||||
let ret = {err: null}
|
||||
|
||||
// ***** This function will (at the moment) only be called by internal routines, so there is no need to check the parameters !
|
||||
|
||||
// check parameters
|
||||
if ((params.type === undefined) || (params.type === '')) {
|
||||
return returnOnError(retur, ERR.NOTYP, getData4map.name)
|
||||
}
|
||||
const typ = params.type
|
||||
let poly = []
|
||||
let south = null, north = null, east = null, west = null, center = null
|
||||
let distance = DEFAULT_DISTANCE
|
||||
if (!((params.box === undefined) || (params.box == ""))) {
|
||||
const box = params.box
|
||||
if (!((box === undefined) || (box === ' ') || (box === 'null'))) {
|
||||
south = parseFloat(box.south)
|
||||
north = parseFloat(box.north)
|
||||
east = parseFloat(box.east)
|
||||
west = parseFloat(box.west)
|
||||
logit(`getData4map: S=${south} N=${north} E=${east} W=${west}`)
|
||||
if(params.box !== undefined) {
|
||||
let val = params.box.split(',')
|
||||
for (let i = 0; i < val.length; i++) {
|
||||
val[i] = parseFloat(val[i])
|
||||
}
|
||||
south = parseFloat(val[1])
|
||||
north = parseFloat(val[3])
|
||||
east = parseFloat(val[2])
|
||||
west = parseFloat(val[0])
|
||||
logit(`getData4map: S=${south} N=${north} E=${east} W=${west}`)
|
||||
}
|
||||
if (!((params.poly === undefined) || (params.poly === ' '))){
|
||||
poly = JSON.parse(params.poly)
|
||||
@@ -96,43 +132,55 @@ export const getData4map = async (params) => {
|
||||
}
|
||||
}
|
||||
}
|
||||
// depending of the type find the measurement field to fetch
|
||||
const vtype = getfieldfromtype(typ)
|
||||
|
||||
// fetch mapdata from mongodb
|
||||
try {
|
||||
let { values, err } = await mongo.readMapdata(query, 0)
|
||||
// fetch mapdata from mongodb
|
||||
let { properties, err } = await mongo.getallProperties(mongo.properties_collection, query)
|
||||
if(err) {
|
||||
return returnOnError(retur, ERR.NOSENSFOUND, getData4map.name)
|
||||
return returnOnError(ret, ERR.NOPROPSFOUND, getData4map.name)
|
||||
}
|
||||
for (let item of values) {
|
||||
let v4map = getValue4Map(typ)
|
||||
for (let sensor of properties) {
|
||||
let id = sensor._id
|
||||
const oneAktData = {
|
||||
location: item.location.loc.coordinates,
|
||||
id: item._id,
|
||||
name: item.name,
|
||||
indoor: item.location.indoor,
|
||||
value: Math.round(item.value[vtype] * 100) / 100
|
||||
location: sensor.location[0].loc.coordinates,
|
||||
id: sensor._id,
|
||||
name: sensor.name[0].name,
|
||||
indoor: sensor.location[0].indoor,
|
||||
lastseen: sensor.values.timestamp
|
||||
}
|
||||
let now = new Date().getTime()
|
||||
let diff = now - item.timestamp
|
||||
if (diff >= 7 * 24 * 3600 * 1000) {
|
||||
oneAktData.value = -2
|
||||
} else if (diff >= 2 * 3600 * 1000) {
|
||||
oneAktData.value = -1
|
||||
oneAktData.value = -1
|
||||
if(oneAktData.lastseen !== '') {
|
||||
let diff = now - oneAktData.lastseen.getTime()
|
||||
if (diff >= 365 * 24 * 3600 * 1000) {
|
||||
oneAktData.value = -4
|
||||
} else if (diff >= 30 * 24 * 3600 * 1000) {
|
||||
oneAktData.value = -3
|
||||
} else if (diff >= 7 * 24 * 3600 * 1000) {
|
||||
oneAktData.value = -2
|
||||
} else if (diff >= 2 * 3600 * 1000) {
|
||||
oneAktData.value = -1
|
||||
} else {
|
||||
if (sensor.values !== undefined) {
|
||||
oneAktData.value = Math.round(sensor.values[v4map] * 100) / 100
|
||||
}
|
||||
}
|
||||
}
|
||||
if (item.timestamp > lastDate) {
|
||||
lastDate = item.timestamp
|
||||
if (sensor.values.timestamp > lastDate) {
|
||||
lastDate = sensor.values.timestamp
|
||||
}
|
||||
aktData.push(oneAktData)
|
||||
}
|
||||
logit(`Query duration: ${start.diffNow('seconds').toObject().seconds * -1} sec`)
|
||||
retur.data.lastDate = lastDate
|
||||
retur.data.valuetype = vtype
|
||||
retur.data.count = aktData.length
|
||||
retur.data.values = aktData
|
||||
return retur
|
||||
ret = {
|
||||
err: null,
|
||||
lastdate: lastDate,
|
||||
count: aktData.length,
|
||||
values: aktData
|
||||
}
|
||||
return ret
|
||||
}
|
||||
catch(e) {
|
||||
return returnOnError(retur, `catch\n${e}`, getData4map.name)
|
||||
return returnOnError(ret, `catch\n${e}`, getData4map.name)
|
||||
}
|
||||
}
|
||||
|
||||
+11
-15
@@ -14,6 +14,8 @@ if (mockdb) {
|
||||
} else {
|
||||
readProperties = mongo.readProperties
|
||||
}
|
||||
|
||||
// Read properties for sensorid and properties for all other sensors on same location
|
||||
export const getOneProperty = async (params) => {
|
||||
let properties = {props: {}, err: null}
|
||||
let {opts, err} = checkParams(params, {mandatory:[{name:'sensorid', type: 'int'}], optional:[]})
|
||||
@@ -22,31 +24,25 @@ export const getOneProperty = async (params) => {
|
||||
}
|
||||
let sensorEntries = [];
|
||||
try {
|
||||
let pp = await readProperties({sid: opts.sensorid});
|
||||
if ((pp.values == null) || (pp.error)) {
|
||||
let pp = await readProperties({sid: opts.sensorid}); // read for given sensorID
|
||||
if ((pp.properties == null) || (pp.error)) {
|
||||
return returnOnError(properties, ERR.NOPROPSREAD.replace('xx', opts.sensorid), getOneProperty.name)
|
||||
}
|
||||
// now find sensors with same location
|
||||
let query
|
||||
if (pp.values.location_id !== undefined) {
|
||||
query = {location_id: pp.values.location_id}
|
||||
} else {
|
||||
query = {id: pp.values.id}
|
||||
}
|
||||
let query = {"location.0.id": pp.properties.location[0].id}
|
||||
let others = await readProperties(query)
|
||||
if (others.error) {
|
||||
return returnOnError(properties, ERR.NOPROPSREAD.replace('xx',others.errortext), getOneProperty.name)
|
||||
}
|
||||
if (others.values.length > 0) {
|
||||
for (const x of others.values) {
|
||||
if(x.name[0].name === undefined) {
|
||||
sensorEntries.push({name: x.name, sid: x._id})
|
||||
} else {
|
||||
sensorEntries.push({name: x.name[0].name, sid: x._id})
|
||||
if (others.properties.length > 0) {
|
||||
for (const x of others.properties) {
|
||||
if(x._id === pp.properties._id) {
|
||||
continue
|
||||
}
|
||||
sensorEntries.push({name: x.name[0].name, sid: x._id})
|
||||
}
|
||||
}
|
||||
properties.props = pp.values
|
||||
properties.props = pp.properties
|
||||
properties.props.othersensors = sensorEntries;
|
||||
} catch (e) {
|
||||
return returnOnError(properties, e, getOneProperty.name)
|
||||
|
||||
+86
-17
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user