Hi everyone,
I wonder if there is any easy way to write value to attribute and push event with only one line of code.
I have a command which asks hardware for data. Then it returns data for few attributes, so my code looks like this:
from time import time()
from tango import AttrQuality
from tango.server import Device, attribute, command
class MyDev(Device):
def init_device(self):
self.attr1_val = 0
self.attr2_val = ""
self.attr3_val = 0
super().init_device()
@attribute
def Attr1(self):
return self.attr1_val
@attribute
def Attr2(self):
return self.attr2_val
@attribute
def Attr3(self):
return self.attr3_val
@command
def ReadData(self):
val1, val2, val3 = read_data_from_hw()
self.attr1_val = val1
self.attr2_val = val2
self.attr3_val = val3
if val1 is not None:
self.push_change_event("Attr1", self.attr1_val)
self.push_change_event("Attr2", self.attr2_val)
self.push_change_event("Attr3", self.attr3_val)
else:
self.push_change_event(
"Attr1", 0, time(), AttrQuality.ATTR_INVALID
)
self.push_change_event(
"Attr2", "", time(), AttrQuality.ATTR_INVALID
)
self.push_change_event(
"Attr3", 0, time(), AttrQuality.ATTR_INVALID
)
Is there simpler way to set attribute and push event at the same time?
I’m thinking for creating property and push event in setter, but maybe there is some mechanism in PyTango? E.g. Attr1.set_value(val1) which sets attribute value and pushes event at the same time.
Second think is pushing invalid values. If you return None from attribute, it gives you Invalid Quality, but if you want to push event you need to write some value (even that value is invalid), then time, then AttrQuality, instead of just None. Anyway to fix this?
This would help clean the code a lot, especially if I have dozens of attributes.
Edit: command is polled, so it periodically reads data and attributes are also polled for periodic events.