From b6376318e6edc796b9710a08fc80e3356b162353 Mon Sep 17 00:00:00 2001
From: David Hammer <dhammer@mailbox.org>
Date: Thu, 10 Nov 2022 12:35:35 +0100
Subject: [PATCH] Check time server tid for each train instead of in init

---
 src/calng/base_correction.py | 35 +++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)

diff --git a/src/calng/base_correction.py b/src/calng/base_correction.py
index 575022fa..5a4e5b5e 100644
--- a/src/calng/base_correction.py
+++ b/src/calng/base_correction.py
@@ -581,6 +581,7 @@ class BaseCorrection(PythonDevice):
                 self._constant_to_correction_names.setdefault(constant, set()).add(name)
         self._buffer_lock = threading.Lock()
         self._last_processing_started = 0  # used for processing time and timeout
+        self._last_train_id_processed = 0  # used to keep track (and as fallback)
 
         # register slots
         def constant_override_fun(friend_fun, constant, preserve_fields):
@@ -682,17 +683,6 @@ class BaseCorrection(PythonDevice):
                 # TODO: add raw fallback mode if CalCat fails (raw data still useful)
                 return
 
-        # check time server connection
-        with self.warning_context(
-            "deviceInternalsState", WarningLampType.TIMESERVER_CONNECTION
-        ) as warn:
-            if self.getActualTimestamp().getTrainId() == 0:
-                warn(
-                    "Warning: likely missing connection to time server, "
-                    "cannot threshold against future train IDs"
-                )
-                self.set("dataFormat.trainFromFutureThreshold", np.iinfo(np.uint64).max)
-
         with self.warning_context(
             "processingState", WarningLampType.FRAME_FILTER
         ) as warn:
@@ -1106,15 +1096,27 @@ class BaseCorrection(PythonDevice):
                 self.log_status_info("Processing data")
 
             train_id = metadata.getAttribute("timestamp", "tid")
-            my_tid = self.getActualTimestamp().getTrainId()
+
+            # check time server connection
+            with self.warning_context(
+                "deviceInternalsState", WarningLampType.TIMESERVER_CONNECTION
+            ) as warn:
+                my_train_id = self.getActualTimestamp().getTrainId()
+                if my_train_id == 0:
+                    my_train_id = self._last_train_id_processed + 1
+                    warn(
+                        "Failed to get current train ID, using previously seen train "
+                        "ID for future train thresholding - if this persists, check "
+                        "connection to timeserver."
+                    )
             with self.warning_context(
-                "inputDataState", WarningLampType.TRAIN_ID
+                    "inputDataState", WarningLampType.TRAIN_ID, only_print_once=True
             ) as warn:
                 if train_id > (
-                    my_tid + self.unsafe_get("dataFormat.trainFromFutureThreshold")
+                    my_train_id + self.unsafe_get("dataFormat.trainFromFutureThreshold")
                 ):
                     warn(
-                        f"Suspecting train from the future: now is {my_tid}, "
+                        f"Suspecting train from the future: 'now' is {my_train_id}, "
                         f"received train ID {train_id}, dropping data"
                     )
                     continue
@@ -1127,7 +1129,7 @@ class BaseCorrection(PythonDevice):
                 except utils.NonMonotonicTrainIdWarning as ex:
                     warn(
                         f"Train ratio tracker noticed issue with train ID: {ex}\n"
-                        f"For the record, I think now is: {my_tid}"
+                        f"For the record, I think now is: {my_train_id}"
                     )
                     self._train_ratio_tracker.reset()
                     self._train_ratio_tracker.update(train_id)
@@ -1164,6 +1166,7 @@ class BaseCorrection(PythonDevice):
                     image_data,
                     cell_table,
                 )
+            self._last_train_id_processed = train_id
             self._buffered_status_update.set("trainId", train_id)
             self._processing_time_ema.update(
                 default_timer() - self._last_processing_started
-- 
GitLab