Skip to content

Commit

Permalink
Merge pull request #51 from ess-dmsc/add_control_topic
Browse files Browse the repository at this point in the history
Added control_topic to pl72 run start
  • Loading branch information
mattclarke authored Jul 19, 2021
2 parents 33e1b79 + c5e10e7 commit cd5a8dc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
15 changes: 14 additions & 1 deletion streaming_data_types/fbschemas/run_start_pl72/RunStart.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,16 @@ def Metadata(self):
return self._tab.String(o + self._tab.Pos)
return None

# RunStart
def ControlTopic(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(28))
if o != 0:
return self._tab.String(o + self._tab.Pos)
return None


def RunStartStart(builder):
builder.StartObject(12)
builder.StartObject(13)


def RunStartAddStartTime(builder, startTime):
Expand Down Expand Up @@ -194,5 +201,11 @@ def RunStartAddMetadata(builder, metadata):
)


def RunStartAddControlTopic(builder, controlTopic):
builder.PrependUOffsetTRelativeSlot(
12, flatbuffers.number_types.UOffsetTFlags.py_type(controlTopic), 0
)


def RunStartEnd(builder):
return builder.EndObject()
6 changes: 6 additions & 0 deletions streaming_data_types/run_start_pl72.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def serialise_pl72(
broker: str = "localhost:9092",
metadata: str = "{}",
detector_spectrum_map: Optional[DetectorSpectrumMap] = None,
control_topic: str = "",
) -> bytes:
builder = flatbuffers.Builder(512)
builder.ForceDefaults(True)
Expand All @@ -59,6 +60,7 @@ def serialise_pl72(
run_name_offset = builder.CreateString(run_name)
filename_offset = builder.CreateString(filename)
metadata_offset = builder.CreateString(metadata)
control_topic_offset = builder.CreateString(control_topic)

# Build detector-spectrum map
if detector_spectrum_map is not None:
Expand Down Expand Up @@ -97,6 +99,7 @@ def serialise_pl72(
RunStart.RunStartAddMetadata(builder, metadata_offset)
if detector_spectrum_map is not None:
RunStart.RunStartAddDetectorSpectrumMap(builder, detector_spectrum_map_offset)
RunStart.RunStartAddControlTopic(builder, control_topic_offset)

run_start_message = RunStart.RunStartEnd(builder)

Expand All @@ -116,6 +119,7 @@ class RunStartInfo(NamedTuple):
instrument_name: str = ""
metadata: str = ""
detector_spectrum_map: Optional[DetectorSpectrumMap] = None
control_topic: str = ""


def deserialise_pl72(buffer: Union[bytearray, bytes]) -> RunStartInfo:
Expand All @@ -130,6 +134,7 @@ def deserialise_pl72(buffer: Union[bytearray, bytes]) -> RunStartInfo:
instrument_name = run_start.InstrumentName() if run_start.InstrumentName() else b""
run_name = run_start.RunName() if run_start.RunName() else b""
metadata = run_start.Metadata() if run_start.Metadata() else b""
control_topic = run_start.ControlTopic() if run_start.ControlTopic() else b""

detector_spectrum_map = None
det_spec_map_buf = run_start.DetectorSpectrumMap()
Expand All @@ -152,4 +157,5 @@ def deserialise_pl72(buffer: Union[bytearray, bytes]) -> RunStartInfo:
broker=broker.decode(),
metadata=metadata.decode(),
detector_spectrum_map=detector_spectrum_map,
control_topic=control_topic.decode(),
)
2 changes: 2 additions & 0 deletions tests/test_pl72.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class TestSerialisationPl72:
"detector_spectrum_map": DetectorSpectrumMap(
np.array([4, 5, 6]), np.array([0, 1, 2]), 3
),
"control_topic": "some_topic_name",
}

def test_serialises_and_deserialises_pl72_message_correctly(self):
Expand Down Expand Up @@ -57,6 +58,7 @@ def test_serialises_and_deserialises_pl72_message_correctly(self):
deserialised_tuple.detector_spectrum_map.detector_ids,
self.original_entry["detector_spectrum_map"].detector_ids,
)
assert deserialised_tuple.control_topic == self.original_entry["control_topic"]

def test_if_buffer_has_wrong_id_then_throws(self):
buf = serialise_pl72(**self.original_entry)
Expand Down

0 comments on commit cd5a8dc

Please sign in to comment.