From 5d5a38f1de412d75bcfec1fd0709739a74d29c43 Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Tue, 22 Jan 2019 15:54:09 +0000
Subject: [PATCH] SW-488: little tweaks and fixes from live testing on scu199

---
 LCS/PyCommon/lcu_utils.py                  |  3 ++-
 SAS/TriggerServices/lib/task_info_cache.py |  5 ++---
 SAS/TriggerServices/lib/trigger_service.py | 11 ++++++-----
 3 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/LCS/PyCommon/lcu_utils.py b/LCS/PyCommon/lcu_utils.py
index 3d78d29916e..3becb6ab161 100644
--- a/LCS/PyCommon/lcu_utils.py
+++ b/LCS/PyCommon/lcu_utils.py
@@ -98,7 +98,8 @@ def stationname2hostname(station_name):
 def hostname2stationname(station_hostname):
     '''Convert a lcu hostname to a parset-like station name , like cs001c or cs001c.control.lofar to CS001'''
     # assume a hostname is encoded as stationname in lowercase with a c appended, like cs001c for CS001
-    return station_hostname.split('.')[0].strip()[:-1].upper()
+    stationname = station_hostname.split('.')[0].strip().upper()
+    return stationname[:-1] if stationname.endswith('C') else stationname
 
 def get_stations_rcu_mode(stations=None):
     '''
diff --git a/SAS/TriggerServices/lib/task_info_cache.py b/SAS/TriggerServices/lib/task_info_cache.py
index 1d2cfadaaf9..66e46bf0ce4 100644
--- a/SAS/TriggerServices/lib/task_info_cache.py
+++ b/SAS/TriggerServices/lib/task_info_cache.py
@@ -115,8 +115,7 @@ class TaskInfoCache(OTDBBusListener):
                   if ti.radb_task['starttime'] <= active_at and ti.radb_task['endtime'] >= active_at]
 
         if task_type is not None:
-            tasks = [ti for ti in tasks
-                     if ti.radb_task['task_type'] == task_type]
+            tasks = [ti for ti in tasks if ti.radb_task['type'] == task_type]
 
         return tasks
 
@@ -238,4 +237,4 @@ if __name__ == '__main__':
     # start listening on all default messaging buses,
     # and let the TaskInfoCache instance log the events as they come along.
     with TaskInfoCache() as cache:
-        waitForInterrupt()
\ No newline at end of file
+        waitForInterrupt()
diff --git a/SAS/TriggerServices/lib/trigger_service.py b/SAS/TriggerServices/lib/trigger_service.py
index 3a58efcc889..722592f8742 100644
--- a/SAS/TriggerServices/lib/trigger_service.py
+++ b/SAS/TriggerServices/lib/trigger_service.py
@@ -23,12 +23,12 @@
 
 from StringIO import StringIO
 from lxml import etree
-from datetime import datetime
+from datetime import datetime, timedelta
 
 from lofar.messaging import Service, EventMessage, ToBus
 from lofar.messaging.Service import MessageHandlerInterface
 from lofar.common.util import waitForInterrupt
-from lofar.common.lcu_utils import stationname2hostname
+from lofar.common.lcu_utils import stationname2hostname, hostname2stationname
 
 from lofar.mom.momqueryservice.momqueryrpc import MoMQueryRPC
 from lofar.specificationservices.specification_service_rpc import SpecificationRPC
@@ -409,7 +409,8 @@ class ALERTHandler(VOEventListenerInterface):
             logger.info('TBB is in correct operational mode: %s' % DEFAULT_TBB_ALERT_MODE)
 
         # Any running observations?
-        active_tasks = self._cache.get_active_tasks(stoptime, 'observation')
+        #TODO: make stoptime a datetime instance eveywhere
+        active_tasks = self._cache.get_active_tasks(datetime(1970, 1, 1) + timedelta(seconds=stoptime), 'observation')
         if active_tasks:
             otdb_ids = sorted([t.radb_task['otdb_id'] for t in active_tasks])
             logger.info('Observation(s) %s is/are running at time %s', otdb_ids, stoptime)
@@ -417,8 +418,8 @@ class ALERTHandler(VOEventListenerInterface):
             logger.warning('No observations running at %s, so TBB\'s are not recording', stoptime)
             return False
 
-        active_stations = self._cache.get_stations()
-        active_tbb_stations = set(DEFAULT_TBB_STATIONS).intersection(active_stations)
+        active_stations = set(hostname2stationname(x) for x in self._cache.get_stations())
+        active_tbb_stations = set(hostname2stationname(x) for x in DEFAULT_TBB_STATIONS).intersection(active_stations)
 
         if len(active_tbb_stations) > 0:
             logger.info('Enough TBB stations available: %s', active_tbb_stations)
-- 
GitLab