Skip to content

Commit

Permalink
adjusted code to allow the server to send either name
Browse files Browse the repository at this point in the history
  • Loading branch information
evalott100 committed Mar 12, 2024
1 parent b84ed1c commit 2277585
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
15 changes: 10 additions & 5 deletions src/pandablocks/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,31 +92,36 @@ class CommandException(Exception):
# zip() because typing does not support variadic type variables. See
# typeshed PR #1550 for discussion.
@overload
def _execute_commands(c1: Command[T]) -> ExchangeGenerator[Tuple[T]]: ...
def _execute_commands(c1: Command[T]) -> ExchangeGenerator[Tuple[T]]:
...


@overload
def _execute_commands(
c1: Command[T], c2: Command[T2]
) -> ExchangeGenerator[Tuple[T, T2]]: ...
) -> ExchangeGenerator[Tuple[T, T2]]:
...


@overload
def _execute_commands(
c1: Command[T], c2: Command[T2], c3: Command[T3]
) -> ExchangeGenerator[Tuple[T, T2, T3]]: ...
) -> ExchangeGenerator[Tuple[T, T2, T3]]:
...


@overload
def _execute_commands(
c1: Command[T], c2: Command[T2], c3: Command[T3], c4: Command[T4]
) -> ExchangeGenerator[Tuple[T, T2, T3, T4]]: ...
) -> ExchangeGenerator[Tuple[T, T2, T3, T4]]:
...


@overload
def _execute_commands(
*commands: Command[Any],
) -> ExchangeGenerator[Tuple[Any, ...]]: ...
) -> ExchangeGenerator[Tuple[Any, ...]]:
...


def _execute_commands(*commands):
Expand Down
5 changes: 4 additions & 1 deletion src/pandablocks/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
"DataConnection",
]

# The name of the samples field used for averaging unscaled fields
# The names of the samples field used for averaging unscaled fields
# In newer versions it's GATE_DURATION but we keep SAMPLES for backwards
# compatibility
GATE_DURATION_FIELD = "PCAP.GATE_DURATION.Value"
SAMPLES_FIELD = "PCAP.SAMPLES.Value"


Expand Down
17 changes: 12 additions & 5 deletions src/pandablocks/hdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pandablocks.commands import Arm

from .asyncio import AsyncioClient
from .connections import SAMPLES_FIELD
from .connections import GATE_DURATION_FIELD, SAMPLES_FIELD
from .responses import EndData, EndReason, FieldCapture, FrameData, ReadyData, StartData

# Define the public API of this module
Expand Down Expand Up @@ -159,11 +159,18 @@ def __init__(self) -> None:

def create_processor(self, field: FieldCapture, raw: bool):
column_name = f"{field.name}.{field.capture}"

if raw and field.capture == "Mean":
return (
lambda data: data[column_name] * field.scale / data[SAMPLES_FIELD]
+ field.offset
)

def mean_callable(data):
if GATE_DURATION_FIELD in data.dtype.names:
gate_duration = data[GATE_DURATION_FIELD]
else:
gate_duration = data[SAMPLES_FIELD]

return (data[column_name] * field.scale / gate_duration) + field.offset

return mean_callable
elif raw and (field.scale != 1 or field.offset != 0):
return lambda data: data[column_name] * field.scale + field.offset
else:
Expand Down
13 changes: 8 additions & 5 deletions tests/test_pandablocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,18 +344,21 @@ def test_get_fields():
]


def test_get_fields_type_ext_out():
@pytest.mark.parametrize("gate_duration_name", ["GATE_DURATION", "SAMPLES"])
def test_get_fields_type_ext_out(gate_duration_name):
"""Test for field type == ext_out, ensuring we add .CAPTURE to the end of the
*ENUMS command"""
conn = ControlConnection()
cmd = GetFieldInfo("PCAP")
assert conn.send(cmd) == b"PCAP.*?\n"

# First yield, the response to "PCAP.*?"
assert (
conn.receive_bytes(b"!SAMPLES 9 ext_out samples\n.\n")
== b"*DESC.PCAP.SAMPLES?\n*ENUMS.PCAP.SAMPLES.CAPTURE?\n"
request_str = bytes(f"!{gate_duration_name} 9 ext_out samples\n.\n", "utf-8")
response_str = bytes(
f"*DESC.PCAP.{gate_duration_name}?\n*ENUMS.PCAP.{gate_duration_name}.CAPTURE?\n",
"utf-8",
)
assert conn.receive_bytes(request_str) == response_str

# Responses to the *DESC and *ENUM commands
responses = [
Expand All @@ -371,7 +374,7 @@ def test_get_fields_type_ext_out():
(
cmd,
{
"SAMPLES": ExtOutFieldInfo(
gate_duration_name: ExtOutFieldInfo(
type="ext_out",
subtype="samples",
description="Number of gated samples in the current capture",
Expand Down

0 comments on commit 2277585

Please sign in to comment.