Commit 972b29b7 authored by Matias Guijarro's avatar Matias Guijarro
Browse files

Merge branch '2870-tango_attr_as_counter-fix-convert-func-raw-none' into 'master'

Resolve "[tango_attr_as_counter] Fix convert func raw none"

Closes #2870

See merge request !3885
parents 979f5be2 a1120c8d
Pipeline #51502 failed with stages
in 103 minutes and 34 seconds
......@@ -33,6 +33,8 @@ YAML_ configuration example:
"""
import weakref
import numpy
from bliss.common.counter import SamplingCounter, SamplingMode
from bliss.common import tango
from bliss import global_map
......@@ -124,12 +126,16 @@ class TangoCounterController(SamplingCounterController):
dev_attrs = self._proxy.read_attributes(attributes_to_read)
# Check error.
for attr in dev_attrs:
attr_values = []
for attr, cnt in zip(dev_attrs, counters):
error = attr.get_err_stack()
if error:
raise tango.DevFailed(*error)
attr_values = [dev_attr.value for dev_attr in dev_attrs]
if cnt.allow_failure:
raise tango.DevFailed(*error)
else:
attr_values.append(numpy.nan)
else:
attr_values.append(attr.value)
# Make a dict to ease counters affectation:
# keys->attributes, items->values
......@@ -231,6 +237,9 @@ class tango_attr_as_counter(SamplingCounter):
else:
self.format_string = self.tango_format
# ALLOW FAILURE
self.__allow_failure = config.get("allow_failure", True)
# INIT
SamplingCounter.__init__(
self,
......@@ -282,12 +291,30 @@ class tango_attr_as_counter(SamplingCounter):
* formatting
"""
log_debug(self, "raw_value=%s", raw_value)
attr_val = raw_value * self.conversion_factor
if raw_value is not None:
attr_val = raw_value * self.conversion_factor
else:
attr_val = numpy.nan
formated_value = float(
self.format_string % attr_val if self.format_string else attr_val
)
return formated_value
@property
def allow_failure(self):
"""
Allow failure during tango attribute read: True or False
- True: PyTango.DevFailed exception will be raised
- False: No exception raised and read will return numpy.nan
"""
return self.__allow_failure
@allow_failure.setter
def allow_failure(self, allow_failure):
self.__allow_failure = allow_failure
@property
def value(self):
"""
......
......@@ -2,7 +2,7 @@
from tango.server import run
from tango.server import Device
from tango.server import attribute, command
from tango import AttrWriteType
from tango import AttrWriteType, AttrQuality
from tango import DevState
"""
......@@ -24,6 +24,7 @@ class Dummy(Device):
powerdensity = attribute(format="%3.2f")
maxpowerdensity = attribute(format="%3.2f")
firstvelocity = attribute(format="%3.2f")
none_attr = attribute(format="%3.2f")
velocity = attribute(
fget="read_velocity", fset="write_velocity", access=AttrWriteType.READ_WRITE
......@@ -72,6 +73,9 @@ class Dummy(Device):
self.vel = 5
self.acc = 125
# set invalid quality to return numpy.nan
self.none_attr.set_quality(AttrQuality.ATTR_INVALID)
# shutter state
self.set_state(DevState.CLOSE)
......@@ -98,6 +102,9 @@ class Dummy(Device):
def read_maxpower(self):
return 0
def read_none_attr(self):
return None
def read_powerdensity(self):
return 0
......
......@@ -7,6 +7,7 @@
mode: MEAN
unit: km
format: "%5.3f"
allow_failure: False
- name: taac_undu_power_0
attr_name: powers
index: 0
......@@ -26,6 +27,8 @@
- name: taac_undu_wrong_attr_name
attr_name: cracoucas
# wrong attr_name.
- name: taac_none_attr
attr_name: none_attr
- class: tango_attr_as_counter
......
......@@ -467,6 +467,7 @@ def test_prepare_once_prepare_many(session):
def test_tango_attr_counter(beacon, dummy_tango_server, session):
_, device = dummy_tango_server
counter = beacon.get("taac_dummy_position")
# `taac_dummy_position` is a tango_attr_as_counter which refers to
......@@ -540,6 +541,22 @@ def test_tango_attr_counter(beacon, dummy_tango_server, session):
with pytest.raises(KeyError):
_ = beacon.get("no_uri_counter")
device.setDisabled(True)
sc = ct(0.01, taac_vel, taac_pos, taac_acc)
pos = sc.get_data()["taac_undu_position"][0]
assert numpy.isnan(pos)
assert taac_pos.allow_failure is False
taac_pos.allow_failure = True
with pytest.raises(tango.DevFailed):
sc = ct(0.01, taac_vel, taac_pos, taac_acc)
taac_nan = beacon.get("taac_none_attr")
sc = ct(0.01, taac_nan)
counter_value = sc.get_data()["taac_none_attr"][0]
assert numpy.isnan(counter_value)
def test_info_counters(beacon, dummy_tango_server):
"""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment