Skip to content

Commit

Permalink
Added special command support for firmware upgrades.
Browse files Browse the repository at this point in the history
  • Loading branch information
LabJackAdmin committed Sep 15, 2010
1 parent 0087b31 commit bcf2ec8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 22 deletions.
48 changes: 27 additions & 21 deletions SkyMoteCommandResponseService.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,34 @@ def dataReceived(self, data):
d = defer.Deferred()
d.addCallback(self.responseReceived)
self.queuedJobs.append([data, d])
else:
print "dataReceived: got data. length: %d" % len(data)
print "dataReceived: ",
print [ hex(ord(c)) for c in data ]

return True

print "dataReceived: got data. length: %d" % len(data)
print "dataReceived: ",
print [ hex(ord(c)) for c in data ]

if len(data.strip()) == 4:
data = data.strip()
print "Got special command:", data
if data.lower() == "lock":
# Prevent others from talking to this device
self.factory.lockout(self.connectionNumber)
elif data.lower() == "done":
# Allow others to talk to the device
self.factory.unlockEveryone()
else:
d = defer.Deferred()
d.addCallback(self.responseReceived)

#self.factory.exchanger.newPacketEvent.set()
readBytes = self.factory.writeRead(data, d)
if len(data.strip()) == 4:
data = data.strip()
print "Got special command:", data
if data.lower() == "lock":
# Prevent others from talking to this device
self.factory.lockout(self.connectionNumber)
elif data.lower() == "done":
# Allow others to talk to the device
self.factory.unlockEveryone()
elif data.lower().startswith("crh"):
# Close and re-open handleOnly
sleepTime = int(data[3])
self.factory.exchanger.closeAndReopenDevice(sleepTime, True)
elif data.lower().startswith("rst"):
# Close and re-open normally
sleepTime = int(data[3])
self.factory.exchanger.closeAndReopenDevice(sleepTime)
else:
d = defer.Deferred()
d.addCallback(self.responseReceived)

#self.factory.exchanger.newPacketEvent.set()
readBytes = self.factory.writeRead(data, d)

def responseReceived(self, data):
print "responseReceived: Got results:", data
Expand Down
33 changes: 32 additions & 1 deletion SkyMoteExchanger.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
from SkyMoteSpontaneousDataService import SkyMoteSpontaneousFactory

from time import sleep
from threading import Event

from LabJackPython import deviceCount

class SkyMoteExchanger(object):
def __init__(self, device, commandResponsePort, spontaneousPort, serviceCollection, deviceLostFnx ):
Expand All @@ -35,7 +38,9 @@ def __init__(self, device, commandResponsePort, spontaneousPort, serviceCollecti
self.spontaneousService.setServiceParent(serviceCollection)

self.deviceLost = deviceLostFnx

self.deviceClosedEvent = Event()
self.closingDevice = False

self.running = True
self.lastCommandTime = datetime.now()

Expand Down Expand Up @@ -76,9 +81,31 @@ def shutdown(self, serviceCollection = None):
def sendSpontaneousData(self, data):
for connection in self.spontaneousService.args[1].connections.values():
connection.sendData(data)

def closeAndReopenDevice(self, sleepTime = 4, handleOnly = False):
print "closeAndReopenDevice called, sleepTime = %s, handleOnly = %s" % (sleepTime, handleOnly)
# Save the serial number of the device.
#devNumber = deviceCount(self.device.devType)

self.closingDevice = True
self.running = False # Tell the looping read to stop
self.deviceClosedEvent.wait() # Wait for the looping read to stop

# Now sleep for the desired number of seconds.
sleep(sleepTime)

# Re-open device
self.device.open(handleOnly = handleOnly, LJSocket = None)

# Restart looping reading
self.running = True
self.closingDevice = False
reactor.callInThread(self.loopingRead)


def loopingRead(self):
self.deviceClosedEvent.clear() # The device isn't close, so set the flag to False.

while self.running:

# Busy wait just for a bit because we recently received a
Expand Down Expand Up @@ -116,10 +143,14 @@ def loopingRead(self):
if str(e).endswith('-7'):
#print "Read timed out."
pass
elif self.closingDevice:
# We're closing the device anyway.
pass
else:
print type(e), e
self.deviceLost()

self.device.close()
print "Shutting down read loop."
self.deviceClosedEvent.set() # Device is closed, so let people know.

0 comments on commit bcf2ec8

Please sign in to comment.