def publishArrived(self, topic, msg, qos, properties, receivedTime, retained=False): pub = MQTTV5.Publishes() if properties: pub.properties = properties logger.info("[MQTT-3.2.3-3] topic name must match the subscription's topic filter") # Topic alias if len(self.outgoingTopicNamesToAliases) < self.topicAliasMaximum: self.outgoingTopicNamesToAliases.append(topic) # add alias if topic in self.outgoingTopicNamesToAliases: pub.properties.TopicAlias = self.outgoingTopicNamesToAliases.index(topic) + 1 # Topic aliases start at 1 else: pub.topicName = topic pub.data = msg pub.fh.QoS = qos pub.fh.RETAIN = retained pub.receivedTime = receivedTime if retained: logger.info("[MQTT-2.1.2-7] Last retained message on matching topics sent on subscribe") if pub.fh.RETAIN: logger.info("[MQTT-2.1.2-9] Set retained flag on retained messages") if qos == 2: pub.qos2state = "PUBREC" if len(self.outbound) >= self.receiveMaximum or not self.connected: if qos > 0 or not self.broker.dropQoS0: self.queued.append(pub) # this should never be infinite in reality if qos > 0 and not self.connected: logger.info("[MQTT-3.1.2-5] storing of QoS 1 and 2 messages for disconnected client %s", self.id) else: self.sendFirst(pub)
def publish(self, topic, payload, qos=0, retained=False, properties=None): publish = MQTTV5.Publishes() publish.fh.QoS = qos publish.fh.RETAIN = retained if qos == 0: publish.packetIdentifier = 0 else: publish.packetIdentifier = self.__nextMsgid() if publish.fh.QoS == 2: pass #publish.pubrec_received = False self.__receiver.outMsgs[publish.packetIdentifier] = publish publish.topicName = topic if properties: publish.properties = properties publish.data = payload if type(payload) == type(b"") else bytes(payload, "utf8") sendtosocket(self.sock, publish.pack()) return publish.packetIdentifier