Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def connect(self, host, port=1883, ssl=False, keepalive=60, version=MQTTv50, raise_exc=True):
# Init connection
self._host = host
self._port = port
self._ssl = ssl
self._keepalive = keepalive
self._is_active = True
MQTTProtocol.proto_ver = version
self._connection = await self._create_connection(
host, port=self._port, ssl=self._ssl, clean_session=self._clean_session, keepalive=keepalive)
await self._connection.auth(self._client_id, self._username, self._password, will_message=self._will_message,
**self._connect_properties)
await self._connected.wait()
def _build_properties_data(cls, properties_dict, protocol_version):
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
def protocol_version(self):
return self._connection._protocol.proto_ver \
if self._connection is not None else MQTTv50
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
def _handle_connack_packet(self, cmd, packet):
self._connected.set()
(flags, result) = struct.unpack("!BB", packet[:2])
if result != 0:
logger.warning('[CONNACK] %s', hex(result))
self.failed_connections += 1
if result == 1 and self.protocol_version == MQTTv50:
logger.info('[CONNACK] Downgrading to MQTT 3.1 protocol version')
MQTTProtocol.proto_ver = MQTTv311
future = asyncio.ensure_future(self.reconnect(delay=True))
future.add_done_callback(self._handle_exception_in_future)
return
else:
self._error = MQTTConnectError(result)
asyncio.ensure_future(self.reconnect(delay=True))
return
else:
self.failed_connections = 0
if len(packet) > 2:
properties, _ = self._parse_properties(packet[2:])
if properties is None:
self._error = MQTTConnectError(10)