Have human-friendly copy for spaceapi #7

Merged
maia merged 2 commits from spaceapi-human into main 2025-02-27 15:05:46 -05:00

3
.gitignore vendored
View file

@ -1,3 +1,4 @@
# Calendar file used for local development. On a production server, the calendar
# file is made available at the same path by reverse proxy.
calendar.ics
calendar.ics
spaceapi.json

View file

@ -22,13 +22,27 @@ async function doSpaceapi(url, targetElementId) {
const spaceapi = await (await fetch(url)).json();
console.log(spaceapi);
const state = spaceapi["state"];
const sensors = spaceapi["sensors"];
targetElement.innerHTML = `
{
"open": "${escape(state.open.toString())}",
"lastchange": "${escape(state.lastchange.toString())}"
}
`;
var openHtml = '';
if (state && state.lastchange && typeof state.open !== 'undefined') {
const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' })
const elapsed = state.lastchange - Date.now() / 1000.0;
const unit = elapsed < 3600 ? 'minutes' : 'hours';
const elapsedInUnit = Math.round(elapsed < 3600 ? elapsed / 60 : elapsed / 3600);
if (state.open) {
openHtml = 'someone might be at the space (as of ' + rtf.format(elapsedInUnit, unit) + ') :D <br>';
} else {
openHtml = 'the space is probably closed (as of ' + rtf.format(elapsedInUnit, unit) + ') <br>';
}
}
var tempHtml = '';
if (sensors && sensors.temperature && sensors.temperature.length > 0) {
tempHtml = 'it is about ' + sensors.temperature[0].value + sensors.temperature[0].unit + ' inside'
}
targetElement.innerHTML = '<p>' + openHtml + tempHtml + '</p>';
}
catch (error) {
targetElement.innerHTML = '<p>Sorry - couldn\'t load the spaceapi :(</p> <small>' + error + '</small>';

View file

@ -6,6 +6,7 @@
<link rel="stylesheet" href="style.css">
<script src="vendor/ical.min.js"></script>
<script src="vendor/rrule.2.8.1.min.js"></script>
<script src="https://polyfill-fastly.io/v3/polyfill.min.js?features=Intl.RelativeTimeFormat,Intl.RelativeTimeFormat.~locale.en"></script>
<script defer src="index-cal.js"></script>
<script defer src="index-spaceapi.js"></script>