Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature custom snapshot_cmd implemented #154

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions component/timelapse.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __init__(self, confighelper: ConfigHelper) -> None:
self.byrendermacro = False
self.hyperlapserunning = False
self.printing = False
self.snapshotCmdWorking = False
self.noWebcamDb = False

self.confighelper = confighelper
Expand All @@ -71,6 +72,10 @@ def __init__(self, confighelper: ConfigHelper) -> None:
'mode': "layermacro",
'camera': "",
'snapshoturl': "http://localhost:8080/?action=snapshot",
'use_snapshot_cmd': False,
'snapshot_cmd': '',
'snapshot_cmd_check': '',
'snapshot_cmd_waittime': 10.0,
'stream_delay_compensation': 0.05,
'gcode_verbose': False,
'parkhead': False,
Expand Down Expand Up @@ -169,6 +174,8 @@ def __init__(self, confighelper: ConfigHelper) -> None:

async def component_init(self) -> None:
await self.getWebcamConfig()
if self.config['use_snapshot_cmd'] == True:
self.snapshotCmdWorking = await self.check_snapshot_cmd()

def overwriteDbconfigWithConfighelper(self) -> None:
blockedsettings = []
Expand Down Expand Up @@ -460,6 +467,26 @@ async def stop_hyperlapse(self) -> None:
logging.exception(msg)
self.hyperlapserunning = False

async def check_snapshot_cmd(self) -> bool:
if self.config['snapshot_cmd'] == '':
return False
if self.config['snapshot_cmd_check'] == '':
return True
cmd = self.config['snapshot_cmd_check']
shell_cmd: SCMDComp = self.server.lookup_component('shell_command')
scmd = shell_cmd.build_shell_command(cmd, None)
try:
cmdstatus = await scmd.run(timeout=2., verbose=False)
except Exception:
logging.exception(f"Error running cmd '{cmd}'")
return False

logging.info(f"check_snapshot_cmd: {cmd} -> {cmdstatus}")
if cmdstatus:
return True
else:
return False

async def newframe(self) -> None:
# make sure webcamconfig is uptodate before grabbing a new frame
await self.getWebcamConfig()
Expand All @@ -470,15 +497,24 @@ async def newframe(self) -> None:

self.framecount += 1
framefile = "frame" + str(self.framecount).zfill(6) + ".jpg"
cmd = "wget " + options + self.config['snapshoturl'] \
+ " -O " + self.temp_dir + framefile
if self.config['use_snapshot_cmd'] == True and not self.snapshotCmdWorking:
logging.info(f"snapshot_cmd_check failed on startup or snapshot_cmd is empty: {self.config['snapshot_cmd']}")

waittime = 2.
if self.config['use_snapshot_cmd'] == True and self.snapshotCmdWorking:
cmd = self.config['snapshot_cmd'] + " " + self.temp_dir + framefile
waittime = self.config['snapshot_cmd_waittime']
else:
cmd = "wget " + options + self.config['snapshoturl'] \
+ " -O " + self.temp_dir + framefile

self.lastframefile = framefile
logging.debug(f"cmd: {cmd}")

shell_cmd: SCMDComp = self.server.lookup_component('shell_command')
scmd = shell_cmd.build_shell_command(cmd, None)
try:
cmdstatus = await scmd.run(timeout=2., verbose=False)
cmdstatus = await scmd.run(timeout=waittime, verbose=False)
except Exception:
logging.exception(f"Error running cmd '{cmd}'")

Expand Down Expand Up @@ -512,6 +548,10 @@ async def handle_gcode_response(self, gresponse: str) -> None:
# print_started
self.cleanup()
self.printing = True
self.snapshotCmdWorking = False

if self.config['use_snapshot_cmd'] == True:
self.snapshotCmdWorking = await self.check_snapshot_cmd()

# start hyperlapse if mode is set
if self.config['mode'] == "hyperlapse":
Expand Down
17 changes: 16 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,24 @@ use or render.
This setting let you choose which camera should be used to take frames from.
It depends on the 'webcam' namespace in the moonraker DB and uses the
'snapshoturl', 'flipX' and 'flipY' associated whith selected camera. Alternatively you can configure
'snapshoturl', 'flip_x' and 'flip_y' in the moonraker.conf if your frontend doesn't support the webcams
'snapshoturl', 'flip_x' and 'flip_y' in the moonraker.conf if your frontend doesn't support the webcams
namespace of moonraker DB.

#### use_snapshot_cmd
'true' enables snapshot_cmd processing.

#### snapshot_cmd
This defines which creates the snapshot. The
See scripts/gphoto2_capture.sh for an example

#### snapshot_cmd_check'
This defines an optional testscript, which is run on start of the job. It checks if the camera is available.
If not, the normal webcam is used.

#### snapshot_cmd_waittime
This defines the wait time for the snapshot to be created.
You should increase the 'park_time' setting for better results.

#### gcode_verbose
'true' enables or 'false' disables verbosity of the Macros

Expand Down
8 changes: 8 additions & 0 deletions scripts/gphoto2_capture.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
set -e
BASEFILE=${1%%.jpg}

# note: make sure that you're camera just creates small/medium jpg and no raws or
# you're raspberrypi will habe all lot of data to process

gphoto2 --quiet --capture-image-and-download --filename="$BASEFILE.%C" --force-overwrite
5 changes: 5 additions & 0 deletions scripts/gphoto2_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

set -e
#fails if camera is not connected
exec gphoto2 --get-config /main/imgsettings/imageformat >& /dev/null