Example #1
0
 def unsubscribe(self, topic, messageId=None):
     # LOG.info("Sending unsubscribe packet")
     header = bytearray()
     varHeader = bytearray()
     payload = bytearray()
     header.append(0x0A << 4 | 0x01 << 1)
     if messageId is not None:
         varHeader.extend(EncodeDecode._encodeValue(self.messageID))
     else:
         varHeader.extend(
             EncodeDecode._encodeValue(random.randint(1, 0xFFFF)))
     payload.extend(EncodeDecode._encodeString(topic))
     header.extend(
         EncodeDecode._encodeLength(len(payload) + len(varHeader)))
     self.transport.write(header)
     self.transport.write(varHeader)
     self.transport.write(payload)
Example #2
0
 def subscribe(self, p_topic, requestedQoS=0, messageId=None):
     """
     Only supports QoS = 0 subscribes
     Only supports one subscription per message
     """
     # LOG.info("Sending subscribe packet - Topic: {}\n\tAddr: {}".format(p_topic, self.m_broker.Host.Name))
     l_varHeader = bytearray()
     l_payload = bytearray()
     #  Type = subscribe, QoS = 1
     if messageId is None:
         l_varHeader.extend(
             EncodeDecode._encodeValue(random.randint(1, 0xFFFF)))
     else:
         l_varHeader.extend(EncodeDecode._encodeValue(messageId))
     l_payload.extend(EncodeDecode._encodeString(p_topic))
     l_payload.append(requestedQoS)
     l_fixHeader = self._build_fixed_header(0x08,
                                            len(l_varHeader) +
                                            len(l_payload),
                                            qosLevel=1)
     self._send_transport(l_fixHeader, l_varHeader, l_payload)
Example #3
0
 def publish(self,
             p_topic,
             p_message,
             qosLevel=0,
             retain=False,
             dup=False,
             messageId=None):
     # LOG.info("Sending publish packet\n\tTopic: {};\n\tHost: {};".format(p_topic, self.m_broker.Host.Name))
     l_varHeader = bytearray()
     l_payload = bytearray()
     #  Type = publish
     l_varHeader.extend(EncodeDecode._encodeString(p_topic))
     if qosLevel > 0:
         if messageId is not None:
             l_varHeader.extend(EncodeDecode._encodeValue(messageId))
         else:
             l_varHeader.extend(
                 EncodeDecode._encodeValue(random.randint(1, 0xFFFF)))
     l_payload.extend(EncodeDecode._put_string(p_message))
     # l_payload.extend(p_message)
     l_fixHeader = self._build_fixed_header(
         0x03,
         len(l_varHeader) + len(l_payload), dup, qosLevel, retain)
     self._send_transport(l_fixHeader, l_varHeader, l_payload)
Example #4
0
 def _build_connect(self, p_broker, p_mqtt):
     """ Build a Mqtt "Connect" Packet.
     This is the first packet sent after the TCP handshake establishing the network connection.
     This is used to establish the Mqtt Protocol connection and login.
     """
     l_varHeader = bytearray()
     l_payload = bytearray()
     l_varHeader.extend(EncodeDecode._encodeString("MQTT"))
     l_varHeader.append(4)
     varLogin = 0
     if p_broker.UserName is not None:
         varLogin += 2
     if p_broker.Password is not None:
         varLogin += 1
     if p_broker.WillMessage is None or p_broker.WillMessage == '' or p_broker.WillTopic is None:
         #  Clean start, no will message
         l_varHeader.append(varLogin << 6 | 0 << 2 | 1 << 1)
     else:
         l_varHeader.append(varLogin << 6 | p_broker.WillRetain << 5
                            | p_broker.WillQoS << 3 | 1 << 2 | 1 << 1)
     l_varHeader.extend(
         EncodeDecode._encodeValue(int(p_broker.Keepalive / 1000)))
     l_payload.extend(EncodeDecode._encodeString(p_mqtt.ClientID))
     if (p_broker.WillMessage is not None or
             p_broker.WillMessage != '') and p_broker.WillTopic is not None:
         # LOG.debug('Adding last will testiment {}'.format(p_broker.WillMessage + p_broker.WillTopic))
         l_payload.extend(EncodeDecode._encodeString(p_broker.WillTopic))
         l_payload.extend(EncodeDecode._encodeString(p_broker.WillMessage))
     if p_broker.UserName is not None and len(p_broker.UserName) > 0:
         LOG.debug('Adding username "{}"'.format(p_broker.UserName))
         l_payload.extend(EncodeDecode._encodeString(p_broker.UserName))
     if p_broker.Password is not None and len(p_broker.Password) > 0:
         LOG.debug('Adding password "{}"'.format(p_broker.Password))
         l_payload.extend(EncodeDecode._encodeString(p_broker.Password))
     l_fixHeader = self._build_fixed_header(
         0x01,
         len(l_varHeader) + len(l_payload))
     return (l_fixHeader, l_varHeader, l_payload)