Source

gadgets / examples / greenhouse.py

Full commit
import time
import threading
from gadgets import get_gadgets, Addresses, Gadget
from gadgets.pins.beaglebone import pins

arguments = {
    'locations': {
        'bed one': {
            'valve': {
                'type': 'shift register switch',
                'channel': 0,
                },
            'siphon': {
                'type': 'input',
                'input_type': 'gpio',
                'pin': pins['gpio'][8][11],
                }
            },
        'bed two': {
            'valve': {
                'type': 'shift register switch',
                'channel': 1,
                },
            'siphon': {
                'type': 'input',
                'input_type': 'gpio',
                'pin': pins['gpio'][8][14],
                }
            },
        'bed three': {
            'valve': {
                'type': 'shift register switch',
                'channel': 2,
                },
            'siphon': {
                'type': 'input',
                'input_type': 'gpio',
                'pin': pins['gpio'][8][12],
                }
            },
        'fish tank': {
            'pump': {
                'type': 'shift register switch',
                'channel': 3,
                },
            },
        'garage': {
            'temperature': {
                'type': 'thermometer',
                'uid': '28-0000047ae901'
                }
            },
        'greenhouse': {
            'temperature': {
                'type': 'thermometer',
                'uid': '28-00000479f8c6'
                }
            },
        }
    }


class Siphons(Gadget):
    """
    I use this Siphons Gadget to control the pumps in
    my aquaponics greenhouse.
    """

    _siphons = {
        'bed one siphon': 135,
        'bed two siphon': 150,
        'bed three siphon': 135
        }

    def event_received(self, event, message):
        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(sender)
            
    @property
    def events(self):
        return ['UPDATE']

    def on_start(self):
        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, location):
        countdown = self._siphons['{0} siphon'.format(location)]
        t = threading.Thread(target=self._siphon_finished, args=(location, countdown))
        t.start()

    def _siphon_finished(self, location, countdown):
        time.sleep(countdown)
        s = self._get_sockets()
        msg = 'turn on {0} valve'.format(location)
        s.send(msg)
        time.sleep(0.5)
        s.send('turn on fish tank pump')
        s.close()

    def _all_valves_closed(self, message):
        """
        >>> addresses = Addresses()
        >>> siphons = Siphons('na', 'siphons monitor', addresses)
        
        """
        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

    def _register(self):
        pass
            
if __name__ == '__main__':
    addresses = Addresses()
    siphons = Siphons('na', 'siphons monitor', addresses)
    gadgets = get_gadgets(arguments, addresses)
    gadgets.add_gadget(siphons)
    gadgets.start()