from flask_socketio import SocketIO from gpiozero import Button, LED from time import sleep import io # To check if we are on a Raspberry Pi import subprocess class Raspberry(object): """ This class will manage three things : - Connecting to a USB webcam - Managing a push button - Activating a flash ( or light ) - Flash an indicator light """ def __init__(self, printer, app, socketio, button_gpio_port_number, indicator_gpio_port_number, is_flash_present,): self.printer = printer self.socketio = socketio self.app = app self.is_flash_present = is_flash_present self.button_gpio = button_gpio_port_number self.led_gpio = indicator_gpio_port_number self.image_path = self.app.config['UPLOAD_FOLDER'] + '/image.jpg' def initialise_gpio(self): self.button = Button(self.button_gpio) self.led = LED(self.led_gpio) button.when_pressed = self.on_button_pressed() def is_raspberry_pi(self, raise_on_errors=False): # Check if we are running on a raspberry pi try: with io.open('/proc/cpuinfo', 'r') as cpuinfo: found = False for line in cpuinfo: if line.startswith('Hardware'): found = True label, value = line.strip().split(':', 1) value = value.strip() if value not in ( 'BCM2708', 'BCM2709', 'BCM2711', 'BCM2835', 'BCM2836' ): self.app.logger.debug('This system does not appear to be a Raspberry Pi.') return False if not found: self.app.logger.error('Couldn\'t get sufficient hardware information from /proc/cpuinfo, Unable to determine if we are on a Raspberry Pi.') return False except IOError: self.app.logger.error('Unable to open `/proc/cpuinfo`.') return False self.app.logger.debug('It seems we are on a Raspberry Pi') return True def take_picture(self): # return a path to a picture try: subprocess.run(['fswebcam', '--no-banner', self.image_path]) except Exception as e: self.app.logger.error('Unable to take a picture :' + e) raise e return True def flash(state): if state: # turn on the flash pass else: # turn off the flash pass def flash_indicator(self): # The indicator will flash three times before return true self.led.on() sleep(0.1) self.led.off() sleep(1) self.led.on() sleep(0.1) self.led.off() sleep(1) self.led.on() sleep(0.1) self.led.off() def on_button_pressed(): self.socketio.emit('button_pressed') # Notify clients that a button has been pressed on the raspberry pi self.flash_indicator() # The indicator will flash a countdown LED self.flash(True) try: self.take_picture() self.printer.print_image(self.image_path) except Exception as e: app.logger.error("Could not take a picture after the button press : " + e) self.led.on() self.flash(False) self.socketio.emit('picture_taken') # Notify clients that a picture has been taken