From ca4f12eb97d9ca0f233b1148ca913912ec474279 Mon Sep 17 00:00:00 2001 From: David Hammer <dhammer@mailbox.org> Date: Fri, 21 Jul 2023 19:20:35 +0200 Subject: [PATCH] Start sketching changes based on dev meeting --- src/calng/PickyBoi.py | 99 +++++++++++++++++++++++++++++++++---------- 1 file changed, 76 insertions(+), 23 deletions(-) diff --git a/src/calng/PickyBoi.py b/src/calng/PickyBoi.py index ffede381..19777a7b 100644 --- a/src/calng/PickyBoi.py +++ b/src/calng/PickyBoi.py @@ -10,6 +10,7 @@ from karabo.bound import ( KARABO_CLASSINFO, NDARRAY_ELEMENT, NODE_ELEMENT, + OUTPUT_CHANNEL, OVERWRITE_ELEMENT, SLOT_ELEMENT, STRING_ELEMENT, @@ -50,6 +51,23 @@ class PickyBoi(PythonDevice): .reconfigurable() .commit(), + UINT64_ELEMENT(expected) + .key("numTrainToCatch") + .assignmentOptional() + .defaultValue(1) + .reconfigurable() + .commit(), + + STRING_ELEMENT(expected) + .key("ppuDevice") + .assignmentOptional() + .defaultValue("") + .commit(), + + SLOT_ELEMENT(expected) + .key("watchPpu") + .commit(), + SLOT_ELEMENT(expected) .key("resetCapturedSchema") .commit(), @@ -63,7 +81,7 @@ class PickyBoi(PythonDevice): super().__init__(config) self.registerInitialFunction(self._initialization) self._schema_is_set = False - self._previous_train_seen = 0 + self._previous_tid = 0 self._just_capture_next = False self.KARABO_SLOT(self.resetCapturedSchema) self.KARABO_SLOT(self.captureNextTrain) @@ -74,37 +92,51 @@ class PickyBoi(PythonDevice): def captureNextTrain(self): self._just_capture_next = True + def watchPpu(self): + ppu_device_id = self.get("ppuDevice") + client = self.remote() + conf = client.getConfiguration(ppu_device_id) + self.handlePpuDeviceConfiguration(conf) + client.registerDeviceMonitor(ppu_device_id, self.handlePpuDeviceConfiguration) + + def handlePpuDeviceConfiguration(self, conf): + ... + # TODO + self._set_new_target_tid(new_target_tid) + self.set("nextTrainToCatch", new_target_tid) + def _initialization(self): self.KARABO_ON_DATA("input", self.input_handler) def input_handler(self, data, meta): - state = self.get("state") - current = Timestamp.fromHashAttributes( - meta.getAttributes("timestamp") - ).getTrainId() - if self._just_capture_next: - self.set("nextTrainToCatch", current) - self._just_capture_next = False - target = self.get("nextTrainToCatch") - if not self._schema_is_set: self.updateState(State.ROTATING) schema_update = Schema() ( - NODE_ELEMENT(schema_update) - .key("captured") - .commit() + OUTPUT_CHANNEL(schema_update) + .key("output") + .dataSchema(hash_to_schema(data)) + .commit(), ) - hash_to_schema(data, schema_update, "captured.") self.updateSchema(schema_update) self._schema_is_set = True - if target > current: + # TODO: handle multiple (consecutive) trains picked + state = self.get("state") + current_tid = Timestamp.fromHashAttributes( + meta.getAttributes("timestamp") + ).getTrainId() + if self._just_capture_next: + self.set("nextTrainToCatch", current_tid) + self._just_capture_next = False + target_tid = self.get("nextTrainToCatch") + + if target_tid > current_tid: if state is not State.MONITORING: self.updateState(State.MONITORING) - elif target < current: - if self._previous_train_seen < target: - self.log.ERROR(f"Missed target train of {target} :(") + elif target_tid < current_tid: + if self._previous_tid < target_tid and state is not State.ERROR: + self.log.ERROR(f"Missed target train of {target_tid} :(") self.updateState(State.ERROR) else: if state is not State.PASSIVE: @@ -112,12 +144,33 @@ class PickyBoi(PythonDevice): else: self.updateState(State.FILLING) self.log.INFO(f"Got target train {target} now :D") - new_hash = Hash() - for path in data.getPaths(): - new_hash[f"captured.{path}"] = data[path] - self.set(new_hash) + # TODO: copy train ID on metadata + self.writeChannel("output", data) self.updateState(State.DISENGAGED) - self._previous_train_seen = current + self._previous_tid = current + + def _set_new_target_tid(self, new_target_tid): + # assumes nextTrainToCatch gets set *after* this function + current_target_tid = self.get("nextTrainToCatch") + if self._previous_tid >= new_target_tid: + self.log.INFO( + f"Moved target train to {new_target_tid} even though last seen was " + f"{self._previous_tid} - will not be able to retroactively catch this." + ) + self.updateState(State.ERROR) + else: + if current_target_tid < new_target_tid: + self.log.INFO( + f"Moved target train from {current_target_tid} to {new_target_tid}" + f"even though last seen was {self._previous_tid} " + f"effectively skipping {current_target_tid}" + ) + self.updateState(State.MONITORING) + + def preReconfigure(self, config): + super().preReconfigure(config) + if config.has("nextTrainToCatch"): + self._set_new_target_tid(config["nextTrainToCatch"]) def hash_to_schema(h, root=None, prefix=""): -- GitLab