From 45fef143c67c62c56a723567778f7440a5e8cc64 Mon Sep 17 00:00:00 2001
From: thijs snijder <snijder@astron.nl>
Date: Tue, 19 Apr 2022 10:24:24 +0200
Subject: [PATCH] first attempt at adding new temperature_manager device

---
 .gitlab-ci.yml                                | 12 ++++++
 CDB/LOFAR_ConfigDb.json                       |  8 ++++
 CDB/stations/simulators_ConfigDb.json         | 10 +++++
 docker-compose/device-temperature-manager.yml | 42 +++++++++++++++++++
 .../startup/01-devices.py                     |  1 +
 sbin/run_integration_test.sh                  |  2 +-
 .../source/devices/temperature-manager.rst    |  4 ++
 tangostationcontrol/docs/source/index.rst     |  1 +
 tangostationcontrol/setup.cfg                 |  1 +
 .../tangostationcontrol/devices/boot.py       |  1 +
 .../devices/temperature_manager.py            |  8 ++--
 .../test_device_temperature_manager.py        | 16 +++++++
 12 files changed, 101 insertions(+), 5 deletions(-)
 create mode 100644 docker-compose/device-temperature-manager.yml
 create mode 100644 tangostationcontrol/docs/source/devices/temperature-manager.rst
 create mode 100644 tangostationcontrol/tangostationcontrol/integration_test/default/devices/test_device_temperature_manager.py

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 5c6e486d8..85d254557 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -109,6 +109,7 @@ docker_build_image_all:
     - bash $CI_PROJECT_DIR/sbin/tag_and_push_docker_image.sh device-sst latest
     - bash $CI_PROJECT_DIR/sbin/tag_and_push_docker_image.sh device-unb2 latest
     - bash $CI_PROJECT_DIR/sbin/tag_and_push_docker_image.sh device-xst latest
+    - bash $CI_PROJECT_DIR/sbin/tag_and_push_docker_image.sh device-temperature-manager latest
 docker_build_image_elk:
   extends: .base_docker_images_except
   only:
@@ -386,6 +387,17 @@ docker_build_image_device_xst:
   script:
 #    Do not remove 'bash' or statement will be ignored by primitive docker shell
     - bash $CI_PROJECT_DIR/sbin/tag_and_push_docker_image.sh device-xst $tag
