Skip to content

Commit 87decc2

Browse files
ChrisYx511claude
andauthored
feat(sources/ljm): Add --quiet and --no-built-in-log CLI flags (#455)
Add argparse to the LJM source with --quiet (suppresses continuous rate output) and --no-built-in-log (disables built-in log file). Refactors file handling to manual open/close with try/finally. Updates Docker entrypoint to pass both flags. Replaces KeyboardInterrupt print with pass. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0cc826c commit 87decc2

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

src/sources/ljm/docker_entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
OM_HOST="${OMNIBUS_SERVER_HOST:-localhost}"
33
cp /config/config.py ./src/sources/ljm/config.py || exit $?
44
echo "Omnibus Server: $OM_HOST"
5-
exec uv run --no-sync ./src/sources/ljm/main.py <<EOF
5+
exec uv run --no-sync ./src/sources/ljm/main.py --quiet --no-built-in-log <<EOF
66
$OM_HOST
77
EOF
88

src/sources/ljm/main.py

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
The main module for the LabJack DAQ source.
33
"""
44

5+
import argparse
56
import sys
67
import time
78
from typing import TypedDict, cast
@@ -76,7 +77,7 @@ class DAQ_SEND_MESSAGE_TYPE(TypedDict):
7677

7778
# Function to pass to the callback function. This needs have one
7879
# parameter/argument, which will be the handle.
79-
def read_data(handle, num_addresses, scans_per_read, scan_rate):
80+
def read_data(handle, num_addresses, scans_per_read, scan_rate, *, quiet=False, no_built_in_log=False):
8081
# Converting to nanoseconds to avoid floating point inaccuracy.
8182
READ_PERIOD: int = int(1 / cast(int, scan_rate) * 1000000000)
8283

@@ -86,8 +87,12 @@ def read_data(handle, num_addresses, scans_per_read, scan_rate):
8687
# Use current time to have a unique starting point on every collection, ns to prevent floating point error.
8788
relative_last_read_time: float = time.time_ns()
8889

89-
now = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime()) # 2021-07-12_22-35-08
90-
with open(f"log_{now}.dat", "wb") as log:
90+
log = None
91+
if not no_built_in_log:
92+
now = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime()) # 2021-07-12_22-35-08
93+
log = open(f"log_{now}.dat", "wb")
94+
95+
try:
9196
while True:
9297
rates.append(time.time())
9398
if len(rates) > 50:
@@ -143,18 +148,36 @@ def read_data(handle, num_addresses, scans_per_read, scan_rate):
143148
else relative_timestamps[-1] + READ_PERIOD
144149
)
145150

146-
log.write(msgpack.packb(data_parsed))
151+
if log:
152+
log.write(msgpack.packb(data_parsed))
147153

148154
# Send data to omnibus.
149155
sender.send(CHANNEL, data_parsed)
150156

151-
print(
152-
f"\rRate: {scans_per_read * len(rates) / (time.time() - rates[0]): >6.0f} ",
153-
end="",
154-
)
157+
if not quiet:
158+
print(
159+
f"\rRate: {scans_per_read * len(rates) / (time.time() - rates[0]): >6.0f} ",
160+
end="",
161+
)
162+
finally:
163+
if log:
164+
log.close()
155165

156166

157167
def main():
168+
parser = argparse.ArgumentParser(description="LabJack DAQ Source")
169+
parser.add_argument(
170+
"--quiet",
171+
action="store_true",
172+
help="Suppress continuous output except for errors and setup information",
173+
)
174+
parser.add_argument(
175+
"--no-built-in-log",
176+
action="store_true",
177+
help="Disable writing to the built-in log file",
178+
)
179+
args = parser.parse_args()
180+
158181
try:
159182
# Open first found LabJack T7 device with any connection type and any indentifier.
160183
handle = ljm.openS("T7", "ANY", "ANY")
@@ -188,9 +211,12 @@ def main():
188211
)
189212

190213
try:
191-
read_data(handle, num_addresses, config.SCANS_PER_READ, scan_rate)
214+
read_data(
215+
handle, num_addresses, config.SCANS_PER_READ, scan_rate,
216+
quiet=args.quiet, no_built_in_log=args.no_built_in_log,
217+
)
192218
except KeyboardInterrupt:
193-
print("KeyboardInterrupt Triggered")
219+
pass
194220

195221
# Stop LJM stream.
196222
print("Stopping stream...")

0 commit comments

Comments
 (0)