1 from threading import Lock
2 from libtuer import ThreadFunction, logger
5 from config import spaceApiKey
8 def __init__ (self, waker):
9 self._state_to_set = None
10 self._state_last_set = None
12 self.set_state = ThreadFunction(self._set_state, "Space API")
13 self._set_state_lock = Lock()
14 waker.register(self.set_state, 10.0) # re-try setting the state every 10 seconds
19 def _do_request(self, state):
20 state_val = 1 if state else 0
22 logger.info("Setting SpaceAPI to %d" % state_val)
23 url = "http://spaceapi.hacksaar.de/status.php?action=update&key=%s&status=%d" % (spaceApiKey, state_val)
24 response = urllib.request.urlopen(url, timeout=5.0)
25 responseText = response.read().decode('utf-8').strip()
26 if response.getcode() == 200 and responseText == "UpdateSuccessful": return True
27 logger.error("SpaceAPI returned unexpected code %d, content %s" % (response.getcode(), responseText))
29 except urllib.request.URLError as e:
30 logger.error("SpaceAPI update returned error: %s" % str(e))
33 # set_state is the asynchronous version of _set_state (see __init__)
34 def _set_state (self, state = None):
35 '''Sets the state, if None: leave state unchanged and re-try if previous attempts failed'''
37 self._state_to_set = state
39 # always have a local variable because of parallelism
40 state = self._state_to_set
42 if not self._set_state_lock.acquire(False):
43 # we don't want many threads to wait here
44 # the next status update will fix everything anyways
49 # check if there's something we need to do
50 if self._state_last_set == state: return
52 success = self._do_request(state)
53 # TODO error too often -> log critical to send mails
55 self._state_last_set = state
57 self._set_state_lock.release()