From d18e368ab975cd6476876362d5a132da6c19e195 Mon Sep 17 00:00:00 2001 From: Alexander Zaft Date: Thu, 23 Mar 2023 07:09:15 +0100 Subject: [PATCH] make entangle mapping a dict Change-Id: I38d863a907469674001f0721140f88c17b53635b Reviewed-on: https://forge.frm2.tum.de/review/c/secop/frappy/+/30911 Tested-by: Jenkins Automated Tests Reviewed-by: Enrico Faulhaber Reviewed-by: Alexander Zaft --- cfg/ccr_cfg.py | 28 ++++++++++++++-------------- cfg/test_cfg.py | 7 +++++++ frappy_demo/test.py | 12 ++++++++++-- frappy_mlz/entangle.py | 24 ++++++------------------ 4 files changed, 37 insertions(+), 34 deletions(-) diff --git a/cfg/ccr_cfg.py b/cfg/ccr_cfg.py index e23f1f8..42bc6cc 100644 --- a/cfg/ccr_cfg.py +++ b/cfg/ccr_cfg.py @@ -13,14 +13,14 @@ Mod('automatik', '\n' 'selects between off, regulate on p1 or regulate on p2 sensor', tangodevice = 'tango://localhost:10000/box/plc/_automatik', - mapping="{'Off':0,'p1':1,'p2':2}", + mapping={'Off':0,'p1':1,'p2':2}, ) Mod('compressor', 'frappy_mlz.entangle.NamedDigitalOutput', 'control the compressor (on/off)', tangodevice = 'tango://localhost:10000/box/plc/_cooler_onoff', - mapping="{'Off':0,'On':1}", + mapping={'Off':0,'On':1}, ) Mod('gas', @@ -31,7 +31,7 @@ Mod('gas', 'note: activation de-activates the vacuum inlet\n' 'note: if the pressure regulation is active, it enslave this device', tangodevice = 'tango://localhost:10000/box/plc/_gas_onoff', - mapping="{'Off':0,'On':1}", + mapping={'Off':0,'On':1}, ) Mod('vacuum', @@ -41,7 +41,7 @@ Mod('vacuum', 'note: activation de-activates the gas inlet\n' 'note: if the pressure regulation is active, it enslave this device', tangodevice = 'tango://localhost:10000/box/plc/_vacuum_onoff', - mapping="{'Off':0,'On':1}", + mapping={'Off':0,'On':1}, ) Mod('p1', @@ -63,14 +63,14 @@ Mod('curve_p2', 'calibration curve for pressure sensor 2', tangodevice = 'tango://localhost:10000/box/plc/_curve', value = 0, - mapping = "{'0-10V':0, '0-1000mbar':1, '1-9V to 0-1 mbar':2, \ - 'DI200':3, 'DI2000':4, 'TTR100':7, 'PTR90':8, \ - 'PTR225/237':9, 'ITR90':10, 'ITR100-D':11, \ - 'ITR100-2':12, 'ITR100-3':13, 'ITR100-4':14, \ - 'ITR100-5':15, 'ITR100-6':16, 'ITR100-7':17, \ - 'ITR100-8':18, 'ITR100-9':19, 'ITR100-A':20, \ - 'CMR361':21, 'CMR362':22, 'CMR363':23, \ - 'CMR364':24, 'CMR365':25}", + mapping = {'0-10V':0, '0-1000mbar':1, '1-9V to 0-1 mbar':2, + 'DI200':3, 'DI2000':4, 'TTR100':7, 'PTR90':8, + 'PTR225/237':9, 'ITR90':10, 'ITR100-D':11, + 'ITR100-2':12, 'ITR100-3':13, 'ITR100-4':14, + 'ITR100-5':15, 'ITR100-6':16, 'ITR100-7':17, + 'ITR100-8':18, 'ITR100-9':19, 'ITR100-A':20, + 'CMR361':21, 'CMR362':22, 'CMR363':23, + 'CMR364':24, 'CMR365':25}, ) Mod('T_tube_regulation', @@ -137,12 +137,12 @@ Mod('T_tube_regulation_heaterrange', 'frappy_mlz.entangle.NamedDigitalOutput', 'heaterrange for tube regulation', tangodevice = 'tango://localhost:10000/box/tube/range1', - mapping="{'Off':0,'Low':1,'Medium':2, 'High':3}", + mapping={'Off':0,'Low':1,'Medium':2, 'High':3}, ) Mod('T_stick_regulation_heaterrange', 'frappy_mlz.entangle.NamedDigitalOutput', 'heaterrange for stick regulation', tangodevice = 'tango://localhost:10000/box/stick/range2', - mapping="{'Off':0,'Low':1,'Medium':2, 'High':3}", + mapping={'Off':0,'Low':1,'Medium':2, 'High':3}, ) diff --git a/cfg/test_cfg.py b/cfg/test_cfg.py index 203e28f..ecc249b 100644 --- a/cfg/test_cfg.py +++ b/cfg/test_cfg.py @@ -44,3 +44,10 @@ Mod('Lower', 'frappy_demo.test.Lower', 'something else', ) + +Mod('Decision', + 'frappy_demo.test.Mapped', + 'Random value from configured property choices. Config accepts anything ' \ + 'that can be converted to a list', + choices = ['Yes', 'Maybe', 'No'], +) diff --git a/frappy_demo/test.py b/frappy_demo/test.py index c454f28..9482c9c 100644 --- a/frappy_demo/test.py +++ b/frappy_demo/test.py @@ -23,8 +23,9 @@ import random -from frappy.datatypes import FloatRange, StringType -from frappy.modules import Communicator, Drivable, Parameter, Readable +from frappy.datatypes import FloatRange, StringType, ValueType +from frappy.modules import Communicator, Drivable, Parameter, Property, \ + Readable from frappy.params import Command @@ -93,3 +94,10 @@ class Lower(Communicator): def communicate(self, command): """lowercase a string""" return str(command).lower() + +class Mapped(Readable): + value = Parameter(datatype=StringType()) + choices = Property('List of choices', + datatype=ValueType(list)) + def read_value(self): + return self.choices[random.randrange(len(self.choices))] diff --git a/frappy_mlz/entangle.py b/frappy_mlz/entangle.py index 732945d..99dac66 100644 --- a/frappy_mlz/entangle.py +++ b/frappy_mlz/entangle.py @@ -37,12 +37,12 @@ from time import sleep, time as currenttime import PyTango from frappy.datatypes import ArrayOf, EnumType, FloatRange, IntRange, \ - LimitsType, StringType, TupleOf + LimitsType, StringType, TupleOf, ValueType from frappy.errors import CommunicationFailedError, ConfigError, \ HardwareError, ProgrammingError from frappy.lib import lazy_property from frappy.modules import Command, Drivable, Module, Parameter, Readable, \ - StatusType, Writable + StatusType, Writable, Property ##### @@ -812,19 +812,13 @@ class NamedDigitalInput(DigitalInput): """ # parameters - mapping = Parameter('A dictionary mapping state names to integers', - datatype=StringType(), export=False) # XXX:!!! + mapping = Property('A dictionary mapping state names to integers', + datatype=ValueType(dict)) def initModule(self): super().initModule() try: mapping = self.mapping - if isinstance(mapping, str): - # pylint: disable=eval-used - mapping = eval(self.mapping.replace('\n', ' ')) - if isinstance(mapping, str): - # pylint: disable=eval-used - mapping = eval(mapping) self.accessibles['value'].setProperty('datatype', EnumType('value', **mapping)) except Exception as e: raise ValueError(f'Illegal Value for mapping: {self.mapping!r}') from e @@ -889,19 +883,13 @@ class NamedDigitalOutput(DigitalOutput): """ # parameters - mapping = Parameter('A dictionary mapping state names to integers', - datatype=StringType(), export=False) + mapping = Property('A dictionary mapping state names to integers', + datatype=ValueType(dict)) def initModule(self): super().initModule() try: mapping = self.mapping - if isinstance(mapping, str): - # pylint: disable=eval-used - mapping = eval(self.mapping.replace('\n', ' ')) - if isinstance(mapping, str): - # pylint: disable=eval-used - mapping = eval(mapping) self.accessibles['value'].setProperty('datatype', EnumType('value', **mapping)) self.accessibles['target'].setProperty('datatype', EnumType('target', **mapping)) except Exception as e: