remote: added done/timeout callback, channel/mac switch

This commit is contained in:
schneider 2011-12-15 16:56:06 +01:00
parent 0517035110
commit 6609d3253c
1 changed files with 30 additions and 1 deletions

View File

@ -5,7 +5,7 @@ import crcmod
import packets import packets
class QueuePacket: class QueuePacket:
def __init__(self, channel, mac, acked, packet): def __init__(self, channel, mac, acked, packet, callback=None):
self.channel = channel self.channel = channel
self.mac = mac self.mac = mac
self.acked = acked self.acked = acked
@ -17,6 +17,7 @@ class QueuePacket:
self.timedout = False self.timedout = False
self.lock = threading.RLock() self.lock = threading.RLock()
self.isdone = False self.isdone = False
self.callback = callback
def __cmp__(self, other): def __cmp__(self, other):
if not isinstance(other,QueuePacket): if not isinstance(other,QueuePacket):
@ -34,10 +35,14 @@ class QueuePacket:
def sent(self, timeoutcallback): def sent(self, timeoutcallback):
with self.lock: with self.lock:
self.timedout = False self.timedout = False
if self.retrieslef > 0:
self.retriesleft-=1
if self.acked: if self.acked:
self.timeoutcallback = timeoutcallback self.timeoutcallback = timeoutcallback
self.timer = threading.Timer(self.timeout, self.timercallback) self.timer = threading.Timer(self.timeout, self.timercallback)
self.timer.start() self.timer.start()
elif self.callback:
self.callback('done')
def done(self): def done(self):
with self.lock: with self.lock:
@ -45,11 +50,15 @@ class QueuePacket:
self.timer.cancel() self.timer.cancel()
self.timer = None self.timer = None
self.isdone = True self.isdone = True
if self.callback:
self.callback('done')
def timercallback(self): def timercallback(self):
with self.lock: with self.lock:
self.timedout = True self.timedout = True
self.timeoutcallback(self) self.timeoutcallback(self)
if retriesleft == 0:
self.callback('timeout')
class Bridge: class Bridge:
def __init__(self, path2device): def __init__(self, path2device):
@ -69,6 +78,11 @@ class Bridge:
self.writer.start() self.writer.start()
self.reader.start() self.reader.start()
self.packetlength = None
self.txmac = None
self.rxmac = None
self.channel = None
self.setPacketLength(0x20) self.setPacketLength(0x20)
self.setTxMAC((1,2,3,2,1)) self.setTxMAC((1,2,3,2,1))
@ -127,7 +141,10 @@ class Bridge:
qp = self.outpackets.get() qp = self.outpackets.get()
#send it and notify the queuepacket #send it and notify the queuepacket
self.sendPacket(qp.packet) self.sendPacket(qp.packet)
self.setTxMac(qp.mac)
self.setChannel(qp.channel)
qp.sent(self.packetTimeout) qp.sent(self.packetTimeout)
self.setChannel(self.gameChannel)
except Exception as e: except Exception as e:
print e print e
@ -173,18 +190,30 @@ class Bridge:
self.ser.writeMessage('1',data); self.ser.writeMessage('1',data);
def setPacketLength(self, length): def setPacketLength(self, length):
if length == self.packetlength:
return
self.free.acquire() self.free.acquire()
self.ser.writeMessage('6', '%c'%length) self.ser.writeMessage('6', '%c'%length)
self.packetLength = length
def setTxMAC(self, mac): def setTxMAC(self, mac):
if mac == self.txmax:
return
self.free.acquire() self.free.acquire()
self.ser.writeMessage('3', ''.join([chr(x) for x in mac])) self.ser.writeMessage('3', ''.join([chr(x) for x in mac]))
self.txmac = mac
def setRxMAC(self, mac): def setRxMAC(self, mac):
if mac == self.rxmac:
return
self.free.acquire() self.free.acquire()
self.ser.writeMessage('4', ''.join([chr(x) for x in mac])) self.ser.writeMessage('4', ''.join([chr(x) for x in mac]))
self.rxmac = mac
def setChannel(self, channel): def setChannel(self, channel):
if channel == self.channel:
return
self.free.acquire() self.free.acquire()
self.ser.writeMessage('5', '%c'%channel) self.ser.writeMessage('5', '%c'%channel)
self.channel = channel