Skip to content

Instantly share code, notes, and snippets.

@ubergesundheit
Last active September 21, 2016 20:43
Show Gist options
  • Save ubergesundheit/379d13375ca95b9d62c4 to your computer and use it in GitHub Desktop.
Save ubergesundheit/379d13375ca95b9d62c4 to your computer and use it in GitHub Desktop.
"use strict"
const BASEURL = "https://api.opensensemap.org"
const BOXES_URL = `${BASEURL}/boxes`
const SINGLE_DAY = 86400000
const SINGLE_WEEK = SINGLE_DAY * 7
let now = Date.now()
now = SINGLE_DAY * Math.floor(now / SINGLE_DAY)
now -= SINGLE_DAY
const LAST_DATE = new Date(now)
let handleBoxes = function (json) {
// prepare boxes in one pass
let boxes = [];
for (let box of json) {
let { name, boxType, _id, loc } = box
let sensors = [];
for (let sensor of box.sensors) {
if (sensor.lastMeasurement !== null) {
let lastMeasurementDate = new Date(sensor.lastMeasurement.updatedAt)
if (lastMeasurementDate > LAST_DATE) {
sensor.lastMeasurement.updatedAt = lastMeasurementDate
sensors.push(`${sensor.title}: ${sensor.lastMeasurement.value} ${sensor.unit}`)
}
}
}
if (sensors.length !== 0) {
boxes.push({ name, boxType, _id, loc, sensors })
console.log(_id, name, sensors)
}
}
console.log(boxes.length)
console.log(boxes)
}
let boxesNames = function (json) {
return json.map(box => box.name)
}
// dl the data..
fetch(BOXES_URL)
.then(res => res.json())
.then(boxesNames)
.then(function (names) {
window.names = names
})
// dl the data..
fetch(BOXES_URL)
.then(res => res.json())
.then(handleBoxes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment