Skip to content
Snippets Groups Projects
statistics_device.py 2.47 KiB
Newer Older
Taya Snijder's avatar
Taya Snijder committed
# This file is part of the Statistics project
#
#
#
# Distributed under the terms of the APACHE license.
# See LICENSE.txt for more info.

Taya Snijder's avatar
Taya Snijder committed
""" Statistics Device Server for LOFAR2.0

"""

# PyTango imports
from tango.server import run
from tango.server import device_property
from tango import AttrWriteType
# Additional import

from clients.sst_client import sst_client

from util.attribute_wrapper import attribute_wrapper
from util.hardware_device import hardware_device

from util.lofar_logging import device_logging_to_python, log_exceptions

import numpy

__all__ = ["SST", "main"]

@device_logging_to_python({"device": "SST"})
class SST(hardware_device):

    # -----------------
    # Device Properties
    # -----------------

    SST_Port = device_property(
        dtype='DevULong',
        mandatory=True
    )

    # ----------
    # Attributes
    # ----------
Taya Snijder's avatar
Taya Snijder committed
    # --------

    packet_count_R = attribute_wrapper(comms_annotation={"parameter": "packet_count"}, datatype=numpy.int64)
    timestamp_R = attribute_wrapper(comms_annotation={"parameter": "timestamp"}, datatype=numpy.int64)

    # --------
    # overloaded functions
    def configure_for_off(self):
        """ user code here. is called when the state is set to OFF """

        # Stop keep-alive
        try:
            self.set_comm_client.stop()
        except Exception as e:
Taya Snijder's avatar
Taya Snijder committed
            self.warn_stream("Exception while stopping sst_client in configure_for_off function: {}. Exception ignored".format(e))

    @log_exceptions()
    def configure_for_initialise(self):
        """ user code here. is called when the sate is set to INIT """
Taya Snijder's avatar
Taya Snijder committed
        """Initialises the attributes and properties of the statistics device."""
        self.sst_client = sst_client("0.0.0.0", self.SST_Port, self.Fault, self)

        # map an access helper class
        for i in self.attr_list():
            try:
                i.set_comm_client(self.sst_client)
            except Exception as e:
                # use the pass function instead of setting read/write fails
                i.set_pass_func()
Taya Snijder's avatar
Taya Snijder committed
                self.warn_stream("error while setting the sst attribute {} read/write function. {}".format(i, e))
                pass

        self.sst_client.start()

    # --------
    # Commands
    # --------

# ----------
# Run server
# ----------
def main(args=None, **kwargs):
Taya Snijder's avatar
Taya Snijder committed
    """Main function of the Statistics Device module."""
    return run((SST,), args=args, **kwargs)


if __name__ == '__main__':
    main()