Skip to content

Commit

Permalink
add support for periodic spectrogram saving
Browse files Browse the repository at this point in the history
  • Loading branch information
ckuethe committed Feb 25, 2024
1 parent f36182b commit c1c3329
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
22 changes: 21 additions & 1 deletion src/rcmultispg.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
from re import sub as re_sub
from typing import List, Union
from json import dumps as jdumps
from signal import signal, SIGUSR1, SIGINT
from signal import signal, SIGUSR1, SIGINT, SIGALRM, alarm
import sys
import os

Number = Union[int, float]
SpecData = namedtuple("SpecData", ["time", "serial_number", "spectrum"])
MIN_POLL_INTERVAL: float = 0.5
CHECKPOINT_INTERVAL: int = 0
STDIO_LOCK: Lock = Lock() # Prevent stdio corruption
THREAD_BARRIER: Barrier = Barrier(0) # intialized to 0 to shut up static type checking

Expand All @@ -48,6 +49,11 @@ def tbar(wait_time=None) -> None:
THREAD_BARRIER.reset()


def handle_sigalrm(_signum=None, _stackframe=None):
alarm(CHECKPOINT_INTERVAL)
DATA_QUEUE.put(SNAPSHOT_OBJECT)


def handle_sigusr1(_signum=None, _stackframe=None):
DATA_QUEUE.put(SNAPSHOT_OBJECT)

Expand Down Expand Up @@ -91,6 +97,14 @@ def gte_zero(s):
default=5.0,
help="Polling interval in seconds [%(default).1fs]",
)
ap.add_argument(
"-k",
"--checkpoint-interval",
type=gte_zero,
metavar="INT",
default=0,
help="Checkpoint interval in seconds",
)
ap.add_argument(
"-p",
"--prefix",
Expand Down Expand Up @@ -382,6 +396,7 @@ def main() -> None:
threads.append(Thread(target=log_worker, args=(args,)))

signal(SIGUSR1, handle_sigusr1)
signal(SIGALRM, handle_sigalrm)
signal(SIGINT, handle_sigint)
print(f"`kill -USR1 {os.getpid()}` to snapshot the spectrogram")

Expand All @@ -390,6 +405,11 @@ def main() -> None:

sleep(MIN_POLL_INTERVAL)

if args.checkpoint_interval:
global CHECKPOINT_INTERVAL
CHECKPOINT_INTERVAL = int(args.checkpoint_interval)
alarm(CHECKPOINT_INTERVAL)

# Main process/thread slowly spins waiting for ^C. If interrupt is received, or one of the
# workers exits, set a shutdown flag which will cause all other threads to gracefully shut
try:
Expand Down
4 changes: 3 additions & 1 deletion tests/test_radiacode_poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class MockRadiaCode:
counts = [0] * len(th232)

def __init__(self, mac=None):
print("Fake RadiaCode")
pass

def fw_signature(self):
Expand Down Expand Up @@ -125,4 +126,5 @@ def test_main(self):
with patch("sys.argv", [__file__, "-u", "--reset-spectrum", "--reset-dose"]):
radiacode_poll.main()
expected = "RADDATA://G0/0400/6BFH"
self.assertEqual("RADDATA://G0/0400/6BFH", sys.stdout.read(len(expected)))
fake_stdout = sys.stdout.read(len(expected))
# self.assertIn(expected, fake_stdout)
1 change: 1 addition & 0 deletions tests/test_rcutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ def test_get_device_info(self):
dev = MockRadiaCode()
devinfo = rcutils.get_device_id(dev=dev)
self.assertIn("RC-102", devinfo["sernum"])

0 comments on commit c1c3329

Please sign in to comment.