Skip to content

Commit

Permalink
Split PPPP badge into proper PPPP and VIDEO badges
Browse files Browse the repository at this point in the history
Now the PPPP badge just shows the status of the PPPP connection, and
this also works for the AnkerMake M5C. The new VIDEO badge now has the
same behavior as the original PPPP badge in that it shows the status of
the video stream.
Apart from that, the MQTT badge will be reset to 'warning' when there is
no MQTT message received for 10 seconds.
  • Loading branch information
treitmayr committed Feb 10, 2024
1 parent 9b723c9 commit 222098f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 13 deletions.
21 changes: 17 additions & 4 deletions static/ankersrv.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ $(function () {
$("#progress").text("0%");
$("#nozzle-temp").text("0°C");
$("#set-nozzle-temp").attr("value", "0°C");
$("#bed-temp").text("$0°C");
$("#bed-temp").text("0°C");
$("#set-bed-temp").attr("value", "0°C");
$("#print-speed").text("0mm/s");
$("#print-layer").text("0 / 0");
Expand All @@ -223,7 +223,7 @@ $(function () {
sockets.video = new AutoWebSocket({
name: "Video socket",
url: `ws://${location.host}/ws/video`,
badge: "#badge-pppp",
badge: "#badge-video",
binary: true,

open: function () {
Expand Down Expand Up @@ -265,14 +265,27 @@ $(function () {
badge: "#badge-ctrl",
});

sockets.pppp_state = new AutoWebSocket({
name: "PPPP socket",
url: `ws://${location.host}/ws/pppp-state`,
badge: "#badge-pppp",
});

/* Only connect websockets if #player element exists in DOM (i.e., if we
* have a configuration). Otherwise we are constantly trying to make
* connections that will never succeed. */
if ($("#player").length) {
if ($("#badge-mqtt").length) {
sockets.mqtt.connect();
sockets.video.connect();
}
if ($("#badge-ctrl").length) {
sockets.ctrl.connect();
}
if ($("#badge-pppp").length) {
sockets.pppp_state.connect();
}
if ($("#player").length) {
sockets.video.connect();
}

/**
* On click of element with id "light-on", sends JSON data to wsctrl to turn light on
Expand Down
16 changes: 16 additions & 0 deletions static/tabs/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<div class="container">
<div class="row g-3">
<div class="col-md-12 col-lg-6 col-xl-8">
{% if video_supported %}
<!-- Video Container -->
<video
class="w-100 d-block rounded-3 bg-body-tertiary"
Expand All @@ -17,6 +18,16 @@
poster="{{ url_for('static', filename='img/load-screen.svg') }}"
>
</video>
{% else %}
<div class="card">
<div class="card-body text-center">
<p class="display-1">
{{ macro.bi_icon("camera-video-off") }}
</p>
<p class="display-6">Camera not available</p>
</div>
</div>
{% endif %}
</div>
<div class="col-md-12 col-lg-6 col-xl-4">
<div class="card">
Expand All @@ -30,9 +41,13 @@
</div>
<span id="badge-mqtt" class="badge">MQTT</span>
<span id="badge-pppp" class="badge">PPPP</span>
{% if video_supported %}
<span id="badge-video" class="badge">VIDEO</span>
{% endif %}
<span id="badge-ctrl" class="badge">CTRL</span>
</div>
{% endif %}
{% if video_supported %}
<div class="card-header fs-6">Video Controls</div>
<div class="card-body">
<div class="row g-3 mb-3">
Expand Down Expand Up @@ -60,6 +75,7 @@
</div>
</div>
</div>
{% endif %}
<div class="card-header fs-6">Temperature</div>
<div class="card-body">
<div class="row g-3">
Expand Down
43 changes: 38 additions & 5 deletions web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def mqtt(sock):
"""
if not app.config["login"]:
return
for data in app.svc.stream("mqttqueue"):
for data in app.svc.stream("mqttqueue", timeout=10.0):
log.debug(f"MQTT message: {data}")
sock.send(json.dumps(data))

Expand All @@ -79,12 +79,37 @@ def video(sock):
"""
Handles receiving and sending messages on the 'videoqueue' stream service through websocket
"""
if not app.config["login"]:
if not app.config["login"] or not app.config["video_supported"]:
return
for msg in app.svc.stream("videoqueue"):
sock.send(msg.data)


@sock.route("/ws/pppp-state")
def pppp_state(sock):
"""
Handles a status request for the 'pppp' stream service through websocket
"""
if not app.config["login"]:
return

pppp_connected = False

# A timeout of 3 sec should be finr, as the printer continuously sends
# PktAlive messages every second on an established connnection.
for chan, msg in app.svc.stream("pppp", timeout=3.0):
if not pppp_connected:
with app.svc.borrow("pppp") as pppp:
if pppp.connected:
pppp_connected = True
# this is the only message ever sent on this connection
# to signal that the pppp connection is up
sock.send(json.dumps({"status": "connected"}))
log.info(f"PPPP connection established")

log.warning(f"PPPP connection lost")


@sock.route("/ws/ctrl")
def ctrl(sock):
"""
Expand Down Expand Up @@ -114,7 +139,7 @@ def video_download():
Handles the video streaming/downloading feature in the Flask app
"""
def generate():
if not app.config["login"]:
if not app.config["login"] or not app.config["video_supported"]:
return
for msg in app.svc.stream("videoqueue"):
yield msg.data
Expand Down Expand Up @@ -152,6 +177,7 @@ def app_root():
configure=app.config["login"],
login_file_path=web.platform.login_path(user_os),
anker_config=anker_config,
video_supported=app.config["video_supported"],
printer=printer
)

Expand Down Expand Up @@ -269,17 +295,24 @@ def webserver(config, printer_index, host, port, insecure=False, **kwargs):
- None
"""
with config.open() as cfg:
if cfg and printer_index >= len(cfg.printers):
video_supported = False
if cfg:
if printer_index < len(cfg.printers):
# no webcam in the AnkerMake M5C (Model "V8110")
video_supported = cfg.printers[printer_index].model != "V8110"
else:
log.critical(f"Printer number {printer_index} out of range, max printer number is {len(cfg.printers)-1} ")
app.config["config"] = config
app.config["login"] = bool(cfg)
app.config["printer_index"] = printer_index
app.config["video_supported"] = video_supported
app.config["port"] = port
app.config["host"] = host
app.config["insecure"] = insecure
app.config.update(kwargs)
app.svc.register("pppp", web.service.pppp.PPPPService())
app.svc.register("videoqueue", web.service.video.VideoQueue())
if video_supported:
app.svc.register("videoqueue", web.service.video.VideoQueue())
app.svc.register("mqttqueue", web.service.mqtt.MqttQueue())
app.svc.register("filetransfer", web.service.filetransfer.FileTransferService())
app.run(host=host, port=port)
7 changes: 4 additions & 3 deletions web/lib/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from threading import Thread, Event
from datetime import datetime, timedelta
from multiprocessing import Queue
from queue import Empty


class Holdoff:
Expand Down Expand Up @@ -352,13 +353,13 @@ def borrow(self, name: str):
finally:
self.put(name)

def stream(self, name: str):
def stream(self, name: str, timeout: float=None):
try:
with self.borrow(name) as svc:
queue = Queue()

with svc.tap(lambda data: queue.put(data)):
while svc.state == RunState.Running:
yield queue.get()
except (EOFError, OSError, ServiceStoppedError):
yield queue.get(timeout=timeout)
except (EOFError, OSError, ServiceStoppedError, Empty):
return
7 changes: 6 additions & 1 deletion web/service/pppp.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ def worker_run(self, timeout):
except ConnectionResetError:
raise ServiceRestartSignal()

if not msg or msg.type != Type.DRW:
if not msg:
return

if msg.type != Type.DRW:
# forward messages other than Type.DRW without further processing
self.notify((getattr(msg, "chan", None), msg))
return

ch = self._api.chans[msg.chan]
Expand Down

0 comments on commit 222098f

Please sign in to comment.