+docker_build_image_device_temperature_manager:
+  extends: .base_docker_images_except
+  only:
+    refs:
+      - merge_requests
+    changes:
+      - docker-compose/device-temperature-manager.yml
+      - docker-compose/lofar-device-base/*
+  script:
+#    Do not remove 'bash' or statement will be ignored by primitive docker shell
+    - bash $CI_PROJECT_DIR/sbin/tag_and_push_docker_image.sh device-temperature-manager $tag
 newline_at_eof:
   stage: linting
   before_script:
diff --git a/CDB/LOFAR_ConfigDb.json b/CDB/LOFAR_ConfigDb.json
index 2032c4de7..ab989d5d8 100644
--- a/CDB/LOFAR_ConfigDb.json
+++ b/CDB/LOFAR_ConfigDb.json
@@ -21,6 +21,14 @@
                 }
             }
         },
+        },
+        "Temperature_manager": {
+            "STAT": {
+                "Temperature_manager": {
+                    "STAT/Temperature_manager/1": {}
+                }
+            }
+        },
         "TileBeam": {
             "STAT": {
                 "TileBeam": {
diff --git a/CDB/stations/simulators_ConfigDb.json b/CDB/stations/simulators_ConfigDb.json
index 59fcdee54..9806a2ca4 100644
--- a/CDB/stations/simulators_ConfigDb.json
+++ b/CDB/stations/simulators_ConfigDb.json
@@ -116,6 +116,16 @@
                 }
             }
         },
+        "Temperature_manager": {
+            "STAT": {
+                "Temperature_manager": {
+                    "STAT/Temperature_manager/1": {
+                        "properties": {
+                        }
+                    }
+                }
+            }
+        },
         "RECV": {
             "STAT": {
                 "RECV": {
diff --git a/docker-compose/device-temperature-manager.yml b/docker-compose/device-temperature-manager.yml
new file mode 100644
index 000000000..5cb69c114
--- /dev/null
+++ b/docker-compose/device-temperature-manager.yml
@@ -0,0 +1,42 @@
+#
+# Requires:
+#   - lofar-device-base.yml
+#
+version: '2'
+
+volumes:
+  iers-data: {}
+
+services:
+  device-pdu:
+    image: device-temperature-manager
+    # build explicitly, as docker-compose does not understand a local image
+    # being shared among services.
+    build:
+        context: ..
+        dockerfile: docker-compose/lofar-device-base/Dockerfile
+        args:
+            SOURCE_IMAGE: ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_USER}-tango-itango:${TANGO_ITANGO_VERSION}
+    container_name: ${CONTAINER_NAME_PREFIX}device-temperature-manager
+    logging:
+      driver: "json-file"
+      options:
+        max-size: "100m"
+        max-file: "10"
+    networks:
+      - control
+    ports:
+      - "5717:5717" # unique port for this DS
+    extra_hosts:
+      - "host.docker.internal:host-gateway"
+    volumes:
+      - ..:/opt/lofar/tango:rw
+    environment:
+      - TANGO_HOST=${TANGO_HOST}
+    working_dir: /opt/lofar/tango
+    entrypoint:
+      - bin/start-ds.sh
+      # configure CORBA to _listen_ on 0:port, but tell others we're _reachable_ through ${HOSTNAME}:port, since CORBA
+      # can't know about our Docker port forwarding
+      - l2ss-temperature-manager Temperature_manager STAT -v -ORBendPoint giop:tcp:0:5717 -ORBendPointPublish giop:tcp:${HOSTNAME}:5717
+    restart: unless-stopped
diff --git a/docker-compose/jupyter/ipython-profiles/stationcontrol-jupyter/startup/01-devices.py b/docker-compose/jupyter/ipython-profiles/stationcontrol-jupyter/startup/01-devices.py
index 1434393a8..da06b7a71 100644
--- a/docker-compose/jupyter/ipython-profiles/stationcontrol-jupyter/startup/01-devices.py
+++ b/docker-compose/jupyter/ipython-profiles/stationcontrol-jupyter/startup/01-devices.py
@@ -12,6 +12,7 @@ pdu = DeviceProxy("STAT/PDU/1")
 beamlet = DeviceProxy("STAT/Beamlet/1")
 digitalbeam = DeviceProxy("STAT/DigitalBeam/1")
 docker = DeviceProxy("STAT/Docker/1")
+temperature_manager = DeviceProxy("STAT/Temperature_manager/1")
 
 # Put them in a list in case one wants to iterate
 devices = [apsct, apspu, recv, sdp, sst, xst, unb2, boot, tilebeam, beamlet, digitalbeam, docker]
diff --git a/sbin/run_integration_test.sh b/sbin/run_integration_test.sh
index b703eae61..8b12f62eb 100755
--- a/sbin/run_integration_test.sh
+++ b/sbin/run_integration_test.sh
@@ -20,7 +20,7 @@ sleep 1 # dsconfig container must be up and running...
 # shellcheck disable=SC2016
 echo '/usr/local/bin/wait-for-it.sh ${TANGO_HOST} --strict --timeout=300 -- true' | make run dsconfig bash -
 
-DEVICES="device-boot device-apsct device-apspu device-sdp device-pdu device-recv device-sst device-unb2 device-xst device-beamlet device-digitalbeam device-tilebeam device-pdu"
+DEVICES="device-boot device-apsct device-apspu device-sdp device-pdu device-recv device-sst device-unb2 device-xst device-beamlet device-digitalbeam device-tilebeam device-pdu device-temperature-manager"
 SIMULATORS="sdptr-sim recv-sim unb2-sim apsct-sim apspu-sim"
 
 # Build only the required images, please do not build everything that makes CI
diff --git a/tangostationcontrol/docs/source/devices/temperature-manager.rst b/tangostationcontrol/docs/source/devices/temperature-manager.rst
new file mode 100644
index 000000000..a69b807b7
--- /dev/null
+++ b/tangostationcontrol/docs/source/devices/temperature-manager.rst
@@ -0,0 +1,4 @@
+temperature-manager
+====================
+
+``temperature_manager == DeviceProxy("STAT/Temperature_manager/1")``
diff --git a/tangostationcontrol/docs/source/index.rst b/tangostationcontrol/docs/source/index.rst
index d89a0cda0..659b7ffc1 100644
--- a/tangostationcontrol/docs/source/index.rst
+++ b/tangostationcontrol/docs/source/index.rst
@@ -28,6 +28,7 @@ Even without having access to any LOFAR2.0 hardware, you can install the full st
    devices/recv
    devices/sdp
    devices/sst-xst
+   devices/temperature-manager
    devices/configure
    configure_station
    alerting
diff --git a/tangostationcontrol/setup.cfg b/tangostationcontrol/setup.cfg
index 997b7947d..b9137a437 100644
--- a/tangostationcontrol/setup.cfg
+++ b/tangostationcontrol/setup.cfg
@@ -50,6 +50,7 @@ console_scripts =
     l2ss-statistics-writer = tangostationcontrol.statistics_writer.statistics_writer:main
     l2ss-unb2 = tangostationcontrol.devices.unb2:main
     l2ss-xst = tangostationcontrol.devices.sdp.xst:main
+    l2ss-temperature-manager = tangostationcontrol.devices.temperature_manager:main
 
 # The following entry points should eventually be removed / replaced
     l2ss-cold-start = tangostationcontrol.toolkit.lts_cold_start:main
diff --git a/tangostationcontrol/tangostationcontrol/devices/boot.py b/tangostationcontrol/tangostationcontrol/devices/boot.py
index 450df1b6f..3760fc5cb 100644
--- a/tangostationcontrol/tangostationcontrol/devices/boot.py
+++ b/tangostationcontrol/tangostationcontrol/devices/boot.py
@@ -244,6 +244,7 @@ class Boot(lofar_device):
                        "STAT/Beamlet/1",
                        "STAT/TileBeam/1",   # Accesses RECV and Beamlet
                        "STAT/DigitalBeam/1",
+                       "STAT/Temperature_manager/1",
                       ],
     )
 
diff --git a/tangostationcontrol/tangostationcontrol/devices/temperature_manager.py b/tangostationcontrol/tangostationcontrol/devices/temperature_manager.py
index 40cfbe681..8ddbe11e3 100644
--- a/tangostationcontrol/tangostationcontrol/devices/temperature_manager.py
+++ b/tangostationcontrol/tangostationcontrol/devices/temperature_manager.py
@@ -16,11 +16,11 @@ import logging
 
 logger = logging.getLogger()
 
-__all__ = ["PDU", "main"]
+__all__ = ["temperature_manager", "main"]
 
 
 @device_logging_to_python()
-class PDU(lofar_device):
+class temperature_manager(lofar_device):
     # -----------------
     # Device Properties
     # -----------------
@@ -53,5 +53,5 @@ class PDU(lofar_device):
 # Run server
 # ----------
 def main(**kwargs):
-    """Main function of the PDU module."""
-    return entry(PDU, **kwargs)
+    """Main function of the temperature manager module."""
+    return entry(temperature_manager, **kwargs)
diff --git a/tangostationcontrol/tangostationcontrol/integration_test/default/devices/test_device_temperature_manager.py b/tangostationcontrol/tangostationcontrol/integration_test/default/devices/test_device_temperature_manager.py
new file mode 100644
index 000000000..29f13b39c
--- /dev/null
+++ b/tangostationcontrol/tangostationcontrol/integration_test/default/devices/test_device_temperature_manager.py
@@ -0,0 +1,16 @@
+
+# -*- coding: utf-8 -*-
+#
+# This file is part of the LOFAR 2.0 Station Software
+#
+#
+#
+# Distributed under the terms of the APACHE license.
+# See LICENSE.txt for more info.
+from .base import AbstractTestBases
+
+class TestDeviceDigitalBeam(AbstractTestBases.TestDeviceBase):
+
+    def setUp(self):
+        """Intentionally recreate the device object in each test"""
+        super().setUp("STAT/Temperature_manager/1")
-- 
GitLab