def setup_client(device): ''' Sets up IoT client object configuration <type device> dict <desc device> valid device configuration <<type client>> Client object (from AWSIoTPythonSDK MQTT PAHO library) <<desc client>> with set flags, callbacks, and TLS credentials ''' try: client = mqtt.Client(device['client_id']) print('MQTT Client initialized; setting flags...') except Exception as err: print(f"Exception Error: Unable to initialize client object\n{err}") # set flags client.bad_connection_flag = False client.bad_auth_flag = False client.connected_flag = False client.disconnected_flag = False print('binding callbacks...') try: client.on_connect = onConnect client.on_disconnect = onDisconnect ''' client.on_message = onMessage client.on_publish = onPublish client.on_subscribe = onSubscribe client.on_unsubscribe = onUnsubscribe client.on_log = onLog ''' print('callbacks bound.') except Exception as err: print(f"Exception Error: error binding callbacks to client\n{err}") print('setting TLS credentials...') try: client.tls_set(ca_certs=device['ca_file'], certfile=device['cert_file'], keyfile=device['key_file'], cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_SSLv23, ciphers=None) print('TLS credentials set successfully') except Exception as err: print(f"Exception Error: error setting tls credentials\n{err}") return client
def get_iot_client(device): ''' Setup and config AWS IoT client <type device> dict <desc device> device configuration values <<type client>> AWS IoT Client Object <<desc client>> none ''' try: client = mqtt.Client(device['clientId']) client.bad_connection_flag = False client.bad_auth_flag = False client.connected_flag = False client.disconnected_flag = False client.on_connect = onConnect client.on_disconnect = onDisconnect client.on_message = onMessage ''' client.on_publish = onPublish client.on_subscribe = onSubscribe client.on_unsubscribe = onUnsubscribe client.on_log = onLog ''' except TypeError as typ_err: print(f"Type Error: {typ_err}") except Exception as exc_err: print(f"Error: binding callbacks failure\n{exc_err}") else: try: client.tls_set(ca_certs=device['caFile'], certfile=device['certFile'], keyfile=device['keyFile'], cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_SSLv23, ciphers=None) except Exception as exc_err: print(f"Error: setting TLS credentials failure\n{exc_err}") sys.exit() else: return client
def _create_paho_client(self, client_id, clean_session, user_data, protocol, use_wss): self._logger.debug("Initializing MQTT layer...") return mqtt.Client(client_id, clean_session, user_data, protocol, use_wss)
def createPahoClient(self, clientID, cleanSession, userdata, protocol, useWebsocket): return mqtt.Client(clientID, cleanSession, userdata, protocol, useWebsocket) # Throw exception when error happens