-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSimulatorDS.py
executable file
·315 lines (279 loc) · 11 KB
/
SimulatorDS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python
# "$Name: $";
# "$Header: /cvsroot/tango-ds/Simulators/SimulatorDS/SimulatorDS.py,v 1.4
# 2008/11/21 11:51:44 sergi_rubio Exp $";
#=============================================================================
#
# file : SimulatorDS.py
#
# description : Python source for the SimulatorDS and its commands.
# The class is derived from Device. It represents the
# CORBA servant object which will be accessed from the
# network. All commands which can be executed on the
# SimulatorDS are implemented in this file.
#
# project : TANGO Device Server
#
# $Author: srubio@cells.es
#
# $Revision: 1.4 $
#
# $Log: SimulatorDS.py,v $
# Revision 1.4 2008/11/21 11:51:44 sergi_rubio
# Adapted_to_fandango.dynamic.DynamicDS_template
#
# Revision 1.3 2008/11/21 11:46:30 sergi_rubio
# *** empty log message ***
#
# Revision 1.2 2008/01/21 14:46:30 sergi_rubio
# Solved default properties initialization
#
# Revision 1.1.1.1 2007/10/17 16:44:12 sergi_rubio
# A Simulator for attributes and states, using dynamic attributes
#
# $Log: $
#
# copyleft : Cells / Alba Synchrotron
# Bellaterra
# Spain
#
#############################################################################
##
## This file is part of Tango-ds.
##
## This is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This software is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
###########################################################################
import sys,traceback,math,random,time
from re import match,search,findall
try: import numpy
except:
print('numpy not available')
numpy = None
try:
import scipy #Too Heavy, if you need them better use PyAttributeProcessor
from scipy.interpolate import interp1d as interpolate
except:
print('Scipy not available' )
numpy = scipy = interpolate = None
import PyTango,fandango
from fandango.dynamic import DynamicDS,DynamicDSClass,DynamicAttribute
from fandango.interface import FullTangoInheritance
from fandango.threads import wait
try: import PyTangoArchiving
except: PyTangoArchiving = None
import Signals
def get_module_dict(module,ks=None):
return dict((k,v) for k,v in module.__dict__.items() if (not ks or k in ks) and not k.startswith('__'))
__doc__ = """
SimulatorDS Class Description: https://github.com/tango-controls/SimulatorDS
<p>This device requires <a href="https://github.com/tango-controls/fandango">
Fandango module<a> to be available in the PYTHONPATH.</p>
<p>
This Python Device Server will allow to declare dynamic attributes which
values will depend on a given time-dependent formula:
</p>
<h5 id="Example:">Example:</h5>
<pre class="wiki"> Square=0.5+square(t) #(t = seconds since the device started)
NoisySinus=2+1.5*sin(3*t)-0.5*random()
SomeNumbers=DevVarLongArray([1000.*i for i in range(1,10)])
</pre><p>
Attributes are DevDouble by default, but any Tango type or python expression
can be used for declaration. <br>
Format is specified at <a class="ext-link"
href="http://www.tango-controls.org/Members/srubio/dynamicattributes">
<span class="icon">tango-controls.org</span></a>
</p>
<p>
Signals that can be easily generated with amplitude between 0.0 and 1.0 are:
</p>
<blockquote>
<p>
rampt(t), sin(t), cos(t), exp(t), triangle(t), square(t,duty), random()
</p>
</blockquote>
<p>
The MaxValue/MinValue property for each Attribute will determine the State
of the Device only if the property DynamicStates is not defined.
</p>
<p>
If defined, <strong>DynamicStates</strong> will use this format:
</p>
<pre class="wiki"> FAULT=2*square(0.9,60)
ALARM=NoisySinus
ON=1
</pre><p>
This device inherits from <strong>fandango.dynamic.DynamicDS</strong> Class
</p>
"""
class SimulatorDS(DynamicDS):
#--------- Add you global variables here --------------------------
LIBS = [math,random,Signals]
NAMES = [math,random,time,PyTango,PyTangoArchiving,
DynamicAttribute,match,search,findall,wait,numpy,scipy,]
OTHERS = dict((k,v) for k,v in
[('fandango',fandango.functional),
('np',numpy),
#('PyTango',PyTango),
#('callbacks',fandango.callbacks),
('interpolate',interpolate)]+
[(f,getattr(fandango,f)) for f in dir(fandango.functional)
if '2' in f or f.startswith('to')]
)
#------------------------------------------------------------------
# Device constructor
#------------------------------------------------------------------
def __init__(self,cl, name):
#PyTango.Device_4Impl.__init__(self,cl,name)
print 'IN SimulatorDS.__INIT__'
_locals = {}
[_locals.update(get_module_dict(m)) for m in self.LIBS]
_locals.update((k.__name__,k) for k in self.NAMES
if hasattr(k,'__name__'))
_locals.update(self.OTHERS)
#_locals.update(locals())
#_locals.update(globals())
DynamicDS.__init__(self,cl,name,_locals=_locals,useDynStates=True)
SimulatorDS.init_device(self)
#------------------------------------------------------------------
# Device destructor
#------------------------------------------------------------------
def delete_device(self):
print "[Device delete_device method] for device",self.get_name()
#------------------------------------------------------------------
# Device initialization
#------------------------------------------------------------------
def init_device(self):
print "In ", self.get_name(), "::init_device()"
try:
DynamicDS.init_device(self) #New in Fandango 11.1
except:
self.get_DynDS_properties() #LogLevel is already set here
if PyTangoArchiving and 'archiving' in str(self.DynamicAttributes):
#+str(self.DynamicCommands):
print 'Adding PyTangoArchiving support ...'
self._locals['archiving'] = PyTangoArchiving.Reader()
self.set_state(PyTango.DevState.ON)
print "Out of ", self.get_name(), "::init_device()"
#------------------------------------------------------------------
# Always excuted hook method
#------------------------------------------------------------------
def always_executed_hook(self):
#print "In ", self.get_name(), "::always_excuted_hook()"
DynamicDS.always_executed_hook(self)
#==================================================================
#
# SimulatorDS read/write attribute methods
#
#==================================================================
#------------------------------------------------------------------
# Read Attribute Hardware
#------------------------------------------------------------------
def read_attr_hardware(self,data):
#print "In ", self.get_name(), "::read_attr_hardware()"
if self.SimulationDelay > 0:
self.info('Delaying read_attribute by %f seconds'
% self.SimulationDelay)
wait(self.SimulationDelay)
#==================================================================
#
# SimulatorDS command methods
#
#==================================================================
#==================================================================
#
# SimulatorDSClass class definition
#
#==================================================================
class SimulatorDSClass(DynamicDSClass):
# Class Properties
class_property_list = {
}
# Device Properties
device_property_list = {
'DynamicAttributes':
[PyTango.DevVarStringArray,
"Attributes and formulas to create for this device.\n<br/>\n"
"This Tango Attributes will be generated dynamically using this "
"syntax:\n<br/>\nT3=int(SomeCommand(7007)/10.)\n\n<br/>\n"
"See the class description to know how to make any method "
"available in attributes declaration.",
[ ] ],
'DynamicStates':
[PyTango.DevVarStringArray,
"This property will allow to declare new States dinamically"
" based on\n<br/>\ndynamic attributes changes. "
"The function Attr will allow to use the\n<br/>\n"
"value of attributes in formulas.<br/>\n\n\n<br/>\n"
"ALARM=Attr(T1)>70<br/>\nOK=1",
[ ] ],
'UseScipy':
[PyTango.DevBoolean,
"Disable numpy or scipy, NOT IMPLEMENTED YET",
[ True ] ],
'SimulationDelay':
[PyTango.DevDouble,
"Delay, in seconds, to be applied to each read_attribute call",
[ 0.0 ] ],
'PushEvents':
[PyTango.DevDouble,
"Set condition for pushing events, N or N>t=periodic; "
"N>diff/rel for change event",
[ 0.0 ] ],
}
# Command definitions
cmd_list = {
}
# Attribute definitions
attr_list = {
}
#------------------------------------------------------------------
# SimulatorDSClass Constructor
#------------------------------------------------------------------
def __init__(self, name):
PyTango.DeviceClass.__init__(self, name)
self.set_type(name);
print "In SimulatorDSClass constructor"
#==================================================================
#
# SimulatorDS class main method
#
#==================================================================
#class PySignalSimulator(SimulatorDS): pass
#class PySignalSimulatorClass(SimulatorDSClass): pass
#SimulatorDS,SimulatorDSClass = FullTangoInheritance('SimulatorDS',
# SimulatorDS,SimulatorDSClass,DynamicDS,DynamicDSClass,ForceDevImpl=True)
def main(args=None):
try:
py = PyTango.Util(args or sys.argv)
db = py.get_database()
ds = py.get_ds_name()
klasses = db.get_server_class_list(py.get_ds_name())
for k in klasses:
d = locals()[k] = type(k,(SimulatorDS,),{})
dc = locals()[k+'Class'] = type(k+'Class',(SimulatorDSClass,),{})
py.add_TgClass(dc,d,k)
if 'SimulatorDS' not in klasses:
py.add_TgClass(SimulatorDSClass,SimulatorDS,'SimulatorDS')
U = PyTango.Util.instance()
fandango.dynamic.CreateDynamicCommands(SimulatorDS,SimulatorDSClass)
U.server_init()
U.server_run()
except PyTango.DevFailed,e:
print('-------> Received a DevFailed exception:')
traceback.print_exc()
except Exception,e:
print('-------> An unforeseen exception occured....')
traceback.print_exc()
if __name__ == '__main__':
main()