Create CCCHH theme
This commit is contained in:
parent
8394b08524
commit
2d267ba9fe
23 changed files with 629 additions and 1 deletions
84
themes/ccchh/assets/js/roomstate.js
Normal file
84
themes/ccchh/assets/js/roomstate.js
Normal file
|
@ -0,0 +1,84 @@
|
|||
import timeDistance, { deLocale } from 'js/util/timeDistance'
|
||||
|
||||
const spaceapiUrl = "https://www.hamburg.ccc.de/dooris/status.json"
|
||||
const interval_ms = 60000
|
||||
|
||||
const classNameOpen = "open"
|
||||
const classNameClosed = "closed"
|
||||
|
||||
|
||||
function updateRoomState(openState, lastChangeTimestamp) {
|
||||
console.debug("SpaceAPI: Setting room " + openState + " since " + lastChangeTimestamp)
|
||||
|
||||
const roomstate = document.querySelector('#roomstate')
|
||||
var statusItem = roomstate.querySelector(".state")
|
||||
var durationItem = roomstate.querySelector(".duration")
|
||||
|
||||
if (openState === null || lastChangeTimestamp === null) {
|
||||
console.warn("SpaceAPI: Room state unknown");
|
||||
statusItem.textContent = "unbekannt"
|
||||
roomstate.classList.remove(classNameOpen)
|
||||
roomstate.classList.remove(classNameClosed)
|
||||
durationItem.textContent = "(seit unbekannt)"
|
||||
|
||||
} else {
|
||||
if (openState) {
|
||||
statusItem.textContent = "offen"
|
||||
roomstate.classList.remove(classNameClosed)
|
||||
roomstate.classList.add(classNameOpen)
|
||||
|
||||
} else {
|
||||
statusItem.textContent = "geschlossen"
|
||||
roomstate.classList.remove(classNameOpen)
|
||||
roomstate.classList.add(classNameClosed)
|
||||
|
||||
}
|
||||
|
||||
const changeDate = new Date(lastChangeTimestamp * 1000)
|
||||
const changeText = timeDistance(deLocale).inWords(changeDate)
|
||||
|
||||
durationItem.textContent = "(" + changeText + ")"
|
||||
}
|
||||
}
|
||||
|
||||
function parseResponse(apiJson) {
|
||||
console.debug('SpaceAPI got:', apiJson)
|
||||
|
||||
if (typeof apiJson.api === "undefined") {
|
||||
console.error("SpaceAPI JSON invalid: 'api' item not present")
|
||||
return [null, null]
|
||||
}
|
||||
if (typeof apiJson.state === "undefined") {
|
||||
console.error("SpaceAPI JSON invalid: 'state' item not present")
|
||||
return [null, null]
|
||||
}
|
||||
if (typeof apiJson.state.open === "undefined") {
|
||||
console.error("SpaceAPI JSON invalid: 'state.open' item not present")
|
||||
return [null, null]
|
||||
}
|
||||
|
||||
console.debug('SpaceAPI state:', apiJson.state)
|
||||
var openState = (apiJson.state.open == true)
|
||||
var lastchange = null
|
||||
|
||||
if (apiJson.state.lastchange !== "undefined") {
|
||||
lastchange = apiJson.state.lastchange
|
||||
}
|
||||
|
||||
return [openState, lastchange]
|
||||
}
|
||||
|
||||
function update() {
|
||||
fetch(spaceapiUrl)
|
||||
.then(resp => resp.json())
|
||||
.then(function (data) {
|
||||
var state = parseResponse(data)
|
||||
updateRoomState(state[0], state[1])
|
||||
})
|
||||
.catch(err => { throw err });
|
||||
}
|
||||
|
||||
window.onload = function () {
|
||||
update()
|
||||
window.setTimeout(update, interval_ms)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue