-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpithrottle.py
More file actions
117 lines (95 loc) · 2.65 KB
/
Copy pathpithrottle.py
File metadata and controls
117 lines (95 loc) · 2.65 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/python
import socket
import re
from pprint import pprint
TCP_IP = '127.0.0.1'
TCP_PORT = 12090
BUFFER_SIZE = 1024
#BUFFER_SIZE = 4
MESSAGE = "Hello, World!"
class PiThrottle:
def __init__(self, *args):
pass
def roster(self, input):
print "Roster list"
self.rosterlist = []
parse=re.search('RL([0-9]+)\\]\\\\\[(.*)',input)
if parse.group(1) > 0:
for entry in parse.group(2).split(']\['):
self.rosterlist.append(entry.split('}|{'))
print '\n'.join(map(str,self.rosterlist))
def turnoutstate(self, input):
print "Turnout status"
self.turnoutstates = {}
for entry in input[6:].split(']\['):
entrylist = entry.split('}|{')
if entrylist[1] != 'Turnout':
self.turnoutstates[entrylist[1]] = entrylist[0]
pprint(self.turnoutstates)
def turnout(self, input):
print "Turnout list"
self.turnouts = {}
for entry in input[6:].split(']\['):
entrylist = entry.split('}|{')
if entrylist[1]:
self.turnouts[entrylist[1]] = { 'sysname': entrylist[0], 'state': entrylist[2] }
else:
self.turnouts[entrylist[0]] = { 'sysname': entrylist[0], 'state': entrylist[2] }
pprint(self.turnouts)
def setturnout(self, input):
parse=re.search('PTA([0-9])(.*)',input)
if parse:
print "Turnout: "+parse.group(2)+" set to "+self.turnoutstates[parse.group(1)]
def route(self, input):
print "Route list"
routelist = []
routes = {}
for entry in input[6:].split(']\['):
entrylist = entry.split('}|{')
routelist.append(entrylist)
if entrylist[1]:
routes[entrylist[1]] = entrylist[0]
else:
routes[entrylist[0]] = entrylist[0]
print '\n'.join(map(str,routelist))
pprint(routes)
# Main code line
if __name__ == '__main__':
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.settimeout(0.05)
throttle = PiThrottle()
# s.send(MESSAGE)
data =""
while True:
try:
data = data + s.recv(BUFFER_SIZE)
bits=data.rsplit('\n',1)
if len(bits)>1:
data = bits[1]
lines = bits[0].split('\n')
for line in lines:
if line:
print line
if re.match("RL(.*)",line):
throttle.roster(line)
if re.match("PPA(.*)",line):
print "Power"
if re.match("PTT(.*)",line):
throttle.turnoutstate(line)
if re.match("PTL(.*)",line):
throttle.turnout(line)
if re.match("PRL(.*)",line):
throttle.route(line)
if re.match("PTA(.*)",line):
throttle.setturnout(line)
# print '\n'.join(map(str,throttle.rosterlist))
# pprint(turnoutstates)
# pprint(turnouts)
# pprint(routes)
except (socket.timeout):
pass
except Exception, e:
print "Exception: "+str(e)
break
s.close()