-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pump.py
184 lines (147 loc) · 6.21 KB
/
Pump.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
#!/usr/bin/env python
#-*- coding: utf-8 -*-
# Author: Sawik
# Email: [email protected]
# License: GNU GPL
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
import wx, serial.tools.list_ports, Events, Threads, Queue, gettext
_ = wx.GetTranslation
class Pump(wx.EvtHandler):
PORT_BAUD_RATE = 9600
E_CONNECTION = _("Connection failed")
MIN_FLOW = 100
MAX_FLOW = 5000
def __init__(self, parent, *args, **kw):
super(Pump, self).__init__(*args, **kw)
self.id = wx.NewId()
self.parent = parent
self.isConnected = False
self.isFlowOn = False
self.pumpQueue = Queue.Queue()
self.injQueue = Queue.Queue()
self.Bind(Events.EVT_SERIALRX, self.ParsePumpResponse)
self.Bind(Events.EVT_FLOW_IS_ON, self.OnFlowOn)
self.ConnectPump(None)
def GetId(self):
return self.id
def GetAvailableComPorts(self):
self.ports = serial.tools.list_ports.comports()
self.availableComPorts = []
for port in serial.tools.list_ports.comports():
self.availableComPorts.append(port.device)
if len(self.availableComPorts) < 1:
print ("[ERROR]\tNo devices connected!")
return []
print("[INFO]\tConnected COM ports: " + str(self.availableComPorts))
return self.availableComPorts
def ConnectPump(self, event):
self.DisconnectPump()
if not self.GetAvailableComPorts():
event = Events.PumpConnectedEvent(Events.PUMP_CONNECTED, self.GetId(), 0, _("No device connected!"))
wx.PostEvent(self.parent, event)
return
numberOfDevices = len(self.availableComPorts)
code = 0
if numberOfDevices > 0:
try:
print("[INFO]\tCreating pump thread...")
self.pumpThread = Threads.PumpThread(self.availableComPorts[0], self.pumpQueue, self)
self.pumpThread.start()
except Exception, ex:
print ("[ERROR]\t {}".format(ex.message))
else:
print("[INFO]\t {}".format("Pump thread created."))
self.pumpThread.Write("FLOW?\r")
code += 1
if numberOfDevices > 1:
try:
print("[INFO]\tCreating injector thread...")
self.injThread = Threads.PumpThread(self.availableComPorts[1], self.injQueue, self)
self.injThread.start()
except Exception, ex:
print ("[ERROR]\t {}".format(ex.message))
else:
print("[INFO]\t {}".format("Injector thread created."))
self.injThread.Write("INJ?\r")
code += 2
if code == 0:
msg = _("Connection error!")
if code == 1:
msg = "Pump connected, no injector."
if code == 2:
msg = "Injector connected, no pump."
if code == 3:
msg = "Pump and injector connected."
event = Events.PumpConnectedEvent(Events.PUMP_CONNECTED, self.GetId(), code, msg)
wx.PostEvent(self.parent, event)
return
def DisconnectPump(self):
if hasattr(self, 'pumpThread'):
print("[INFO]\tDisconnecting pump...")
self.pumpThread.Stop()
print("[INFO]\tPump disconnected.")
if hasattr(self, 'injThread'):
print("[INFO]\tDisconnecting injector...")
self.injThread.Stop()
print("[INFO]\tInjector disconnected.")
return
def ParsePumpResponse(self, event):
try:
pumpResponse = event.data.strip().split(":")
except Exception, ex:
print ex.message
return
print('[DATA]\t{}'.format(pumpResponse))
newEvent = False
if pumpResponse[0] == "ON":
newEvent = Events.FlowIsOnEvent(Events.FLOW_IS_ON, self.parent.GetId(), True)
if pumpResponse[0] == "OFF":
newEvent = Events.FlowIsOnEvent(Events.FLOW_IS_ON, self.parent.GetId(), False)
if pumpResponse[0] == "FLOW":
newEvent = Events.FlowInfoEvent(Events.FLOW_INFO, self.parent.GetId(), pumpResponse[1])
if pumpResponse[0] == "INJ":
newEvent = Events.InjInfoEvent(Events.INJ_INFO, self.parent.GetId(), pumpResponse[1])
if newEvent:
wx.PostEvent(self, newEvent)
wx.PostEvent(self.parent, newEvent)
def StartFlow(self, event):
if hasattr(self, 'pumpThread'):
if self.isFlowOn:
self.pumpThread.Write("OFF\r")
else:
self.pumpThread.Write("ON\r")
return
def OnFlowOn(self, event):
self.isFlowOn = event.GetData()
return
def SetFlow(self, flow):
try:
flowInt = int(flow)
except Exception, ex:
print("[ERROR]\t{}".format(ex.message))
return
if flowInt > self.MAX_FLOW or flowInt < self.MIN_FLOW:
msg = _("Flow limits: {} - {}").format(self.MIN_FLOW, self.MAX_FLOW)
event = Events.ErrorMsgEvent(Events.ERROR_MSG, self.parent.GetId(), msg)
wx.PostEvent(self.parent, event)
return
if hasattr(self, 'pumpThread'):
self.pumpThread.Write("FLOW:{}\r".format(flow))
else:
print("[ERROR]\tThere is no pump thread started!")
return