From c5db6e2c1c45a41bdd4a85990c0d0b883915b3c3 Mon Sep 17 00:00:00 2001 From: Kevin Kub Date: Wed, 21 Oct 2020 08:43:33 +0200 Subject: [PATCH] --- incidence.js | 109 +++++++++++++++++++++++++-------------------------- 1 file changed, 53 insertions(+), 56 deletions(-) diff --git a/incidence.js b/incidence.js index da3e41d..40df359 100644 --- a/incidence.js +++ b/incidence.js @@ -1,72 +1,69 @@ // Licence: Robert Koch-Institut (RKI), dl-de/by-2-0 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=${location.longitude.toFixed(3)}%2C${location.latitude.toFixed(3)}&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelWithin&returnGeometry=false&outSR=4326&f=json` +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' -let widget = await createWidget() +const widget = await createWidget() if (!config.runsInWidget) { await widget.presentSmall() } - Script.setWidget(widget) Script.complete() async function createWidget(items) { - let location - - if(args.widgetParameter) { - - const fixedCoordinates = args.widgetParameter.split(",").map(parseFloat) - - location = { - latitude: fixedCoordinates[0], - longitude: fixedCoordinates[1] - } - - } else { - - Location.setAccuracyToThreeKilometers() - location = await Location.current() - - } - - const data = await new Request(apiUrl(location)).loadJSON() - - if(!data || !data.features || !data.features.length) { - const errorList = new ListWidget() - errorList.addText("Keine Ergebnisse für den aktuellen Ort gefunden.") - return errorList - } - - const attr = data.features[0].attributes - const incidence = attr.cases7_per_100k.toFixed(1) - const cityName = attr.GEN + const data = await getData() const list = new ListWidget() - - if(Device.isUsingDarkAppearance()){ - const gradient = new LinearGradient() - gradient.locations = [0, 1] - gradient.colors = [ - new Color("111111"), - new Color("222222") - ] - list.backgroundGradient = gradient - } - const header = list.addText("🦠 Inzidenz".toUpperCase()) header.font = Font.mediumSystemFont(13) - - list.addSpacer() - - const label = list.addText(incidence+"") - label.font = Font.boldSystemFont(24) - label.textColor = Color.green() - - if(incidence >= 50) { - label.textColor = Color.red() - } else if(incidence >= 35) { - label.textColor = Color.orange() + 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) + if(data.shouldCache) { + list.refreshAfterDate = new Date(Date.now() + 60*60*1000) + } + } else { + list.addSpacer() + list.addText("Daten nicht verfügbar") } - - list.addText(cityName) - return list +} + +async function getData() { + try { + const location = await getLocation() + if(location) { + let data = await new Request(apiUrl(location)).loadJSON() + const attr = data.features[0].attributes + return { incidence: attr.cases7_per_100k.toFixed(1), areaName: attr.GEN, shouldCache: true }; + } 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; + } } \ No newline at end of file