How to use the gmqtt.mqtt.package.PackageFactory function in gmqtt

To help you get started, we’ve selected a few gmqtt examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wialon / gmqtt / gmqtt / mqtt / package.py View on Github external
if will_message:
            packet += will_prop_bytes
            cls._pack_str16(packet, will_message.topic)
            cls._pack_str16(packet, will_message.payload)

        if username is not None:
            cls._pack_str16(packet, username)

            if password is not None:
                cls._pack_str16(packet, password)

        return packet


class UnsubscribePacket(PackageFactory):
    @classmethod
    def build_package(cls, topic, protocol, **kwargs) -> Tuple[int, bytes]:
        remaining_length = 2
        if not isinstance(topic, (list, tuple)):
            topics = [topic]
        else:
            topics = topic

        for t in topics:
            remaining_length += 2 + len(t)

        properties = cls._build_properties_data(kwargs, protocol.proto_ver)
        remaining_length += len(properties)

        command = MQTTCommands.UNSUBSCRIBE | 0x2
        packet = bytearray()
github wialon / gmqtt / gmqtt / mqtt / package.py View on Github external
if protocol_version < MQTTv50:
            return bytearray()
        data = bytearray()
        for property_name, property_value in properties_dict.items():
            property = Property.factory(name=property_name)
            if property is None:
                logger.warning('[GMQTT] property {} is not supported, it was ignored'.format(property_name))
                continue
            property_bytes = property.dumps(property_value)
            data.extend(property_bytes)
        result = pack_variable_byte_integer(len(data))
        result.extend(data)
        return result


class LoginPackageFactor(PackageFactory):
    @classmethod
    def build_package(cls, client_id, username, password, clean_session, keepalive, protocol, will_message=None, **kwargs):
        remaining_length = 2 + len(protocol.proto_name) + 1 + 1 + 2 + 2 + len(client_id)

        connect_flags = 0
        if clean_session:
            connect_flags |= 0x02

        if will_message:
            will_prop_bytes = cls._build_properties_data(will_message.properties, protocol.proto_ver)
            remaining_length += 2 + len(will_message.topic) + 2 + len(will_message.payload) + len(will_prop_bytes)
            connect_flags |= 0x04 | ((will_message.qos & 0x03) << 3) | ((will_message.retain & 0x01) << 5)

        if username is not None:
            remaining_length += 2 + len(username)
            connect_flags |= 0x80
github wialon / gmqtt / gmqtt / mqtt / package.py View on Github external
command = MQTTCommands.UNSUBSCRIBE | 0x2
        packet = bytearray()
        packet.append(command)
        packet.extend(pack_variable_byte_integer(remaining_length))
        local_mid = cls.id_generator.next_id()
        packet.extend(struct.pack("!H", local_mid))
        packet.extend(properties)
        for t in topics:
            cls._pack_str16(packet, t)

        logger.info('[SEND UNSUB] %s', topics)

        return local_mid, packet


class SubscribePacket(PackageFactory):
    @classmethod
    def build_package(cls, subscription, protocol, **kwargs) -> Tuple[int, bytes]:
        remaining_length = 2
        if not isinstance(subscription, (list, tuple)):
            subscriptions = [subscription]
        else:
            subscriptions = subscription

        topics = []
        for s in subscriptions:
            remaining_length += 2 + len(s.topic) + 1
            topics.append(s.topic)
        properties = cls._build_properties_data(kwargs, protocol.proto_ver)
        remaining_length += len(properties)

        command = MQTTCommands.SUBSCRIBE | (False << 3) | 0x2
github wialon / gmqtt / gmqtt / mqtt / package.py View on Github external
return mid, packet


class DisconnectPacket(PackageFactory):
    @classmethod
    def build_package(cls, protocol, reason_code=0, **properties):
        if protocol.proto_ver == MQTTv50:
            prop_bytes = cls._build_properties_data(properties, protocol_version=protocol.proto_ver)
            remaining_length = 1 + len(prop_bytes)
            return struct.pack('!BBB', MQTTCommands.DISCONNECT.value, remaining_length, reason_code) + prop_bytes
        else:
            return struct.pack('!BB', MQTTCommands.DISCONNECT.value, 0)


class CommandWithMidPacket(PackageFactory):

    @classmethod
    def build_package(cls, cmd, mid, dup, reason_code=0, proto_ver=MQTTv50) -> bytes:
        if dup:
            cmd |= 0x8
        if proto_ver == MQTTv50:
            remaining_length = 4
            packet = struct.pack('!BBHBB', cmd, remaining_length, mid, reason_code, 0)
        else:
            remaining_length = 2
            packet = struct.pack('!BBH', cmd, remaining_length, mid)
        return packet
github wialon / gmqtt / gmqtt / mqtt / package.py View on Github external
packet.append(command)
        packet.extend(pack_variable_byte_integer(remaining_length))
        local_mid = cls.id_generator.next_id()
        packet.extend(struct.pack("!H", local_mid))
        packet.extend(properties)
        for s in subscriptions:
            cls._pack_str16(packet, s.topic)
            subscribe_options = s.retain_handling_options << 4 | s.retain_as_published << 3 | s.no_local << 2 | s.qos
            packet.append(subscribe_options)

        logger.info('[SEND SUB] %s %s', local_mid, topics)

        return local_mid, packet


