Skip to content

Instantly share code, notes, and snippets.

@mariopesch

mariopesch/osem-widget.js

Last active Oct 30, 2020
Embed
What would you like to do?
const boxId = "" // add your boxID
const phenomenonId = "" //add your sensorID
const apiUrl = (boxId) => `https://api.opensensemap.org/boxes/${boxId}`
let widget = await createWidget()
if (!config.runsInWidget) {
await widget.presentSmall()
}
Script.setWidget(widget)
Script.complete()
async function createWidget(items) {
const data = await new Request(apiUrl(boxId)).loadJSON()
if (!data || !data.name || !data.sensors) {
const errorList = new ListWidget()
errorList.addText("Keine Ergebnisse für den aktuellen Ort gefunden.")
return errorList
}
const name = data.name
const sensors = data.sensors
const sensor = sensors.find(sensor => sensor._id == phenomenonId)
const list = new ListWidget()
if (Device.isUsingDarkAppearance()) {
const gradient = new LinearGradient()
gradient.colors = [
new Color("111111"),
new Color("222222")
]
list.backgroundGradient = gradient
}
const header = list.addText(name)
header.font = Font.mediumSystemFont(13)
list.addSpacer()
list.addText(sensor.title)
const label = list.addText(sensor.lastMeasurement.value)
label.font = Font.boldSystemFont(24)
label.textColor = Color.green()
var timeStamp = sensor.lastMeasurement.createdAt
var dateTime = timeStamp.split('T')
var time = list.addText(dateTime[1].slice(0, -8))
time.font = Font.boldSystemFont(8)
time.textColor = Color.blue()
return list
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment