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

Adding Default Webcam Method #124

Draft
wants to merge 2 commits into
base: chrivers/webserver-mainsail-rpc
Choose a base branch
from
Draft
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
31 changes: 31 additions & 0 deletions db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"announcements": {},
"gcode_metadata": {},
"history": {},
"mainsail": {
"dashboard": {
"nonExpandPanels": {
"widescreen": []
}
},
"gcodeViewer": {
"klipperCache": {
"axis_maximum": [
235,
235,
250,
0
],
"axis_minimum": [
-15,
-15,
-2,
0
]
}
}
},
"moonraker": {},
"update_manager": {},
"webcams": []
}
10 changes: 3 additions & 7 deletions web/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,12 @@ def app_root():
anker_config = "No printers found, please load your login config..."
printer = None

if ":" in request.host:
request_host, request_port = request.host.split(":", 1)
else:
request_host = request.host
request_port = "80"
host, port = web.util.get_host_port(app)

return render_template(
"index.html",
request_host=request_host,
request_port=request_port,
request_host=host,
request_port=port,
configure=app.config["login"],
login_file_path=web.platform.login_path(user_os),
anker_config=anker_config,
Expand Down
40 changes: 35 additions & 5 deletions web/moonraker/rpc/server/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from jsonrpc import dispatcher
from flask import current_app as app

import web.util


@dispatcher.add_method(name="server.connection.identify")
def server_connection_identify(client_name, version, type, url, access_token=None, api_key=None):
Expand Down Expand Up @@ -161,12 +163,14 @@ def server_temperature_store():

@dispatcher.add_method(name="server.config")
def server_config():
host, port = web.util.get_host_port(app)

return {
"config": {
"server": {
"host": "0.0.0.0",
"port": 7125,
"ssl_port": 7130,
"host": host,
"port": port,
"ssl_port": port,
"enable_debug_logging": True,
"enable_asyncio_debug": True,
"klippy_uds_address": "/tmp/klippy_uds",
Expand Down Expand Up @@ -228,7 +232,7 @@ def server_config():
"flip_h": False,
"flip_v": False,
"rotate_90": False,
"stream_url": "/webcam/?action=stream",
"stream_url": f"ws://{host}:{port}/ws/video",
"webcam_enabled": True
},
"history": {},
Expand Down Expand Up @@ -299,7 +303,7 @@ def server_config():
"password": "{secrets.mqtt_credentials.password}",
"enable_moonraker_api": True,
# "status_objects": "\nwebhooks\ntoolhead=position,print_time\nidle_timeout=state\ngcode_macro M118"
}
}
},
"files": [
{
Expand Down Expand Up @@ -327,6 +331,32 @@ def server_config():
}


@dispatcher.add_method(name="server.webcams.list")
def server_webcams_list():
host, port = web.util.get_host_port(app)

return {
"webcams": [
{
"name": "Printer",
"location": "printer",
"service": "jmuxer-stream",
"enabled": True,
"icon": "mdiWebcam",
"target_fps": 15,
"target_fps_idle": 5,
"stream_url": f"ws://{host}:{port}/ws/video",
"snapshot_url": "",
"flip_horizontal": False,
"flip_vertical": False,
"rotation": 0,
"aspect_ratio": "4:3",
"source": "config"
}
]
}


@dispatcher.add_method(name="server.logs.rollover")
def server_logs_rollover(application):
...
30 changes: 30 additions & 0 deletions web/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,33 @@ def upload_file_to_printer(app, file):

with app.svc.borrow("filetransfer") as ft:
ft.send_file(file, user_name)


def get_host_port(app):
"""
Extracts the host and port from the incoming request.
If no host or port is provided in the request,
the function falls back to the host and port from the application configuration.

Args:
- app (object): The application object.

Returns:
A list containing.
- request_host (str): A string representing the hostname.
- request_port (str): A string representing the port number. Defaults to '80'.

Raises:
- None
"""
if hasattr(request, "host"):
if ":" in request.host:
request_host, request_port = request.host.split(":", 1)
else:
request_host = request.host
request_port = "80"
else:
request_host = app.config["host"]
request_port = app.config["port"]

return [request_host, request_port]