-
Notifications
You must be signed in to change notification settings - Fork 11
/
NamedState.py
165 lines (132 loc) · 5.92 KB
/
NamedState.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
from HardwareRepository import HardwareRepository
from HardwareRepository.BaseHardwareObjects import Device
import logging
class NamedState(Device):
def __init__(self, name):
Device.__init__(self,name)
self.stateList = []
def _init(self):
self.stateChan = self.getChannelObject("state")
try:
self.moveStateChan = self.getChannelObject("hardware_state")
except KeyError:
self.moveStateChan = None
try:
self.changeCmd = self.getCommandObject("command")
except KeyError:
self.changeCmd = None
try:
self.stateListChannel = self.getChannelObject("statelist")
except KeyError:
self.stateListChannel = None
try:
self.commandtype = self.getProperty("commandtype")
except KeyError:
self.commandtype = None
self.stateChan.connectSignal("update", self.stateChanged)
if self.moveStateChan:
self.moveStateChan.connectSignal("update", self.hardwareStateChanged)
Device._init(self)
def init(self):
self._getStateList()
def connectNotify(self, signal):
if signal == 'stateChanged':
self.emit(signal, (self.getState(), ))
def stateChanged(self, channelValue):
logging.info('hw NamedState %s. got new value %s' % (self.name(), channelValue))
self.setIsReady(True)
self.emit('stateChanged', (self.getState(), ))
def hardwareStateChanged(self, channelValue):
logging.info('hw NamedState %s. Hardware state is now %s' % (self.name(), channelValue))
self.hdw_state = channelValue
self.emit('hardwareStateChanged', (self.hdw_state,))
def getStateList(self):
return self.stateList
def _getStateList(self):
if self.stateListChannel is not None:
# This is the case for ApertureDiameterList *configured as statelist channel in xml
statelist = self.stateListChannel.getValue()
for statename in statelist:
#cenvert in str because statename is numpy.int32 from Tango and deosn't work in QCombox !!!!!!
self.stateList.append(str(statename))
else:
# This is the case where state names are listed in xml
try:
states = self['states']
except:
logging.getLogger().error('%s does not define named states.', str(self.name()))
else:
for state in states:
statename = state.getProperty('name')
self.stateList.append(statename)
def update_values(self):
pass
def getUserName(self):
try:
name = self.getProperty("username")
except:
name = None
if name is None:
name = ""
return name
def getCurrentState(self):
return self.getState()
def getState(self):
try:
stateValue = self.stateChan.getValue()
if self.commandtype is not None and self.commandtype == 'index':
# this is the case of aperture diameters. the state channel gives only the index in the list
listvalue = self.stateList[int(stateValue)]
return listvalue
else:
return stateValue
except:
import traceback
logging.debug(traceback.format_exc())
return "unknown"
def setState(self, statename):
self.emit('hardwareStateChanged', ("STANDBY",))
logging.getLogger().exception('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>..............changing state for %s to ws: %s.' % ( self.getUserName(), statename))
if self.commandtype == 'index':
logging.getLogger().info(' this is index mode. %s' % str(self.stateList))
try:
statevalue = self.stateList.index(statename)
logging.getLogger().info(' this is index mode. setting actual value s to ws: %s.' % ( statevalue))
except:
logging.getLogger().exception('changing state for %s to ws: %s.failed. not such state' % ( self.getUserName(), statename))
self.emit('stateChanged', (self.getState(), ))
self.emit('hardwareStateChanged', ("ERROR",))
return
else:
statevalue = unicode(statename)
try:
logging.getLogger().exception('changing state for %s to ws: %s' % ( self.getUserName(), statevalue))
if self.changeCmd is not None:
logging.getLogger().exception(' - using command mode')
self.changeCmd(statevalue)
else:
logging.getLogger().exception(' - using attribute mode')
try:
#probleme de unicode tester en mettant un unicode
self.stateChan.setValue(statevalue)
except:
logging.getLogger().exception("cannot write attribute")
self.emit('stateChanged', (self.getState(), ))
self.emit('hardwareStateChanged', ("ERROR",))
except:
logging.getLogger().exception('Cannot change state for %s to %s: ' % ( self.getUserName(), statevalue))
def test():
import os
hwr_directory = os.environ["XML_FILES_PATH"]
hwr = HardwareRepository.HardwareRepository(os.path.abspath(hwr_directory))
hwr.connect()
ap_pos = hwr.getHardwareObject("/aperture_position")
ap_diam = hwr.getHardwareObject("/aperture_diameter")
yag_pos = hwr.getHardwareObject("/scintillator")
md2_phase = hwr.getHardwareObject("/md2j_phase")
print "Aperture Position: ", ap_pos.getState()
print "Aperture Diameter: ", ap_diam.getState()
print "Yag Posiion: ",yag_pos.getState()
print "MD2 Phase: ", md2_phase.getState()
if __name__ == '__main__':
test()