Skip to content

Commit

Permalink
Merge pull request #82 from egueli/error_page
Browse files Browse the repository at this point in the history
Error page
  • Loading branch information
mikevanis authored May 25, 2021
2 parents 7bf5b0a + 2f8bb37 commit c168da4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
13 changes: 13 additions & 0 deletions naturewatch_camera_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,16 @@ def create_app():

flask_app.logger.debug("Initialisation finished")
return flask_app

def create_error_app(e):
"""
Create flask app about an error occurred in the main app
:return: Flask app object
"""
flask_app = Flask(__name__, static_folder="static/client/build")

@flask_app.route('/')
def index():
return f"<html><body><h1>Unable to start NaturewatchCameraServer.</h1>An error occurred:<pre>{e}</pre></body></html>"

return flask_app
32 changes: 28 additions & 4 deletions naturewatch_camera_server/__main__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
from naturewatch_camera_server import create_app
from naturewatch_camera_server import create_app, create_error_app
from picamera.exc import PiCameraError
import argparse
import subprocess

parser = argparse.ArgumentParser(description='Launch My Naturewatch Camera')
parser.add_argument('-p', action='store', dest='port', default=5000,
help='Port number to attach to')
args = parser.parse_args()

class CameraNotFoundException(Exception):
pass

def is_camera_enabled():
# inspired by https://stackoverflow.com/questions/58250817/raspberry-pi-camera-module-check-if-connected#comment102874971_58250817
vcgencmd_result = subprocess.run(['/opt/vc/bin/vcgencmd', 'get_camera'], stdout=subprocess.PIPE)
result_text = vcgencmd_result.stdout.decode('utf-8').strip()
properties = dict(pair.split('=') for pair in result_text.split(' '))
return properties['supported'] == '1'

if __name__ == '__main__':
app = create_app()
app.camera_controller.start()
app.change_detector.start()
try:
app = create_app()
app.camera_controller.start()
app.change_detector.start()
except Exception as e:
if isinstance(e, PiCameraError) and "Camera is not enabled" in str(e):
# This error message appears even if the camera _is_ enabled, but the camera is not found.
# e.g. due to a connection problem.
# We don't want to mislead users into messing with raspi-config, so check if the
# camera interface is really disabled.
if (is_camera_enabled()):
e = CameraNotFoundException("Unable to access camera. Is the cable properly connected?")

app = create_error_app(e)

app.run(debug=False, threaded=True, port=args.port, host='0.0.0.0')

0 comments on commit c168da4

Please sign in to comment.