Skip to content
Snippets Groups Projects

Resolve L2SS-449 "Add deviceproxy to devices"

Closed Jan David Mol requested to merge L2SS-449-add-deviceproxy-to-devices into master
2 files
+ 20
19
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -15,7 +15,7 @@ from abc import abstractmethod
@@ -15,7 +15,7 @@ from abc import abstractmethod
# PyTango imports
# PyTango imports
from tango.server import Device, command, DeviceMeta, attribute
from tango.server import Device, command, DeviceMeta, attribute
from tango import AttrWriteType, DevState, DebugIt, Attribute, DeviceProxy
from tango import AttrWriteType, DevState, DebugIt, Attribute, DeviceProxy, DevFailed
import time
import time
import math
import math
@@ -86,6 +86,15 @@ class lofar_device(Device, metaclass=AbstractDeviceMetas):
@@ -86,6 +86,15 @@ class lofar_device(Device, metaclass=AbstractDeviceMetas):
self.set_state(DevState.OFF)
self.set_state(DevState.OFF)
 
# register a proxy to ourselves, to interact with
 
# our attributes and commands as a client would.
 
#
 
# this is required to get/set attributes.
 
#
 
# we cannot write directly to our attribute, as that would not
 
# trigger a write_{name} call. See https://www.tango-controls.org/community/forum/c/development/c/accessing-own-deviceproxy-class/?page=1#post-2021
 
self.proxy = DeviceProxy(self.get_name())
 
@log_exceptions()
@log_exceptions()
def delete_device(self):
def delete_device(self):
"""Hook to delete resources allocated in init_device.
"""Hook to delete resources allocated in init_device.
@@ -221,12 +230,6 @@ class lofar_device(Device, metaclass=AbstractDeviceMetas):
@@ -221,12 +230,6 @@ class lofar_device(Device, metaclass=AbstractDeviceMetas):
2) Any remaining default properties are set.
2) Any remaining default properties are set.
"""
"""
# we cannot write directly to our attribute, as that would not
# trigger a write_{name} call. See https://www.tango-controls.org/community/forum/c/development/c/accessing-own-deviceproxy-class/?page=1#post-2021
# obtain a proxy to myself, to write values
proxy = DeviceProxy(self.get_name())
# collect all attributes for which defaults are provided
# collect all attributes for which defaults are provided
attributes_with_defaults = [name for name in dir(self)
attributes_with_defaults = [name for name in dir(self)
# collect all attribute members
# collect all attribute members
@@ -244,7 +247,7 @@ class lofar_device(Device, metaclass=AbstractDeviceMetas):
@@ -244,7 +247,7 @@ class lofar_device(Device, metaclass=AbstractDeviceMetas):
# set the attribute to the configured default
# set the attribute to the configured default
self.debug_stream(f"Setting attribute {name} to {default_value}")
self.debug_stream(f"Setting attribute {name} to {default_value}")
proxy.write_attribute(name, default_value)
self.proxy.write_attribute(name, default_value)
except Exception as e:
except Exception as e:
# log which attribute we're addressing
# log which attribute we're addressing
raise Exception(f"Cannot assign default to attribute {name}") from e
raise Exception(f"Cannot assign default to attribute {name}") from e
@@ -271,13 +274,11 @@ class lofar_device(Device, metaclass=AbstractDeviceMetas):
@@ -271,13 +274,11 @@ class lofar_device(Device, metaclass=AbstractDeviceMetas):
pollperiod: how often to check the attribute, in seconds.
pollperiod: how often to check the attribute, in seconds.
"""
"""
attr = getattr(self, attr_name)
# Poll every half a second
# Poll every half a second
for _ in range(math.ceil(timeout/pollperiod)):
for _ in range(math.ceil(timeout/pollperiod)):
if attr != value:
if getattr(self.proxy, attr_name) != value:
return
return
time.sleep(pollperiod)
time.sleep(pollperiod)
raise Exception(f"{attr} != {value} after f{timeout} seconds still.")
raise Exception(f"{attr_name} != {value} after {timeout} seconds still.")
Loading