2018-10-05 17:25:52 +02:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Displays the state of a spaceapi endpoint
|
|
|
|
|
|
|
|
Requires the following libraries:
|
2018-10-05 17:54:43 +02:00
|
|
|
* requests
|
2018-10-05 17:25:52 +02:00
|
|
|
* json
|
|
|
|
* time
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* spaceapi.url: String representation of the api endpoint
|
|
|
|
* spaceapi.name: String overwriting the space name
|
|
|
|
* spaceapi.prefix: Prefix for the space string
|
|
|
|
* spaceapi.interval: time between updates
|
|
|
|
"""
|
|
|
|
|
|
|
|
import bumblebee.input
|
|
|
|
import bumblebee.output
|
|
|
|
import bumblebee.engine
|
|
|
|
|
2018-10-05 17:54:43 +02:00
|
|
|
import requests
|
2018-10-05 17:25:52 +02:00
|
|
|
import json
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
class Module(bumblebee.engine.Module):
|
|
|
|
def __init__(self, engine, config):
|
|
|
|
super(Module, self).__init__(
|
|
|
|
engine, config, bumblebee.output.Widget(full_text=self.getState)
|
|
|
|
)
|
|
|
|
self._state = False
|
|
|
|
self._error = False
|
|
|
|
self._url = self.parameter("url",
|
|
|
|
default="http://club.entropia.de/spaceapi")
|
|
|
|
self._name = self.parameter("name", default="")
|
|
|
|
self._lastQuery = 0
|
|
|
|
self._sleeptime = self.parameter("interval", default=300)
|
|
|
|
|
|
|
|
def getState(self, widget):
|
|
|
|
string = self.parameter("prefix", default="")
|
|
|
|
string += self._name + ": "
|
|
|
|
if self._error:
|
|
|
|
string += "ERROR"
|
|
|
|
elif self._state:
|
|
|
|
string += "Open"
|
|
|
|
else:
|
|
|
|
string += "Closed"
|
|
|
|
return string
|
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
if self._error:
|
|
|
|
return ["critical"]
|
|
|
|
elif self._state:
|
|
|
|
return ["warning"]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
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:
|
2018-10-05 17:54:43 +02:00
|
|
|
|
|
|
|
with requests.get(self._url) as u:
|
|
|
|
data = u.json()
|
2018-10-05 17:25:52 +02:00
|
|
|
self._state = data["state"]["open"]
|
|
|
|
self._name = self.parameter("name", default=data["space"])
|
|
|
|
self._error = False
|
|
|
|
except Exception:
|
|
|
|
# Displays ERROR status
|
|
|
|
self._error = True
|
|
|
|
|
|
|
|
|
|
|
|
# Author: Tobias Manske <tobias.manske@mailbox.org>
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|