|
| 1 | +""" |
| 2 | +Example script for connecting a UDMI device to Cloud IoT Core. |
| 3 | +
|
| 4 | +This script demonstrates how to use the `create_device_with_jwt` factory |
| 5 | +to instantiate a device, configure it with the necessary GCP IoT Core |
| 6 | +credentials (project, registry, device), and run its main loop. |
| 7 | +
|
| 8 | +The device will authenticate using a JWT generated from an RS256 private key. |
| 9 | +""" |
| 10 | + |
| 11 | +import logging |
| 12 | +import sys |
| 13 | + |
| 14 | +from udmi.core.factory import JwtAuthArgs |
| 15 | +from udmi.core.factory import create_device_with_jwt |
| 16 | +from udmi.schema import EndpointConfiguration |
| 17 | + |
| 18 | +# --- Configuration Constants --- |
| 19 | +# CONFIGURE THESE VALUES to match your GCP IoT Core setup |
| 20 | +PROJECT_ID = "gcp-project-id" |
| 21 | +REGION = "us-central1" |
| 22 | +REGISTRY_ID = "ZZ-TRI-FECTA" |
| 23 | +DEVICE_ID = "AHU-1" |
| 24 | +MQTT_HOST = "mqtt.bos.goog" |
| 25 | +MQTT_PORT = 8883 # secure port, mqtt client will automatically use TLS |
| 26 | +ALGORITHM = "RS256" # Algorithm for signing the JWT |
| 27 | +PRIVATE_KEY_FILE = "/path/to/ZZ-TRI-FECTA/devices/AHU-1/rsa_private.pem" |
| 28 | + |
| 29 | +# The full client ID string required by Cloud IoT Core's MQTT bridge |
| 30 | +CLIENT_ID = ( |
| 31 | + f"projects/{PROJECT_ID}/locations/{REGION}/" |
| 32 | + f"registries/{REGISTRY_ID}/devices/{DEVICE_ID}" |
| 33 | +) |
| 34 | + |
| 35 | +if __name__ == "__main__": |
| 36 | + # Set up basic logging to see device activity in the console |
| 37 | + logging.basicConfig(level=logging.DEBUG, |
| 38 | + format='%(asctime)s - %(levelname)s - %(message)s') |
| 39 | + |
| 40 | + # Main execution block with error handling |
| 41 | + try: |
| 42 | + # 1. Create the UDMI EndpointConfiguration object |
| 43 | + # This tells the client where to connect |
| 44 | + endpoint_config = EndpointConfiguration( |
| 45 | + client_id=CLIENT_ID, |
| 46 | + hostname=MQTT_HOST, |
| 47 | + port=MQTT_PORT |
| 48 | + ) |
| 49 | + logging.info("Creating device instance using the JWT factory...") |
| 50 | + |
| 51 | + # 2. Use the factory to create the device instance. |
| 52 | + # This factory wires up all the necessary components: |
| 53 | + # - MqttMessagingClient: Handles the Paho MQTT connection |
| 54 | + # - JwtAuthProvider: Generates new JWTs when needed |
| 55 | + # - MessageDispatcher: Routes MQTT messages to/from the device |
| 56 | + # - Device: The main orchestrator |
| 57 | + # - SystemManager: The default manager for state/config |
| 58 | + device = create_device_with_jwt( |
| 59 | + endpoint_config=endpoint_config, |
| 60 | + jwt_auth_args=JwtAuthArgs( |
| 61 | + project_id=PROJECT_ID, |
| 62 | + key_file=PRIVATE_KEY_FILE, |
| 63 | + algorithm=ALGORITHM |
| 64 | + ) |
| 65 | + ) |
| 66 | + |
| 67 | + # 3. Start the device's main loop. |
| 68 | + # This will: |
| 69 | + # a. Connect to the MQTT bridge |
| 70 | + # b. Handle the JWT authentication |
| 71 | + # c. Subscribe to config/command topics |
| 72 | + # d. Publish the initial state |
| 73 | + # e. Enter the continuous run loop |
| 74 | + device.run() |
| 75 | + except FileNotFoundError: |
| 76 | + # Add a specific error for this common configuration issue |
| 77 | + logging.error( |
| 78 | + f"Critical Error: Private key file not found at {PRIVATE_KEY_FILE}") |
| 79 | + logging.error( |
| 80 | + "Please update the PRIVATE_KEY_FILE constant in this script.") |
| 81 | + sys.exit(1) |
| 82 | + except Exception as e: |
| 83 | + # Catch-all for other critical errors (e.g., network, auth failures) |
| 84 | + logging.error(f"A critical error occurred: {e}", exc_info=True) |
| 85 | + sys.exit(1) |
| 86 | + |
| 87 | + logging.info("Device shut down gracefully. Exiting.") |
0 commit comments