Esempio n. 1
0
    def __init__(self, config):

        # Config
        #self.relay_pin = config['relay']
        self.open_pin = config['open']
        self.close_pin = config['close']
        self.state_pin = config['state']
        self.id = config['id']
        self.mode = int(config.get('state_mode') == 'normally_closed')
        self.invert_relay = bool(config.get('invert_relay'))

        # Setup
        self._state = None
        self.onStateChange = EventHook()

        # Set relay pin to output, state pin to input, and add a change listener to the state pin
        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(self.relay_pin, GPIO.OUT)
        GPIO.setup(self.state_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        GPIO.add_event_detect(self.state_pin,
                              GPIO.BOTH,
                              callback=self.__stateChanged,
                              bouncetime=300)

        # Set default relay state to false (off)
        GPIO.output(self.relay_pin, self.invert_relay)
Esempio n. 2
0
class MotorLock():
    def __init__(self):
        self.locked = True
        self.onStatusChange = EventHook()
        self.logger = logging.getLogger("motorlock")
        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(4, GPIO.OUT)
        GPIO.setup(18, GPIO.OUT)
        GPIO.setup(17, GPIO.IN)
        GPIO.setup(21, GPIO.IN)
        GPIO.add_event_detect(17, GPIO.FALLING, callback = self.button_callback, bouncetime = 1000)
        GPIO.add_event_detect(21, GPIO.FALLING, callback = self.button_callback, bouncetime = 1000)
        self.locked = (GPIO.input(4) == GPIO.LOW)

    def button_callback(self, channel):
        self.logger.debug("GPIO button interrupt called on channel " + str(channel))
        sleep(1)
        if GPIO.input(channel) == GPIO.HIGH:
            self.logger.debug("Bounce")
            return

        if channel == 17:
            self.logger.warning("Lock button pressed")
            self.lock()
        elif channel == 21:
            self.logger.warning("Unlock button pressed")
            self.unlock()

    def lock(self, semi = False):
#        if self.locked:
#            self.logger.warning("already locked")
#            return
        self.logger.info("locking...")
        self.locked = True
        GPIO.output(4, GPIO.LOW)
        if not semi:
            GPIO.output(18, GPIO.HIGH)

        self.onStatusChange.fire()

    def unlock(self):
        if not self.locked:
            self.logger.warn("already unlocked")
        self.logger.info("unlocking...")
        self.locked = False

        GPIO.output(4, GPIO.HIGH)
        GPIO.output(18, GPIO.LOW)

        self.onStatusChange.fire()

    def isUnlocked(self):
        return not self.locked
Esempio n. 3
0
class State(object):

    def __init__(self, config):

        # Config
        self.state_pin = config['pin']
        self.id = config['id']
        self.mode = int(config.get('state_mode') == 'normally_closed')

        # Setup
        self._state = None
        self.onStateChange = EventHook()

        # Set state pin to BOTH (detect RISE && FALL, represents open and close), and add a change listener to the state pin
        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(self.state_pin, GPIO.IN)
        GPIO.add_event_detect(self.state_pin, GPIO.BOTH, callback=self.__stateChanged)
        print "\n********INIT STATE********"
        print '{}\t\t{}\t\t{}'.format("ID","PIN", "MODE")
        print '{}\t\t{}\t\t{}'.format(self.id,self.state_pin, GPIO.gpio_function(self.state_pin))


    # Release rpi resources
    def __del__(self):
        GPIO.cleanup()


    # State is a read only property that actually gets its value from the pin
    @property
    def state(self):
        # Read the mode from the config. Then compare the mode to the current state. IE. If the circuit is normally closed and the state is 1 then the circuit is closed.
        # and vice versa for normally open
        state = GPIO.input(self.state_pin)
        # Debuging ouput, uncomment if needed
        # print "\n********STATE********"
        # print '{}\t\t{}\t\t{}\t\t{}'.format("ID","PIN", "MODE", "STATE")
        # print '{}\t\t{}\t\t{}\t\t{}'.format(self.id,self.state_pin, self.mode, state)
        if  state == self.mode:
            return 'closed'
        else:
            return 'open'


    # Provide an event for when the state pin changes
    def __stateChanged(self, channel):
        print "\n*****STATECHANGED*****"
        print "\tChannel: {}".format(channel)
        print "\tPin: {}".format(self.state_pin)
        if channel == self.state_pin:
            # Had some issues getting an accurate value so we are going to wait for a short timeout
            # after a statechange and then grab the state
            time.sleep(SHORT_WAIT)
            self.onStateChange.fire(self.state)
Esempio n. 4
0
 def __init__(self):
     self.locked = True
     self.onStatusChange = EventHook()
     self.logger = logging.getLogger("motorlock")
     GPIO.setwarnings(False)
     GPIO.setmode(GPIO.BCM)
     GPIO.setup(4, GPIO.OUT)
     GPIO.setup(18, GPIO.OUT)
     GPIO.setup(17, GPIO.IN)
     GPIO.setup(21, GPIO.IN)
     GPIO.add_event_detect(17, GPIO.FALLING, callback = self.button_callback, bouncetime = 1000)
     GPIO.add_event_detect(21, GPIO.FALLING, callback = self.button_callback, bouncetime = 1000)
     self.locked = (GPIO.input(4) == GPIO.LOW)
Esempio n. 5
0
    def __init__(self, config):

        # Config
        self.state_pin = config['pin']
        self.id = config['id']
        self.mode = int(config.get('state_mode') == 'normally_closed')

        # Setup
        self._state = None
        self.onStateChange = EventHook()

        # Set state pin to BOTH (detect RISE && FALL, represents open and close), and add a change listener to the state pin
        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(self.state_pin, GPIO.IN)
        GPIO.add_event_detect(self.state_pin, GPIO.BOTH, callback=self.__stateChanged)
        print "\n********INIT STATE********"
        print '{}\t\t{}\t\t{}'.format("ID","PIN", "MODE")
        print '{}\t\t{}\t\t{}'.format(self.id,self.state_pin, GPIO.gpio_function(self.state_pin))
Esempio n. 6
0
class SimLock():
    def __init__(self):
        self.locked = True
        self.onStatusChange = EventHook()
        self.logger = logging.getLogger("simlock")

    def lock(self):
        if self.locked:
            self.logger.warning("already locked")
            return
        self.logger.info("locking...")
        self.locked = True
        self.onStatusChange.fire()

    def unlock(self):
        if not self.locked:
            self.logger.warn("already unlocked")
        self.logger.info("unlocking...")
        self.locked = False
        self.onStatusChange.fire()

    def isUnlocked(self):
        return not self.locked
Esempio n. 7
0
 def __init__(self):
     self.locked = True
     self.onStatusChange = EventHook()
     self.logger = logging.getLogger("simlock")
Esempio n. 8
0
def createDefaultEventHook():
	return EventHook()
Esempio n. 9
0
class GarageDoor(object):
    def __init__(self, config):

        # Config
        #self.relay_pin = config['relay']
        self.open_pin = config['open']
        self.close_pin = config['close']
        self.state_pin = config['state']
        self.id = config['id']
        self.mode = int(config.get('state_mode') == 'normally_closed')
        self.invert_relay = bool(config.get('invert_relay'))

        # Setup
        self._state = None
        self.onStateChange = EventHook()

        # Set relay pin to output, state pin to input, and add a change listener to the state pin
        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(self.relay_pin, GPIO.OUT)
        GPIO.setup(self.state_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        GPIO.add_event_detect(self.state_pin,
                              GPIO.BOTH,
                              callback=self.__stateChanged,
                              bouncetime=300)

        # Set default relay state to false (off)
        GPIO.output(self.relay_pin, self.invert_relay)

    # Release rpi resources
    def __del__(self):
        GPIO.cleanup()

    # These methods all just mimick the button press, they dont differ other than that
    # but for api sake I'll create three methods. Also later we may want to react to state
    # changes or do things differently depending on the intended action

    def open(self):
        if self.state == 'closed':
            self.__open()

    def close(self):
        if self.state == 'open':
            self.__close()

#    def stop(self):
#        self.__press()

# State is a read only property that actually gets its value from the pin

    @property
    def state(self):
        # Read the mode from the config. Then compare the mode to the current state. IE. If the circuit is normally closed and the state is 1 then the circuit is closed.
        # and vice versa for normally open
        state = GPIO.input(self.state_pin)
        if state == self.mode:
            return 'closed'
        else:
            return 'open'

    # Mimick a button press by switching the GPIO pin on and off quickly
#    def __press(self):
#        GPIO.output(self.relay_pin, not self.invert_relay)
#        time.sleep(SHORT_WAIT)
#        GPIO.output(self.relay_pin, self.invert_relay)

# Mimick button press on open pin

    def __open(self):
        GPIO.output(self.open_pin, not self.invert_relay)
        time.sleep(SHORT_WAIT)
        GPIO.output(self.open_pin, self.invert_relay)

    # Mimick button press on close pin

    def __close(self):
        GPIO.output(self.close_pin, not self.invert_relay)
        time.sleep(SHORT_WAIT)
        GPIO.output(self.close_pin, self.invert_relay)

    # Provide an event for when the state pin changes
    def __stateChanged(self, channel):
        if channel == self.state_pin:
            # Had some issues getting an accurate value so we are going to wait for a short timeout
            # after a statechange and then grab the state
            time.sleep(SHORT_WAIT)
            self.onStateChange.fire(self.state)
Esempio n. 10
0
 def __init__(self):
     self.authorized_event = EventHook(
     )  # Called when refresh token is obtained
     self.device_authorization_event = EventHook(
     )  # Called when device needs to be authorized