"use strict"; const moment = require('moment'); // ********************************************* // Calculate moving average over the data array. // // params: // data: array of data // mav: time in minutes to average // name: name of sensor // api: default=false, true = API -> no akt. values // // return: // array with averaged values // TODO <----- die ersten Einträge in newData mit 0 füllen bis zum Beginn des average // ********************************************* async function calcMovingAverage(db, sid, data, mav, api, factor) { var newDataT = [], newDataR = []; var avgTime = mav*60; // average time in sec let havepressure = false; // true: we have pressure if (avgTime === 0) { // if there's nothing to average, then avgTime = 1; } // first convert date to timestamp (in secs) for (var i=0; i aktuelle Druck am Ort // temp -> aktuelle Temperatur // alti -> Höhe über NN im m // // NEU NEU NEU // Formel aus dem BMP180 Datenblatt // // p0 = ph / pow(1.0 - (altitude/44330.0), 5.255); // // // // Rückgabe: normierter Druck auf Sehhhöhe // function calcSealevelPressure(data, p, alti) { if (!((alti == 0) || (alti == undefined))) { for (let i = 0; i < data.length; i++) { if (p=='') { data[i] = data[i] / Math.pow(1.0 - (alti / 44330.0), 5.255); } else { data[i][p] = data[i][p] / Math.pow(1.0 - (alti / 44330.0), 5.255); } } } return data } // Aus der 'properties'-collection die altitude für die // übergebene sid rausholen async function getAltitude(db,sid) { let collection = db.collection('properties'); try { let values = await collection.findOne({"_id":sid}); return values.location[values.location.length-1].altitude; } catch(e) { console.log("GetAltitude Error",e); return 0 } } // Get address from coordinates using OpenStreetMap Nominatim API // params: db, sensorid // returns: {address: {street, plz, city}, err} async function getAddress(db, sid) { const axios = require('axios'); let ret = {address: {street: "", plz: "", city: ""}, err: null}; // Get sensor properties to extract coordinates let collection = db.collection('properties'); let props; try { props = await collection.findOne({"_id": sid}); if (!props || !props.location || props.location.length === 0) { return {address: ret.address, err: "No location found for sensor"}; } } catch(e) { console.log("getAddress - DB Error:", e); return {address: ret.address, err: e.message}; } // Get coordinates from last location entry let coord = props.location[props.location.length - 1].loc.coordinates; let lat = coord[1]; let lon = coord[0]; // Call Nominatim API for reverse geocoding let url = `https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=json`; try { const response = await axios(encodeURI(url), { headers: { 'User-Agent': 'MultiGeiger-Web/2.9.6' // Nominatim requires User-Agent } }); if (response.status !== 200) { return {address: ret.address, err: `Nominatim API returned status ${response.status}`}; } let akt = response.data.address; // Try to find city name in various fields const CITY = ['city', 'town', 'village', 'suburb', 'county']; let city = "unknown"; for (let c of CITY) { if(akt[c] !== undefined) { city = akt[c]; break; } } ret.address = { street: (akt.road ? akt.road : ""), plz: (akt.postcode ? akt.postcode : ""), city: city }; } catch (e) { console.log("getAddress - API Error:", e.message); return {address: ret.address, err: e.message}; } return ret; } module.exports.calcMovingAverage = calcMovingAverage; module.exports.calcSealevelPressure = calcSealevelPressure; module.exports.getAltitude = getAltitude; module.exports.getAddress = getAddress;