Skip to content

3. Share the motion reports

Run a local MoQ relay, add the MoQ module from the Marketplace, send the detector's reports to the relay from motion-watcher, and read them in a second project, motion-log, that writes them to a file.

The detector’s reports are inside the motion-watcher stream. In this step you’ll send them to a MoQ relay, and read them there from a second project, motion-log, that keeps a file of every time motion starts and stops. The two projects share nothing but the relay: you can stop, change and play either one, and the other carries on.

motion-watcher sends its reports to the relay, and motion-log reads them there.

Install the open-source moq-relay, and give it a configuration that listens on this machine only and lets anyone on it publish and read:

Terminal window
cargo install moq-relay --version "=0.17.7" --locked
~/moq-relay.toml
[listen]
bind = "127.0.0.1:4443"
[listen.tls]
generate = ["localhost"]
[web.http]
listen = "127.0.0.1:4443"
[auth]
public = ["**"]

Run it in a terminal of its own, and leave it running:

Terminal window
moq-relay ~/moq-relay.toml

In motion-watcher, choose File › Marketplace…, open tatolab-moq, and press Add to motion-watcher. It brings the MoQ nodes.

Add three nodes to the stream with Tab, and set their inputs in Properties, or right on their cards:

Node Rename it Inputs
MoqGateway gateway kind moq-relay, relay_url http://127.0.0.1:4443, broadcast motion-watcher
MoqTrack motion-track gateway: choose the gateway node; track motion
MoqTrackSend send track: choose the track node

Rename a node in Properties, in its name field. Then wire the detector’s motion pin into the send’s content pin. The gateway connects to the relay and publishes the broadcast motion-watcher.hang, and the send writes every report to its track motion.

The gateway is the stream’s one connection to the relay. A MoqTrack has no pins of its own to wire: it declares one track of the gateway’s broadcast, and each send names the track it writes to.

In the Projects window, make another project, motion-log; it opens in a window of its own. Add tatolab-moq to it from the Marketplace, as you did for motion-watcher.

Then, in the Files view, press New node file and name it nodes/motion_event_log.py. Replace the template with the node that keeps the file:

nodes/motion_event_log.py
import json
from dataclasses import dataclass
from pathlib import Path
from tatolab.stream import Input, Records, RuntimeContextFullAccess, RuntimeContextLimitedAccess, log, node
@dataclass
class MotionEventLogConfig:
path: str = "motion-events.jsonl"
@node.meta(category="Output", description="Appends a line to a file each time motion starts or stops.")
@node.process
class MotionEventLog:
motion = Input(Records, delivery="ordered", description="Motion reports, with `changed` and `moving`.")
def __init__(self, config: MotionEventLogConfig) -> None:
self.path = config.path
self.moving: bool | None = None
def setup(self, ctx: RuntimeContextFullAccess) -> None:
path = Path(ctx.stream_project_directory) / self.path
self.file = path.open("a")
log.info("logging motion", path=str(path))
def process(self, ctx: RuntimeContextLimitedAccess) -> None:
while (report := ctx.inputs.read(self.motion)) is not None:
moving = bool(report["moving"])
if moving == self.moving:
continue
self.moving = moving
event = {"event": "started" if moving else "stopped", "changed": round(report["changed"], 4)}
self.file.write(json.dumps(event) + "\n")
self.file.flush()
def teardown(self, ctx: RuntimeContextFullAccess) -> None:
self.file.close()

Save it.

delivery="ordered" means the log reads every report, in order, where the detector’s newest reads only the latest frame.

Delete motion-log’s test pattern, and add:

Node Rename it Inputs
MoqGateway gateway kind moq-relay, relay_url http://127.0.0.1:4443, broadcast motion-log
MoqTrackReceive motion gateway: choose the gateway node; broadcast motion-watcher.hang, track motion, family records
MotionEventLog event-log

Wire the receive’s content into the log’s motion. Every stream that reads from a relay needs a gateway of its own: it’s the stream’s connection to the relay. Its broadcast names what this stream would publish, so give it a name of its own.

Press Play in motion-log’s window. Select the receive: the Node visualizer shows the reports arriving from motion-watcher.

Wave at the camera, then look at the file the log keeps, motion-events.jsonl in the motion-log folder, in a terminal or any editor:

Terminal window
cat ~/Documents/"Tato Studio"/motion-log/motion-events.jsonl
Output
{"event": "started", "changed": 0.1391}
{"event": "stopped", "changed": 0.0018}

In motion-watcher’s window, press Stop. motion-log keeps running, waiting for the track. Press Play in motion-watcher again, and motion-log reads the reports again on its own.

This is the point of sharing through a relay: the camera project doesn’t know motion-log exists. Add a third project that reads the same track, or move motion-log to another machine and point its gateway at a relay both can reach, and motion-watcher doesn’t change. See Across machines.

Next you’ll look inside both running streams: Look inside.