class SimpleCommandPacket(PackageFactory):
    @classmethod
    def build_package(cls, command) -> bytes:
        return struct.pack('!BB', command, 0)


class PublishPacket(PackageFactory):
    @classmethod
    def build_package(cls, message, protocol) -> Tuple[int, bytes]:
        command = MQTTCommands.PUBLISH | ((message.dup & 0x1) << 3) | (message.qos << 1) | (message.retain & 0x1)

        packet = bytearray()
        packet.append(command)

        remaining_length = 2 + len(message.topic) + message.payload_size
        prop_bytes = cls._build_properties_data(message.properties, protocol_version=protocol.proto_ver)
        remaining_length += len(prop_bytes)
github Sovetnikov / fbns_mqtt / src / fbns_mqtt.py View on Github external
def __init__(self, data):
        self.token = _spop(data, 'token')
        self.connectionKey = _spop(data, 'ck')
        self.packageName = _spop(data, 'pn')
        self.collapseKey = _spop(data, 'cp')
        self.payload = _spop(data, 'fbpushnotif')
        self.notificationId = _spop(data, 'nid')
        self.isBuffered = _spop(data, 'bu')
        self.viewId = _spop(data, 'view_id')
        self.numEndpoints = _spop(data, 'num_endpoints')

        if data:
            raise Exception('FBNSPush unexpected data: {data}'.format(**locals))


class FBNSConnectPackageFactor(PackageFactory):
    @classmethod
    def build_package(cls, fbns_auth: FBNSAuth, clean_session, keepalive, protocol, will_message=None, **kwargs):
        keepalive = 900

        connect_payload = thrift.Connect()
        connect_payload.clientIdentifier = fbns_auth.clientId

        client_info = thrift.ClientInfo()
        client_info.userId = fbns_auth.userId
        client_info.userAgent = '[FBAN/MQTT;FBAV/64.0.0.14.96;FBBV/125398467;FBDM/{density=4.0,width=1440,height=2392};FBLC/en_US;FBCR/;FBMF/LGE;FBBD/lge;FBPN/com.instagram.android;FBDV/RS988;FBSV/6.0.1;FBLR/0;FBBK/1;FBCA/armeabi-v7a:armeabi;]'
        client_info.clientCapabilities = 439
        client_info.endpointCapabilities = 128
        client_info.publishFormat = 1
        client_info.noAutomaticForeground = True
        client_info.makeUserAvailableInForeground = False
        client_info.deviceId = fbns_auth.deviceId
github wialon / gmqtt / gmqtt / mqtt / package.py View on Github external
cls._pack_str16(packet, s.topic)
            subscribe_options = s.retain_handling_options << 4 | s.retain_as_published << 3 | s.no_local << 2 | s.qos
            packet.append(subscribe_options)

        logger.info('[SEND SUB] %s %s', local_mid, topics)

        return local_mid, packet


class SimpleCommandPacket(PackageFactory):
    @classmethod
    def build_package(cls, command) -> bytes:
        return struct.pack('!BB', command, 0)


class PublishPacket(PackageFactory):
    @classmethod
    def build_package(cls, message, protocol) -> Tuple[int, bytes]:
        command = MQTTCommands.PUBLISH | ((message.dup & 0x1) << 3) | (message.qos << 1) | (message.retain & 0x1)

        packet = bytearray()
        packet.append(command)

        remaining_length = 2 + len(message.topic) + message.payload_size
        prop_bytes = cls._build_properties_data(message.properties, protocol_version=protocol.proto_ver)
        remaining_length += len(prop_bytes)

        if message.payload_size == 0:
            logger.debug("Sending PUBLISH (q%d), '%s' (NULL payload)", message.qos, message.topic)
        else:
            logger.debug("Sending PUBLISH (q%d), '%s', ... (%d bytes)", message.qos, message.topic, message.payload_size)
github wialon / gmqtt / gmqtt / mqtt / package.py View on Github external
cls._pack_str16(packet, message.topic)

        if message.qos > 0:
            # For message id
            mid = cls.id_generator.next_id()
            packet.extend(struct.pack("!H", mid))
        else:
            mid = None
        packet.extend(prop_bytes)

        packet.extend(message.payload)

        return mid, packet


class DisconnectPacket(PackageFactory):
    @classmethod
    def build_package(cls, protocol, reason_code=0, **properties):
        if protocol.proto_ver == MQTTv50:
            prop_bytes = cls._build_properties_data(properties, protocol_version=protocol.proto_ver)
            remaining_length = 1 + len(prop_bytes)
            return struct.pack('!BBB', MQTTCommands.DISCONNECT.value, remaining_length, reason_code) + prop_bytes
        else:
            return struct.pack('!BB', MQTTCommands.DISCONNECT.value, 0)


class CommandWithMidPacket(PackageFactory):

    @classmethod
    def build_package(cls, cmd, mid, dup, reason_code=0, proto_ver=MQTTv50) -> bytes:
        if dup:
            cmd |= 0x8