diff --git a/phytopi/camera/camera.py b/phytopi/camera/camera.py new file mode 100644 index 0000000..8608127 --- /dev/null +++ b/phytopi/camera/camera.py @@ -0,0 +1,48 @@ +import io +import os +import sys +import subprocess +import signal + +from phytopi import db, models, app + +class CameraWorker(object): + def __init__(self, pidfile='/tmp/raspimjpeg.pid', executable='/usr/local/bin/raspimjpeg', fifo='/dev/shm/mjpeg/mjpeg_fifo', app=None): + self.pidfile = pidfile + self.executable = executable + self.fifo = fifo + self.proc = None + self.pid = None + self.app = app + + def start(self): + # create FIFO if not existent + if not os.path.isfile(self.fifo): + os.mkfifo(self.fifo) + + # check if process is already running + if os.path.isfile(self.pidfile): + with open(self.pidfile) as fh: + self.pid = int(fh.read()) + + # if not, spawn new process and create pid file + self.proc = subprocess.Popen(self.executable, stdout=subprocess.PIPE) + self.pid = self.proc.pid + with open(self.pidfile) as fh: + fh.write(self.pid) + + def stop(self): + # if process was spawned by this instance, killing is easy + if self.proc: + self.proc.kill() + else: + # otherwise send SIGTERM to pid + os.kill(self.pid, signal.SIGTERM) + os.unlink(self.fifo) + + def send_cmd(self, cmd): + with open(self.fifo, 'w') as fh: + fh.write(cmd) + + def read_output(self): + return self.proc.stdout.readlines() \ No newline at end of file diff --git a/phytopi/camera_daemon/camera_daemon.py b/phytopi/camera_daemon/camera_daemon.py deleted file mode 100644 index e69de29..0000000 diff --git a/phytopi/camera_daemon/__init__.py b/phytopi/camera_old/__init__.py similarity index 100% rename from phytopi/camera_daemon/__init__.py rename to phytopi/camera_old/__init__.py diff --git a/phytopi/camera/base_camera.py b/phytopi/camera_old/base_camera.py similarity index 100% rename from phytopi/camera/base_camera.py rename to phytopi/camera_old/base_camera.py diff --git a/phytopi/camera/camera_pi.py b/phytopi/camera_old/camera_pi.py similarity index 100% rename from phytopi/camera/camera_pi.py rename to phytopi/camera_old/camera_pi.py diff --git a/phytopi/phytopi.db b/phytopi/phytopi.db deleted file mode 100644 index 0519c72..0000000 Binary files a/phytopi/phytopi.db and /dev/null differ diff --git a/phytopi/routes.py b/phytopi/routes.py index 35e73b6..a85074f 100644 --- a/phytopi/routes.py +++ b/phytopi/routes.py @@ -1,6 +1,6 @@ from flask import render_template, Response, flash, jsonify, request, stream_with_context, send_file -from phytopi.camera.camera_pi import Camera +from phytopi.camera.camera import CameraWorker from phytopi.forms import CameraSettingsForm from phytopi import app @@ -10,7 +10,8 @@ from phytopi.models import Dataset from io import BytesIO import zipstream -camera = Camera(app=app) +camera = CameraWorker(app=app) +camera.start() # camera = app.camera save_frames = False diff --git a/requirements_pi.txt b/requirements_pi.txt deleted file mode 100644 index abee2bc..0000000 --- a/requirements_pi.txt +++ /dev/null @@ -1,19 +0,0 @@ -alembic>=1.0.1 -Click>=7.0 -Flask>=1.0.2 -Flask-Migrate>=2.3.0 -Flask-SQLAlchemy>=2.3.2 -Flask-WTF>=0.14.2 -itsdangerous>=1.1.0 -Jinja2>=2.10 -Mako>=1.0.7 -MarkupSafe>=1.0 -picamera>=1.13 -Pillow>=5.3.0 -python-dateutil>=2.7.5 -python-editor>=1.0.3 -six>=1.11.0 -SQLAlchemy>=1.2.12 -Werkzeug>=0.14.1 -WTForms>=2.2.1 -zipstream>=1.1.4