-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
140 additions
and
51 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
const ApiUrl = "https://status.kreativitaet-trifft-technik.de/api/openState"; | ||
|
||
const StateClosed = { | ||
name: "closed", | ||
color: "btn-danger" | ||
}; | ||
|
||
const StateClosing = { | ||
name: "closing", | ||
color: "btn-warning" | ||
}; | ||
|
||
const StateOpen = { | ||
name: "open", | ||
color: "btn-success" | ||
}; | ||
|
||
const States = { | ||
text_id_prefix: "status-text-", | ||
button_id_prefix: "status-", | ||
all_states: [ | ||
StateClosed, | ||
StateClosing, | ||
StateOpen | ||
], | ||
state_map: { | ||
"none": StateClosed, | ||
"closing": StateClosing, | ||
"open": StateOpen, | ||
"open+": StateOpen, | ||
}, | ||
}; | ||
|
||
/** | ||
* @param {string} room_name | ||
* @param {string} color | ||
*/ | ||
function set_button_color(room_name, color) { | ||
let id = States.button_id_prefix + room_name; | ||
for (let state of States.all_states) { | ||
document.getElementById(id).classList.remove(state.color); | ||
} | ||
document.getElementById(id).classList.add(color); | ||
} | ||
|
||
/** | ||
* @param {string} room_name | ||
* @param {string} state_name | ||
*/ | ||
function set_room_status(room_name, state_name) { | ||
let state = States.state_map[state_name]; | ||
if (state) { | ||
let room = Rooms.find(r => r.name === room_name); | ||
|
||
if (room) { | ||
for (let status of States.all_states) { | ||
document.getElementById(`${States.text_id_prefix + status.name}-${room.name}`).classList.add("d-none"); | ||
} | ||
document.getElementById(`${States.text_id_prefix + state.name}-${room.name}`).classList.remove("d-none"); | ||
set_button_color(room_name, state.color) | ||
} else { | ||
throw new Error(`Unknown room supplied: ${room_name}`) | ||
} | ||
} else { | ||
throw new Error(`Unknown status option supplied: ${state_name}`) | ||
} | ||
} | ||
|
||
async function fetch_current_status() { | ||
let currentStatus = await (await fetch(ApiUrl)).json(); | ||
console.info("Processing current status information", currentStatus); | ||
|
||
for (let room_name in currentStatus) { | ||
set_room_status(room_name, currentStatus[room_name].state); | ||
} | ||
} | ||
|
||
function set_all_rooms(state_name = "closed") { | ||
if (!States.state_map[status]) { | ||
throw new Error(`Unknown status option supplied: ${state_name}`) | ||
} | ||
|
||
for (let room of Rooms) { | ||
set_room_status(room.name, state_name); | ||
} | ||
} | ||
|
||
fetch_current_status(); | ||
setInterval(fetch_current_status, 5000); |
This file contains 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