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

View file

@ -12,12 +12,17 @@ import zipstream
camera = CameraWorker(app=app)
camera.start()
save_frames = False
@app.route('/')
@app.route('/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)
@app.route('/toggle_timelapse', methods=['GET', 'POST'])