Esempio n. 1
0
def main():

  config.load_config('{}/{}'.format('config', 'test_config.ini'))

  formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  file_handler = RotatingFileHandler(getattr(config, 'cfg').get('box', 'log_file'),
                                     maxBytes=5242880, backupCount=4)
  file_handler.setLevel(logging.DEBUG)
  file_handler.setFormatter(formatter)
  root_logger = logging.getLogger()
  root_logger.addHandler(file_handler)
  root_logger.setLevel(logging.DEBUG)

  try:
    camera = CameraController()
    camera.initialize()

    camera.capture_and_download('test1')
    code_scanner = CodeScanner()
    code_information = code_scanner.scan_image('/home/pi/pictures/test1.jpg')
    if code_information is not None:
      print('Decoded from QRCode: "{}"'.format(code_information.decode('utf8')))
    else:
      print('No decodable QRCode found')
    camera.close()
  except ConnectionError as e:
    print('Connection Error "%s"' % e)
  except CaptureError as e:
    print('Capture Error "%s"' % e)
Esempio n. 2
0
    def initialize(self):
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        file_handler = RotatingFileHandler(getattr(config, 'cfg').get(
            'box', 'log_file'),
                                           maxBytes=5242880,
                                           backupCount=4)
        file_handler.setLevel(logging.INFO)
        file_handler.setFormatter(formatter)
        root_logger = logging.getLogger()
        root_logger.addHandler(file_handler)

        init(autoreset=True)

        photo_count = getattr(config, 'cfg').getint('box', 'photo_count')
        led_controller = LedController()
        led_controller.initialize()
        led_controller.blink_green(1)
        led_controller.blink_blue(1)
        scanner = CodeScanner()
        camera_controller = CameraController()
        camera_controller.initialize()
        motor_controller = MotorController()
        motor_controller.initialize()

        base_address = getattr(config, 'cfg').get('server', 'base_address')
        auth_address = '{}{}'.format(
            base_address,
            getattr(config, 'cfg').get('server', 'auth_endpoint'))
        reauth_address = '{}{}'.format(
            base_address,
            getattr(config, 'cfg').get('server', 'reauth_endpoint'))

        auth = TokenAuth(auth_address,
                         reauth_address,
                         username=getattr(config,
                                          'cfg').get('credentials',
                                                     'username'),
                         password=getattr(config,
                                          'cfg').get('credentials',
                                                     'password'))

        image_handler = ImageHandler(photo_count=photo_count, auth=auth)
        image_handler.setDaemon(True)
        image_handler.start()

        phenobox_machine = PhenoboxMachine(camera_controller, scanner,
                                           motor_controller, led_controller,
                                           image_handler, auth, photo_count)
        phenobox_machine.initialize()
        return (phenobox_machine, motor_controller, led_controller,
                camera_controller, image_handler)
class Phenobox():
    def __init__(self):

        self.initialize()

        self.eventQueue = Queue()
        self._logger = logging.getLogger(__name__)
        self._logger.setLevel(logging.INFO)
        self.door_state = self.input_controller.get_door_state()

    def door_state_changed(self, state):
        """
    If it is opened while the box is not in the IDLE or ERROR state it will transition to the ERROR state

    :param state: The current/new DoorState

    :return: None
    """
        self.door_state = state
        if state == DoorState.OPEN:
            if not self.phenobox_statemachine.is_IDLE(
            ) and not self.phenobox_statemachine.is_ERROR():
                self._logger.info('Door Opened while running')
                self.phenobox_statemachine.door_opened(error_code=3)

    def start_pressed(self, press):
        if press == ButtonPress.SHORT:
            print(Fore.CYAN + "State: " + self.phenobox_statemachine.state)
        if self.phenobox_statemachine.is_IDLE(
        ) or self.phenobox_statemachine.is_ERROR():
            if press == ButtonPress.LONG:
                print(Fore.MAGENTA + "Shutdown Requested")
                self._logger.info('Shutdown action placed')
                self.eventQueue.put(UserEvent.SHUTDOWN)
            else:
                if self.door_state == DoorState.CLOSED:
                    print(Fore.BLUE + "Start")
                    if self.phenobox_statemachine.is_IDLE():
                        self._logger.info('Start action placed')
                        self.eventQueue.put(UserEvent.START)
                    else:
                        self._logger.info('Restart action placed')
                        self.eventQueue.put(UserEvent.RESTART)
                else:
                    print(Fore.RED + "Please close the door before starting!")

    def initialize(self):
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        file_handler = RotatingFileHandler(getattr(config, 'cfg').get(
            'box', 'log_file'),
                                           maxBytes=5242880,
                                           backupCount=4)
        file_handler.setLevel(logging.INFO)
        file_handler.setFormatter(formatter)
        root_logger = logging.getLogger()
        root_logger.addHandler(file_handler)

        init(autoreset=True)

        photo_count = getattr(config, 'cfg').getint('box', 'photo_count')
        self.illumination = Illumination(
            13)  # use GPIO pin 13 for illumination PWM control
        self.led_controller = LedController()
        self.led_controller.initialize()
        self.led_controller.blink_green(1)
        self.led_controller.blink_blue(1)
        self.scanner = CodeScanner()
        self.camera_controller = CameraController()
        self.camera_controller.initialize()
        self.motor_controller = MotorController()
        self.motor_controller.initialize()

        self.image_handler = ImageHandler(photo_count=photo_count)
        self.image_handler.setDaemon(True)
        self.image_handler.start()

        self.phenobox_statemachine = PhenoboxStateMachine(
            self.camera_controller, self.scanner, self.motor_controller,
            self.led_controller, self.image_handler, self.illumination,
            photo_count)
        self.phenobox_statemachine.initialize()
        self.input_controller = InputController()
        self.input_controller.initialize(self.door_state_changed,
                                         self.start_pressed)
        signal.signal(signal.SIGUSR1, sigUSR1_handler)

    def _terminate(self):
        self.led_controller.clear_all()
        self.camera_controller.close()
        self.led_controller.blink_green(interval=1)
        self.led_controller.blink_blue(interval=1)
        self.image_handler.stop()
        self.image_handler.join()
        self.led_controller.clear_all()
        gpio.clean()

    def run(self):
        shutdown = False
        try:
            while True:
                try:
                    event = self.eventQueue.get(True, 0.1)
                except Empty:
                    continue
                if event == UserEvent.START:
                    self.phenobox_statemachine.start()
                if event == UserEvent.RESTART:
                    self.phenobox_statemachine.restart()
                if event == UserEvent.SHUTDOWN:
                    shutdown = True
                    break

        except KeyboardInterrupt:
            print("keyboard interrupt")
        print(Fore.MAGENTA + 'Shutdown initiated')
        self._terminate()
        if shutdown:
            subprocess.call(
                ['sudo shutdown -h now "System halted by GPIO action" &'],
                shell=True)