update space API concept
authorRalf Jung <post@ralfj.de>
Sat, 26 Oct 2013 20:09:13 +0000 (22:09 +0200)
committerRalf Jung <post@ralfj.de>
Sat, 26 Oct 2013 20:09:13 +0000 (22:09 +0200)
concept_spaceapi.py

index e99146343976c11588982f837c2a75b8ec6291a4..472b26d490819a39475e0a83459e71b0be3f6551 100644 (file)
@@ -1,24 +1,24 @@
 
 
 class SpaceApi:
 
 
 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._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):
        
        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