From c10001754fa822a3fd5f3574257650b44d50a458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reinhard=20X=2E=20F=C3=BCrst?= Date: Tue, 28 Nov 2023 15:12:20 +0000 Subject: [PATCH] getCityCoords added remove 'noise_' on every value --- .vscode/launch.json | 8 ++++---- actions/data4map.js | 4 ++-- actions/getaddress.js | 19 +++++++++++++++++++ actions/getsensorData.js | 8 ++++---- databases/influx.js | 2 +- databases/mongo.js | 27 ++++++++++++++++++++++----- routes/api.js | 1 + sensorspecials/noise.js | 27 +++++++++++++++++++++------ 8 files changed, 74 insertions(+), 22 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 36fc1dd..6b15ff5 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -30,10 +30,10 @@ ], "program": "${workspaceFolder}/bin/www.js", "env": { - "INFLUXHOST": "esprimo", - "INFLUXTOKEN": "uhsrE9MZtlVD18JnP19y0O0NW9C8HaQV3ElyAT_VwUlreluNsc9rM4djI9H10f6Dcw4oMFUzSngAo9dxtLjUoA==", - "MONGOHOST": "esprimo", - "MONGOPORT": "27098", + "MONGOUSRP": "rexfue:noise4mongo", + "MONGOAUTH": "true", + "INFLUXTOKEN": "q35XUBaElzcy8dDd9HF2_mpeHvYCampZg_9mJNP5jeBQRopq3EWIzNTZ555QLSIXhZC05RXCoFgjiaT7VzyNkQ==", + "DEVELOP": "true", } }, { diff --git a/actions/data4map.js b/actions/data4map.js index 3a6964f..7ce7d64 100644 --- a/actions/data4map.js +++ b/actions/data4map.js @@ -9,7 +9,7 @@ const DEFAULT_DISTANCE = 10 // Value to use fpr map const value4map = [ - { typ: 'noise', value: 'noise_LA_max'}, + { typ: 'noise', value: 'LA_max'}, { typ: 'pm', value: 'P1'}, { typ: 'thp', value: 'temperature'}, { typ: 'radioactivity', value: 'counts_per_minute'}, @@ -29,7 +29,7 @@ const getValue4Map = (t) => { const vtype2measurement = { P1: 'pm', P2: 'pm', P0: 'pm', temperature: 'thp', humidity: 'thp', pressure: 'thp', - noise_LAeq: 'noise', + LAeq: 'noise', counts_per_minute: 'radioactivity' }; diff --git a/actions/getaddress.js b/actions/getaddress.js index cd3aefb..97bceec 100644 --- a/actions/getaddress.js +++ b/actions/getaddress.js @@ -2,8 +2,27 @@ import {returnOnError} from "../utilities/reporterror.js" import axios from 'axios' import {logit} from "../utilities/logit.js" import {getOneProperty} from "./getproperties.js" +import { response } from "express" const NOMINATIM_URL = `https://nominatim.openstreetmap.org/reverse?lat=${'xx'}&lon=${'yy'}&format=json` +const NOMINATIM_CITY_URL = `https://nominatim.openstreetmap.org/?q="${'xx'}"&format=json` + +export const getCityCoords = async (params) => { + let ret = {coords: [], city: params.city, err: null} + let url = NOMINATIM_CITY_URL.replace('xx', params.city) +// let url = 'https://nominatim.openstreetmap.org/?q="K%C3%B6ln"&format=json' + try { + const response = await axios(url) + if (response.status !== 200) { + return returnOnError(ret, 'RESPSTATUS', getCityCoord.name, response.status) + } + ret.coords = [response.data[0].lat,response.data[0].lon] + logit(JSON.stringify(ret.coords)) + } catch (e) { + return returnOnError(ret, e, getCityCoords.name) + } + return ret +} export const getAddress = async (params) => { let ret = {address: "", err: null} diff --git a/actions/getsensorData.js b/actions/getsensorData.js index 88186e7..c8e5ece 100644 --- a/actions/getsensorData.js +++ b/actions/getsensorData.js @@ -250,8 +250,8 @@ export var getLongAvg = async (params) => { // 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" } +// { location: [ 9.00, 48.80 ], id: 29174, lastSeen: "2019-10-23T12:03:00.000Z", max: "65" } +// { location: [ 9.10, 49.80 ], id: 28194, lastSeen: "2019-10-22T16:03:00.000Z", max: "45" } // ......... // ], lastDate: "2019-10-29T15:05:59.000Z" } // @@ -295,8 +295,8 @@ export const getMAPaktData = async (opt) => { 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 (item.values.hasOwnProperty('LA_max')) { + oneAktData['noise_max'] = item.values.LA_max.toFixed(0) // und merken } if (dati > lastDate) { lastDate = dati diff --git a/databases/influx.js b/databases/influx.js index 2b18fbb..b63377d 100644 --- a/databases/influx.js +++ b/databases/influx.js @@ -142,7 +142,7 @@ esum = data |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value") |> rename(columns: {"E10tel_eq" : "n_sum"}) peak = data - |> filter(fn: (r) => r._field == "noise_LA_max") + |> filter(fn: (r) => r._field == "LA_max") |> aggregateWindow( every: 1h, fn: (column, tables=<-) => tables diff --git a/databases/mongo.js b/databases/mongo.js index 54661fd..7c67c86 100644 --- a/databases/mongo.js +++ b/databases/mongo.js @@ -28,7 +28,7 @@ export const properties_collection = 'properties' export const connectMongo = async () => { try { -// logit(`Try to connect to ${MONGO_URL}`) +// logit(`Try to connect to ${MONGO_URL}`) let client = await MongoClient.connect(MONGO_URL) // logit(`Mongodbase connected to ${MONGO_URL}`) return client @@ -154,8 +154,25 @@ export const fetchActData = async (opts) => { let options = { projection: {_id: 0, values: 1, datetime: 1}, sort: {datetime: 1}} let client = await connectMongo() try { - ret.values = await client.db(MONGOBASE).collection('noise_sensors') - .find(query, options).toArray() + // ret.values = await client.db(MONGOBASE).collection('noise_sensors') + // .find(query, options).toArray() + ret.values = await client.db(MONGOBASE).collection('noise_sensors').aggregate([ + {$match: query}, + {$sort: { datetime: 1}}, + {$replaceWith: + { + datetime: {$dateToString: {format: '%Y-%m-%dT%H:%M:%SZ', date: '$datetime'}}, + LA_min: '$values.LA_min', + LA_max: '$values.LA_max', + LAeq: '$values.LAeq', + E10tel_eq: '$values.E10tel_eq' + } + }, + // {$project: { + // datetime: {$dateToString: {format: '%Y-%m-%dT%H:%M:%SZ', date: '$datetime'}}, + // _id: 0, values:1 + // }}, + ]).toArray() } catch(e) { ret.err = e @@ -279,7 +296,7 @@ export const fetchNoiseAVGData = async (opts) => { _id: grpId, n_average: {$avg: "$values.E10tel_eq"}, // calculate the average n_sum: {$sum: "$values.E10tel_eq"}, // calculate the sum - peakcount: {$sum: {$cond: [{$gte: ["$values.noise_LA_max", peak]}, 1, 0]}}, // count peaks + peakcount: {$sum: {$cond: [{$gte: ["$values.LA_max", peak]}, 1, 0]}}, // count peaks count: {$sum: 1}, // count entries } }, @@ -345,7 +362,7 @@ async function getAverageData(db,opt) { _id: grpId, n_average: {$avg: '$E10tel_eq'}, // calculate the average n_sum: {$sum: '$E10tel_eq'}, // calculate the sum - peakcount: {$sum: {$cond: [{$gte: ["$noise_LA_max", peak]}, 1, 0]}}, // count peaks + peakcount: {$sum: {$cond: [{$gte: ["$LA_max", peak]}, 1, 0]}}, // count peaks count: {$sum: 1}, // count entries } }, diff --git a/routes/api.js b/routes/api.js index 995b388..4a18f9c 100644 --- a/routes/api.js +++ b/routes/api.js @@ -15,6 +15,7 @@ const cmdTable = [ {cmd: 'getoneproperty', func: getProps.getOneProperty}, {cmd: 'getakwdata', func: getAKWs.getakwdata}, {cmd: 'getaddress', func: holAddr.getAddress}, + {cmd: 'getcitycoords', func: holAddr.getCityCoords}, {cmd: 'getsensordata', func: getData.getSensorData}, {cmd: 'getmapdata', func: getData4map} ] diff --git a/sensorspecials/noise.js b/sensorspecials/noise.js index 638e91f..6d37807 100644 --- a/sensorspecials/noise.js +++ b/sensorspecials/noise.js @@ -45,7 +45,7 @@ export const getNoiseData = async (params, possibles, props) => { sid: opts.sensorid, indoor: props.location[0].indoor, span: opts.span, - start: DateTime.fromISO(opts.start.slice(7)).toUTC().toISO(), + start: DateTime.fromISO(opts.start.slice(7)).toUTC().toFormat("yyyy-LL-dd'T'HH:mm:ss'Z'"), data: opts.data, peak: opts.peak, count: erg.values.length, @@ -78,8 +78,8 @@ export const getNoiseData = async (params, possibles, props) => { // return: // JSON: // { sid: 29212, span: 1, start: "2019-10-23T00:00", count: 381, values: [ -// { datetime: "2019-10-22T22:05:34.000Z", noise_LAeq: 42.22, noise_LA_min: 39.91, noise_LA_max: 45.18, E10tel_eq: 16672.47212551061 }, -// { datetime: "2019-10-22T22:07:59.000Z", noise_LAeq: 53.72, noise_LA_min: 39.97, noise_LA_max: 63.54, E10tel_eq: 235504.9283896009 }, +// { datetime: "2019-10-22T22:05:34.000Z", LAeq: 42.22, LA_min: 39.91, LA_max: 45.18, E10tel_eq: 16672.47212551061 }, +// { datetime: "2019-10-22T22:07:59.000Z", LAeq: 53.72, LA_min: 39.97, LA_max: 63.54, E10tel_eq: 235504.9283896009 }, // ......... // ]} // CSV @@ -92,6 +92,21 @@ export const getNoiseData = async (params, possibles, props) => { // ********************************************* const getLiveData = async (opts) => { const erg = await getActData(opts) + if (opts.csv) { + let csvStr = "datetime,LAeq,LAmax,LAmin,10^(LAeq/10)\n" + if (!erg.err) { + for (let item of erg.values) { + if (item.n_AVG != -1) { + csvStr += item.datetime + ',' + + item.LAeq + ',' + + item.LA_max + ',' + + item.LA_min+ ',' + + item.E10tel_eq + '\n' + } + } + } + return csvStr + } return erg } @@ -182,7 +197,7 @@ async function getdavgData(opts) { pk += item.peakcount; if (werte.datetime === undefined) { let dt = DateTime.fromISO(item.datetime) - werte.datetime = dt.startOf('day').toISO() + werte.datetime = dt.startOf('day').toFormat("yyyy-LL-dd'T'HH:mm:ss'Z'") } } } @@ -210,7 +225,7 @@ async function getdavgData(opts) { const addDatetime = (werte, item) => { if (werte.datetime === undefined) { let dt = DateTime.fromISO(item.datetime) - werte.datetime = dt.startOf('day').toISO() + werte.datetime = dt.startOf('day').toFormat("yyyy-LL-dd'T'HH:mm:ss'Z'") } } @@ -434,7 +449,7 @@ const getNoiseAVGData = async (opts) => { ret = await influx.fetchNoiseAVGData(opts) // Influx stores the average from 00:00h to 01:00h as 01:00h, so we have to shift the time 1 hour back for (let x=0; x < ret.values.length; x++) { - ret.values[x].datetime = DateTime.fromISO(ret.values[x].datetime).toUTC().minus({hours:1}).toISO() + ret.values[x].datetime = DateTime.fromISO(ret.values[x].datetime).toUTC().minus({hours:1}).toFormat("yyyy-LL-dd'T'HH:mm:ss'Z'") } } else { ret.err = 'DBASEUNKNOWN'