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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user