-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLJSocket.py
207 lines (162 loc) · 7.57 KB
/
LJSocket.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
import LabJackPython
import skymote
from RawDevice import RawDeviceFactory
from ModbusDevice import ModbusDeviceFactory
from SkyMoteExchanger import SkyMoteExchanger
# The command Response port is for UD Family devices.
COMMAND_RESPONSE_STARTING_PORT = 6001
# The Modbus port is for Modbus commands. For any LabJack device that supports Modbus.
MODBUS_STARTING_PORT = 5021
# The Spontaneous port is where spontaneous data from Skymotes will go.
SPONTANEOUS_STARTING_PORT = 7021
from twisted.internet import reactor
from twisted.application import internet
from twisted.internet import protocol
from twisted.protocols import basic
from collections import namedtuple
import struct
# 3 = U3
# 6 = U6
# 9 = UE9
PRODUCT_IDS = [3, 6, 9, 0x501]
DeviceLine = namedtuple('DeviceLine', 'devType crport modbusport spontport localId serialNumber')
# Override how namedtuple prints. Just join the fields with spaces
class DeviceLine(DeviceLine):
def __repr__(self):
return " ".join(str(i) for i in self)
class DeviceManager(object):
def __init__(self, serviceCollection):
self.serviceCollection = serviceCollection
self.devices = dict()
self.deviceCountsByType = dict()
for prodID in PRODUCT_IDS:
self.deviceCountsByType[prodID] = 0
# Initialize ports
self.nextCRPort = COMMAND_RESPONSE_STARTING_PORT
self.nextModbusPort = MODBUS_STARTING_PORT
self.nextSpontPort = SPONTANEOUS_STARTING_PORT
self.rawDeviceServices = dict()
self.modbusDeviceServices = dict()
self.exchangers = dict()
#self.tmpDict = {3: 0, 6: 0, 9: 0, 0x501: 1}
reactor.addSystemEventTrigger('during', 'shutdown', self.shutdownExchangers)
def scanSetupModbusService(self, d):
print "setting self.modbusDeviceSerial =", d.serialNumber
self.modbusDeviceSerial = d.serialNumber
if self.modbusService is None:
self.modbusFactory = ModbusDeviceFactory(self, self.modbusDeviceSerial)
self.modbusService = internet.TCPServer(MODBUS_PORT, self.modbusFactory)
self.modbusService.setServiceParent(self.serviceCollection)
else:
# We already have a Modbus service, just change what device serves
self.modbusFactory = self.modbusDeviceSerial
def scanExistingDevices(self):
"""Check everything we know about. If something is gone, remove it."""
for serial, d in self.devices.items():
print "Checking %s for a vaild handle." % serial
if not LabJackPython.isHandleValid(d.handle):
# We lost this device
self.deviceCountsByType[d.devType] -= 1
d.close()
print "Deleting device", self.devices[serial]
self.devices.pop(serial)
if d.devType == 0x501:
ex = self.exchangers[d.serialNumber][0]
ex.shutdown(self.serviceCollection)
self.exchangers.pop(d.serialNumber)
else:
self.serviceCollection.removeService(self.rawDeviceServices[serial])
self.rawDeviceServices.pop(serial)
self.serviceCollection.removeService(self.modbusDeviceServices[serial])
self.modbusDeviceServices.pop(serial)
def incrementPorts(self):
self.nextCRPort += 1
self.nextModbusPort += 1
self.nextSpontPort += 1
def _openSkymoteBridge(self, device):
""" Opens any new bridges and starts up exchangers for them.
"""
d = skymote.Bridge( autoOpen = False )
d.loadGenericDevice(device)
self.deviceCountsByType[0x501] += 1
print "Opened d =", d
d.modbusPortNum = self.nextModbusPort
d.spontPortNum = self.nextSpontPort
self.devices[d.serialNumber] = d
se = SkyMoteExchanger(d, self.nextModbusPort, self.nextSpontPort, self.serviceCollection, self.scanExistingDevices)
self.exchangers[d.serialNumber] = (se, self.nextModbusPort, self.nextSpontPort)
self.incrementPorts()
def scan(self):
print "scanning"
self.scanExistingDevices()
devices = LabJackPython.openAllLabJacks()
for device in devices:
prodID = device.devType
if prodID == 0x501:
self._openSkymoteBridge(device)
continue
device._registerAtExitClose()
self.deviceCountsByType[prodID] += 1
print "Opened d =", device
self.devices[device.serialNumber] = device
# Set up the Modbus port
device.modbusPortNum = self.nextModbusPort
modbusFactory = ModbusDeviceFactory(self, device.serialNumber)
modbusService = internet.TCPServer(self.nextModbusPort, modbusFactory)
modbusService.setServiceParent(self.serviceCollection)
self.modbusDeviceServices[device.serialNumber] = modbusService
# Set up C/R port
port = self.nextCRPort
device.crPortNum = port
factory = RawDeviceFactory(self, device.serialNumber)
service = internet.TCPServer(port, factory)
service.setServiceParent(self.serviceCollection)
self.rawDeviceServices[device.serialNumber] = service
self.incrementPorts()
return self.buildScanResponse()
def buildScanResponse(self):
''' Builds a list of lines about each device LJSocket knows about. '''
returnLines = list()
for serial, d in self.devices.items():
if d.devType == 0x501:
line = DeviceLine(d.devType, 'x', d.modbusPortNum, d.spontPortNum, d.localId, d.serialNumber)
else:
line = DeviceLine(d.devType, d.crPortNum, d.modbusPortNum, 'x', d.localId, d.serialNumber)
returnLines.append(line)
return returnLines
def writeRead(self, serial, writeBytes):
"""
Write writeBytes to LabJack with given serial, read and
return the response.
"""
print "writeRead: serial: %s" % serial
if serial not in self.devices:
print "writeRead: no device with serial %s" % serial
return ""
print "writeRead: writing %d bytes" % len(writeBytes)
d = self.devices[serial]
writeList = [ ord(b) for b in writeBytes ]
readList = d._writeRead(writeList, 64, [], False, False, False)
readLen = len(readList)
readBytes = struct.pack('>' + 'B'*readLen, *readList)
print "writeRead: read %d bytes" % len(readBytes)
print "writeRead:", [ ord(b) for b in readBytes ]
return readBytes
def shutdownExchangers(self):
print "Shutting down exchangers"
for ex, p1, p2 in self.exchangers.values():
ex.shutdown()
print "Done"
return True
class SocketServiceProtocol(basic.LineReceiver):
def lineReceived(self, line):
if "scan" == line.lower():
deviceLines = self.factory.manager.scan()
numDevices = len(deviceLines)
self.sendLine("OK " + str(numDevices))
for line in deviceLines:
self.sendLine(str(line))
self.transport.loseConnection()
# Assigned a manager attribute in socketService
class SocketServiceFactory(protocol.ServerFactory):
protocol = SocketServiceProtocol