def build_publish_pkt(self, mid, topic, payload, qos, retain, dup): """Build PUBLISH packet.""" pkt = MqttPkt() payloadlen = len(payload) packetlen = 2 + len(topic) + payloadlen if qos > 0: packetlen += 2 pkt.mid = mid pkt.command = NC.CMD_PUBLISH | ((dup & 0x1) << 3) | (qos << 1) | retain pkt.remaining_length = packetlen ret = pkt.alloc() if ret != NC.ERR_SUCCESS: return ret, None #variable header : Topic String pkt.write_string(topic) if qos > 0: pkt.write_uint16(mid) #payloadlen if payloadlen > 0: pkt.write_bytes(payload, payloadlen) return NC.ERR_SUCCESS, pkt
def build_publish_pkt(self, mid, topic, payload, qos, retain, dup): """Build PUBLISH packet.""" pkt = MqttPkt() payloadlen = len(payload) packetlen = 2 + len(topic) + payloadlen if qos > 0: packetlen += 2 pkt.mid = mid pkt.command = NC.CMD_PUBLISH | ((dup & 0x1) << 3) | (qos << 1) | retain pkt.remaining_length = packetlen rc = pkt.alloc() if rc != NC.ERR_SUCCESS: return rc, None #variable header : Topic String pkt.write_string(topic, len(topic)) if qos > 0: pkt.write_uint16(mid) #payloadlen if payloadlen > 0: pkt.write_bytes(payload, payloadlen) return NC.ERR_SUCCESS, pkt
def send_simple_command(self, cmd): pkt = MqttPkt() pkt.command = cmd pkt.remaining_length = 0 rc = pkt.alloc() if rc != NC.ERR_SUCCESS: return rc return self.packet_queue(pkt)
def send_simple_command(self, cmd): """Send simple mqtt commands.""" pkt = MqttPkt() pkt.command = cmd pkt.remaining_length = 0 ret = pkt.alloc() if ret != NC.ERR_SUCCESS: return ret return self.packet_queue(pkt)
def pubcomp(self, mid): """Send PUBCOMP response to server.""" if self.sock == NC.INVALID_SOCKET: return NC.ERR_NO_CONN self.logger.info("Send PUBCOMP (msgid=%s)", mid) pkt = MqttPkt() pkt.command = NC.CMD_PUBCOMP pkt.remaining_length = 2 ret = pkt.alloc() if ret != NC.ERR_SUCCESS: return ret #variable header: acknowledged message id pkt.write_uint16(mid) return self.packet_queue(pkt)
def send_unsubscribe(self, dup, topic): """Send unsubscribe COMMAND to server.""" pkt = MqttPkt() pktlen = 2 + 2 + len(topic) pkt.command = NC.CMD_UNSUBSCRIBE | (dup << 3) | (1 << 1) pkt.remaining_length = pktlen ret = pkt.alloc() if ret != NC.ERR_SUCCESS: return ret #variable header mid = self.mid_generate() pkt.write_uint16(mid) #payload pkt.write_string(topic) return self.packet_queue(pkt)