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)
Exemple #2
0
 def _event_unsubscribe(self, packet, _qos, _dup, _retain):
     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)
Exemple #3
0
 def _event_unsubscribe(self, packet, _qos, _dup, _retain):
     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)
Exemple #4
0
 def _event_connect(self, packet, _qos, _dup, _retain):
     """This will decode a received 'connect' packet.
     """
     #  Strip the protocol name and version number
     packet = packet[len("06MQisdp3"):]
     #  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
     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)
Exemple #5
0
 def _event_subscribe(self, packet, qos, _dup, _retain):
     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)
Exemple #6
0
 def _event_subscribe(self, packet, qos, _dup, _retain):
     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)
Exemple #7
0
 def _event_publish(self, packet, qos, dup, retain):
     #  Extract the topic name
     topic = EncodeDecode._decodeString(packet)
     packet = packet[len(topic) + 2:]
     #  Extract the message ID if appropriate
     messageId = None
     if qos > 0:
         messageId = EncodeDecode._decodeValue(packet[:2])
         packet = packet[2:]
     #  Extract the message
     #  Whatever remains is the message
     message = str(packet)
     #  LOG.info('Mqtt Publish: {}\n\t{}'.format(topic, message))
     self.publishReceived(topic, message, qos, dup, retain, messageId)
Exemple #8
0
 def _event_publish(self, packet, qos, dup, retain):
     #  Extract the topic name
     topic = EncodeDecode._decodeString(packet)
     packet = packet[len(topic) + 2:]
     #  Extract the message ID if appropriate
     messageId = None
     if qos > 0:
         messageId = EncodeDecode._decodeValue(packet[:2])
         packet = packet[2:]
     #  Extract the message
     #  Whatever remains is the message
     message = str(packet)
     #  LOG.info('Mqtt Publish: {}\n\t{}'.format(topic, message))
     self.publishReceived(topic, message, qos, dup, retain, messageId)
    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)