From: Ralf Jung Date: Sat, 26 Oct 2013 20:09:13 +0000 (+0200) Subject: update space API concept X-Git-Url: https://git.ralfj.de/saartuer.git/commitdiff_plain/37cb1be6ad266835984d712ba801cd9108da20ec?ds=inline;hp=-c update space API concept --- 37cb1be6ad266835984d712ba801cd9108da20ec diff --git a/concept_spaceapi.py b/concept_spaceapi.py index e991463..472b26d 100644 --- a/concept_spaceapi.py +++ b/concept_spaceapi.py @@ -1,24 +1,24 @@ class SpaceApi: - __init__ (self): - self.state_to_set = None # public write, TODO: getters, setters? - self.state_last_set = None # public read + __init__ (self, waker): + self._state_to_set = None # public write + self._state_last_set = None # public read FIXME why? getter? self._running = True - self._t = threading.Thread(target=self.loop) - _t.start() + self.set_state = ThreadFunction(self._set_state, "Space API") + waker.register(self.set_state, 10.0) # re-try setting the state every 10 seconds def stop (self): - self._running = False + self.set_state.stop() - def loop (self): - while self._running: - if (self.state_to_set is not None): - if self.state_to_set != self.state_last_set: - stts = self.state_to_set # cache for parallelism - error = do_request(stts) # TODO - #TODO logging - if not error: - self.state_last_set = stts - #TODO logging - time.sleep(SPACE_API_LOOP_DELAY) \ No newline at end of file + def _set_state (self, state = None): + '''Sets the state, if None: leave state unchanged and re-try if previous attempts failed''' + if state is not None: + self._state_to_set = state + # check if there's something we need to do + if self._state_last_set == self._state_to_set: return + # take action! + error = do_request(stts) # TODO + #TODO logging + if not error: + self.state_last_set = stts