in src/main.py [0:0]
def __init__(self, ggv2_component_config):
'''
Initialises the AWS Greengrass V2 custom component including the IPC and MQTT PubSub Client
Greengrass Config expected to be passed from AWS Greengrass V2 deployment recipe.
'''
try:
#######################################################
# Log the start of the process
log.info('Initialising AWS Greengrass V2 BLE Gateway Component')
super().__init__()
#######################################################
# Parse config from AWS Greengrass component recipe
log.info('Parsing AWS Greengrass V2 Config.')
log.info('AWS Greengrass V2 Config: {}'.format(ggv2_component_config))
self.ipc_pubsub_timeout = ggv2_component_config['ipc_pubsub_timeout']
self.mqtt_pubsub_timeout = ggv2_component_config['mqtt_pubsub_timeout']
# Completed processing recipe driven config for the Greengrass application.
log.info('Parsing AWS Greengrass V2 Config Complete.')
#######################################################
# Initilise BLE control and data MQTT/IPC topics
log.info('Initilising BLE control MQTT/IPC topics.')
self.thing_name = USER = os.getenv('AWS_IOT_THING_NAME')
self.pubsub_thing_topic = 'aws-greengrass/things/{}/ble'.format(self.thing_name)
self.pubsub_control_topic = '{}/control'.format(self.pubsub_thing_topic)
self.pubsub_data_topic = '{}/data'.format(self.pubsub_thing_topic)
self.pubsub_ble_state_topic = '{}/state'.format(self.pubsub_thing_topic)
self.pubsub_error_topic = '{}/error'.format(self.pubsub_thing_topic)
# Rx / TX topics for proxying BLE <-> IPC / MQTT.
# Uses cloud perspective. i.e: TX is cloud >-tx-> to BLE. RX is cloud <-rx-< from BLE
self.pubsub_data_rx_topic = '{}/rx'.format(self.pubsub_data_topic)
self.pubsub_data_tx_topic = '{}/tx'.format(self.pubsub_data_topic)
# For each supported control message, create a /command and command/response topic
self.ble_connect_topic = '{}/connect'.format(self.pubsub_control_topic)
self.ble_connect_response_topic = '{}/response'.format(self.ble_connect_topic)
self.ble_disconnect_topic = '{}/disconnect'.format(self.pubsub_control_topic)
self.ble_disconnect_response_topic = '{}/response'.format(self.ble_disconnect_topic)
self.ble_list_topic = '{}/list'.format(self.pubsub_control_topic)
self.ble_list_response_topic = '{}/response'.format(self.ble_list_topic)
self.ble_scan_topic = '{}/scan'.format(self.pubsub_control_topic)
self.ble_scan_response_topic = '{}/response'.format(self.ble_scan_topic)
self.ble_control_pub_topics = [self.ble_connect_response_topic, self.ble_disconnect_response_topic, self.ble_list_response_topic, self.ble_scan_response_topic, self.pubsub_ble_state_topic, self.pubsub_error_topic]
self.ble_control_sub_topics = [self.ble_connect_topic, self.ble_disconnect_topic, self.ble_list_topic, self.ble_scan_topic]
log.info('BLE Control Publish Topics: {}'.format(self.ble_control_pub_topics))
log.info('BLE Control Subscribe Topics: {}'.format(self.ble_control_sub_topics))
log.info('Initilising BLE control MQTT/IPC topics complete.')
#######################################################
# Initilise internal BLE proxy topic to pass BLE messages into the message routing / processing methods
# Note: Don't expose this externally, is ony for internal BLE to PubSub proxy identification.
self.ble_proxy_topic = '$aws/greengrass/ble/do-proxy/'
#######################################################
# Init local Topic and MQTT IoT Core PubSub message service.
log.info('Initialising IPC Topic PubSub inter-service messaging.')
self.ipc_topic_pubsub = IpcTopicPubSub(self.receive_message_router, self.ble_control_sub_topics, self.ipc_pubsub_timeout)
log.info('Initialising IPC MQTT IoT Core PubSub messaging.')
self.mqtt_core_pubsub = MqttCorePubSub(self.receive_message_router, self.ble_control_sub_topics, self.mqtt_pubsub_timeout)
#######################################################
# Init the Bluetooth Low Energy (BLE) message controller
log.info('Initialising Bluetooth BLE Discovery and Control service')
self.ble_scanner = BleScanner()
self.ble_controller = BleUartController(self.receive_message_router, self.ble_state_change_callback)
log.info('Initialising Bluetooth BLE Discovery and Control service Complete')
log.info('Initialising AWS Greengrass V2 BLE Gateway Component complete!')
except ValueError as val_error: # pragma: no cover # includes JSON parsing errors
msg = 'VAL_ERROR: {} - GREENGRASS CONFIG: {}'.format(val_error, ggv2_component_config)
log.error(msg)
self.publish_error(500, msg)
except KeyError as key_error: # pragma: no cover # includes requests for fields that don't exist in the received config
msg = 'KEY_ERROR: {} - GREENGRASS CONFIG: {}'.format(key_error, ggv2_component_config)
log.error(msg)
self.publish_error(500, msg)
except Exception as err: # pragma: no cover
msg = 'EXCEPTION: {} - GREENGRASS CONFIG: {}'.format(err, ggv2_component_config)
log.error(msg)
self.publish_error(500, msg)