Restore timelapse button status on reload

This commit is contained in:
Alexander Minges 2019-02-01 16:56:41 +01:00
parent cd2484a8d8
commit 6ae1f9f22f
2 changed files with 19 additions and 9 deletions

View file

@ -5,6 +5,7 @@ import stat
import subprocess import subprocess
import signal import signal
from time import sleep
from enum import Enum from enum import Enum
from PIL import Image from PIL import Image
from phytopi import db, models, app from phytopi import db, models, app
@ -56,21 +57,27 @@ class CameraWorker(object):
os.unlink(self.pidfile) os.unlink(self.pidfile)
self.status = CameraStatus.STOPPED self.status = CameraStatus.STOPPED
def send_cmd(self, cmd): def _send_cmd(self, cmd):
with open(self.fifo, 'w') as fh: with open(self.fifo, 'w') as fh:
fh.write('{}\n'.format(cmd)) fh.write('{}\n'.format(cmd))
def _send_cmds(self, cmds):
for cmd in cmds:
self._send_cmd(cmd)
# don't flood the fifo
sleep(1)
def start_timelapse(self, interval=300): def start_timelapse(self, interval=300):
if self.status == CameraStatus.IDLE: if self.status == CameraStatus.IDLE:
self.send_cmd('tv {}'.format(str(interval))) cmds = ['tv {}'.format(interval), 'tl 1']
self.send_cmd('tl 1') self._send_cmds(cmds)
self.status = CameraStatus.TIMELAPSE self.status = CameraStatus.TIMELAPSE
else: else:
raise CameraActionError('Camera not idle!') raise CameraActionError('Camera not idle!')
def stop_timelapse(self): def stop_timelapse(self):
if self.status == CameraStatus.TIMELAPSE: if self.status == CameraStatus.TIMELAPSE:
self.send_cmd('tl 0') self._send_cmd('tl 0')
self.status == CameraStatus.IDLE self.status == CameraStatus.IDLE
else: else:
raise CameraActionError('Camera not in timelapse mode!') raise CameraActionError('Camera not in timelapse mode!')
@ -80,6 +87,4 @@ class CameraWorker(object):
img = Image.open('/dev/shm/mjpeg/cam.jpg') img = Image.open('/dev/shm/mjpeg/cam.jpg')
img.save(output, format='JPEG') img.save(output, format='JPEG')
output.seek(0, 0) output.seek(0, 0)
return output.getvalue() return output.getvalue()

View file

@ -12,12 +12,17 @@ import zipstream
camera = CameraWorker(app=app) camera = CameraWorker(app=app)
camera.start() camera.start()
save_frames = False
@app.route('/') @app.route('/')
@app.route('/index') @app.route('/index')
def index(): def index():
content = {'timelapse': save_frames} global camera
if camera.status == CameraStatus.TIMELAPSE:
timelapse = True
else:
timelapse = False
content = {'timelapse': timelapse}
return render_template('index.html', content=content) return render_template('index.html', content=content)
@app.route('/toggle_timelapse', methods=['GET', 'POST']) @app.route('/toggle_timelapse', methods=['GET', 'POST'])