-
Notifications
You must be signed in to change notification settings - Fork 0
/
stateTest.py
executable file
·92 lines (67 loc) · 2.2 KB
/
stateTest.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
#! /usr/bin/python
import sys, os, select, pg, time, traceback, datetime, socket
class StateTestError( Exception):
value = None
def __init__( self, value):
self.value = value
print >> sys.stderr, sys.exc_info()[0]
print >> sys.stderr, '-'*60
traceback.print_exc(file=sys.stderr)
print >> sys.stderr, '-'*60
def __str__( self):
return repr( self.value)
class _Q:
db = None # our database connection
def open( self):
self.db = pg.connect( dbname="ls", host="contrabass.ls-cat.org", user="lsuser" )
def close( self):
self.db.close()
def __init__( self):
self.open()
def reset( self):
self.db.reset()
def query( self, qs):
if qs == '':
return rtn
if self.db.status == 0:
self.reset()
try:
# ping the server
qr = self.db.query(qs)
except:
print "Failed query: %s" % (qs)
if self.db.status == 1:
print >> sys.stderr, sys.exc_info()[0]
print >> sys.stderr, '-'*60
traceback.print_exc(file=sys.stderr)
print >> sys.stderr, '-'*60
return None
# reset the connection, should
# put in logic here to deal with transactions
# as transactions are rolled back
#
self.db.reset()
if self.db.status != 1:
# Bad status even after a reset, bail
raise StateTestError( 'Database Connection Lost')
qr = self.db.query( qs)
return qr
def dictresult( self, qr):
return qr.dictresult()
def e( self, s):
return pg.escape_string( s)
def fileno( self):
return self.db.fileno()
def getnotify( self):
return self.db.getnotify()
if __name__ == "__main__":
_q = _Q()
qs = 'select "State" & 1003 as rsp from cats.machinestate() where "Station"=2'
lastRsp = None
while 1:
qr = _q.query( qs)
rsp = qr.dictresult()[0]["rsp"]
if lastRsp == None or rsp != lastRsp:
print rsp
lastRsp = rsp
time.sleep(0.2)