Skip to content

Instantly share code, notes, and snippets.

@cheanrod
Created November 1, 2020 09:26
Show Gist options
  • Save cheanrod/fdbb61f1b8f61e85161e5ee3dcc151ad to your computer and use it in GitHub Desktop.
Save cheanrod/fdbb61f1b8f61e85161e5ee3dcc151ad to your computer and use it in GitHub Desktop.
Scriptable widget for Luftdaten.info
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: cyan; icon-glyph: database;
const warningColor = new Color("#E50000")
const legendFont = 12
const valueFont = 24
const timeFont = 8
let sensor = 1721
let param = args.widgetParameter
if (param != null && param.length > 0) {
sensor = param
}
const widget = new ListWidget()
const sensorValues = await fetchsensorValues()
await createWidget()
// used for debugging if script runs inside the app
if (!config.runsInWidget) {
await widget.presentSmall()
}
Script.setWidget(widget)
Script.complete()
// build the content of the widget
async function createWidget() {
widget.backgroundColor = new Color("#38b5ad", 1.0)
const logoImg = await getImage('luftdaten-logo.png')
widget.setPadding(16,6,6,6)
// logo
const logoImageStack = widget.addStack()
logoImageStack.addSpacer()
const wimg = logoImageStack.addImage(logoImg)
wimg.imageSize = new Size(50,50)
logoImageStack.addSpacer()
// p1 value
let row = widget.addStack()
row.addSpacer()
let column = row.addStack()
column.layoutVertically()
const p1Text = column.addText("PM10")
p1Text.font = Font.mediumRoundedSystemFont(legendFont)
p1Text.textColor = Color.white()
const p1Value = column.addText(sensorValues[0].toString())
p1Value.font = Font.mediumRoundedSystemFont(valueFont)
if (sensorValues[0] > 30) {
p1Value.textColor = warningColor
} else {
p1Value.textColor = Color.black()
}
row.addSpacer()
// p2 value
let row2 = widget.addStack()
row2.addSpacer()
let column2 = row2.addStack()
column2.layoutVertically()
const p2Text = column2.addText("PM2.5")
p2Text.font = Font.mediumRoundedSystemFont(legendFont)
p2Text.textColor = Color.white()
const p2Value = column2.addText(sensorValues[1].toString())
p2Value.font = Font.mediumRoundedSystemFont(valueFont)
if (sensorValues[1] > 30) {
p2Value.textColor = warningColor
} else {
p2Value.textColor = Color.black()
}
row2.addSpacer()
// timestamp
const timestampStack = widget.addStack()
timestampStack.addSpacer()
const timestr = sensorValues[2].replace(' ', 'T')
const date = new Date(timestr + '+00')
let timestampText = timestampStack.addText(date.toLocaleString())
timestampText.font = Font.mediumRoundedSystemFont(timeFont)
timestampText.textColor = Color.white()
timestampStack.addSpacer()
}
async function fetchsensorValues() {
let url
let counter = 0
url = 'https://data.sensor.community/airrohr/v1/sensor/' + sensor + '/'
const req = new Request(url)
const apiResult = await req.loadJSON()
let p1
let p2
for (var i in apiResult[0].sensordatavalues) {
if (apiResult[0].sensordatavalues[i].value_type == 'P1')
p1 = apiResult[0].sensordatavalues[i].value
else if (apiResult[0].sensordatavalues[i].value_type == 'P2')
p2 = apiResult[0].sensordatavalues[i].value
}
return [p1, p2, apiResult[0].timestamp]
}
// get images from local filestore or download them once
async function getImage(image) {
let fm = FileManager.local()
let dir = fm.documentsDirectory()
let path = fm.joinPath(dir, image)
if (fm.fileExists(path)) {
return fm.readImage(path)
} else {
// download once
let imageUrl
switch (image) {
case 'luftdaten-logo.png':
imageUrl = "https://luftdaten.info/wp-content/uploads/2018/01/luftdaten_logo_partner.png"
break
default:
console.log(`Sorry, couldn't find ${image}.`);
}
let iconImage = await loadImage(imageUrl)
fm.writeImage(path, iconImage)
return iconImage
}
}
// helper function to download an image from a given url
async function loadImage(imgUrl) {
const req = new Request(imgUrl)
return await req.loadImage()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment