def _event_puback(self, packet, _qos, _dup, _retain): LOG.info( 'ProtocolEvent "PubAck Packet" received: {} {}\n\tAddr:{}'.format( len(packet), packet, self.m_broker.Host.Name)) # Extract the message ID messageId = EncodeDecode._decodeValue(packet[:2]) self.pubackReceived(messageId)
def _event_suback(self, packet, _qos, _dup, _retain): messageId = EncodeDecode._decodeValue(packet[:2]) packet = packet[2:] # LOG.info('Event SubAck received - MsgID:{} Acks: {} {}\n\tAddr:{}'.format(messageId, len(packet), packet, self.m_broker.Host.Name)) # Extract the granted QoS levels grantedQos = [] while len(packet): grantedQos.append(packet[0]) packet = packet[1:] self.subackReceived(grantedQos, messageId)
def _event_unsubscribe(self, packet, _qos, _dup, _retain): LOG.info('Event Unsubscribe received: {} {}'.format( len(packet), packet)) messageId = EncodeDecode._decodeValue(packet[:2]) packet = packet[2:] # Extract the unsubscribing topics topics = [] while len(packet): topic = EncodeDecode._decodeString(packet) packet = packet[len(topic) + 2:] topics.append(topic) LOG.info('Mqtt UnSubscribe: {}'.format(topics)) self.unsubscribeReceived(topics, messageId)
def _event_subscribe(self, packet, qos, _dup, _retain): LOG.info('ProtocolEvent "Subscribe Packet" received: {} {}'.format( len(packet), packet)) messageId = EncodeDecode._decodeValue(packet[:2]) packet = packet[2:] topics = [] while len(packet): topic = EncodeDecode._decodeString(packet) packet = packet[len(topic) + 2:] qos = packet[0] packet = packet[1:] # Add them to the list of (topic, qos)s topics.append((topic, qos)) LOG.info('Mqtt Subscribe: {}'.format(topics)) self.subscribeReceived(topics, messageId)
def _event_connect(self, packet, _qos, _dup, _retain): """This will decode a received 'connect' packet. """ LOG.info('ProtocolEvent "Connect Packet" received.') # Strip variable header packet = packet[10:] # Extract the connect flags f_username = packet[0] & 0x80 == 0x80 f_password = packet[0] & 0x40 == 0x40 f_willRetain = packet[0] & 0x20 == 0x20 l_willQos = packet[0] & 0x18 >> 3 f_willFlag = packet[0] & 0x04 == 0x04 f_cleanStart = packet[0] & 0x02 == 0x02 packet = packet[1:] keepalive = EncodeDecode._decodeValue( packet[:2]) # Extract the keepalive period packet = packet[2:] clientID = EncodeDecode._decodeString( packet) # Extract the client id_event_connect packet = packet[len(clientID) + 2:] # Extract the will topic and message, if applicable l_willTopic = None l_willMessage = None if f_willFlag: # Extract the will topic l_willTopic = EncodeDecode._decodeString(packet) packet = packet[len(l_willTopic) + 2:] # Extract the will message # Whatever remains is the will message # ## l_willMessage = packet l_willMessage = EncodeDecode._decodeString(packet) packet = packet[len(l_willMessage) + 2:] l_username = None if f_username: # Extract user name if one is present. l_username = EncodeDecode._decodeString(packet) packet = packet[len(l_username) + 2:] l_password = None if f_password: # Extract password if one is present. l_password = EncodeDecode._decodeString(packet) packet = packet[len(l_password) + 2:] LOG.info('Mqtt Connected.') self.connectReceived(clientID, keepalive, l_willTopic, l_willMessage, l_willQos, f_willRetain, f_cleanStart, l_username, l_password)
def _event_publish(self, packet, qos, dup, retain): """ Receive a "Published" message Here we get a published message from the broker. Extract the parts of the packet. @param packet: is a bytearray containing the variable header and payload combined. """ # Extract the topic portion of the packet. l_topic = EncodeDecode._decodeString(packet) packet = packet[len(l_topic) + 2:] # Extract the message ID if appropriate messageId = None if qos > 0: messageId = EncodeDecode._decodeValue(packet[:2]) packet = packet[2:] LOG.debug('Publish MsgID:{}'.format(messageId)) # Extract whatever remains as the message l_json = EncodeDecode._get_string(packet) # l_json = packet.decode('utf-8') l_message = json_tools.decode_json_unicode(l_json) # l_topic is a string # l_message is a string self.publishReceived(l_topic, l_message, qos, dup, retain, messageId)
def _event_unsuback(self, packet, _qos, _dup, _retain): LOG.info('Event UnsubAck received: {} {}'.format(len(packet), packet)) messageId = EncodeDecode._decodeValue(packet[:2]) self.unsubackReceived(messageId)
def _event_pubcomp(self, packet, _qos, _dup, _retain): LOG.info('Event PubComp received: {} {}'.format(len(packet), packet)) messageId = EncodeDecode._decodeValue(packet[:2]) self.pubcompReceived(messageId)