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

fix: Use Moonraker Webcam API to streamline Config #125

Merged
merged 2 commits into from
Jul 28, 2023
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/test_code.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: test-code
on: [push, pull_request]
jobs:
lint-python-code:
runs-on: ubuntu-18.04
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: ricardochaves/[email protected]
Expand Down
96 changes: 54 additions & 42 deletions component/timelapse.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)
if TYPE_CHECKING:
from confighelper import ConfigHelper
from .webcam import WebcamManager, WebCam
from websockets import WebRequest
from . import shell_command
from . import klippy_apis
Expand Down Expand Up @@ -53,12 +54,6 @@ def __init__(self, confighelper: ConfigHelper) -> None:
self.server = confighelper.get_server()
self.klippy_apis: APIComp = self.server.lookup_component('klippy_apis')
self.database: DBComp = self.server.lookup_component("database")
try:
self.webcams_db = self.database.wrap_namespace("webcams")
except Exception as e:
self.noWebcamDb = True
logging.info(f"No 'Webcams' namespace in database! "
f"Exception: {e}")

# setup static (nonDB) settings
out_dir_cfg = confighelper.get(
Expand Down Expand Up @@ -123,21 +118,6 @@ def __init__(self, confighelper: ConfigHelper) -> None:
# support the settings endpoint
self.overwriteDbconfigWithConfighelper()

# Read Webcam config from Database
try:
camUUID = self.config['camera']
if not self.config['camera'] == "" and not self.noWebcamDb:
webcamconfig = self.webcams_db[camUUID]
if isinstance(webcamconfig, asyncio.Future):
self.parseWebcamConfig(webcamconfig.result())
else:
self.parseWebcamConfig(webcamconfig)

except Exception as e:
logging.info(f"something went wrong getting Cam"
f" UUID:'{camUUID}' from Database. "
f"Exception: {e}")

# check if ffmpeg is installed
self.ffmpeg_installed = os.path.isfile(self.ffmpeg_binary_path)
if not self.ffmpeg_installed:
Expand Down Expand Up @@ -187,6 +167,9 @@ def __init__(self, confighelper: ConfigHelper) -> None:
"/machine/timelapse/lastframeinfo", ['GET'],
self.webrequest_lastframeinfo)

async def component_init(self) -> None:
await self.getWebcamConfig()

def overwriteDbconfigWithConfighelper(self) -> None:
blockedsettings = []

Expand All @@ -210,25 +193,47 @@ def overwriteDbconfigWithConfighelper(self) -> None:
logging.debug(f"blockedsettings {self.config['blockedsettings']}")

async def getWebcamConfig(self) -> None:
# Read Webcam config from Database
webcam_name = self.config['camera']
try:
camUUID = self.config['camera']
if not self.config['camera'] == "" and not self.noWebcamDb:
webcamconfig = self.webcams_db[camUUID]
if isinstance(webcamconfig, asyncio.Future):
self.parseWebcamConfig(await webcamconfig)
else:
self.parseWebcamConfig(webcamconfig)
wcmgr: WebcamManager = self.server.lookup_component("webcam")
cams = wcmgr.get_webcams()

if not cams:
logging.info("WARNING: no camera configured, " +
"using the fallback config")
fallback = {'snapshot_url': self.config['snapshoturl'],
'rotation': self.config['rotation'],
'flip_horizontal': self.config['flip_x'],
'flip_vertical': self.config['flip_y']
}
self.parseWebcamConfig(fallback)
return

if webcam_name and webcam_name in cams:
camera = cams[webcam_name]
else:
camera = list(cams.values())[0]

self.parseWebcamConfig(camera.as_dict())

except Exception as e:
logging.info(f"something went wrong getting"
f"Cam UUID:{camUUID} from Database. "
f"Exception: {e}")
f"Cam Camera:{webcam_name} from Database. "
f"Exception: {e}"
)

def parseWebcamConfig(self, webcamconfig) -> None:
snapshoturl = webcamconfig['urlSnapshot']
flip_x = webcamconfig['flipX']
flip_y = webcamconfig['flipY']
rotation = webcamconfig['rotate']
snapshoturl = webcamconfig['snapshot_url']
flip_x = webcamconfig['flip_horizontal']
flip_y = webcamconfig['flip_vertical']
rotation = webcamconfig['rotation']

oldWebcamConfig = {"url": self.config['snapshoturl'],
"flip_x": self.config['flip_x'],
"flip_y": self.config['flip_y'],
"rotation": self.config['rotation']
}

self.config['snapshoturl'] = self.confighelper.get('snapshoturl',
snapshoturl
Expand All @@ -242,14 +247,6 @@ def parseWebcamConfig(self, webcamconfig) -> None:
self.config['rotation'] = self.confighelper.getint('rotation',
rotation
)
logging.debug("snapshoturlConfig:"
f"{self.config['snapshoturl']}")

logging.debug("flip x/y, rotation: "
f"{self.config['flip_x']}/"
f"{self.config['flip_y']}, "
f"{self.config['rotation']}"
)

if not self.config['snapshoturl'].startswith('http'):
if not self.config['snapshoturl'].startswith('/'):
Expand All @@ -259,6 +256,21 @@ def parseWebcamConfig(self, webcamconfig) -> None:
self.config['snapshoturl'] = "http://localhost" + \
self.config['snapshoturl']

# check if settings have changed and if so creat log entry
newWebcamConfig = {"url": self.config['snapshoturl'],
"flip_x": self.config['flip_x'],
"flip_y": self.config['flip_y'],
"rotation": self.config['rotation']
}

if not oldWebcamConfig == newWebcamConfig:
logging.info("snapshoturl: "
f"{self.config['snapshoturl']}, "
f"Flip V/H: {self.config['flip_y']}/"
f"{self.config['flip_y']}, "
f"rotation: {self.config['rotation']}"
)

async def webrequest_lastframeinfo(self,
webrequest: WebRequest
) -> Dict[str, Any]:
Expand Down