Minimal new API definition

This commit is contained in:
Peter Ludikovsky 2020-07-23 16:19:09 +02:00 committed by Peter Ludikovsky
parent a4eb236e48
commit 9a3a90a588
Signed by: pludi
GPG Key ID: FB1A00FEE77E2C36
4 changed files with 193 additions and 84 deletions

View File

@ -1,7 +1,3 @@
# spacestatus
# SpaceStatus
A simple `bash` script to display the open/close status of `/usr/space`.
Depends on:
- `isc-dhcp-server`
- `arping`
The new API for the Hacker-/Makerspace `/usr/space`, replacing the old `bash` script.

191
docs/SpaceStatusAPI.json Normal file
View File

@ -0,0 +1,191 @@
{
"openapi": "3.0.2",
"info": {
"title": "SpaceStatusAPI",
"contact": {
"name": "Peter Ludikovsky",
"email": "peter@ludikovsky.name"
},
"license": {
"name": "CC-BY-SA 4.0",
"url": "https://creativecommons.org/licenses/by-sa/4.0/"
},
"version": "1.0"
},
"servers": [
{
"url": "https://api.usrspace.at/v1"
}
],
"paths": {
"/spaceapi": {
"get": {
"summary": "Returns the current status as JSON suitable for SpaceAPI compatible software",
"responses": {
"200": {
"description": "SpaceAPI compatible response",
"content": {
"application/json": {
"schema": {
"$ref": "https://raw.githubusercontent.com/SpaceApi/schema/master/13.json"
}
}
}
}
}
}
},
"/access": {
"get": {
"summary": "Returns the current Open/Close status",
"responses": {
"200": {
"description": "Current status",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"isOpen"
],
"properties": {
"isOpen": {
"type": "boolean",
"description": "If the Space is open"
},
"openSince": {
"type": "string",
"format": "date-time",
"description": "Since when it's open, optional"
},
"openTo": {
"type": "string",
"format": "date-time",
"description": "Probable closing time, optional"
},
"message": {
"type": "string",
"description": "An optional message, eg. reason for opening"
}
}
},
"examples": {
"closed": {
"summary": "Closed Space",
"value": {
"isOpen": false
}
},
"open_end": {
"summary": "Open End",
"value": {
"isOpen": true,
"openSince": "2020-07-23T19:00:00+0200",
"message": "Donnerstags-Stammtisch"
}
},
"open": {
"summary": "Open & Closing time",
"value": {
"isOpen": true,
"openSince": "2016-05-01T19:00:00+0200",
"openTo": "2016-05-01T22:00:00+0200"
}
}
}
}
}
}
}
}
},
"/access/open": {
"post": {
"description": "Open the Space",
"parameters": [
{
"name": "openTo",
"description": "Time until closing again",
"schema": {
"type": "string",
"format": "date-time"
},
"in": "query",
"example": "2020-07-23T22:00:00+0200"
},
{
"name": "message",
"description": "An optional message, eg. reason for opening",
"schema": {
"type": "string"
},
"in": "query",
"example": "Donnerstags-Stammtisch"
}
],
"responses": {
"200": {
"description": "OK"
},
"401": {
"description": "Unauthorized"
},
"409": {
"description": "Already open"
}
}
},
"patch": {
"description": "Change the current session",
"parameters": [
{
"name": "openTo",
"description": "Time until closing again",
"schema": {
"type": "string",
"format": "date-time"
},
"in": "query",
"example": "2020-07-23T22:00:00+0200"
},
{
"name": "message",
"description": "An optional message, eg. reason for opening",
"schema": {
"type": "string"
},
"in": "query",
"example": "Donnerstags-Stammtisch"
}
],
"responses": {
"200": {
"description": "OK"
},
"401": {
"description": "Unauthorized"
},
"404": {
"description": "Space currently closed"
}
}
}
},
"/access/close": {
"get": {
"description": "Close the Space",
"responses": {
"200": {
"description": "OK"
},
"401": {
"description": "Unauthorized"
},
"409": {
"description": "Already closed"
}
}
}
}
}
}

View File

@ -1 +0,0 @@
{"api":"0.13","space":"/usr/space","logo":"","url":"https://usrspace.at","location":{"lat":47.92305,"lon":16.21173,"address":"Mühlgasse 8, 2544 Leobersdorf, Austria"},"state":{"open":%s,"lastchange":%d},"contact":{"twitter":"@usrspace_at","email":"kernel AT usrspace.at","facebook":"https://facebook.com/usrspace.at","keymasters":[{"name":"Peter","twitter":"@pludikovsky"}]},"issue_report_channels":["email","twitter"],"feeds":{"calendar":{"type":"ical","url":"https://usrspace.at/termine.ics"}},"cache":{"schedule":"m.05"},"projects":["https://gitlab.usrspace.at/","https://www.usrspace.at/w/index.php?title=Kategorie:Projekte"]}

View File

@ -1,77 +0,0 @@
#!/usr/bin/env bash
[ ! -z "$DEBUG" ] && set -x
#set -eu
STATUS_HTML=/var/tmp/spacestatus.html
STATUS_JSON=/var/tmp/spacestatus.json
function write_html {
status=$1
TEMPLATE_HTML='<!DOCTYPE html><html><head><title>Status /usr/space: %s</title></head><body><p><code style="color: %s">%s</code></p></html>'
CLOSED_HTML='Closed darkred Closed'
OPEN_HTML='Open brightgreen Open'
if [ "$status" -gt 0 ]
then
printf "$TEMPLATE_HTML" $OPEN_HTML > $STATUS_HTML
else
printf "$TEMPLATE_HTML" $CLOSED_HTML > $STATUS_HTML
fi
}
function write_json {
status=$1
TEMPLATE_JSON=$( cat /usr/local/etc/spacestatus.json )
CLOSED_JSON='false'
OPEN_JSON='true'
# Previous status Open & Open now -> skip
grep -q '"open":true' $STATUS_JSON
if [[ $? -eq 0 && "$status" -gt 0 ]]
then
return
fi
# Previous status Closed & Closed now -> skip
grep -q '"open":false' $STATUS_JSON
if [[ $? -eq 0 && "$status" -eq 0 ]]
then
return
fi
if [ "$status" -gt 0 ]
then
printf "$TEMPLATE_JSON" $OPEN_JSON $( date +%s ) > $STATUS_JSON
else
printf "$TEMPLATE_JSON" $CLOSED_JSON $( date +%s ) > $STATUS_JSON
fi
}
function publish {
CURL=/usr/bin/curl
WEBDAV=https://example.com
CREDS='user:pass'
${CURL} --user "${CREDS}" -T "${STATUS_HTML}" "${WEBDAV}"
${CURL} --user "${CREDS}" -T "${STATUS_JSON}" "${WEBDAV}"
}
tmpcount=$(mktemp)
exec 3>$tmpcount
/usr/sbin/dhcp-lease-list --parsable | awk '{print $4}' | while read ip
do
/usr/bin/arping -w 1 -f -q $ip
[ $? -eq 0 ] && echo 1 >&3
done
exec 3>&-
leases=$( cat $tmpcount | wc -l )
write_html $leases
write_json $leases
publish
rm $tmpcount