Merge pull request #300 from rad4day/master

[modules/spaceapi] refactor, add comments, use interval function
This commit is contained in:
tobi-wan-kenobi 2018-10-06 19:46:41 +02:00 committed by GitHub
commit 31c4e9ebe6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,14 +4,12 @@
Requires the following libraries: Requires the following libraries:
* requests * requests
* json
* time
Parameters: Parameters:
* spaceapi.url: String representation of the api endpoint * spaceapi.url: String representation of the api endpoint
* spaceapi.name: String overwriting the space name * spaceapi.name: String overwriting the space name
* spaceapi.prefix: Prefix for the space string * spaceapi.prefix: Prefix for the space string
* spaceapi.interval: time between updates * spaceapi.interval: time between updates in minutes
""" """
import bumblebee.input import bumblebee.input
@ -19,8 +17,6 @@ import bumblebee.output
import bumblebee.engine import bumblebee.engine
import requests import requests
import json
import time
class Module(bumblebee.engine.Module): class Module(bumblebee.engine.Module):
@ -28,44 +24,47 @@ class Module(bumblebee.engine.Module):
super(Module, self).__init__( super(Module, self).__init__(
engine, config, bumblebee.output.Widget(full_text=self.getState) engine, config, bumblebee.output.Widget(full_text=self.getState)
) )
self._state = False
# Represents the state of the hackerspace
self._open = False
# Set to true if there was an error calling the spaceapi
self._error = False self._error = False
# The URL representing the api endpoint
self._url = self.parameter("url", self._url = self.parameter("url",
default="http://club.entropia.de/spaceapi") default="http://club.entropia.de/spaceapi")
# Space Name, can be set manually in case of multiple widgets,
# so you're able to distinguish
self._name = self.parameter("name", default="") self._name = self.parameter("name", default="")
self._lastQuery = 0
self._sleeptime = self.parameter("interval", default=300) # Only execute every 5 minutes by default
self.interval(self.parameter("interval", default=5))
def getState(self, widget): def getState(self, widget):
string = self.parameter("prefix", default="") text = self.parameter("prefix", default="")
string += self._name + ": " text += self._name + ": "
if self._error: if self._error:
string += "ERROR" text += "ERROR"
elif self._state: elif self._open:
string += "Open" text += "Open"
else: else:
string += "Closed" text += "Closed"
return string return text
def state(self, widget): def state(self, widget):
if self._error: if self._error:
return ["critical"] return ["critical"]
elif self._state: elif self._open:
return ["warning"] return ["warning"]
else: else:
return [] return []
def update(self, widgets): def update(self, widgets):
unixtime = time.mktime(time.gmtime())
# Only query again after interval has passed
if self._lastQuery + self._sleeptime < int(unixtime):
self._lastQuery = int(unixtime)
try: try:
with requests.get(self._url) as u: with requests.get(self._url) as u:
data = u.json() json = u.json()
self._state = data["state"]["open"] self._open = json["state"]["open"]
self._name = self.parameter("name", default=data["space"]) self._name = self.parameter("name", default=json["space"])
self._error = False self._error = False
except Exception: except Exception:
# Displays ERROR status # Displays ERROR status