1
2 """SpecVariable module
3
4 This module defines the class for Spec variable objects
5 """
6
7 __author__ = 'Matias Guijarro'
8 __version__ = '1.0'
9
10 import SpecConnectionsManager
11 import SpecEventsDispatcher
12 import SpecWaitObject
13
14 (UPDATEVALUE, FIREEVENT) = (SpecEventsDispatcher.UPDATEVALUE, SpecEventsDispatcher.FIREEVENT)
15
17 """SpecVariable class
18
19 Thin wrapper around SpecChannel objects, to make
20 variables watching, setting and getting values easier.
21 """
22 - def __init__(self, varName = None, specVersion = None, timeout = None, prefix=True):
23 """Constructor
24
25 Keyword arguments:
26 varName -- the variable name in Spec
27 specVersion -- 'host:port' string representing a Spec server to connect to (defaults to None)
28 timeout -- optional timeout (defaults to None)
29 """
30 self.connection = None
31 self.isConnected = self.isSpecConnected
32
33 if varName is not None and specVersion is not None:
34 self.connectToSpec(varName, specVersion, timeout, prefix)
35 else:
36 self.channelName = None
37 self.specVersion = None
38
39
40 - def connectToSpec(self, varName, specVersion, timeout = None, prefix=True):
60
61
63 """Return whether the remote Spec version is connected or not."""
64 return self.connection is not None and self.connection.isSpecConnected()
65
66
68 """Return the watched variable current value."""
69 chan = self.connection.getChannel(self.channelName)
70
71 return chan.read()
72
73
75 """Set the watched variable value
76
77 Arguments:
78 value -- the new variable value
79 """
80 if self.isConnected():
81 chan = self.connection.getChannel(self.channelName)
82
83 return chan.write(value)
84
85
86 - def waitUpdate(self, waitValue = None, timeout = None):
87 """Wait for the watched variable value to change
88
89 Keyword arguments:
90 waitValue -- wait for a specific variable value
91 timeout -- optional timeout
92 """
93 if self.isConnected():
94 w = SpecWaitObject.SpecWaitObject(self.connection)
95
96 w.waitChannelUpdate(self.channelName, waitValue = waitValue, timeout = timeout)
97
98 return w.value
99
100
102 """SpecVariableA class - asynchronous version of SpecVariable
103
104 Thin wrapper around SpecChannel objects, to make
105 variables watching, setting and getting values easier.
106 """
107 - def __init__(self, varName = None, specVersion = None, dispatchMode = UPDATEVALUE, prefix=True, callbacks={}):
108 """Constructor
109
110 Keyword arguments:
111 varName -- name of the variable to monitor (defaults to None)
112 specVersion -- 'host:port' string representing a Spec server to connect to (defaults to None)
113 """
114 self.connection = None
115 self.channelName = ''
116 self.__callbacks = {
117 'connected': None,
118 'disconnected': None,
119 'update': None,
120 }
121 for cb_name in self.__callbacks.iterkeys():
122 if callable(callbacks.get(cb_name)):
123 self.__callbacks[cb_name] = SpecEventsDispatcher.callableObjectRef(callbacks[cb_name])
124
125
126 if varName is not None and specVersion is not None:
127 self.connectToSpec(varName, specVersion, dispatchMode = dispatchMode, prefix=prefix)
128 else:
129 self.varName = None
130 self.specVersion = None
131
132
164
165
167 return self.connection is not None and self.connection.isSpecConnected()
168
169
171 self.connected()
172 if self.__callbacks.get("connected"):
173 cb = self.__callbacks["connected"]()
174 if cb is not None:
175 cb()
176
177
179 """Callback triggered by a 'connected' event from Spec
180
181 To be extended by derivated classes.
182 """
183 pass
184
185
187 self.disconnected()
188 if self.__callbacks.get("disconnected"):
189 cb = self.__callbacks["disconnected"]()
190 if cb is not None:
191 cb()
192
193
195 """Callback triggered by a 'disconnected' event from Spec
196
197 To be extended by derivated classes.
198 """
199 pass
200
201
203 """Callback triggered by a variable update
204
205 Extend it to do something useful.
206 """
207 pass
208
209
211 """Return the watched variable current value."""
212 if self.connection is not None:
213 chan = self.connection.getChannel(self.channelName)
214
215 return chan.read()
216
217
219 """Set the watched variable value
220
221 Arguments:
222 value -- the new variable value
223 """
224 if self.connection is not None:
225 chan = self.connection.getChannel(self.channelName)
226
227 return chan.write(value)
228