def connect(self, cleansession=True): self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock.settimeout(5.0) self.sock.connect((self.host, self.port)) connect = MQTTSN.Connects() connect.ClientId = self.clientid connect.CleanSession = cleansession connect.KeepAliveTimer = 0 self.sock.send(connect.pack()) response, address = MQTTSN.unpackPacket(MQTTSN.getPacket(self.sock)) assert response.mh.MsgType == MQTTSN.CONNACK self.startReceiver()
def connect(self, cleansession=True): self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #self.sock.settimeout(5.0) self.sock.connect((self.host, self.port)) connect = MQTTSN.Connects() connect.ClientId = self.clientid connect.CleanSession = cleansession connect.KeepAliveTimer = 0 self.sock.send(connect.pack()) response, address = MQTTSN.unpackPacket(MQTTSN.getPacket(self.sock)) assert response.mh.MsgType == MQTTSN.CONNACK self.startReceiver()
def receive(self, callback=None): packet = None try: packet, address = MQTTSN.unpackPacket(MQTTSN.getPacket(self.socket)) #print("getting packet",packet,"\n") except Exception as e: if sys.exc_info()[0] != socket.timeout: print("getting packet unexpected exception", sys.exc_info()) raise Exception("Packet receive error ",e) if packet == None: return elif debug: print("\nReceived packet data =",packet,"\n") return if self.observe == packet.mh.MsgType: #print("found what we were looking for",self.observe) self.observed.append(packet) return if packet.mh.MsgType == MQTTSN.CONNACK: if hasattr(callback, "on_connect"): print("found on connect") callback.on_connect(self.client,address,packet.ReturnCode) elif packet.mh.MsgType == MQTTSN.DISCONNECT: if hasattr(callback, "on_disconnect"): callback.on_disconnect(self.client,packet.Duration) elif packet.mh.MsgType == MQTTSN.SUBACK: ##added by me if hasattr(callback, "on_subscribe"): callback.on_subscribe(self.client,packet.TopicId,packet.MsgId,packet.ReturnCode) elif packet.mh.MsgType == MQTTSN.ADVERTISE: if hasattr(callback, "advertise"): callback.advertise(self.client,address, packet.GwId, packet.Duration) elif packet.mh.MsgType == MQTTSN.REGISTER: if callback and hasattr(callback, "register"): callback.register(self.client,packet.TopicId, packet.TopicName) elif packet.mh.MsgType == MQTTSN.REGACK: if callback and hasattr(callback, "regack"): print("received regack") callback.regack(self.client,packet.TopicId) elif packet.mh.MsgType == MQTTSN.PUBACK: "check if we are expecting a puback" if packet.MsgId in self.outMsgs and \ self.outMsgs[packet.MsgId].Flags.QoS == 1: del self.outMsgs[packet.MsgId] if hasattr(callback, "published"): callback.published(self.client,packet.MsgId) else: raise Exception("No QoS 1 message with message id "+str(packet.MsgId)+" sent") elif packet.mh.MsgType == MQTTSN.PUBREC: if packet.MsgId in self.outMsgs: self.pubrel.MsgId = packet.MsgId self.socket.send(self.pubrel.pack()) else: raise Exception("PUBREC received for unknown msg id "+ \ str(packet.MsgId)) elif packet.mh.MsgType == MQTTSN.PUBREL: "release QOS 2 publication to self.client, & send PUBCOMP" msgid = packet.MsgId if msgid not in self.inMsgs: pass # what should we do here? else: pub = self.inMsgs[packet.MsgId] if callback == None or \ callback.messageArrived(self.client,pub.TopicId, pub.Data, 2, pub.Flags.Retain, pub.MsgId): del self.inMsgs[packet.MsgId] self.pubcomp.MsgId = packet.MsgId self.socket.send(self.pubcomp.pack()) if callback == None: return (pub.pub.TopicId, pub.Data, 2, pub.Flags.Retain, pub.MsgId) elif packet.mh.MsgType == MQTTSN.PUBCOMP: #"finished with this message id" if packet.MsgId in self.outMsgs: del self.outMsgs[packet.MsgId] if hasattr(callback, "published"): callback.published(self.client,packet.MsgId) else: raise Exception("PUBCOMP received for unknown msg id "+ \ str(packet.MsgId)) elif packet.mh.MsgType == MQTTSN.PUBLISH: #"finished with this message id ?" #print("here ",packet) qos = packet.Flags.QoS topicname = packet.TopicName TopicId=packet.TopicId data = packet.Data #print("here ",TopicId) if packet.Flags.QoS in [0, 3]: if qos == 3: qos = -1 if packet.Flags.TopicIdType == MQTTSN.TOPICID: topicname = packet.Data[:packet.TopicId] data = packet.Data[packet.TopicId:] if callback == None: return (TopicId, data, qos, packet.Flags.Retain, packet.MsgId) else: callback.messageArrived(self.client,TopicId, data, qos, packet.Flags.Retain, packet.MsgId) elif packet.Flags.QoS == 1: if callback == None: return (packet.TopicId, packet.Data, 1, packet.Flags.Retain, packet.MsgId) else: if callback.messageArrived(self.client,TopicId,data, 1, packet.Flags.Retain, packet.MsgId): self.puback.MsgId = packet.MsgId self.socket.send(self.puback.pack().encode()) elif packet.Flags.QoS == 2: self.inMsgs[packet.MsgId] = packet self.pubrec.MsgId = packet.MsgId self.socket.send(self.pubrec.pack()) elif packet.mh.MsgType == MQTTSN.SEARCHGW: ##added by me print(" Searching Gateway ") if hasattr(callback, "searchgw"): callback.searchgw(self.client,packet) elif packet.mh.MsgType == MQTTSN.ADVERTISE: ##added by me print(" Advertise info is ") if hasattr(callback, "advertise"): callback.advertise(self.client,packet) elif packet.mh.MsgType == MQTTSN.GWINFO: ##added by me print(" Search Gateway info is ") if hasattr(callback, "gwinfo"): print(" Search Gateway callback ") callback.gwinfo(self.client,packet) else: print("unexpected packet",packet) #raise Exception("Unexpected packet"+str(packet)) return "" return packet
def receive(self, topicmap, callback=None): packet = None try: packet, address = MQTTSN.unpackPacket(MQTTSN.getPacket( self.socket)) except: if sys.exc_info()[0] != socket.timeout: #print("unexpected exception", sys.exc_info()) raise sys.exc_info() if packet == None: time.sleep(0.1) return elif debug: print(packet) if self.observe == packet.mh.MsgType: #print("observed", packet) self.observed.append(packet) elif packet.mh.MsgType == MQTTSN.ADVERTISE: if hasattr(callback, "advertise"): callback.advertise(address, packet.GwId, packet.Duration) elif packet.mh.MsgType == MQTTSN.REGISTER: topicmap.register(packet.TopicId, packet.TopicName) elif packet.mh.MsgType == MQTTSN.PUBACK: "check if we are expecting a puback" if packet.MsgId in self.outMsgs and \ self.outMsgs[packet.MsgId].Flags.QoS == 1: del self.outMsgs[packet.MsgId] if hasattr(callback, "published"): callback.published(packet.MsgId) else: raise Exception("No QoS 1 message with message id " + str(packet.MsgId) + " sent") elif packet.mh.MsgType == MQTTSN.PUBREC: if packet.MsgId in self.outMsgs: self.pubrel.MsgId = packet.MsgId self.socket.send(self.pubrel.pack()) else: raise Exception("PUBREC received for unknown msg id "+ \ str(packet.MsgId)) elif packet.mh.MsgType == MQTTSN.PUBREL: "release QOS 2 publication to client, & send PUBCOMP" msgid = packet.MsgId if packet.MsgId not in self.inMsgs: pass # what should we do here? else: pub = self.inMsgs[packet.MsgId] topicname = topicmap.registered[pub.TopicId] if callback == None or \ callback.messageArrived(topicname, pub.Data, 2, pub.Flags.Retain, pub.MsgId): del self.inMsgs[packet.MsgId] self.pubcomp.MsgId = packet.MsgId self.socket.send(self.pubcomp.pack()) if callback == None: return (topicname, pub.Data, 2, pub.Flags.Retain, pub.MsgId) elif packet.mh.MsgType == MQTTSN.PUBCOMP: "finished with this message id" if packet.MsgId in self.outMsgs: del self.outMsgs[packet.MsgId] if hasattr(callback, "published"): callback.published(packet.MsgId) else: raise Exception("PUBCOMP received for unknown msg id "+ \ str(packet.MsgId)) elif packet.mh.MsgType == MQTTSN.PUBLISH: "finished with this message id" if packet.Flags.QoS in [0, 3]: qos = packet.Flags.QoS topicname = topicmap.registered[packet.TopicId] data = packet.Data if qos == 3: qos = -1 if packet.Flags.TopicIdType == MQTTSN.TOPICID: topicname = packet.Data[:packet.TopicId] data = packet.Data[packet.TopicId:] if callback == None: return (topicname, data, qos, packet.Flags.Retain, packet.MsgId) else: callback.messageArrived(topicname, data, qos, packet.Flags.Retain, packet.MsgId) elif packet.Flags.QoS == 1: topicname = topicmap.registered[packet.TopicId] if callback == None: return (topicname, packet.Data, 1, packet.Flags.Retain, packet.MsgId) else: if callback.messageArrived(topicname, packet.Data, 1, packet.Flags.Retain, packet.MsgId): self.puback.MsgId = packet.MsgId self.socket.send(self.puback.pack()) elif packet.Flags.QoS == 2: self.inMsgs[packet.MsgId] = packet self.pubrec.MsgId = packet.MsgId self.socket.send(self.pubrec.pack()) else: raise Exception("Unexpected packet" + str(packet)) return packet
def receive(self, callback=None): packet = None try: packet, address = MQTTSN.unpackPacket(MQTTSN.getPacket(self.socket)) except: if sys.exc_info()[0] != socket.timeout: print "unexpected exception", sys.exc_info() raise sys.exc_info() if packet == None: time.sleep(0.1) return elif debug: print packet if self.observe == packet.mh.MsgType: print "observed", packet self.observed.append(packet) elif packet.mh.MsgType == MQTTSN.ADVERTISE: if hasattr(callback, "advertise"): callback.advertise(address, packet.GwId, packet.Duration) elif packet.mh.MsgType == MQTTSN.REGISTER: if callback and hasattr(callback, "register"): callback.register(packet.TopicId, packet.Topicname) elif packet.mh.MsgType == MQTTSN.PUBACK: "check if we are expecting a puback" if self.outMsgs.has_key(packet.MsgId) and \ self.outMsgs[packet.MsgId].Flags.QoS == 1: del self.outMsgs[packet.MsgId] if hasattr(callback, "published"): callback.published(packet.MsgId) else: raise Exception("No QoS 1 message with message id "+str(packet.MsgId)+" sent") elif packet.mh.MsgType == MQTTSN.PUBREC: if self.outMsgs.has_key(packet.MsgId): self.pubrel.MsgId = packet.MsgId self.socket.send(self.pubrel.pack()) else: raise Exception("PUBREC received for unknown msg id "+ \ str(packet.MsgId)) elif packet.mh.MsgType == MQTTSN.PUBREL: "release QOS 2 publication to client, & send PUBCOMP" msgid = packet.MsgId if not self.inMsgs.has_key(msgid): pass # what should we do here? else: pub = self.inMsgs[packet.MsgId] if callback == None or \ callback.messageArrived(pub.TopicName, pub.Data, 2, pub.Flags.Retain, pub.MsgId): del self.inMsgs[packet.MsgId] self.pubcomp.MsgId = packet.MsgId self.socket.send(self.pubcomp.pack()) if callback == None: return (pub.TopicName, pub.Data, 2, pub.Flags.Retain, pub.MsgId) elif packet.mh.MsgType == MQTTSN.PUBCOMP: "finished with this message id" if self.outMsgs.has_key(packet.MsgId): del self.outMsgs[packet.MsgId] if hasattr(callback, "published"): callback.published(packet.MsgId) else: raise Exception("PUBCOMP received for unknown msg id "+ \ str(packet.MsgId)) elif packet.mh.MsgType == MQTTSN.PUBLISH: "finished with this message id" if packet.Flags.QoS in [0, 3]: qos = packet.Flags.QoS topicname = packet.TopicName data = packet.Data if qos == 3: qos = -1 if packet.Flags.TopicIdType == MQTTSN.TOPICID: topicname = packet.Data[:packet.TopicId] data = packet.Data[packet.TopicId:] if callback == None: return (topicname, data, qos, packet.Flags.Retain, packet.MsgId) else: callback.messageArrived(topicname, data, qos, packet.Flags.Retain, packet.MsgId) elif packet.Flags.QoS == 1: if callback == None: return (packet.topicName, packet.Data, 1, packet.Flags.Retain, packet.MsgId) else: if callback.messageArrived(packet.TopicName, packet.Data, 1, packet.Flags.Retain, packet.MsgId): self.puback.MsgId = packet.MsgId self.socket.send(self.puback.pack()) elif packet.Flags.QoS == 2: self.inMsgs[packet.MsgId] = packet self.pubrec.MsgId = packet.MsgId self.socket.send(self.pubrec.pack()) else: raise Exception("Unexpected packet"+str(packet)) return packet