Ejemplo n.º 1
0
class Lockd:
    def __init__(self, config):
        self.logger = logging.getLogger('logger')
        self.logger.info('Starting lockd')

        #add_custom_print_exception()

        serialdevice = config.get('Master Controller', 'serialdevice')
        baudrate = config.get('Master Controller', 'baudrate')

        self.serial_interface = serialinterface.SerialInterface(serialdevice, baudrate, timeout=.1)

        self.input_queue = Queue.Queue()

        udpcommand = UDPCommand('127.0.0.1', 2323, self.input_queue)

        self.doors = {}

        self.master = None

        display = None
        
        self.display_controller = None

        self.logic = DoorLogic()

        for section in config.sections():
            if config.has_option(section, 'type'):
                t = config.get(section, 'type')
                if t == 'door':
                    door_name = section
                    self.logger.debug('Adding door "%s"'%door_name)
                    buttons = {1: 'manual_control', 2: 'bell_code'}
                    door = Door(door_name, config, self.serial_interface, self.input_queue, buttons)
                    door_address = config.get(door_name, 'address')
                    self.doors[door_address] = door
                    self.logic.add_door(door)
                else:
                    self.logger.warning('Unknown entry type "%s"', t)
            elif section == 'Master Controller':
                #txseq = int(config.get(section, 'txsequence'))
                #rxseq = int(config.get(section, 'rxsequence'))
                #key = config.get(section, 'key')
                
                buttons_section = 'Master Controller Buttons'
                buttons = {}
                for button_name in config.options(buttons_section):
                    button_pin = int(config.get(buttons_section, button_name))
                    buttons[button_pin] = button_name

                leds_section = 'Master Controller LEDs'
                leds = {}
                for led_name in config.options(leds_section):
                    led_pin = int(config.get(leds_section, led_name))
                    leds[led_name] = led_pin
                
                self.master = MasterController(self.serial_interface, self.input_queue, buttons, leds) 
            
            elif section == 'Display':
                display_type = config.get(section, 'display_type') 
                max_update_rate = float(config.get(section, 'max_update_rate'))
                if display_type == "Nokia_1600":
                    from display import Display
                    display = Display(self.serial_interface)
                elif display_type == 'simulation':
                    from display_pygame import Display
                    display = Display()
                elif display_type == 'network':
                    from display_network import Display
                    display = Display()
                elif display_type == 'None':
                    display = None
                else:
                    self.logger.warning('Unknown display type "%s"', display_type)
            elif section == 'Status Receiver':
                host = config.get(section, 'host')
                port = int(config.get(section, 'port'))
                self.announcer = Announcer(host, port)
                self.logic.add_state_listener(self.announcer.update_state)
        
        if self.master == None:
            self.logger.error('Please specify a self.master controller')
            sys.exit(1)

        self.interface_logic = UserInterfaceLogic(self.master)
        self.logic.add_state_listener(self.interface_logic.update_state)
        
        if display != None:
            self.display_controller = DisplayController(display, max_update_rate)
            self.display_logic = DisplayLogic(self.display_controller)
            self.logic.add_state_listener(self.display_logic.update_state)
            for door in self.doors.values():
                self.display_logic.add_door(door)

        else:
            self.logger.warning('No display specified.')

        self.input_queue.put({'origin_name': 'init',
                        'origin_type': DoorLogic.Origin.INTERNAL,
                        'input_name': '',
                        'input_type': DoorLogic.Input.COMMAND,
                        'input_value': 'down'})

    def process(self):
        timeout = False
        while not timeout:
            address, message = self.serial_interface.readMessage()
            if address != False:
                self.logger.debug('Received data from address %s :%s' %
                        (address, str(list(message))))
            else:
                timeout = True

            if address in self.doors:
                self.doors[address].update(message)
            elif address == self.master.address:
                self.master.update(message)

        if not self.input_queue.empty():
            input = self.input_queue.get()
            self.logic.policy(input['origin_name'], input['origin_type'],
                        input['input_name'], input['input_type'], 
                        input['input_value'])
        for d in self.doors:
            self.doors[d].tick()
        self.master.tick()
        self.announcer.tick()
        self.interface_logic.tick()
        self.logic.tick()

        if self.display_controller != None:
            self.display_logic.tick()
            self.display_controller.tick()
Ejemplo n.º 2
0
    command_queue.put('announce_closed')
    
    while True:
        timeout = False
        while not timeout:
            address, message = ser.readMessage()
            if address != False:
                logger.debug('Received data from address %s :%s'%(address, str(list(message))))
            else:
                timeout = True

            if address in doors:
                doors[address].update(message)
            elif address == '0':
                master.update(message)

        if not command_queue.empty():
            command = command_queue.get()
            logger.info("-------------Received new command: %s----------------"%command)
            if command == 'unlock':
                for door in [door for door in doors.values() if door.initial_unlock == True]:
                    logger.info("Unlocking %s"%door.name)
                    door.unlock(relock_timeout=5)
            elif command == 'lock':
                for door in doors.values():
                    door.lock()
                command_queue.put('announce_closed')
            elif command == 'announce_open':
                announcer.announce_open()
                master.announce_open()
Ejemplo n.º 3
0
    command_queue.put('announce_closed')

    while True:
        timeout = False
        while not timeout:
            address, message = ser.readMessage()
            if address != False:
                logger.debug('Received data from address %s :%s' %
                             (address, str(list(message))))
            else:
                timeout = True

            if address in doors:
                doors[address].update(message)
            elif address == '0':
                master.update(message)

        if not command_queue.empty():
            command = command_queue.get()
            logger.info(
                "-------------Received new command: %s----------------" %
                command)
            if command == 'unlock':
                for door in [
                        door for door in doors.values()
                        if door.initial_unlock == True
                ]:
                    logger.info("Unlocking %s" % door.name)
                    door.unlock(relock_timeout=5)
            elif command == 'lock':
                for door in doors.values():