-
Notifications
You must be signed in to change notification settings - Fork 9
/
DuoStateBrick.py
605 lines (532 loc) · 22 KB
/
DuoStateBrick.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
from BlissFramework import BaseComponents
from BlissFramework import Icons
from BlissFramework.Utils import widget_colors
from qt import *
import new
import re
import logging
###
### Brick to control a hardware object with two states
###
class DuoStateBrick(BaseComponents.BlissWidget):
STATES = {
'unknown': (None, True, True, False, False),
'disabled': (widget_colors.LIGHT_RED, False, False, False, False),
'error': (widget_colors.LIGHT_RED, True, True, False, False),
'out': (widget_colors.DARK_GRAY, True, True, False, True),
'moving': (widget_colors.LIGHT_YELLOW, False, False, None, None),
'in': (widget_colors.LIGHT_GREEN, True, True, True, False),
'automatic': (widget_colors.WHITE, True, True, False, False)
}
LABEL_CLASS=QLabel
def __init__(self, *args):
BaseComponents.BlissWidget.__init__(self,*args)
self.wrapperHO=None
self.__expertMode = False
self.addProperty('mnemonic','string','')
self.addProperty('forceNoControl','boolean',False)
self.addProperty('expertModeControlOnly', 'boolean', False)
self.addProperty('icons','string','')
self.addProperty('in','string','in')
self.addProperty('out','string','out')
self.addProperty('setin','string','Set in')
self.addProperty('setout','string','Set out')
self.addProperty('username','string','')
self.defineSlot('allowControl',())
#self.defineSlot('stateChanged',())
self.defineSignal('duoStateBrickIn',())
self.defineSignal('duoStateBrickOut',())
self.defineSignal('duoStateBrickMoving',())
self.containerBox=QVGroupBox("none",self)
self.containerBox.setInsideMargin(4)
self.containerBox.setInsideSpacing(0)
#self.stateLabel = myLabel('<b> </b>', self.containerBox)
self.stateLabel = self.LABEL_CLASS('<b> </b>', self.containerBox)
self.buttonsBox=QHBox(self.containerBox)
self.setInButton=QPushButton("Set in",self.buttonsBox)
self.setInButton.setToggleButton(True)
self.connect(self.setInButton,SIGNAL('toggled(bool)'),self.setIn)
self.setOutButton=QPushButton("Set out",self.buttonsBox)
self.setOutButton.setToggleButton(True)
self.connect(self.setOutButton,SIGNAL('toggled(bool)'),self.setOut)
#self.stateLabel.setHintWidget(self.buttonsBox)
QVBoxLayout(self)
self.layout().addWidget(self.containerBox)
self.stateLabel.setAlignment(QLabel.AlignCenter)
QToolTip.add(self.stateLabel,"Shows the current control state")
QToolTip.add(self.setInButton,"Changes the control state")
QToolTip.add(self.setOutButton,"Changes the control state")
def setExpertMode(self, expert):
self.__expertMode = expert
self.buttonsBox.show()
if not expert and self["expertModeControlOnly"]:
self.buttonsBox.hide()
def setIn(self,state):
#print "DuoStateBrick.setIn",state
if state:
self.wrapperHO.setIn()
else:
self.setInButton.blockSignals(True)
self.setInButton.setState(QPushButton.On)
self.setInButton.blockSignals(False)
def setOut(self,state):
#print "DuoStateBrick.setOut",state
if state:
self.wrapperHO.setOut()
else:
self.setOutButton.blockSignals(True)
self.setOutButton.setState(QPushButton.On)
self.setOutButton.blockSignals(False)
def updateLabel(self,label):
self.containerBox.setTitle(label)
def stateChanged(self,state,state_label=None):
#logging.info("DuoStateBrick.stateChanged(%s)" % state)
try:
color=self.STATES[state][0]
except KeyError:
state='unknown'
color=self.STATES[state][0]
if color is None:
color=QWidget.paletteBackgroundColor(self)
self.stateLabel.setPaletteBackgroundColor(QColor(color))
try:
state_label=self[state]
except KeyError:
state_label=None
if state_label is not None:
self.stateLabel.setText('<b>%s</b>' % state_label)
else:
self.stateLabel.setText('<b>%s</b>' % state)
try:
in_enable=self.STATES[state][1]
out_enable=self.STATES[state][2]
except KeyError:
in_enable=False
out_enable=False
self.setInButton.setEnabled(in_enable)
self.setOutButton.setEnabled(out_enable)
try:
in_state=self.STATES[state][3]
out_state=self.STATES[state][4]
except KeyError:
in_state=QPushButton.Off
out_state=QPushButton.Off
if in_state is not None:
if in_state:
self.setInButton.blockSignals(True)
self.setInButton.setState(QPushButton.On)
self.setInButton.blockSignals(False)
else:
self.setInButton.blockSignals(True)
self.setInButton.setState(QPushButton.Off)
self.setInButton.blockSignals(False)
if out_state is not None:
if out_state:
self.setOutButton.blockSignals(True)
self.setOutButton.setState(QPushButton.On)
self.setOutButton.blockSignals(False)
else:
self.setOutButton.blockSignals(True)
self.setOutButton.setState(QPushButton.Off)
self.setOutButton.blockSignals(False)
if state=='in':
self.emit(PYSIGNAL("duoStateBrickMoving"), (False, ))
self.emit(PYSIGNAL("duoStateBrickIn"), (True, ))
elif state=='out':
self.emit(PYSIGNAL("duoStateBrickMoving"), (False, ))
self.emit(PYSIGNAL("duoStateBrickOut"), (True, ))
elif state=='moving':
self.emit(PYSIGNAL("duoStateBrickMoving"), (True, ))
elif state=='error' or state=='unknown' or state=='disabled':
self.emit(PYSIGNAL("duoStateBrickMoving"), (False, ))
self.emit(PYSIGNAL("duoStateBrickIn"), (False, ))
self.emit(PYSIGNAL("duoStateBrickOut"), (False, ))
def allowControl(self,enable):
if self['forceNoControl']:
return
if enable:
self.buttonsBox.show()
else:
self.buttonsBox.hide()
def run(self):
if self.wrapperHO is None:
self.containerBox.hide()
def stop(self):
self.containerBox.show()
def propertyChanged(self,propertyName,oldValue,newValue):
#print "DuoStateBrick.propertyChanged",propertyName,newValue
if propertyName=='mnemonic':
if self.wrapperHO is not None:
self.disconnect(self.wrapperHO,PYSIGNAL('duoStateChanged'), self.stateChanged)
h_obj=self.getHardwareObject(newValue)
if h_obj is not None:
self.wrapperHO=WrapperHO(h_obj)
self.containerBox.show()
if self['username']=='':
self['username']=self.wrapperHO.userName()
help=self['setin']+" the "+self['username'].lower()
QToolTip.add(self.setInButton,help)
help=self['setout']+" the "+self['username'].lower()
QToolTip.add(self.setOutButton,help)
self.containerBox.setTitle(self['username'])
self.connect(self.wrapperHO,PYSIGNAL('duoStateChanged'), self.stateChanged)
self.stateChanged(self.wrapperHO.getState())
else:
self.wrapperHO=None
#self.containerBox.hide()
elif propertyName=='expertModeControlOnly':
if newValue:
if self.__expertMode:
self.buttonsBox.show()
else:
self.buttonsBox.hide()
else:
self.buttonsBox.show()
elif propertyName=='forceNoControl':
if newValue:
self.buttonsBox.hide()
else:
self.buttonsBox.show()
elif propertyName=='icons':
w=self.fontMetrics().width("Set out")
icons_list=newValue.split()
try:
self.setInButton.setPixmap(Icons.load(icons_list[0]))
except IndexError:
self.setInButton.setText(self['setin'])
self.setInButton.setMinimumWidth(w)
try:
self.setOutButton.setPixmap(Icons.load(icons_list[1]))
except IndexError:
self.setOutButton.setText(self['setout'])
self.setOutButton.setMinimumWidth(w)
elif propertyName=='in':
if self.wrapperHO is not None:
self.stateChanged(self.wrapperHO.getState())
elif propertyName=='out':
if self.wrapperHO is not None:
self.stateChanged(self.wrapperHO.getState())
elif propertyName=='setin':
icons=self['icons']
w=self.fontMetrics().width("Set out")
icons_list=icons.split()
try:
i=icons_list[0]
except IndexError:
self.setInButton.setText(newValue)
self.setInButton.setMinimumWidth(w)
help=newValue+" the "+self['username'].lower()
QToolTip.add(self.setInButton,help)
elif propertyName=='setout':
icons=self['icons']
w=self.fontMetrics().width("Set out")
icons_list=icons.split()
try:
i=icons_list[1]
except IndexError:
self.setOutButton.setText(newValue)
self.setOutButton.setMinimumWidth(w)
help=newValue+" the "+self['username'].lower()
QToolTip.add(self.setOutButton,help)
elif propertyName=='username':
if newValue=='':
if self.wrapperHO is not None:
name=self.wrapperHO.userName()
if name!='':
self['username']=name
return
help=self['setin']+" the "+newValue.lower()
QToolTip.add(self.setInButton,help)
help=self['setout']+" the "+newValue.lower()
QToolTip.add(self.setOutButton,help)
self.containerBox.setTitle(self['username'])
else:
BaseComponents.BlissWidget.propertyChanged(self,propertyName,oldValue,newValue)
###
### Wrapper around different hardware objects, to make them have the
### same behavior to the brick
###
class WrapperHO(QObject):
wagoStateDict={'in':'in', 'out':'out', 'unknown':'unknown'}
actuatorStateDict={'in':'in', 'out':'out', 'unknown':'unknown'}
shutterStateDict={'fault':'error', 'opened':'in', 'closed':'out',\
'unknown':'unknown', 'moving':'moving', 'automatic':'automatic',\
'disabled':'disabled', 'error':'error'}
motorWPosDict=('out', 'in')
motorWStateDict=('disabled', 'error', None, 'moving',\
'moving', 'moving')
STATES = ('unknown','disabled','error','out','moving','in','automatic')
def __init__(self,hardware_obj):
QObject.__init__(self)
self.setIn = new.instancemethod(lambda self: None, self)
self.setOut = self.setIn
self.getState = new.instancemethod(lambda self: "unknown", self)
self.dev=hardware_obj
try:
sClass = str(self.dev.__class__)
i, j = re.search("'.*'", sClass).span()
except:
dev_class = sClass
else:
dev_class = sClass[i+1:j-1]
self.devClass = dev_class.split('.')[-1]
#print "Type is",self.devClass
if self.devClass=="Device":
self.devClass="Procedure"
if self.devClass=="TangoShutter":
self.devClass="Shutter"
if self.devClass=="MicrodiffInOut":
self.devClass="Actuator"
#2011-08-30-bessy-mh: let the wrapper also feel responsible for my new ShutterEpics hardware object
# identical to the original Shutter hardware object
if self.devClass == "ShutterEpics":
self.devClass = "Shutter"
#2011-08-30-bessy-mh: end
#2013-10-31-bessy-mh: ... and for the MD2 shutter hardware object too!
if self.devClass == "MD2v4_FastShutter":
self.devClass = "Shutter"
#2013-10-31-bessy-mh: end
if not self.devClass in ("WagoPneu", "Shutter", "SpecMotorWSpecPositions", "Procedure", "Actuator"):
self.devClass = "WagoPneu"
initFunc = getattr(self, "init%s" % self.devClass)
initFunc()
self.setIn = getattr(self, "setIn%s" % self.devClass)
self.setOut = getattr(self, "setOut%s" % self.devClass)
self.getState = getattr(self, "getState%s" % self.devClass)
def __getstate__(self):
dict = self.__dict__.copy()
del dict["setIn"]
del dict["setOut"]
del dict["getState"]
return dict
def __setstate__(self, dict):
self.__dict__ = dict.copy()
self.setIn = new.instancemethod(lambda self: None, self)
self.setOut = self.setIn
self.getState = new.instancemethod(lambda self: "unknown", self)
def userName(self):
return self.dev.userName()
# WagoPneu HO methods
def initWagoPneu(self):
#print "initWagoPneu"
self.dev.connect(self.dev,'wagoStateChanged', self.stateChangedWagoPneu)
def setInWagoPneu(self):
#print "setInWagoPneu"
self.emit(PYSIGNAL('duoStateChanged'), ('moving', ))
self.dev.wagoIn()
def setOutWagoPneu(self):
#print "setOutWagoPneu"
self.emit(PYSIGNAL('duoStateChanged'), ('moving', ))
self.dev.wagoOut()
def stateChangedWagoPneu(self,state):
#print "stateChangedWagoPneu",state
try:
state=WrapperHO.wagoStateDict[state]
except KeyError:
state='error'
self.emit(PYSIGNAL('duoStateChanged'), (state, ))
def getStateWagoPneu(self):
#print "getStateWagoPneu"
state=self.dev.getWagoState()
try:
state=WrapperHO.wagoStateDict[state]
except KeyError:
state='error'
return state
# Actuator HO methods
def initActuator(self):
#print "initActuator"
self.dev.connect(self.dev,'actuatorStateChanged', self.stateChangedActuator)
def setInActuator(self):
#print "setInActuator"
self.emit(PYSIGNAL('duoStateChanged'), ('moving', ))
self.dev.actuatorIn()
def setOutActuator(self):
#print "setOutActuator"
self.emit(PYSIGNAL('duoStateChanged'), ('moving', ))
self.dev.actuatorOut()
def stateChangedActuator(self,state):
#print "stateChangedActuator",state
try:
state=WrapperHO.actuatorStateDict[state]
except KeyError:
state='error'
self.emit(PYSIGNAL('duoStateChanged'), (state, ))
def getStateActuator(self):
#print "getStateActuator"
state=self.dev.getActuatorState()
try:
state=WrapperHO.actuatorStateDict[state]
except KeyError:
state='error'
return state
# Shutter HO methods
def initShutter(self):
#print "initShutter"
self.dev.connect(self.dev, 'shutterStateChanged', self.stateChangedShutter)
def setInShutter(self):
#print "setInShutter"
self.dev.openShutter()
def setOutShutter(self):
#print "setOutShutter"
self.dev.closeShutter()
def stateChangedShutter(self,state):
#print "stateChangedShutter"
#logging.info("stateChangedShutter %s" % state)
try:
state=WrapperHO.shutterStateDict[state]
except KeyError:
state='error'
self.emit(PYSIGNAL('duoStateChanged'), (state, ))
def getStateShutter(self):
#print "getStateShutter"
state=self.dev.getShutterState()
try:
state=WrapperHO.shutterStateDict[state]
except KeyError:
state='error'
return state
# SpecMotorWSpecPositions HO methods
def initSpecMotorWSpecPositions(self):
#print "initSpecMotorWSpecPositions"
self.positions=None
self.dev.connect(self.dev, 'predefinedPositionChanged', self.positionChangedSpecMotorWSpecPositions)
self.dev.connect(self.dev, 'stateChanged', self.stateChangedSpecMotorWSpecPositions)
self.dev.connect(self.dev, 'newPredefinedPositions', self.newPredefinedSpecMotorWSpecPositions)
def setInSpecMotorWSpecPositions(self):
#print "setInSpecMotorWSpecPositions"
if self.positions is not None:
self.dev.moveToPosition(self.positions[1])
def setOutSpecMotorWSpecPositions(self):
#print "setOutSpecMotorWSpecPositions"
if self.positions is not None:
self.dev.moveToPosition(self.positions[0])
def stateChangedSpecMotorWSpecPositions(self,state):
#logging.info("stateChangedSpecMotorWSpecPositions %s" % state)
try:
state = WrapperHO.motorWStateDict[state]
except IndexError:
state = 'error'
if state is not None:
#print "emiting duoStateChanged",state
self.emit(PYSIGNAL('duoStateChanged'), (state, ))
def positionChangedSpecMotorWSpecPositions(self,pos_name,pos):
#print "positionChangedSpecMotorWSpecPositions",pos_name,pos
if self.dev.getState()!=self.dev.READY:
return
state="error"
if self.positions is not None:
for i in range(len(self.positions)):
if pos_name==self.positions[i]:
state=WrapperHO.motorWPosDict[i]
#print "emiting duoStateChanged",state
self.emit(PYSIGNAL('duoStateChanged'), (state, ))
def getStateSpecMotorWSpecPositions(self):
#print "getStateSpecMotorWSpecPositions"
if self.positions is None:
return "error"
curr_pos=self.dev.getCurrentPositionName()
if curr_pos is None:
state=self.dev.getState()
try:
state=WrapperHO.motorWStateDict[state]
except IndexError:
state='error'
return state
else:
for i in range(len(self.positions)):
if curr_pos==self.positions[i]:
return WrapperHO.motorWPosDict[i]
return 'error'
def newPredefinedSpecMotorWSpecPositions(self):
#print "newPredefinedSpecMotorWSpecPositions"
self.positions=self.dev.getPredefinedPositionsList()
self.positionChangedSpecMotorWSpecPositions(self.dev.getCurrentPositionName(),self.dev.getPosition())
# Procedure HO methods
def initProcedure(self):
#print "initProcedure"
cmds=self.dev.getCommands()
self.setInCmd=None
self.setOutCmd=None
try:
channel=self.dev.getChannelObject("dev_state")
except KeyError:
channel=None
self.stateChannel=channel
if self.stateChannel is not None:
self.state_dict={'OPEN':'in', 'CLOSED':'out', 'ERROR':'error', '1':'in', '0':'out'}
self.stateChannel.connectSignal('update', self.channelUpdate)
else:
self.state_dict={}
for c in cmds:
if c.name()=="set in":
self.setInCmd=c
if self.stateChannel is not None:
self.setInCmd.connectSignal('commandReplyArrived', self.procedureSetInEnded)
self.setInCmd.connectSignal('commandBeginWaitReply', self.procedureStarted)
self.setInCmd.connectSignal('commandFailed', self.procedureAborted)
self.setInCmd.connectSignal('commandAborted', self.procedureAborted)
elif c.name()=="set out":
self.setOutCmd=c
if self.stateChannel is not None:
self.setOutCmd.connectSignal('commandReplyArrived', self.procedureSetOutEnded)
self.setOutCmd.connectSignal('commandBeginWaitReply', self.procedureStarted)
self.setOutCmd.connectSignal('commandFailed', self.procedureAborted)
self.setOutCmd.connectSignal('commandAborted', self.procedureAborted)
def channelUpdate(self,value):
try:
key=self.dev.statekey
except AttributeError:
pass
else:
try:
value=value[key]
except TypeError:
value='error'
try:
value=self.state_dict[value]
except KeyError:
pass
self.emit(PYSIGNAL('duoStateChanged'), (value, ))
def setInProcedure(self):
if self.setInCmd is not None:
self.setInCmd()
def setOutProcedure(self):
if self.setOutCmd is not None:
self.setOutCmd()
"""
def stateChangedProcedure(self,state):
#print "stateChangedProcedure",state
pass
"""
def getStateProcedure(self):
#print "getStateProcedure"
if self.stateChannel is not None:
try:
state=self.stateChannel.getValue()
except:
state='error'
else:
try:
key=self.dev.statekey
except AttributeError:
pass
else:
try:
state=state[key]
except TypeError:
state='error'
try:
state=self.state_dict[state]
except KeyError:
pass
return state
return "unknown"
def procedureSetInEnded(self, *args):
self.emit(PYSIGNAL('duoStateChanged'), ('in', ))
def procedureSetOutEnded(self, *args):
self.emit(PYSIGNAL('duoStateChanged'), ('out', ))
def procedureStarted(self, *args):
self.emit(PYSIGNAL('duoStateChanged'), ('moving', ))
def procedureAborted(self, *args):
self.emit(PYSIGNAL('duoStateChanged'), ('error', ))