1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | ''' |
---|
4 | log EPICS data into RRD files |
---|
5 | ''' |
---|
6 | |
---|
7 | ########### SVN repository information ################### |
---|
8 | # $Date: 2010-08-26 19:55:55 +0000 (Thu, 26 Aug 2010) $ |
---|
9 | # $Author: jemian $ |
---|
10 | # $Revision: 207 $ |
---|
11 | # $HeadURL$ |
---|
12 | # $Id: pvwatch.py 207 2010-08-26 19:55:55Z jemian $ |
---|
13 | ########### SVN repository information ################### |
---|
14 | |
---|
15 | #------------------------------------------------------------- |
---|
16 | |
---|
17 | import time |
---|
18 | import pprint |
---|
19 | import epics # Matt Newville's EpicsCA support |
---|
20 | |
---|
21 | #------------------------------------------------------------- |
---|
22 | |
---|
23 | |
---|
24 | class pvwatch: |
---|
25 | '''A single monitored EPICS PV''' |
---|
26 | |
---|
27 | name = None # EPICS PV name |
---|
28 | ch = None # EPICS channel object |
---|
29 | value = None # value at last update |
---|
30 | count = 0 # number of updates received |
---|
31 | timestamp = None # Python time of last update |
---|
32 | connected = False # current EPICS connection state |
---|
33 | |
---|
34 | def __init__(self, name): |
---|
35 | self.name = name |
---|
36 | self.ch = epics.PV( name, |
---|
37 | connection_callback = self.connectHandler, |
---|
38 | callback = self.updateHandler, |
---|
39 | auto_monitor = True) |
---|
40 | |
---|
41 | def __str__(self): |
---|
42 | n = "%s" % self.name |
---|
43 | v = "%s" % self.value |
---|
44 | c = "%d" % self.count |
---|
45 | return "%40s %20s %15s" % (n, v, c) |
---|
46 | |
---|
47 | def connectHandler(self, **kw): |
---|
48 | self.timestamp = time.time() |
---|
49 | self.connected = kw['conn'] |
---|
50 | self.ch = kw['pv'] |
---|
51 | # cannot call self.get() here! |
---|
52 | #print self.name, " connected = ", self.connected |
---|
53 | |
---|
54 | def updateHandler(self, **kw): |
---|
55 | #pprint.pprint(kw) |
---|
56 | self.timestamp = time.time() |
---|
57 | self.value = kw['value'] |
---|
58 | self.count += 1 |
---|
59 | #print self.name, " = ", self.value |
---|
60 | |
---|
61 | def get(self): |
---|
62 | self.value = self.ch.get() |
---|