WIP: Acces-libre : Raspberry Pi integration #8

Draft
n07070 wants to merge 14 commits from acces-libre into master
2 changed files with 66 additions and 46 deletions
Showing only changes of commit dfeb1be0f0 - Show all commits

View File

@ -77,10 +77,14 @@ printer = Printer(app,0x04b8, 0x0e28)
printer.init_printer() printer.init_printer()
# Find out if we are running on a Raspberry Pi # Find out if we are running on a Raspberry Pi
rpi = Raspberry(printer, app, socketio, configuration_file['rpi']['button_gpio_port_number'], configuration_file['rpi']['indicator_gpio_port_number'], configuration_file['rpi']['flash'] ) rpi = Raspberry(printer,
app,
socketio,
configuration_file['rpi']['button_gpio_port_number'], configuration_file['rpi']['indicator_gpio_port_number'],
configuration_file['rpi']['flash_gpio_port_number'],
configuration_file['rpi']['flash'] )
RASPBERRY_PI_CONNECTED = rpi.is_raspberry_pi() RASPBERRY_PI_CONNECTED = rpi.is_raspberry_pi()
if RASPBERRY_PI_CONNECTED:
rpi.initialise_gpio()
############################################################# #############################################################
@ -91,7 +95,7 @@ if RASPBERRY_PI_CONNECTED:
web = Web(app, printer) web = Web(app, printer)
if __name__ == "__main__": if __name__ == "__main__":
app.run(ssl_context='adhoc') app.run(debug=True, use_reloader=False, host='0.0.0.0', ssl_context='adhoc')
limiter = Limiter( limiter = Limiter(
get_remote_address, get_remote_address,

View File

@ -1,5 +1,5 @@
from flask_socketio import SocketIO from flask_socketio import SocketIO
from gpiozero import Button, LED from gpiozero import Button, LED, DigitalOutputDevice
from time import sleep from time import sleep
import io # To check if we are on a Raspberry Pi import io # To check if we are on a Raspberry Pi
import subprocess import subprocess
@ -13,24 +13,17 @@ class Raspberry(object):
- Flash an indicator light - Flash an indicator light
""" """
def __init__(self, printer, app, socketio, button_gpio_port_number, indicator_gpio_port_number, is_flash_present,): def __init__(self, printer, app, socketio, button_gpio_port_number, indicator_gpio_port_number, flash_gpio_port_number, is_flash_present,):
self.printer = printer self.printer = printer
self.socketio = socketio self.socketio = socketio
self.app = app self.app = app
self.flash_gpio = flash_gpio_port_number
self.is_flash_present = is_flash_present self.is_flash_present = is_flash_present
self.button_gpio = button_gpio_port_number self.button_gpio = button_gpio_port_number
self.led_gpio = indicator_gpio_port_number self.led_gpio = indicator_gpio_port_number
self.image_path = self.app.config['UPLOAD_FOLDER'] + '/image.jpg' 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): def is_raspberry_pi(self, raise_on_errors=False):
# Check if we are running on a raspberry pi # Check if we are running on a raspberry pi
try: try:
@ -59,48 +52,71 @@ class Raspberry(object):
return False return False
self.app.logger.debug('It seems we are on a Raspberry Pi') 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: try:
subprocess.run(['fswebcam', '--no-banner', self.image_path]) self.initialise_gpio()
except Exception as e: except Exception as e:
self.app.logger.error('Unable to take a picture :' + e) self.app.logger.debug('Could not init GPIO : ' + str(e))
raise e raise e
return True return True
def flash(state): def initialise_gpio(self):
if state: self.app.logger.debug('Initializing GPIO')
# turn on the flash
pass
else:
# turn off the flash
pass
def flash_indicator(self): self.led = LED(self.led_gpio)
# The indicator will flash three times before return true self.app.logger.debug('Activated indicator LED')
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.button = Button(self.button_gpio, pull_up=True, bounce_time=0.1)
self.button.when_pressed = self.on_button_pressed
self.app.logger.debug('Activated button')
# The "flash" is a relay-controlled device ( light bulb for example )
self.flash = DigitalOutputDevice(self.flash_gpio)
self.app.logger.debug('Activated flash')
def take_picture(self):
try:
subprocess.run(['fswebcam', '--no-banner', self.image_path])
except Exception as e:
self.app.logger.error('Unable to take a picture :' + str(e))
raise e
return True
def indicator_countdown(self,iters=10,multi=10):
for i in range(iters,0,-1):
self.led.on()
sleep(i/multi)
self.led.off()
sleep(i/multi)
def indicator_led(self,timing=0.2,l=5):
for i in range(l):
self.app.logger.debug("LED turned on")
self.led.on()
sleep(timing)
self.led.off()
self.app.logger.debug("LED turned off")
sleep(timing)
def flash_toggle(self):
self.app.logger.debug("Flash turned on")
self.flash.on()
sleep(0.3)
self.flash.off()
self.app.logger.debug("Flash turned off")
def on_button_pressed(self):
self.app.logger.debug("Button has been pressed")
self.socketio.emit('button_pressed') # Notify clients that a button has been pressed on the raspberry pi 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.indicator_countdown(iters=5,multi=20) # The indicator will flash a countdown LED
self.flash(True) self.flash_toggle()
try: try:
self.take_picture() self.take_picture()
self.printer.print_image(self.image_path) self.printer.print_image(self.image_path)
self.socketio.emit('picture_taken') # Notify clients that a picture has been taken
except Exception as e: except Exception as e:
app.logger.error("Could not take a picture after the button press : " + e) self.app.logger.error("Could not take a picture after the button press : " + str(e))
self.led.on() return True
self.flash(False)
self.socketio.emit('picture_taken') # Notify clients that a picture has been taken