Package SpecClient :: Module SpecVariable
[hide private]
[frames] | no frames]

Source Code for Module SpecClient.SpecVariable

  1  #$Id: SpecVariable.py,v 1.4 2005/03/17 12:43:46 guijarro Exp $ 
  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   
16 -class SpecVariable:
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 #alias 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):
41 """Connect to a remote Spec 42 43 Connect to Spec 44 45 Arguments: 46 varName -- the variable name in Spec 47 specVersion -- 'host:port' string representing a Spec server to connect to 48 timeout -- optional timeout (defaults to None) 49 """ 50 if prefix: 51 self.channelName = 'var/' + str(varName) 52 else: 53 self.channelName = str(varName) 54 self.specVersion = specVersion 55 56 self.connection = SpecConnectionsManager.SpecConnectionsManager().getConnection(specVersion) 57 58 w = SpecWaitObject.SpecWaitObject(self.connection) 59 w.waitConnection(timeout)
60 61
62 - def isSpecConnected(self):
63 """Return whether the remote Spec version is connected or not.""" 64 return self.connection is not None and self.connection.isSpecConnected()
65 66
67 - def getValue(self):
68 """Return the watched variable current value.""" 69 chan = self.connection.getChannel(self.channelName) 70 71 return chan.read()
72 73
74 - def setValue(self, value):
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
101 -class SpecVariableA:
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
133 - def connectToSpec(self, varName, specVersion, dispatchMode = UPDATEVALUE, prefix=True):
134 """Connect to a remote Spec 135 136 Connect to Spec and register channel for monitoring variable 137 138 Arguments: 139 varName -- name of the variable 140 specVersion -- 'host:port' string representing a Spec server to connect to 141 """ 142 self.varName = varName 143 self.specVersion = specVersion 144 if prefix: 145 self.channelName = 'var/%s' % varName 146 else: 147 self.channelName = varName 148 149 self.connection = SpecConnectionsManager.SpecConnectionsManager().getConnection(specVersion) 150 SpecEventsDispatcher.connect(self.connection, 'connected', self._connected) 151 SpecEventsDispatcher.connect(self.connection, 'disconnected', self._disconnected) 152 153 # 154 # register channel 155 # 156 self.connection.registerChannel(self.channelName, self.update, dispatchMode = dispatchMode) 157 cb = self.__callbacks.get("update") 158 if cb is not None: 159 cb = cb() 160 self.connection.registerChannel(self.channelName, cb, dispatchMode = dispatchMode) 161 162 if self.connection.isSpecConnected(): 163 self.connected()
164 165
166 - def isSpecConnected(self):
167 return self.connection is not None and self.connection.isSpecConnected()
168 169
170 - def _connected(self):
171 self.connected() 172 if self.__callbacks.get("connected"): 173 cb = self.__callbacks["connected"]() 174 if cb is not None: 175 cb()
176 177
178 - def connected(self):
179 """Callback triggered by a 'connected' event from Spec 180 181 To be extended by derivated classes. 182 """ 183 pass
184 185
186 - def _disconnected(self):
187 self.disconnected() 188 if self.__callbacks.get("disconnected"): 189 cb = self.__callbacks["disconnected"]() 190 if cb is not None: 191 cb()
192 193
194 - def disconnected(self):
195 """Callback triggered by a 'disconnected' event from Spec 196 197 To be extended by derivated classes. 198 """ 199 pass
200 201
202 - def update(self, value):
203 """Callback triggered by a variable update 204 205 Extend it to do something useful. 206 """ 207 pass
208 209
210 - def getValue(self):
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
218 - def setValue(self, value):
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