diff --git a/src/dynamic-srv.py b/src/dynamic-srv.py index 7536a1677cc97d9b669bba646d290fef98950693..ab95fd3c8679280e105d311b589a91a5db97e58d 100755 --- a/src/dynamic-srv.py +++ b/src/dynamic-srv.py @@ -221,35 +221,39 @@ class Dynamic(PyTango.Device_4Impl): #link to the Tango DB self.db = PyTango.Database() - aux = self.FiltersScript.split('/') - path = '/'.join(aux[:-1]) - if path: - if path not in sys.path: - sys.path.insert(0, path) - module_name = aux[-1].replace('.py','') - try: - self.module = __import__(module_name) - except ImportError as ex: - sys.stderr.write(str(ex)+"\n") + if self.FiltersScript: + aux = self.FiltersScript.split('/') + path = '/'.join(aux[:-1]) + if path: + if path not in sys.path: + sys.path.insert(0, path) + module_name = aux[-1].replace('.py','') + try: + self.module = __import__(module_name) + except ImportError as ex: + sys.stderr.write(str(ex)+"\n") + self.module = None + self.set_state(PyTango.DevState.FAULT) + self.set_status("Invalid Filters Script " + self.FiltersScript ) + return + else: self.module = None - self.set_state(PyTango.DevState.FAULT) - self.set_status("Invalid Filters Script " + self.FiltersScript ) - return # import the configuration script as a module - try: - self.cfg_module_name = os.path.splitext(os.path.basename(self.ConfiguratorScript))[0] - self.cfg_module_path = os.path.dirname(self.ConfiguratorScript) - print(self.cfg_module_name,self.cfg_module_path) - sys.path.append(self.cfg_module_path) - self.cfg_module = importlib.import_module(self.cfg_module_name) - print("imported cfg") - except Exception as ex: - self.cfg_module = None - #self.warn_stream("Invalid cfg script file\n") - print("Invalid cfg script file\n",ex) - self.set_state(PyTango.DevState.OFF) - return + if self.ConfiguratorScript: + try: + self.cfg_module_name = os.path.splitext(os.path.basename(self.ConfiguratorScript))[0] + self.cfg_module_path = os.path.dirname(self.ConfiguratorScript) + #print(self.cfg_module_name,self.cfg_module_path) + sys.path.append(self.cfg_module_path) + self.cfg_module = importlib.import_module(self.cfg_module_name) + #print("imported cfg") + except Exception as ex: + self.cfg_module = None + #self.warn_stream("Invalid cfg script file\n") + print("Invalid cfg script file\n",ex) + self.set_state(PyTango.DevState.OFF) + return #creation of dynamic attributes from cfg script dynattrs = self.cfg_module.createDynAttrsDict() @@ -261,20 +265,13 @@ class Dynamic(PyTango.Device_4Impl): try: attr_proxy = PyTango.AttributeProxy(str(attr_obj.tango_name)) attr_config = attr_proxy.get_config() - - if str(attr_config.data_format) == "SCALAR": - attr = PyTango.Attr(str(attr_obj.name), attr_config.data_type, attr_config.writable) - elif str(attr_config.data_format) == "SPECTRUM": - attr = PyTango.SpectrumAttr(str(attr_obj.name), attr_config.data_type, attr_config.writable,attr_config.max_dim_x) - elif str(attr_config.data_format) == "IMAGE": - attr = PyTango.ImageAttr(str(attr_obj.name), attr_config.data_type, attr_config.writable,attr_config.max_dim_x,attr_config.max_dim_y) + attr = PyTango.Attr(str(attr_obj.name), attr_config.data_type, attr_config.writable) self.add_attribute(attr, self.read_proxy, self.write_proxy) self.myattrs[attr_obj.name] = attr_proxy - - except PyTango.DevFailed as ex: + except PyTango.DevFailed as ex: self.warn_stream("%s" % str(ex)) continue - if self.myattrs.has_key(attr_obj.name): + if attr_obj.name in self.myattrs: PyTango.Except.throw_exception("Exist", "Attribute "+attr_obj.name+ " already defined", "NewAttribute") @@ -292,7 +289,7 @@ class Dynamic(PyTango.Device_4Impl): else: self.myattrs[attr_obj.name] = 0 if attr_obj.default: - self.myattrs[attr_obj.name] = eval(attr_obj.default) + self.myattrs[attr_obj.name] = attr_obj.default if attr_obj.memorized == 1: dev_attr_props = self.db.get_device_attribute_property(self.get_name(),[attr_obj.name]) attr_prop = dev_attr_props[attr_obj.name] @@ -517,7 +514,7 @@ class Dynamic(PyTango.Device_4Impl): :rtype: PyTango.DevVoid """ # self.debug_stream("In LoadWorkingFile()") - if len(self.attr_WorkingFile_read) == 0: + if len(self.ConfiguratorScript) == 0: self.error_stream('WorkingFile not defined, cannot procede.') self.set_status('WorkingFile not defined, cannot procede.') return @@ -531,6 +528,7 @@ class Dynamic(PyTango.Device_4Impl): conf_file.write(argin) self.debug_stream('Wrote file ' + file_name) except Exception as ex: + print(ex) self.error_stream('Unable to write file ' + file_name + ': ' + str(ex)) self.set_status('Unable to write file ' + self.ConfiguratorScript) finally: @@ -545,7 +543,7 @@ class Dynamic(PyTango.Device_4Impl): # #------------------------------------------------------------------ def read_dynamic_attribute(self, attr_name): - if self.read_functions[attr_name] != '': + if self.read_functions[attr_name]: read_method = getattr(self.module,self.read_functions[attr_name]) num_of_args = read_method.func_code.co_argcount if num_of_args == 1: @@ -563,7 +561,7 @@ class Dynamic(PyTango.Device_4Impl): # #------------------------------------------------------------------ def write_dynamic_attribute(self, attr_name, value_in): - if self.write_functions[attr_name] != '': + if self.write_functions[attr_name]: write_method = getattr(self.module,self.write_functions[attr_name]) num_of_args = write_method.func_code.co_argcount if num_of_args == 2: @@ -599,11 +597,11 @@ class DynamicClass(PyTango.DeviceClass): 'ConfiguratorScript': [PyTango.DevString, "Path to the config file", - [ "./config.py"] ], + [] ], 'FiltersScript': [PyTango.DevString, "Path to the python script that will be executed.", - [ "./test/script_test.py"] ], + [] ], 'PythonExecutable': [PyTango.DevString, "Path to the python executable.", @@ -625,6 +623,9 @@ class DynamicClass(PyTango.DeviceClass): 'ClearAll': [[PyTango.DevVoid,""], [PyTango.DevVoid,""]], + 'LoadCfgFile': + [[PyTango.DevString,""], + [PyTango.DevVoid,""]], } diff --git a/src/dynattr_conf.py b/src/dynattr_conf.py new file mode 100644 index 0000000000000000000000000000000000000000..da7f16532f7c43bfc790726657a0c632552d66e6 --- /dev/null +++ b/src/dynattr_conf.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +### Common section +import PyTango +from colibry.ds.executer import DynAttribute + +# Instantiate dynamic attributes dictionary +def createDynAttrsDict(): + dyn_attrs = {} + attr_list = [("IntShortAttr",PyTango.ArgType.DevShort), + ("StringAttr",PyTango.ArgType.DevString), + ("BoolAttr",PyTango.ArgType.DevBoolean), + ("FloatAttr",PyTango.ArgType.DevFloat), + ("DoubleAttr",PyTango.ArgType.DevDouble), + ] + + # initialize dictionary with base information + for attr in attr_list: + dyn_attrs[attr[0]] = DynAttribute(attr[0],attr[1]) + + # add further info specifically + dyn_attrs["IntShortAttr"].default = 1 # default value + dyn_attrs["IntShortAttr"].memorized = True # memorized in DB + + dyn_attrs["FloatAttr"].default = 3.66 # default value + dyn_attrs["FloatAttr"].permission = PyTango.AttrWriteType.READ + + return dyn_attrs diff --git a/src/loadcfg.py b/src/loadcfg.py new file mode 100644 index 0000000000000000000000000000000000000000..d4885d3e10df03e8cb348b291361f026c0ca7023 --- /dev/null +++ b/src/loadcfg.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Wed Jan 12 15:19:19 2022 + +@author: martin +""" + +import PyTango + +file_name = "./dynattr_conf.py" +try: + loaded_file = open(file_name, 'r') + file_content = loaded_file.read() +except Exception as ex: + raise Exception('Unable to open file ' + str(file_name) + str(ex)) +finally: + loaded_file.close() + +try: + dev_proxy = PyTango.DeviceProxy("diproi/daq/dynattr") + dev_proxy.LoadCfgFile(file_content) + print('Configiuration file loaded.') +except PyTango.DevFailed as ex: + print('Could not get the requested file. ' + str(ex)) +finally: + del dev_proxy