covid-ios.js/incidence.js

81 lines
3.4 KiB
JavaScript
Raw Normal View History

2020-10-19 08:33:25 +02:00
// Licence: Robert Koch-Institut (RKI), dl-de/by-2-0
2020-10-27 19:18:08 +01:00
const apiUrl = (location) => `https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=1%3D1&outFields=GEN,cases7_per_100k&geometry=12.361%2C51.375&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelWithin&returnGeometry=false&outSR=4326&f=json`
const apiUrl2 = (location) => `https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=1%3D1&outFields=GEN,cases7_per_100k&geometry=12.361%2C51.375&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelWithin&returnGeometry=false&outSR=4326&f=json`
2020-10-21 08:43:33 +02:00
const apiUrlStates = 'https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/Coronaf%E4lle_in_den_Bundesl%E4ndern/FeatureServer/0/query?where=1%3D1&outFields=cases7_bl_per_100k&returnGeometry=false&outSR=4326&f=json'
2020-10-17 20:09:59 +02:00
2020-10-21 08:43:33 +02:00
const widget = await createWidget()
2020-10-17 20:09:59 +02:00
if (!config.runsInWidget) {
await widget.presentSmall()
}
Script.setWidget(widget)
Script.complete()
async function createWidget(items) {
2020-10-21 08:43:33 +02:00
const data = await getData()
2020-10-17 20:09:59 +02:00
const list = new ListWidget()
2020-10-18 09:52:16 +02:00
const header = list.addText("🦠 Inzidenz".toUpperCase())
header.font = Font.mediumSystemFont(13)
2020-10-21 08:43:33 +02:00
if(data) {
if(!data.shouldCache) {
list.addSpacer(6)
const loadingIndicator = list.addText("Ort wird ermittelt...".toUpperCase())
loadingIndicator.font = Font.mediumSystemFont(13)
loadingIndicator.textOpacity = 0.5
}
list.addSpacer()
const label = list.addText(data.incidence+"")
label.font = Font.boldSystemFont(24)
label.textColor = data.incidence >= 50 ? Color.red() : data.incidence >= 35 ? Color.orange() : Color.green()
list.addText(data.areaName)
2020-10-27 19:18:08 +01:00
const label2 = list.addText(data2.incidence+"")
label2.font = Font.boldSystemFont(24)
label2.textColor = data2.incidence >= 50 ? Color.red() : data2.incidence >= 35 ? Color.orange() : Color.green()
list.addText(data2.areaName)
2020-10-21 08:43:33 +02:00
if(data.shouldCache) {
list.refreshAfterDate = new Date(Date.now() + 60*60*1000)
}
} else {
list.addSpacer()
list.addText("Daten nicht verfügbar")
2020-10-19 08:33:25 +02:00
}
2020-10-17 20:09:59 +02:00
return list
2020-10-21 08:43:33 +02:00
}
async function getData() {
try {
const location = await getLocation()
if(location) {
let data = await new Request(apiUrl(location)).loadJSON()
const attr = data.features[0].attributes
2020-10-27 19:18:08 +01:00
let data2 = await new Request(apiUrl2(location)).loadJSON()
const attr2 = data2.features[0].attributes
return { incidence: attr.cases7_per_100k.toFixed(1), areaName: attr.GEN, incidence2: attr2.cases7_per_100k.toFixed(1), areaName2: attr2.GEN, shouldCache: true };
2020-10-21 08:43:33 +02:00
} else {
let data = await new Request(apiUrlStates).loadJSON()
const incidencePerState = data.features.map((f) => f.attributes.cases7_bl_per_100k)
const averageIncidence = incidencePerState.reduce((a, b) => a + b) / incidencePerState.length
return { incidence: averageIncidence.toFixed(1), areaName: "Deutschland", shouldCache: false };
}
} catch(e) {
return null
}
}
async function getLocation() {
try {
if(args.widgetParameter) {
const fixedCoordinates = args.widgetParameter.split(",").map(parseFloat)
return { latitude: fixedCoordinates[0], longitude: fixedCoordinates[1] }
} else {
Location.setAccuracyToThreeKilometers()
return await Location.current()
}
} catch(e) {
return null;
}
2020-10-17 20:09:59 +02:00
}