示例#1
0
    def run(self):
        super().run()
        logger.info('Starting daemon for "%s"', self.device)
        # determine lock and unlock distance and command
        states = {
            state: State(state,
                         self.configuration.getint(state.title(), 'distance'),
                         self.configuration.get(state.title(), 'command'))
            for state in ['lock', 'unlock']
        }
        # set initial state
        state = states['unlock']
        while True:
            last_state = state
            # determine current distance
            current_distance = self.device.distance
            logger.debug('Current distance: %s', current_distance)
            # set new state
            if current_distance >= states['lock'].distance:
                state = states['lock']
            elif current_distance <= states['unlock'].distance:
                state = states['unlock']

            if state != last_state:
                logger.info('Running command for new state %s', state.name)
                subprocess.run(state.command.split())
            # sleep for configured interval
            time.sleep(self.configuration.getint('Proximity', 'interval'))
        # disconnect from device
        self.device.disconnect()
示例#2
0
 def disconnect(self):
     '''
     Disconnect the device
     '''
     if not self.connected:
         return
     logger.debug('Disconnecting %s', self)
     if self.sock:
         self.sock.close()
         self.sock = None
示例#3
0
    def scan_ports(self):
        '''
        Find a suitable port for connection

        :return: suitable port
        :rtype: int
        '''
        for port in range(1, 30):
            try:
                self.connect(port)
                self.disconnect()
                return port
            except:
                logger.debug('Couldn\'t get connection on port %s', port)
示例#4
0
    def connect(self, port=None):
        '''
        Connect to the device

        :param port: port used for connection
        :type port: int
        '''
        if self.connected:
            return
        if not port:
            port = self.port
        logger.debug('Connecting %s on port %s', self, port)
        self.sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM,
                                              bluez.btsocket())
        self.sock.connect((self.mac, port))
示例#5
0
    def scan_ports(self):
        '''
        Find a suitable port for connection

        :return: suitable port
        :rtype: int
        '''
        for port in range(1, 30):
            try:
                self.connect(port)
                logger.debug('Could connect on port %s', port)
                return port
            except bluetooth.btcommon.BluetoothError:
                logger.debug('Couldn\'t get connection on port %s', port)
        raise DeviceException(
            '{}: Couldn\'t find suitable port for connection'.format(self))
示例#6
0
    def distance(self):
        '''
        Determinte distance of the device

        :return: distance of the device
        :rtype: int
        '''
        if not self.connected:
            logger.debug('Device disconnected -> reconnecting')
            self.connect()
        p = subprocess.run(['hcitool', 'rssi', self.mac],
                           stdout=subprocess.PIPE,
                           stderr=subprocess.DEVNULL)
        if p.returncode == 0:
            match = rssi_re.match(p.stdout.decode('utf-8'))
            if match:
                return abs(int(match.group(1)))
        return 255