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

Source Code for Module SpecClient.SpecCounter

 1  """SpecCounter module 
 2   
 3  This module defines the classes for counter objects 
 4   
 5  Classes: 
 6  SpecCounter -- class representing a counter in Spec 
 7  SpecCounterA -- class representing a counter in Spec, to be used with a GUI 
 8  """ 
 9   
10  __author__ = 'Matias Guijarro' 
11  __version__ = '1.0' 
12   
13  import SpecConnectionsManager 
14  import SpecEventsDispatcher 
15  import SpecWaitObject 
16   
17  (COUNTING) = (3) 
18  (UNKNOWN, SCALER, TIMER, MONITOR) = (0, 1,2,3) 
19   
20 -class SpecCounter:
21 """SpecCounter class"""
22 - def __init__(self, specName = None, specVersion = None, timeout = None):
23 """Constructor 24 25 Keyword arguments: 26 specName -- the name of the counter in Spec (defaults to None) 27 specVersion -- 'host:port' string representing a Spec server to connect to (defaults to None) 28 timeout -- optional timeout for connection (defaults to None) 29 """ 30 self.channelName = '' 31 self.connection = None 32 self.type = UNKNOWN 33 34 if specName is not None and specVersion is not None: 35 self.connectToSpec(specName, specVersion, timeout) 36 else: 37 self.specName = None 38 self.specVersion = None
39 40
41 - def connectToSpec(self, specName, specVersion, timeout = None):
42 """Connect to a remote Spec 43 44 Connect to Spec 45 46 Arguments: 47 specName -- name of the counter in Spec 48 specVersion -- 'host:port' string representing a Spec server to connect to 49 timeout -- optional timeout for connection (defaults to None) 50 """ 51 self.specName = specName 52 self.specVersion = specVersion 53 54 self.connection = SpecConnectionsManager.SpecConnectionsManager().getConnection(specVersion) 55 56 w = SpecWaitObject.SpecWaitObject(self.connection) 57 w.waitConnection(timeout) 58 59 c = self.connection.getChannel('var/%s' % self.specName) 60 index = c.read() 61 if index == 0: 62 self.type = TIMER 63 elif index == 1: 64 self.type = MONITOR 65 else: 66 self.type = SCALER
67 68
69 - def count(self, time):
70 """Count up to a certain time or monitor count 71 72 Arguments: 73 time -- count time 74 """ 75 if self.connection is not None: 76 c1 = self.connection.getChannel('scaler/.all./count') 77 c2 = self.connection.getChannel('scaler/%s/value' % self.specName) 78 79 if self.type == MONITOR: 80 time = -time 81 82 c1.write(time) 83 84 w = SpecWaitObject.SpecWaitObject(self.connection) 85 w.waitChannelUpdate('scaler/.all./count', waitValue = 0) 86 87 return c2.read()
88 89
90 - def getValue(self):
91 """Return current counter value.""" 92 if self.connection is not None: 93 c = self.connection.getChannel('scaler/%s/value' % self.specName) 94 95 return c.read()
96