-
-
Save herrjonas/1d7d7dc883854e70fa7e5918e5fd2b1a to your computer and use it in GitHub Desktop.
iOS Widget, das den CO2-Gehalt in verschiedenen Hafven Coworking, Maker und Meeting Spaces anzeigt (für die scriptable.app)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Forked from https://gist.github.com/marco79cgn/23ce08fd8711ee893a3be12d4543f2d2 | |
| // Nov 7th 2020 16:44 CET | |
| /* | |
| Scriptable iOS widget that shows the latest co2 amount in the air in the Hafven Coworking, Maker and Meeting Spaces | |
| Installation guide: | |
| 1. Install Scriptable App on your iOS device | |
| 2. Create a new script inside the Scriptable App by tapping the '+' in the upper right corner | |
| 3. Copy and paste this script into the scipt window and name the script the way you want | |
| 4. add a scriptable widget to your home screen and choose the script name from step 3. in widget-settings | |
| 5. Choose the space which's data you'd like to display by setting the "Parameter" field to "Open Space", "Silent Space", "Meeting Space" or "Grossraum" | |
| */ | |
| // Copyright (C) 2020 by Jonas Lindemann for Hafven GmbH & Co. KG <hello@jonaslindemann.com> | |
| // | |
| // Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. | |
| // | |
| // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL | |
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | |
| // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER | |
| // IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE | |
| // OF THIS SOFTWARE. | |
| // | |
| // The Hafven brand is a registered trademark (c) Hafven GmbH & Co. KG | |
| /* | |
| SETUP AND INITIALISATION | |
| */ | |
| // handle user input from Widget "Parameter" | |
| var widgetInput = ''; | |
| if (args.widgetParameter && Array.isArray(args.widgetParameter)) { | |
| widgetInput = args.widgetParameter.join(' '); | |
| } else if (args.widgetParameter && typeof args.widgetParameter === 'string') { | |
| widgetInput = args.widgetParameter; | |
| } | |
| // create the widget | |
| const widget = new ListWidget(); | |
| await createWidget(); | |
| // used for debugging if script runs inside the app | |
| if (!config.runsInWidget) { | |
| await widget.presentSmall(); | |
| } | |
| Script.setWidget(widget); | |
| Script.complete(); | |
| /* | |
| HELPER FUNCTIONS | |
| */ | |
| // get hafven logo from local filestore or download once | |
| async function getHafvenLogo() { | |
| let fm = FileManager.local(); | |
| let dir = fm.documentsDirectory(); | |
| let path = fm.joinPath(dir, 'hafven-logo.png'); | |
| if (fm.fileExists(path)) { | |
| return fm.readImage(path); | |
| } else { | |
| // download once | |
| let iconImage = await loadImage('https://collaborates.s3.eu-central-1.amazonaws.com/community.hafven.de/hafven-logo.png'); | |
| 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(); | |
| } | |
| // get the sensor data from the API | |
| async function getApiData(stationId) { | |
| const url = 'https://4tpp0r1b6a.execute-api.eu-central-1.amazonaws.com/v1/sensors?station_id=' + encodeURI(stationId); | |
| req = new Request(url); | |
| return await req.loadJSON(); | |
| } | |
| // build the content of the widget | |
| async function createWidget() { | |
| widget.addSpacer(4); | |
| widget.backgroundColor = new Color("#ffffff", 1.0); | |
| const logoImg = await getHafvenLogo(); | |
| const sensorData = await getApiData(widgetInput); | |
| const timeStamp = new Date(sensorData[0].time_utc * 1000); | |
| const colorDark = new Color("#000000", 1); | |
| const colorlighter = new Color("#000000", 0.5); | |
| const colorGood = new Color("#6DD400", 1); | |
| const colorModerate = new Color("#F7B500", 1); | |
| const colorBad = new Color("#FF6565", 1); | |
| const fontSmall = Font.mediumRoundedSystemFont(12); | |
| const fontLarge = Font.mediumRoundedSystemFont(32); | |
| widget.setPadding(5, 5, 5, 5); | |
| const logoStack = widget.addStack(); | |
| const wimg = logoStack.addImage(logoImg); | |
| wimg.imageSize = new Size(60, 30); | |
| let row = widget.addStack(); | |
| row.layoutHorizontally(); | |
| row.topAlignContent(); | |
| const spaceTitle = row.addText(sensorData[0].station_name); | |
| spaceTitle.font = Font.mediumRoundedSystemFont(12); | |
| spaceTitle.textColor = colorlighter; | |
| let row2 = widget.addStack(); | |
| row2.layoutHorizontally(); | |
| row2.addSpacer(2); | |
| const ppm = row2.addText(String(sensorData[0].co2)); | |
| ppm.font = Font.mediumRoundedSystemFont(32); | |
| ppm.textColor = new Color("000000"); | |
| let row3 = widget.addStack(); | |
| row3.layoutHorizontally(); | |
| row3.addSpacer(2); | |
| const unit = row3.addText('CO2 in ppm'); | |
| unit.font = Font.mediumRoundedSystemFont(12); | |
| unit.textColor = new Color("000000"); | |
| const timeText = row3.addText(' ' + timeStamp.toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'}) + ' Uhr'); | |
| timeText.font = fontSmall; | |
| timeText.textColor = colorlighter; | |
| row3.addSpacer(2); | |
| let row5 = widget.addStack(); | |
| row5.layoutHorizontally(); | |
| row5.addSpacer(2); | |
| row5.cornerRadius = 5; | |
| row5.setPadding(2,2,2,2); | |
| var healthStatus; | |
| switch (true) { | |
| case (sensorData[0].co2 > 1000): | |
| healthStatus = row5.addText("Schlechte Luftqualität. Bitte lüften!"); | |
| row5.backgroundColor = colorBad; | |
| break; | |
| case (sensorData[0].co2 > 600): | |
| healthStatus = row5.addText("Moderate Luftqualität"); | |
| row5.backgroundColor = colorModerate; | |
| break; | |
| default: | |
| healthStatus = row5.addText("Gute Luftqualität"); | |
| row5.backgroundColor = colorGood; | |
| break; | |
| } | |
| healthStatus.font = fontSmall; | |
| healthStatus.textColor = colorlighter; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Intro
Das Hafven Scriptable Widget zeigt den CO2-Gehalt in verschiedenen Hafven Coworking, Maker und Meeting Spaces an. Die Daten werden mithilfe von Netatmo Healthy Home Coach Geräten alle 10 Minuten erhoben und dauerhaft in einer Statistikdatenbank gespeichert. Die Daten sind über eine REST-API abrufbar, die auch in diesem Projekt genutzt wird.
Die angezeigten Grenzwerte von 600ppm bzw. 1.000ppm orientieren sich an den Empfehlungen der Deutschen Gesetzlichen Unfallversicherung.
Anforderungen
iOS 14
Scriptable version 1.5 (oder neuer)
Installation
Danke
Danke an @marco79cgn für die Inspiration durch sein großartiges DM-Klopapier-Widget
Disclaimer
Die angezeigten Daten sollen lediglich eine Orientierung dabei bieten, wie sich Raumluftqualität im Laufe der Zeit entwickelt. Selbst wenn die Raumluftqualität gut ist können, Krankheiten weiter übertragen werden.