Skip to content
Snippets Groups Projects
Commit b9e49e63 authored by Jan David Mol's avatar Jan David Mol
Browse files

Merge branch '2021-04-16-Branched_from_master-INI_file_device' into 'master'

Resolve #2021 "04 16 branched from master ini file device"

See merge request !24
parents bfdbf07a 4afe2759
No related branches found
No related tags found
1 merge request!24Resolve #2021 "04 16 branched from master ini file device"
{
"servers": {
"PCC": {
"1": {
"PCC": {
"LTS/PCC/1": {
"properties": {
"OPC_Server_Name": [
"host.docker.internal"
]
}
}
}
}
},
"SDP": {
"1": {
"SDP": {
"LTS/SDP/1": {
"properties": {
"OPC_Server_Name": [
"host.docker.internal"
]
}
}
}
}
},
"example_device": {
"1": {
"example_device": {
"LTS/example_device/1": {
"attribute_properties": {
"Ant_mask_RW": {
"archive_period": [
"600000"
]
}
},
"properties": {
"OPC_Server_Name": [
"host.docker.internal"
],
"OPC_Server_Port": [
"4842"
],
"OPC_Time_Out": [
"5.0"
]
}
}
}
}
},
"ini_device": {
"1": {
"ini_device": {
"LTS/ini_device/1": {
"attribute_properties": {
"Ant_mask_RW": {
"archive_period": [
"600000"
]
}
},
"properties": {
"OPC_Server_Name": [
"host.docker.internal"
],
"OPC_Server_Port": [
"4842"
],
"OPC_Time_Out": [
"5.0"
]
}
}
}
}
},
"SNMP": {
"1": {
"SNMP": {
"LTS/SNMP/1": {
"attribute_properties": {
"Ant_mask_RW": {
"archive_period": [
"600000"
]
}
},
"properties": {
"SNMP_community": [
"public"
],
"SNMP_host": [
"192.168.178.17"
],
"SNMP_timeout": [
"5.0"
]
}
}
}
}
}
}
}
# -*- coding: utf-8 -*-
#
# This file is part of the SDP project
#
#
#
# Distributed under the terms of the APACHE license.
# See LICENSE.txt for more info.
""" SDP Device Server for LOFAR2.0
"""
# PyTango imports
from tango.server import run
from tango.server import device_property
from tango import AttrWriteType
#attribute extention and hardware device imports
from src.attribute_wrapper import attribute_wrapper
from src.hardware_device import hardware_device
import numpy
# Additional import
from clients.opcua_connection import OPCUAConnection
__all__ = ["APSCTL", "main"]
class APSCTL(hardware_device):
"""
**Properties:**
- Device Property
OPC_Server_Name
- Type:'DevString'
OPC_Server_Port
- Type:'DevULong'
OPC_Time_Out
- Type:'DevDouble'
"""
# -----------------
# Device Properties
# -----------------
OPC_Server_Name = device_property(
dtype='DevString',
mandatory=True
)
OPC_Server_Port = device_property(
dtype='DevULong',
mandatory=True
)
OPC_Time_Out = device_property(
dtype='DevDouble',
mandatory=True
)
# ----------
# Attributes
# ----------
N_unb = 2
N_fpga = 4
N_ddr = 2
N_qsfp = 6
# Central CP per Uniboard
UNB2_Power_ON_OFF_RW = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_Power_ON_OFF_RW"], datatype=numpy.bool_, dims=(N_unb,), access=AttrWriteType.READ_WRITE)
UNB2_Front_Panel_LED_RW = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_Front_Panel_LED_RW"], datatype=numpy.uint8, dims=(N_unb,), access=AttrWriteType.READ_WRITE)
UNB2_Mask_RW = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_Mask_RW"], datatype=numpy.bool_, dims=(N_unb,), access=AttrWriteType.READ_WRITE)
# Central MP per Uniboard
UNB2_I2C_bus_OK_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_I2C_bus_OK_R"], datatype=numpy.bool_, dims=(N_unb,))
UNB2_Front_Panel_LED_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_Front_Panel_LED_R"], datatype=numpy.uint8, dims=(N_unb,))
UNB2_EEPROM_Serial_Number_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_EEPROM_Serial_Number_R"], datatype=numpy.str, dims=(N_unb,))
UNB2_EEPROM_Unique_ID_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_EEPROM_Unique_ID_R"], datatype=numpy.uint32, dims=(N_unb,))
UNB2_DC_DC_48V_12V_VIN_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_DC_DC_48V_12V_VIN_R"], datatype=numpy.double, dims=(N_unb,))
UNB2_DC_DC_48V_12V_VOUT_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_DC_DC_48V_12V_VOUT_R"], datatype=numpy.double, dims=(N_unb,))
UNB2_DC_DC_48V_12V_IOUT_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_DC_DC_48V_12V_IOUT_R"], datatype=numpy.double, dims=(N_unb,))
UNB2_DC_DC_48V_12V_TEMP_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_DC_DC_48V_12V_TEMP_R"], datatype=numpy.double, dims=(N_unb,))
UNB2_POL_QSFP_N01_VOUT_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_POL_QSFP_N01_VOUT_R"], datatype=numpy.double, dims=(N_unb,))
UNB2_POL_QSFP_N01_IOUT_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_POL_QSFP_N01_IOUT_R"], datatype=numpy.double, dims=(N_unb,))
UNB2_POL_QSFP_N01_TEMP_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_POL_QSFP_N01_TEMP_R"], datatype=numpy.double, dims=(N_unb,))
UNB2_POL_QSFP_N23_VOUT_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_POL_QSFP_N23_VOUT_R"], datatype=numpy.double, dims=(N_unb,))
UNB2_POL_QSFP_N23_IOUT_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_POL_QSFP_N23_IOUT_R"], datatype=numpy.double, dims=(N_unb,))
UNB2_POL_QSFP_N23_TEMP_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_POL_QSFP_N23_TEMP_R"], datatype=numpy.double, dims=(N_unb,))
UNB2_POL_SWITCH_1V2_VOUT_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_POL_SWITCH_1V2_VOUT_R"], datatype=numpy.double, dims=(N_unb,))
UNB2_POL_SWITCH_1V2_IOUT_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_POL_SWITCH_1V2_IOUT_R"], datatype=numpy.double, dims=(N_unb,))
UNB2_POL_SWITCH_1V2_TEMP_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_POL_SWITCH_1V2_TEMP_R"], datatype=numpy.double, dims=(N_unb,))
UNB2_POL_SWITCH_PHY_VOUT_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_POL_SWITCH_PHY_VOUT_R"], datatype=numpy.double, dims=(N_unb,))
UNB2_POL_SWITCH_PHY_IOUT_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_POL_SWITCH_PHY_IOUT_R"], datatype=numpy.double, dims=(N_unb,))
UNB2_POL_SWITCH_PHY_TEMP_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_POL_SWITCH_PHY_TEMP_R"], datatype=numpy.double, dims=(N_unb,))
UNB2_POL_CLOCK_VOUT_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_POL_CLOCK_VOUT_R"], datatype=numpy.double, dims=(N_unb,))
UNB2_POL_CLOCK_IOUT_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_POL_CLOCK_IOUT_R"], datatype=numpy.double, dims=(N_unb,))
UNB2_POL_CLOCK_TEMP_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_POL_CLOCK_TEMP_R"], datatype=numpy.double, dims=(N_unb,))
# monitor points per FPGA
UNB2_FPGA_DDR4_SLOT_TEMP_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_DDR4_SLOT_TEMP_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_DDR4_SLOT_PART_NUMBER_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_DDR4_SLOT_PART_NUMBER_R"], datatype=numpy.str, dims=(N_unb * N_qsfp,N_fpga))
UNB2_FPGA_QSFP_CAGE_0_TEMP_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_QSFP_CAGE_0_TEMP_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_QSFP_CAGE_1_TEMP_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_QSFP_CAGE_1_TEMP_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_QSFP_CAGE_2_TEMP_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_QSFP_CAGE_2_TEMP_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_QSFP_CAGE_3_TEMP_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_QSFP_CAGE_3_TEMP_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_QSFP_CAGE_4_TEMP_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_QSFP_CAGE_4_TEMP_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_QSFP_CAGE_5_TEMP_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_QSFP_CAGE_5_TEMP_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_QSFP_CAGE_0_LOS_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_QSFP_CAGE_0_LOS_R"], datatype=numpy.uint8, dims=(N_unb,N_fpga))
UNB2_FPGA_QSFP_CAGE_1_LOS_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_QSFP_CAGE_1_LOS_R"], datatype=numpy.uint8, dims=(N_unb,N_fpga))
UNB2_FPGA_QSFP_CAGE_2_LOS_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_QSFP_CAGE_2_LOS_R"], datatype=numpy.uint8, dims=(N_unb,N_fpga))
UNB2_FPGA_QSFP_CAGE_3_LOS_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_QSFP_CAGE_3_LOS_R"], datatype=numpy.uint8, dims=(N_unb,N_fpga))
UNB2_FPGA_QSFP_CAGE_4_LOS_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_QSFP_CAGE_4_LOS_R"], datatype=numpy.uint8, dims=(N_unb,N_fpga))
UNB2_FPGA_QSFP_CAGE_5_LOS_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_QSFP_CAGE_5_LOS_R"], datatype=numpy.uint8, dims=(N_unb,N_fpga))
UNB2_FPGA_POL_CORE_VOUT_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_POL_CORE_VOUT_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_POL_CORE_IOUT_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_POL_CORE_IOUT_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_POL_CORE_TEMP_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_POL_CORE_TEMP_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_POL_ERAM_VOUT_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_POL_ERAM_VOUT_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_POL_ERAM_IOUT_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_POL_ERAM_IOUT_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_POL_ERAM_TEMP_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_POL_ERAM_TEMP_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_POL_RXGXB_VOUT_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_POL_RXGXB_VOUT_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_POL_RXGXB_IOUT_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_POL_RXGXB_IOUT_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_POL_RXGXB_TEMP_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_POL_RXGXB_TEMP_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_POL_TXGXB_VOUT_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_POL_TXGXB_VOUT_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_POL_TXGXB_IOUT_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_POL_TXGXB_IOUT_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_POL_TXGXB_TEMP_R = attribute_wrapper(comms_annotation=["2:PCC", "2:UNB2_FPGA_POL_TXGXB_TEMP_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_POL_HGXB_VOUT_R = attribute_wrapper(comms_annotation=["2:UNB2_FPGA_POL_HGXB_VOUT_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_POL_HGXB_IOUT_R = attribute_wrapper(comms_annotation=["2:UNB2_FPGA_POL_HGXB_IOUT_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_POL_HGXB_TEMP_R = attribute_wrapper(comms_annotation=["2:UNB2_FPGA_POL_HGXB_TEMP_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_POL_PGM_VOUT_R = attribute_wrapper(comms_annotation=["2:UNB2_FPGA_POL_PGM_VOUT_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_POL_PGM_IOUT_R = attribute_wrapper(comms_annotation=["2:UNB2_FPGA_POL_PGM_IOUT_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
UNB2_FPGA_POL_PGM_TEMP_R = attribute_wrapper(comms_annotation=["2:UNB2_FPGA_POL_PGM_TEMP_R"], datatype=numpy.double, dims=(N_unb,N_fpga))
def delete_device(self):
"""Hook to delete resources allocated in init_device.
This method allows for any memory or other resources allocated in the
init_device method to be released. This method is called by the device
destructor and by the device Init command (a Tango built-in).
"""
self.debug_stream("Shutting down...")
self.Off()
self.debug_stream("Shut down. Good bye.")
# --------
# overloaded functions
# --------
def off(self):
""" user code here. is called when the state is set to OFF """
# Stop keep-alive
self.opcua_connection.stop()
def initialise(self):
""" user code here. is called when the sate is set to INIT """
"""Initialises the attributes and properties of the PCC."""
# set up the OPC ua client
self.OPCua_client = OPCUAConnection("opc.tcp://{}:{}/".format(self.OPC_Server_Name, self.OPC_Server_Port), "http://lofar.eu", self.OPC_Time_Out, self.Fault, self)
# map an access helper class
for i in self.attr_list():
try:
i.set_comm_client(self.OPCua_client)
except:
self.debug_stream("error in getting APSCTL attribute: {} from client".format(i))
self.OPCua_client.start()
# --------
# Commands
# --------
# ----------
# Run server
# ----------
def main(args=None, **kwargs):
"""Main function of the SDP module."""
return run((APSCTL,), args=args, **kwargs)
if __name__ == '__main__':
main()
...@@ -117,13 +117,13 @@ class PCC(hardware_device): ...@@ -117,13 +117,13 @@ class PCC(hardware_device):
# overloaded functions # overloaded functions
# -------- # --------
@log_exceptions() @log_exceptions()
def off(self): def configure_for_off(self):
""" user code here. is called when the state is set to OFF """ """ user code here. is called when the state is set to OFF """
# Stop keep-alive # Stop keep-alive
self.OPCua_client.stop() self.OPCua_client.stop()
@log_exceptions() @log_exceptions()
def initialise(self): def configure_for_initialise(self):
""" user code here. is called when the state is set to INIT """ """ user code here. is called when the state is set to INIT """
# Init the dict that contains function to OPC-UA function mappings. # Init the dict that contains function to OPC-UA function mappings.
......
...@@ -116,14 +116,14 @@ class SDP(hardware_device): ...@@ -116,14 +116,14 @@ class SDP(hardware_device):
# overloaded functions # overloaded functions
# -------- # --------
@log_exceptions() @log_exceptions()
def off(self): def configure_for_off(self):
""" user code here. is called when the state is set to OFF """ """ user code here. is called when the state is set to OFF """
# Stop keep-alive # Stop keep-alive
self.opcua_connection.stop() self.opcua_connection.stop()
@log_exceptions() @log_exceptions()
def initialise(self): def configure_for_initialise(self):
""" user code here. is called when the sate is set to INIT """ """ user code here. is called when the sate is set to INIT """
"""Initialises the attributes and properties of the SDP.""" """Initialises the attributes and properties of the SDP."""
......
from util.comms_client import CommClient
import configparser
import numpy
__all__ = ["ini_client"]
numpy_to_ini_dict = {
numpy.int64: int,
numpy.double: float,
numpy.float64: float,
numpy.bool_: bool,
str: str
}
numpy_to_ini_get_dict = {
numpy.int64: configparser.ConfigParser.getint,
numpy.double: configparser.ConfigParser.getfloat,
numpy.float64: configparser.ConfigParser.getfloat,
numpy.bool_: configparser.ConfigParser.getboolean,
str: str
}
ini_to_numpy_dict = {
int: numpy.int64,
float: numpy.float64,
bool: numpy.bool_,
str: numpy.str_
}
import os
class ini_client(CommClient):
"""
this class provides an example implementation of a comms_client.
Durirng initialisation it creates a correctly shaped zero filled value. on read that value is returned and on write its modified.
"""
def start(self):
super().start()
def __init__(self, filename, fault_func, streams, try_interval=2):
"""
initialises the class and tries to connect to the client.
"""
self.config = configparser.ConfigParser()
self.filename = filename
super().__init__(fault_func, streams, try_interval)
# Explicitly connect
if not self.connect():
# hardware or infra is down -- needs fixing first
fault_func()
return
def connect(self):
self.config_file = open(self.filename, "r")
self.connected = True # set connected to true
return True # if successful, return true. otherwise return false
def disconnect(self):
self.connected = False # always force a reconnect, regardless of a successful disconnect
self.streams.debug_stream("disconnected from the 'client' ")
def _setup_annotation(self, annotation):
"""
this function gives the client access to the comm client annotation data given to the attribute wrapper.
The annotation data can be used to provide whatever extra data is necessary in order to find/access the monitor/control point.
the annotation can be in whatever format may be required. it is up to the user to handle its content
example annotation may include:
- a file path and file line/location
- COM object path
Annotations:
name: Required, the name of the ini variable
section: Required, the section of the ini variable
"""
# as this is an example, just print the annotation
self.streams.debug_stream("annotation: {}".format(annotation))
name = annotation.get('name')
if name is None:
ValueError("ini client requires a variable `name` in the annotation to set/get")
section = annotation.get('section')
if section is None:
ValueError("requires a `section` specified in the annotation to open")
return section, name
def _setup_value_conversion(self, attribute):
"""
gives the client access to the attribute_wrapper object in order to access all
necessary data such as dimensionality and data type
"""
dim_y = attribute.dim_y
dim_x = attribute.dim_x
dtype = attribute.numpy_type
return dim_y, dim_x, dtype
def _setup_mapping(self, name, section, dtype, dim_y, dim_x):
"""
takes all gathered data to configure and return the correct read and write functions
"""
def read_function():
self.config.read_file(self.config_file)
value = self.config.get(section, name)
value = data_handler(value, dtype)
if dim_y > 1:
# if data is an image, slice it according to the y dimensions
value = numpy.array(numpy.split(value, indices_or_sections=dim_y))
return value
def write_function(value):
if type(value) is list:
write_value = ", ".join([str(v) for v in value])
else:
write_value = str(value)
self.config.read_file(self.config_file)
self.config.set(section, name, write_value)
fp = open(self.filename, 'w')
self.config.write(fp)
return read_function, write_function
def setup_attribute(self, annotation=None, attribute=None):
"""
MANDATORY function: is used by the attribute wrapper to get read/write functions.
must return the read and write functions
"""
# process the comms_annotation
section, name = self._setup_annotation(annotation)
# get all the necessary data to set up the read/write functions from the attribute_wrapper
dim_y, dim_x, dtype = self._setup_value_conversion(attribute)
# configure and return the read/write functions
read_function, write_function = self._setup_mapping(name, section, dtype, dim_y, dim_x)
# return the read/write functions
return read_function, write_function
def data_handler(string, dtype):
value = []
if dtype is numpy.bool_:
# Handle special case for Bools
for i in string.split(","):
i = i.strip(" ")
if "True" == i:
value.append(True)
elif "False" == i:
value.append(False)
else:
raise ValueError("String to bool failed. String is not True/False, but is: '{}'".format(i))
value = dtype(value)
elif dtype is numpy.str_:
for i in string.split(","):
val = numpy.str_(i)
value.append(val)
value = numpy.array(value)
else:
# regular case, go through the separator
for i in string.split(","):
i = i.replace(" ", "")
val = dtype(i)
value.append(val)
# convert values from buildin type to numpy type
value = dtype(value)
return value
# -*- coding: utf-8 -*-
#
# This file wraps around a tango device class and provides a number of abstractions useful for hardware devices. It works together
#
# Distributed under the terms of the APACHE license.
# See LICENSE.txt for more info.
"""
"""
# PyTango imports
from tango.server import run
from tango.server import device_property
from tango import AttrWriteType
from tango import DevState
# Additional import
from util.attribute_wrapper import attribute_wrapper
from util.hardware_device import hardware_device
import configparser
import numpy
from clients.ini_client import *
__all__ = ["ini_device"]
def write_ini_file(filename):
with open(filename, 'w') as configfile:
config = configparser.ConfigParser()
config['scalar'] = {}
config['scalar']['double_scalar_R'] = '1.2'
config['scalar']['bool_scalar_R'] = 'True'
config['scalar']['int_scalar_R'] = '5'
config['scalar']['str_scalar_R'] = 'this is a test'
config['spectrum'] = {}
config['spectrum']['double_spectrum_R'] = '1.2, 2.3, 3.4, 4.5'
config['spectrum']['bool_spectrum_R'] = 'True, True, False, False'
config['spectrum']['int_spectrum_R'] = '1, 2, 3, 4'
config['spectrum']['str_spectrum_R'] = '"a", "b", "c", "d"'
config['image'] = {}
config['image']['double_image_R'] = '1.2, 2.3, 3.4, 4.5, 5.6, 6.7'
config['image']['bool_image_R'] = 'True, True, False, False, True, False'
config['image']['int_image_R'] = '1, 2, 3, 4, 5, 6'
config['image']['str_image_R'] = '"a", "b", "c", "d", "e", "f"'
config.write(configfile)
class ini_device(hardware_device):
"""
This class is the minimal (read empty) implementation of a class using 'hardware_device'
"""
# ----------
# Attributes
# ----------
"""
attribute wrapper objects can be declared here. All attribute wrapper objects will get automatically put in a list (attr_list) for easy access
example = attribute_wrapper(comms_annotation="this is an example", datatype=numpy.double, dims=(8, 2), access=AttrWriteType.READ_WRITE)
...
"""
double_scalar_RW = attribute_wrapper(comms_annotation={"section": "scalar", "name": "double_scalar_RW"}, datatype=numpy.double, access=AttrWriteType.READ_WRITE)
double_scalar_R = attribute_wrapper(comms_annotation={"section": "scalar", "name": "double_scalar_R"}, datatype=numpy.double)
bool_scalar_RW = attribute_wrapper(comms_annotation={"section": "scalar", "name": "bool_scalar_RW"}, datatype=numpy.bool_, access=AttrWriteType.READ_WRITE)
bool_scalar_R = attribute_wrapper(comms_annotation={"section": "scalar", "name": "bool_scalar_R"}, datatype=numpy.bool_)
int_scalar_RW = attribute_wrapper(comms_annotation={"section": "scalar", "name": "int_scalar_RW"}, datatype=numpy.int64, access=AttrWriteType.READ_WRITE)
int_scalar_R = attribute_wrapper(comms_annotation={"section": "scalar", "name": "int_scalar_R"}, datatype=numpy.int64)
str_scalar_RW = attribute_wrapper(comms_annotation={"section": "scalar", "name": "str_scalar_RW"}, datatype=numpy.str_, access=AttrWriteType.READ_WRITE)
str_scalar_R = attribute_wrapper(comms_annotation={"section": "scalar", "name": "str_scalar_R"}, datatype=numpy.str_)
double_spectrum_RW = attribute_wrapper(comms_annotation={"section": "spectrum", "name": "double_spectrum_RW"}, datatype=numpy.double, dims=(4,), access=AttrWriteType.READ_WRITE)
double_spectrum_R = attribute_wrapper(comms_annotation={"section": "spectrum", "name": "double_spectrum_R"}, datatype=numpy.double, dims=(4,))
bool_spectrum_RW = attribute_wrapper(comms_annotation={"section": "spectrum", "name": "bool_spectrum_RW"}, datatype=numpy.bool_, dims=(4,), access=AttrWriteType.READ_WRITE)
bool_spectrum_R = attribute_wrapper(comms_annotation={"section": "spectrum", "name": "bool_spectrum_R"}, datatype=numpy.bool_, dims=(4,))
int_spectrum_RW = attribute_wrapper(comms_annotation={"section": "spectrum", "name": "int_spectrum_RW"}, datatype=numpy.int64, dims=(4,), access=AttrWriteType.READ_WRITE)
int_spectrum_R = attribute_wrapper(comms_annotation={"section": "spectrum", "name": "int_spectrum_R"}, datatype=numpy.int64, dims=(4,))
str_spectrum_RW = attribute_wrapper(comms_annotation={"section": "spectrum", "name": "str_spectrum_RW"}, datatype=numpy.str_, dims=(4,), access=AttrWriteType.READ_WRITE)
str_spectrum_R = attribute_wrapper(comms_annotation={"section": "spectrum", "name": "str_spectrum_R"}, datatype=numpy.str_, dims=(4,))
double_image_RW = attribute_wrapper(comms_annotation={"section": "image", "name": "double_image_RW"}, datatype=numpy.double, dims=(3, 2), access=AttrWriteType.READ_WRITE)
double_image_R = attribute_wrapper(comms_annotation={"section": "image", "name": "double_image_R"}, datatype=numpy.double, dims=(3, 2))
bool_image_RW = attribute_wrapper(comms_annotation={"section": "image", "name": "bool_image_RW"}, datatype=numpy.bool_, dims=(3, 2), access=AttrWriteType.READ_WRITE)
bool_image_R = attribute_wrapper(comms_annotation={"section": "image", "name": "bool_image_R"}, datatype=numpy.bool_, dims=(3, 2))
int_image_RW = attribute_wrapper(comms_annotation={"section": "image", "name": "int_image_RW"}, datatype=numpy.int64, dims=(3, 2), access=AttrWriteType.READ_WRITE)
int_image_R = attribute_wrapper(comms_annotation={"section": "image", "name": "int_image_R"}, datatype=numpy.int64, dims=(3, 2))
str_image_RW = attribute_wrapper(comms_annotation={"section": "image", "name": "str_image_RW"}, datatype=numpy.str_, dims=(3, 2), access=AttrWriteType.READ_WRITE)
str_image_R = attribute_wrapper(comms_annotation={"section": "image", "name": "str_image_R"}, datatype=numpy.str_, dims=(3, 2))
# --------
# overloaded functions
# --------
def configure_for_initialise(self):
""" user code here. is called when the sate is set to INIT """
"""Initialises the attributes and properties of the PCC."""
# set up the OPC ua client
self.ini_client = ini_client("example.ini", self.Fault, self)
# map an access helper class
for i in self.attr_list():
i.set_comm_client(self.ini_client)
self.ini_client.start()
# ----------
# Run server
# ----------
def main(args=None, **kwargs):
write_ini_file("example.ini")
"""Main function of the hardware device module."""
return run((ini_device,), args=args, **kwargs)
if __name__ == '__main__':
main()
...@@ -64,7 +64,7 @@ class test_device(hardware_device): ...@@ -64,7 +64,7 @@ class test_device(hardware_device):
# -------- # --------
# overloaded functions # overloaded functions
# -------- # --------
def initialise(self): def configure_for_initialise(self):
""" user code here. is called when the sate is set to INIT """ """ user code here. is called when the sate is set to INIT """
"""Initialises the attributes and properties of the PCC.""" """Initialises the attributes and properties of the PCC."""
......
File mode changed from 100755 to 100644
File mode changed from 100755 to 100644
...@@ -85,7 +85,7 @@ class hardware_device(Device): ...@@ -85,7 +85,7 @@ class hardware_device(Device):
self.set_state(DevState.INIT) self.set_state(DevState.INIT)
self.setup_value_dict() self.setup_value_dict()
self.initialise() self.configure_for_initialise()
self.set_state(DevState.STANDBY) self.set_state(DevState.STANDBY)
...@@ -100,7 +100,7 @@ class hardware_device(Device): ...@@ -100,7 +100,7 @@ class hardware_device(Device):
:return:None :return:None
""" """
self.on() self.configure_for_on()
self.set_state(DevState.ON) self.set_state(DevState.ON)
@command() @command()
...@@ -119,7 +119,7 @@ class hardware_device(Device): ...@@ -119,7 +119,7 @@ class hardware_device(Device):
# Turn off # Turn off
self.set_state(DevState.OFF) self.set_state(DevState.OFF)
self.off() self.configure_for_off()
# Turn off again, in case of race conditions through reconnecting # Turn off again, in case of race conditions through reconnecting
self.set_state(DevState.OFF) self.set_state(DevState.OFF)
...@@ -138,18 +138,18 @@ class hardware_device(Device): ...@@ -138,18 +138,18 @@ class hardware_device(Device):
:return:None :return:None
""" """
self.fault() self.configure_for_fault()
self.set_state(DevState.FAULT) self.set_state(DevState.FAULT)
# functions that can be overloaded # functions that can be overloaded
def fault(self): def configure_for_fault(self):
pass pass
def off(self): def configure_for_off(self):
pass pass
def on(self): def configure_for_on(self):
pass pass
def initialise(self): def configure_for_initialise(self):
pass pass
def always_executed_hook(self): def always_executed_hook(self):
......
File mode changed from 100755 to 100644
import logging import logging
from functools import wraps from functools import wraps
import sys
# Always also log the hostname because it makes the origin of the log clear. # Always also log the hostname because it makes the origin of the log clear.
import socket import socket
...@@ -14,7 +15,7 @@ def configure_logger(logger: logging.Logger, log_extra=None): ...@@ -14,7 +15,7 @@ def configure_logger(logger: logging.Logger, log_extra=None):
# log to the tcp_input of logstash in our ELK stack # log to the tcp_input of logstash in our ELK stack
handler = AsynchronousLogstashHandler("elk", 5959, database_path='pending_log_messages.db') handler = AsynchronousLogstashHandler("elk", 5959, database_path='pending_log_messages.db')
# configure log messages # configure log messages
formatter = LogstashFormatter(extra=log_extra, tags=["python", "lofar"]) formatter = LogstashFormatter(extra=log_extra, tags=["python", "lofar"])
handler.setFormatter(formatter) handler.setFormatter(formatter)
......
...@@ -34,4 +34,3 @@ def startup(device: str, force_restart: bool): ...@@ -34,4 +34,3 @@ def startup(device: str, force_restart: bool):
else: else:
print("Device {} has successfully reached ON state.".format(device)) print("Device {} has successfully reached ON state.".format(device))
return proxy return proxy
%% Cell type:code id:waiting-chance tags:
``` python
import time
import numpy
```
%% Cell type:code id:moving-alexandria tags:
``` python
d=DeviceProxy("LTS/ini_device/1")
```
%% Cell type:code id:ranking-aluminum tags:
``` python
state = str(d.state())
if state == "OFF":
d.initialise()
time.sleep(1)
state = str(d.state())
if state == "STANDBY":
d.on()
state = str(d.state())
if state == "ON":
print("Device is now in on state")
```
%% Output
Device is now in on state
%% Cell type:code id:beneficial-evidence tags:
``` python
attr_names = d.get_attribute_list()
for i in attr_names:
try:
exec("print(i, d.{})".format(i))
except:
pass
```
%% Output
double_scalar_RW [0.]
double_scalar_R [1.2]
bool_scalar_RW [False]
bool_scalar_R [ True]
int_scalar_RW [0]
int_scalar_R [5]
str_scalar_RW ('',)
str_scalar_R ('this is',)
double_spectrum_RW [0. 0. 0. 0.]
double_spectrum_R [1.2 2.3 3.4 4.5]
bool_spectrum_RW [False False False False]
bool_spectrum_R [ True True False False]
int_spectrum_RW [0 0 0 0]
int_spectrum_R [1 2 3 4]
str_spectrum_RW ('', '', '', '')
str_spectrum_R ('"a"', ' "b"', ' "c"', ' "d"')
double_image_RW [[0. 0. 0.]
[0. 0. 0.]]
double_image_R [[1.2 2.3 3.4]
[4.5 5.6 6.7]]
bool_image_RW [[False False False]
[False False False]]
bool_image_R [[ True True False]
[False True False]]
int_image_RW [[0 0 0]
[0 0 0]]
int_image_R [[1 2 3]
[4 5 6]]
str_image_RW (('', '', ''), ('', '', ''))
str_image_R (('"a"', ' "b"', ' "c"'), (' "d"', ' "e"', ' "f"'))
State <function __get_command_func.<locals>.f at 0x7f3efee95c80>
Status <function __get_command_func.<locals>.f at 0x7f3efee95c80>
%% Cell type:code id:sharing-mechanics tags:
``` python
d.int_scalar_RW
```
%% Output
array([0])
%% Cell type:code id:2f03759a tags:
``` python
d.str_image_RW = [["1", "2", "3"],["4", "5", "6"]]
```
%% Cell type:code id:3187f3bb tags:
``` python
d.str_image_RW
```
%% Output
(('1', '2', '3'), ('4', '5', '6'))
%% Cell type:code id:eb406dce tags:
``` python
numpy.str_(["a", "b", "c", "d", "e", "f"])
```
%% Output
"['a', 'b', 'c', 'd', 'e', 'f']"
%% Cell type:code id:7b270085 tags:
``` python
array = []
string = '"a", "b", "c", "d", "e", "f"'
for i in string.split(","):
value = numpy.str_(i)
array.append(value)
len(array)
```
%% Output
6
%% Cell type:code id:69ecc437 tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment