Commit a697cf72 authored by alain's avatar alain 🐙
Browse files

handle sensor statusses better

parent 77ee0a6e
......@@ -42,8 +42,8 @@ export default {
const allResponse = await fetch(`https://data.waag.org/api/getStations`)
const all = await allResponse.json()
// const latestRawResponse = await fetch(`https://data.waag.org/api/getAllSensors`)
// let latestRaw = await latestRawResponse.json()
const latestRawResponse = await fetch(`https://data.waag.org/api/getAllSensors`)
let latestRaw = await latestRawResponse.json()
const latestResponse = await fetch(`https://data.waag.org/api/holu/holukit/calibrated/recent`)
let latest = await latestResponse.json()
......@@ -59,6 +59,7 @@ export default {
})
.map(sensor => {
const sensorData = latest.find(o => +o.id === +sensor.id)
const sensorDataReturn = {
id: sensor.id.toString(),
......@@ -68,23 +69,30 @@ export default {
coordinates: [sensor.longitude, sensor.latitude],
mean: null,
timestamp: null,
dataAge: null
dataAge: null,
status: null
}
if(sensorData) {
const dataAgeInHours = ((new Date() - new Date(sensorData.time)) / 3600000) - 1
if(dataAgeInHours >= 48 || dataAgeInHours < 0) {
sensorDataReturn.name = sensorDataReturn.name + " (offline)"
sensorDataReturn.status = 0
} else if(dataAgeInHours < 8) {
sensorDataReturn.mean = sensorData[`${parameter}_mean`]
} else {
sensorDataReturn.name = sensorDataReturn.name + " (no recent data)"
sensorDataReturn.status = 1
}
sensorDataReturn.timestamp = sensorData.time
sensorDataReturn.dataAge = dataAgeInHours
} else {
sensorDataReturn.name = sensorDataReturn.name + " (offline)"
const sensorDataRaw = latestRaw.find(o => +o.id === +sensor.id)
if(sensorDataRaw) {
sensorDataReturn.status = 2
} else {
sensorDataReturn.status = 0
}
offlineSensors.push(sensor.id)
}
......
//import { roundBy } from "../../data-on-a-map-app/src/util/math"
import { addMissingHours, pad } from "../../data-on-a-map-app/src/util/data"
//const host = "/api"
const host = "http://hl.ao.waag.org/api"
//const host = "http://localhost:4567"
export default {
name: "holu",
sides: 24,
angle: 0,
factor: 0.5,
dataStart: "2019-06-25T00:00:00.000Z",
all: async function(parameter) {
try {
const allResponse = await fetch(`https://data.waag.org/api/getStations`)
const all = await allResponse.json()
const latestResponse = await fetch(`${host}/sensor/${parameter}`)
let latest = await latestResponse.json()
latest = latest.map(s => {
const key = Object.keys(s)[0]
return {
id: key.slice(-3),
data: s[key]
}
})
let offlineSensors = []
let unregisteredSensors = []
let places = {}
const data = all.map(sensor => {
const sensorData = latest.find(o => {
return +o.id === +sensor.id
})
const sensorDataReturn = {
id: sensor.id.toString(),
name: "HoLu-kit " + sensor.id.toString() + ": " + sensor.name,
source: "holu",
coordinates: [sensor.longitude, sensor.latitude],
mean: null,
max: null
}
if(sensorData) {
const sensorDataToUse = (sensorData.data[0] ? sensorData.data[0][Object.keys(sensorData.data[0])[0]] : sensorData.data[1][Object.keys(sensorData.data[1])[0]])
const dataAge = (new Date() - new Date(sensorDataToUse.timestamp))
//console.log(sensor.id, sensorDataToUse.timestamp, `${roundBy(dataAge / 1000 / 60 / 60, 2)} uur`)
if(dataAge < 3600000) {
sensorDataReturn.mean = sensorDataToUse.value
} else {
sensorDataReturn.name = sensorDataReturn.name + " (geen recente data)"
}
} else {
offlineSensors.push(sensor.id)
sensorDataReturn.name = sensorDataReturn.name + " (offline)"
}
return sensorDataReturn
})
latest.forEach(sensor => {
if(!all.find(o => +o.id === +sensor.id)) {
unregisteredSensors.push(sensor.id)
}
})
console.log(`Offline Sensors (${offlineSensors.length}):`, offlineSensors.sort((a, b) => a - b).join(', '))
console.log(`Unregistered Sensors (${unregisteredSensors.length}):`, unregisteredSensors.sort((a, b) => a - b).join(', '))
// Object.keys(places).forEach(function(place) {
// places[place].amountOfSensors = places[place].allAverages.length
// places[place].averageOfAverages = places[place].allAverages.reduce((a, b) => a + b, 0) / places[place].allAverages.length
// places[place].averageOfPeaks = places[place].allPeaks.reduce((a, b) => a + b, 0) / places[place].allPeaks.length
// places[place].highestAverage = Math.max(...places[place].allAverages)
// places[place].highestPeak = Math.max(...places[place].allPeaks)
// })
// console.log(places)
return data
} catch (error) {
console.log(error)
}
},
stationData: async function(station, parameter, start, end) {
try {
const stationMeta = {
name: station.name,
description: (station.type === "extended" ? (
'<span>Dit is een extended HOLU-meetstation. Dit prototype meet PM2.5 en PM10 en ook NO2 en O3. Als onderdeel van de pilot wordt er onderzoek gedaan naar de kwaliteit van de data afkomstig van deze meetstations.<br /><a class="more" href="https://hollandseluchten.waag.org/holu-sensorkit/">Lees meer over de HOLU kit</a></span>'
) : (
'<span>Dit is een basic HOLU-meetstation. Dit prototype meet PM2.5 en PM10. Als onderdeel van de pilot wordt er onderzoek gedaan naar de kwaliteit van de data afkomstig van deze meetstations.<br /><a class="more" href="https://hollandseluchten.waag.org/holu-sensorkit/">Lees meer over de HOLU kit</a></span>'
)
)
}
const response = await fetch(`${host}/sensor/${pad(station.id, 3)}/${parameter}?start=${start.slice(0,10)}&end=${end.slice(0,10)}`)
const responseData = await response.json()
const conformedData = responseData[0][Object.keys(responseData[0])[0]].map(m => {
return {
timestamp: new Date(m.timestamp).getTime(),
value: m.value,
}
})
const data = addMissingHours(conformedData)
return { stationMeta, data }
} catch (error) {
console.log(error)
}
},
download: function(id, parameter, start, end) {
return `${host}/sensor/${pad(id, 3)}/${parameter}/csv?start=${start}&end=${end}`
}
}
\ No newline at end of file
data-on-a-map-app @ 0484a252
Subproject commit f57105d08018b10b246e528c4deb24ada2408390
Subproject commit 0484a2527d8c652b7a78ede15285e63d230cb22e
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment