-
Notifications
You must be signed in to change notification settings - Fork 1
/
Valve_Relay_Server.py
317 lines (283 loc) · 8.96 KB
/
Valve_Relay_Server.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# Copyright []
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
### BEGIN NODE INFO
[info]
name = Valve / Relay Controller
version = 1.0
description = Controller for Evaporator Valves and Relays
[startup]
cmdline = %PYTHON% %FILE%
timeout = 20
[shutdown]
message = 987654321
timeout = 20
### END NODE INFO
"""
import platform
global serial_server_name
serial_server_name = (platform.node() + '_serial_server').replace('-', '_').lower()
from labrad.server import setting
from labrad.devices import DeviceServer, DeviceWrapper
from twisted.internet.defer import inlineCallbacks, returnValue
import labrad.units as units
from labrad.types import Value
import time
from traceback import format_exc
TIMEOUT = Value(5, 's')
BAUD = 9600
BYTESIZE = 8
STOPBITS = 1
PARITY = 0
class ValveRelayWrapper(DeviceWrapper):
@inlineCallbacks
def connect(self, server, port):
"""Connect to a device."""
print('connecting to "%s" on port "%s"...' % (server.name, port), end=' ')
self.server = server
self.ctx = server.context()
self.port = port
p = self.packet()
# p.open(port)
# Testing no pulse serial connect
p.open(port, True)
p.baudrate(BAUD)
p.bytesize(BYTESIZE)
p.stopbits(STOPBITS)
p.setParity = PARITY
p.read() # clear out the read buffer
p.timeout(TIMEOUT)
# p.timeout(None)
print(" CONNECTED ")
yield p.send()
def packet(self):
"""Create a packet in our private context"""
return self.server.packet(context=self.ctx)
def shutdown(self):
"""Disconnect from the serial port when we shut down"""
return self.packet().close().send()
@inlineCallbacks
def write(self, code):
"""Write a data value to the device"""
yield self.packet().write(code).send()
@inlineCallbacks
def read(self):
"""Read a response line from the device"""
p = self.packet()
p.read_line()
ans = yield p.send()
returnValue(ans.read_line)
@inlineCallbacks
def query(self, code):
""" Write, then read. """
p = self.packet()
p.write_line(code)
p.read_line()
ans = yield p.send()
returnValue(ans.read_line)
class ValveRelayServer(DeviceServer):
name = 'valve_relay_server'
deviceName = 'Valve and Relay Controller'
deviceWrapper = ValveRelayWrapper
def __init__(self):
super().__init__()
self.state = dict()
self.state['turbo pump'] = False
self.state['scroll pump'] = False
self.state['gate valve'] = False
self.state['chamber valve'] = False
self.state['turbo valve'] = False
@inlineCallbacks
def initServer(self):
print('loading config info...', end=' ')
self.reg = self.client.registry()
yield self.loadConfigInfo()
print('done.')
print(self.serialLinks)
yield DeviceServer.initServer(self)
@inlineCallbacks
def loadConfigInfo(self):
reg = self.reg
yield reg.cd(['', 'Servers', 'Evaporator Valves/Relays', 'Links'], True)
dirs, keys = yield reg.dir()
p = reg.packet()
print("Created packet")
print("printing all the keys", keys)
for k in keys:
print("k=", k)
p.get(k, key=k)
ans = yield p.send()
print("ans=", ans)
self.serialLinks = dict((k, ans[k]) for k in keys)
@inlineCallbacks
def findDevices(self):
try:
devs = []
for name, (serServer, port) in list(self.serialLinks.items()):
if serServer not in self.client.servers:
print(serServer)
print(self.client.servers)
continue
server = self.client[serServer]
ports = yield server.list_serial_ports()
if port not in ports:
continue
devName = '%s - %s' % (serServer, port)
devs += [(devName, (server, port))]
return devs
except:
print(format_exc())
@setting(405, returns='s')
def turbo_valve_open(self, c):
"""Opens turbo valve."""
dev = self.selectedDevice(c)
yield dev.write("otr")
ans = yield dev.read()
self.state['turbo valve'] = True
return ans
@setting(406, returns='s')
def turbo_valve_close(self, c):
"""Closess turbo valve."""
dev = self.selectedDevice(c)
yield dev.write("ctr")
ans = yield dev.read()
self.state['turbo valve'] = False
return ans
@setting(407, returns='s')
def chamber_valve_open(self, c):
"""Opens chamber valve."""
dev = self.selectedDevice(c)
yield dev.write("ocr")
ans = yield dev.read()
self.state['chamber valve'] = True
return ans
@setting(408, returns='s')
def chamber_valve_close(self, c):
"""Closes chamber valve."""
dev = self.selectedDevice(c)
yield dev.write("ccr")
ans = yield dev.read()
self.state['chamber valve'] = False
return ans
@setting(409, returns='s')
def gate_open(self, c):
"""Opens Gate valve."""
dev = self.selectedDevice(c)
yield dev.write("ogr")
ans = yield dev.read()
self.state['gate valve'] = True
return ans
@setting(410, returns='s')
def gate_close(self, c):
"""Closes Gate valve."""
dev = self.selectedDevice(c)
yield dev.write("cgr")
ans = yield dev.read()
self.state['gate valve'] = False
return ans
# @setting(411,returns='s')
# def valve_four_open(self,c):
# """Opens valve 4."""
# dev=self.selectedDevice(c)
# yield dev.write("o4r")
# ans = yield dev.read()
# return ans
#
# @setting(412,returns='s')
# def valve_four_close(self,c):
# """Closes valve 4."""
# dev=self.selectedDevice(c)
# yield dev.write("c4r")
# ans = yield dev.read()
# return ans
#
# @setting(413,returns='s')
# def valve_five_open(self,c):
# """Opens valve 5."""
# dev=self.selectedDevice(c)
# yield dev.write("o5r")
# ans = yield dev.read()
# return ans
#
# @setting(414,returns='s')
# def valve_five_close(self,c):
# """Closes valve 5."""
# dev=self.selectedDevice(c)
# yield dev.write("c5r")
# ans = yield dev.read()
# return ans
#
# @setting(415,returns='s')
# def valve_six_open(self,c):
# """Opens valve 6."""
# dev=self.selectedDevice(c)
# yield dev.write("o6r")
# ans = yield dev.read()
# return ans
#
# @setting(416,returns='s')
# def valve_six_close(self,c):
# """Closes valve 6."""
# dev=self.selectedDevice(c)
# yield dev.write("c6r")
# ans = yield dev.read()
# return ans
@setting(417, returns='s')
def iden(self, c):
"""Identifies the valve controller."""
dev = self.selectedDevice(c)
yield dev.write("ir")
ans = yield dev.read()
return ans
@setting(418, returns='s')
def scroll_on(self, c):
"""Starts the scroll pump."""
dev = self.selectedDevice(c)
yield dev.write("spr")
ans = yield dev.read()
self.state['scroll pump'] = True
return ans
@setting(419, returns='s')
def scroll_off(self, c):
"""Stops the scroll pump."""
dev = self.selectedDevice(c)
yield dev.write("ssr")
ans = yield dev.read()
self.state['scroll pump'] = False
return ans
@setting(420, returns='s')
def turbo_on(self, c):
"""Starts the turbo pump."""
dev = self.selectedDevice(c)
yield dev.write("tpr")
ans = yield dev.read()
self.state['turbo pump'] = True
return ans
@setting(421, returns='s')
def turbo_off(self, c):
"""Stops the turbo pump."""
dev = self.selectedDevice(c)
yield dev.write("tsr")
ans = yield dev.read()
self.state['turbo pump'] = False
return ans
@setting(422, name='s', returns='b')
def returnstate(self, c, name):
return self.state[name]
#
__server__ = ValveRelayServer()
if __name__ == '__main__':
from labrad import util
util.runServer(__server__)