Expand description
The streamer binary: streams LabJack T7 analog input samples into NATS JetStream.
The streamer is the first stage of the data path: streamer -> local NATS
JetStream -> archiver -> Parquet, with the exporter serving the Parquet files as
CSV over NATS. It reads its active configuration from a local KV bucket
(avenabox by default), optionally mirrors that entry from a central NATS
deployment, opens the LabJack over Ethernet, and publishes one FlatBuffer
Scan message per channel per LJM read. Structured configurations publish to
<root>.<site>.<box>.<source>.live.chNN (for example
avenars.<site>.<box>.<source>.live.chNN); legacy configurations publish to
<root>.<asset>.data.chNN (see the subjects module).
A configuration change in KV that alters SampleConfig (channels, rates, identity
and subject fields, rotate_secs or labjack_on_off) stops the active LabJack
stream and restarts it with the new settings. Calibrations, filters and label fields
are not part of it, so changing them leaves the stream running. Setting
labjack_on_off to false stops sampling until the configuration changes again.
Β§Configuration
NATS_SERVERS- Comma-separated local NATS server URLs. Default:nats://127.0.0.1:4222.NATS_CREDS_FILE- Credentials file for the local NATS connection. Default:apt.creds.JS_DOMAIN- JetStream domain for the local connection. Unset or empty uses the default domain.CFG_BUCKET- Local KV bucket that holds the streamer configuration. Also the fallback forCENTRAL_CFG_BUCKET. Default:avenabox.CFG_KEY- Key of the configuration entry in that bucket. Also the fallback forCENTRAL_CFG_KEY. Default:unknown-site.macbook.unknown-source.config.CENTRAL_NATS_SERVERS- Comma-separated central NATS server URLs. When neither this norCFG_NATS_SERVERSis set, central mirroring is off.CFG_NATS_SERVERS- Fallback forCENTRAL_NATS_SERVERS.CENTRAL_NATS_CREDS_FILE- Credentials file for the central connection. Default: the value ofNATS_CREDS_FILE, orapt.creds.CENTRAL_CFG_BUCKET- Central KV bucket to mirror from. Default:CFG_BUCKET, oravenabox.CENTRAL_CFG_KEY- Central KV key to mirror from. Default:CFG_KEY, orunknown-site.macbook.unknown-source.config.CENTRAL_JS_DOMAIN- JetStream domain on the central server. Falls back toCFG_JS_DOMAIN; unset uses the default domain.STREAM_MAX_BYTES- Byte limit for the JetStream stream, greater than zero. Unset means unlimited (-1).STREAMER_MAX_LABJACK_FAILURES- Consecutive sampler failures before the process exits. Values that do not parse or are zero use the default. Default:5.STREAMER_LABJACK_RETRY_DELAY_SECS- Delay before restarting the sampler after a failure, in seconds. Default:5.LABJACK_IP- IPv4 address of the LabJack T7. Required unlessLABJACK_IDENTIFIERholds an IPv4 address.LABJACK_IDENTIFIER- Fallback forLABJACK_IP, used only when it parses as an IPv4 address.LABJACK_SERIAL- Expected serial number. When set, a device with another serial is rejected.ANYor a non-numeric value disables the check.LABJACK_NAME- Logical device name. Only logged.LJM_PATH- Path to the LJM shared library (dynlinkbuilds only). When unset, no path is passed toljmrs.
Β§Design
- Timestamps are derived, not read. The LJM stream API returns values without
timestamps, so
StreamClocknumbers the batches and computes each batchβs first-sample time from the actual scan rate. It slews the timeline toward the system clock by at most 1 ms a minute, so drift between the LabJack crystal and the host clock does not grow over runs that last weeks, without chasing read-latency noise (seeStreamClock). - Publishing does not wait for JetStream acknowledgements. The acks for a batch are checked on a spawned task so a slow ack does not delay the next LabJack read, which would risk a device buffer overflow.
- Scans that LJM fills with
-9999.0after a buffer overflow are published as NaN so they are not archived as real readings. - After
STREAMER_MAX_LABJACK_FAILURESconsecutive failures the process exits with success, so a systemd unit that restarts only on failure leaves it down instead of looping on a missing device. - Central configuration is parsed before it is written to local KV, so an invalid central value cannot replace the last working local one.
ModulesΒ§
- labjack π
- LabJack T7 connection and handle-inspection helpers for the
streamerbinary. - ljm_
mode π - LabJack LJM library initialization for the selected linking mode.
- nats_
config π - Shared NATS server-list and JetStream context helpers.
- sample_
data_ πgenerated - subjects π
- NATS subject builders for LabJack live data and archive exports.
StructsΒ§
- Central
KvSync πConfig - Connection and key details for mirroring configuration from central NATS.
- LabJack
Guard π - RAII guard that stops streaming and closes the LabJack handle on drop.
- Nested
Config π - Raw top-level configuration document stored in NATS KV.
- Sample
Config π - Normalized streamer configuration used by the sampling loop.
- Sensor
Settings π - Raw sensor settings section from the dashboard configuration document.
- Stream
Clock π - Tracks timestamp continuity for successive LabJack stream batches.
EnumsΒ§
- Clock
Adjustment π - What the clock decided to do at the end of one drift window.
ConstantsΒ§
- CLOCK_
CHECK_ πWINDOW_ NS - Length of each window over which clock drift is measured, in nanoseconds (60 s).
- CLOCK_
LOG_ πEVERY_ WINDOWS - Windows between summary log lines about slewing (60, about one hour).
- CLOCK_
MAX_ πSLEW_ NS - Largest adjustment made in one window when slewing, in nanoseconds (1 ms).
- CLOCK_
OFFSET_ πHISTORY_ WINDOWS - Windows over which the slewing offset is taken as the smallest window offset (15, about a quarter of an hour).
- CLOCK_
SLEW_ πTHRESHOLD_ NS - Smallest window offset that is acted on, in nanoseconds (5 ms).
- CLOCK_
STEP_ πTHRESHOLD_ NS - Offset treated as a system clock step rather than drift, in nanoseconds (2 s).
- CLOCK_
STEP_ πWINDOWS - Consecutive windows a step-sized offset must persist before the timeline jumps.
- LJM_
DUMMY_ πVALUE - Value LJM inserts for scans lost to a stream buffer overflow when it
auto-recovers (LJMβs
LJM_DUMMY_VALUEconstant).
FunctionsΒ§
- central_
kv_ πsync_ config_ from_ env - Builds central-to-local KV mirroring settings from environment variables.
- connect_
nats_ πwith_ creds - Connects to NATS using a credentials file and explicit server list.
- decide_
adjustment π - Decides how to adjust the timeline from two consecutive window offsets.
- deinterleave_
channel π - Copies one channelβs samples out of an interleaved LJM batch into
out. - derive_
sample_ πinterval_ ns - Converts the actual LabJack scan rate into a sample interval in nanoseconds.
- ensure_
kv_ πbucket - Opens a KV bucket, creating it if it does not exist.
- ensure_
stream_ πexists - Creates or reconciles the JetStream stream used for live LabJack samples.
- env_
nonempty π - Returns a trimmed environment variable value when it is set and non-empty.
- labjack_
retry_ πdelay_ from_ env - Reads the delay between sampler restarts after a failed run.
- load_
config_ πfrom_ kv - Loads and parses the initial streamer configuration from local NATS KV.
- local_
creds_ πpath_ from_ env - Returns the credentials file used for local NATS connections.
- main π
- Starts the streamer service, configuration watchers, and sampler supervisor.
- max_
labjack_ πfailures_ from_ env - Reads the number of consecutive sampler failures after which the streamer exits.
- mirror_
central_ πkv_ once - Performs one central-to-local KV mirror pass during streamer bootstrap.
- mirror_
remote_ πkv_ entry_ to_ local - Copies one remote KV configuration entry into the local KV bucket if needed.
- replace_
dummy_ πsamples - Replaces LJM auto-recovery placeholders with NaN and returns how many.
- run_
central_ πkv_ sync - Watches central NATS KV and mirrors valid updates into the local bucket.
- run_
sampler π - Supervises LabJack sampling across config changes and transient failures.
- sample_
config_ πfrom_ json - Parses a NATS KV JSON value into a runtime sampling config.
- sample_
config_ πfrom_ nested - Converts the dashboard JSON shape into the runtime sampling configuration.
- sample_
with_ πconfig - Runs one LabJack streaming session with a fixed configuration.
- stream_
max_ πbytes_ from_ env - Reads the optional JetStream stream byte limit from
STREAM_MAX_BYTES. - unix_
time_ πnow_ ns - Returns the current Unix timestamp in nanoseconds.
- watch_
kv_ πconfig - Watches local NATS KV for streamer configuration updates.