Commits

Anonymous committed 909620e

updating greenhouse example

  • Participants
  • Parent commits c9487ee

Comments (0)

Files changed (2)

File examples/greenhouse.py

     """
 
     _siphons = {
-        'bed one siphon open': 135,
-        'bed two siphon open': 150,
-        'bed three siphon open': 135
+        'bed one siphon': 135,
+        'bed two siphon': 150,
+        'bed three siphon': 135
         }
 
     def event_received(self, event, message):
-        siphon = self._get_siphon(event)
-        if siphon != '':
-            self._status[siphon] = True
-            self.sockets.send('turn off {0} valve'.format(siphon))
-            if self._all_siphons_running:
+        sender = message['sender']
+        if sender in self._siphons:
+            location = sender.replace(' siphon', '')
+            self.sockets.send('turn off {0} valve'.format(location))
+            if self._all_valves_closed(message):
                 self.sockets.send('turn off fish tank pump')
-            self._start_timer(siphon)
+            self._start_timer(sender)
             
     @property
     def events(self):
-        return self._siphons.keys()
+        return ['UPDATE']
 
     def on_start(self):
-        self._status = self._initial_status
         self.sockets.send('turn on bed one valve')
         self.sockets.send('turn on bed two valve')
         self.sockets.send('turn on bed three valve')
         self.sockets.send('turn on fish tank pump')
 
-    def _start_timer(self, siphon):
-        countdown = self._siphons['{0} siphon open'.format(siphon)]
-        print 'countdown', countdown
-        t = threading.Thread(target=self._siphon_finished, args=(siphon, countdown))
+    def _start_timer(self, location):
+        countdown = self._siphons['{0} siphon'.format(location)]
+        t = threading.Thread(target=self._siphon_finished, args=(location, countdown))
         t.start()
 
-    def _siphon_finished(self, siphon, countdown):
-        print siphon, countdown
+    def _siphon_finished(self, location, countdown):
         time.sleep(countdown)
         s = self._get_sockets()
-        self._status[siphon] = False
-        msg = 'turn on {0} valve'.format(siphon)
-        print msg
+        msg = 'turn on {0} valve'.format(location)
         s.send(msg)
         time.sleep(0.5)
         s.send('turn on fish tank pump')
         s.close()
 
-    def _get_siphon(self, event):
+    def _all_valves_closed(self, message):
         """
         >>> addresses = Addresses()
         >>> siphons = Siphons('na', 'siphons monitor', addresses)
-        >>> siphons._get_siphon('bed two siphon open')
-        'bed two'
-        >>> siphons._get_siphon('bed one siphon open')
-        'bed one'
+        
         """
-        parts = event.split(' ')
-        if len(parts) == 4 and parts[0] == 'bed' and parts[1] in ['one', 'two', 'three']:
-            return ' '.join([parts[0], parts[1]])
-        return ''
-
-    @property
-    def _initial_status(self):
-        """
-        >>> addresses = Addresses()
-        >>> siphons = Siphons('na', 'siphons monitor', addresses)
-        >>> siphons._initial_status
-        {'bed three': False, 'bed one': False, 'bed two': False}
-        """
-        return dict([(self._get_siphon(siphon), False) for siphon in self._siphons.keys()])
-
-    @property
-    def _all_siphons_running(self):
-        """
-        >>> addresses = Addresses()
-        >>> siphons = Siphons('na', 'siphons monitor', addresses)
-        >>> siphons._status = {'bed one': False, 'bed two': False}
-        >>> siphons._all_siphons_running
-        False
-        >>> siphons._status = {'bed one': True, 'bed two': False}
-        >>> siphons._all_siphons_running
-        False
-        >>> siphons._status = {'bed one': True, 'bed two': True}
-        >>> siphons._all_siphons_running
-        True
-        """
-        for key, value in self._status.iteritems():
-            if not value:
+        for siphon in self._siphons:
+            location = siphon.replace(' siphon', '')
+            val = message.get('locations', {}).get(location, {}).get('output', {}).get('valve', {}).get('value')
+            if val:
                 return False
         return True
 

File gadgets/coordinator.py

             self._update_method(message)
         else:
             self._update_location(message)
+            self._state['sender'] = message.get('sender')
         self.sockets.send('UPDATE', self._state)
         
     def _is_valid_update_message(self, message):