Skip to content

Commit

Permalink
Futher work on timelapse support
Browse files Browse the repository at this point in the history
  • Loading branch information
foosel committed Jan 3, 2013
1 parent 35e6b19 commit 02f2436
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ The following example config should explain the available options:
# use this option to enable timelapse support via snapshot, e.g. via MJPG-Streamer
snapshot = http://10.0.0.1:8080/?action=snapshot

[folder]
# absolute path where to store gcode uploads. Defaults to the uploads folder in the Printer WebUI settings dir
uploads = /path/to/upload/folder
# absolute path where to store gcode uploads. Defaults to the timelapse folder in the Printer WebUI settings dir
timelapse = /path/to/timelapse/folder

Setup on a Raspberry Pi running Raspbian
----------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion printer_webui/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def mcStateChange(self, state):
if oldState == self.comm.STATE_PRINTING:
self.timelapse.onPrintjobStopped()
elif state == self.comm.STATE_PRINTING:
self.timelapse.onPrintjobStarted()
self.timelapse.onPrintjobStarted(self.filename)

def mcMessage(self, message):
"""
Expand Down
3 changes: 2 additions & 1 deletion printer_webui/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def settings():
},
"webcam": {
"stream": None,
"snapshot": None
"snapshot": None,
"ffmpeg": None
},
"folder": {
"uploads": None,
Expand Down
83 changes: 83 additions & 0 deletions printer_webui/timelapse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# coding=utf-8
__author__ = "Gina Häußge <[email protected]>"
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'

from printer_webui.settings import settings

import os
import threading
import urllib
import time

class Timelapse(object):
def __init__(self):
self.imageNumber = None
self.inTimelapse = False
self.gcodeFile = None

self.captureDir = settings().getBaseFolder("timelapse")
self.snapshotUrl = settings().get("webcam", "snapshot")

def onPrintjobStarted(self, gcodeFile):
self.startTimelapse(gcodeFile)

def onPrintjobStopped(self):
self.stopTimelapse()

def onPrintjobProgress(self, oldPos, newPos, percentage):
pass

def onZChange(self, oldZ, newZ):
pass

def startTimelapse(self, gcodeFile):
self.imageNumber = 0
self.inTimelapse = True
self.gcodeFile = os.path.basename(gcodeFile)

def stopTimelapse(self):
self.imageNumber = None
self.inTimelapse = False

def captureImage(self):
if self.captureDir is None:
return

filename = os.path.join(self.captureDir, "tmp_%i.jpg" % (self.imageNumber))
self.imageNumber += 1;

captureThread = threading.Thread(target=self.captureWorker, kwargs={"filename": filename})
captureThread.start()

def captureWorker(self, filename):
urllib.urlretrieve(self.snapshotUrl, filename)

class ZTimelapse(Timelapse):
def __init__(self):
Timelapse.__init__(self)

def onZChange(self, oldZ, newZ):
self.captureImage()

class TimedTimelapse(Timelapse):
def __init__(self, interval=1):
Timelapse.__init__(self)

self.interval = interval
if self.interval < 1:
self.interval = 1 # force minimum interval of 1s

self.timerThread = None

def onPrintjobStarted(self):
Timelapse.onPrintjobStarted(self)
if self.timerThread is not None:
return

self.timerThread = threading.Thread(target=self.timerWorker)
self.timerThread.start()

def timerWorker(self):
while self.inTimelapse:
self.captureImage()
time.sleep(self.interval)

0 comments on commit 02f2436

Please sign in to comment.