def provision_new_device_blocking(registration_id=None): """provision a new device This is a blocking function, called from the async wrapper below. azure.iot.device has an aio API, but it's proven to fail with fairly low concurrency Instead, we call this function in a background process To avoid resource issues, we spawn a new process for every call! """ from azure.iot.device import ProvisioningDeviceClient # TODO: allow requesting keys for an existing id ?! if registration_id is None: registration_id = new_device_id() device_key = derive_device_key(registration_id, group_symmetric_key) provisioning_device_client = ProvisioningDeviceClient.create_from_symmetric_key( provisioning_host=provisioning_host, registration_id=registration_id, id_scope=id_scope, symmetric_key=device_key, ) result = provisioning_device_client.register() if result.status == "assigned": return (result.registration_state, device_key) else: raise ValueError("registration status was %s" % result.status)
def register_device(registration_id, derived_device_key): provisioning_device_client = ProvisioningDeviceClient.create_from_symmetric_key( provisioning_host=provisioning_host, registration_id=registration_id, id_scope=id_scope, symmetric_key=derived_device_key, ) return provisioning_device_client.register()
def result_from_register(registration_id, symmetric_key): provisioning_device_client = ProvisioningDeviceClient.create_from_symmetric_key( provisioning_host=PROVISIONING_HOST, registration_id=registration_id, id_scope=ID_SCOPE, symmetric_key=symmetric_key, ) return provisioning_device_client.register()
def result_from_register(registration_id, symmetric_key, protocol): protocol_boolean_mapping = {"mqtt": False, "mqttws": True} provisioning_device_client = ProvisioningDeviceClient.create_from_symmetric_key( provisioning_host=PROVISIONING_HOST, registration_id=registration_id, id_scope=ID_SCOPE, symmetric_key=symmetric_key, websockets=protocol_boolean_mapping[protocol], ) return provisioning_device_client.register()
class Wizard(object): def __init__(self, first_name, last_name, dict_of_stuff): self.first_name = first_name self.last_name = last_name self.props = dict_of_stuff provisioning_host = os.getenv("PROVISIONING_HOST") id_scope = os.getenv("PROVISIONING_IDSCOPE") registration_id = os.getenv("PROVISIONING_REGISTRATION_ID") symmetric_key = os.getenv("PROVISIONING_SYMMETRIC_KEY") provisioning_device_client = ProvisioningDeviceClient.create_from_symmetric_key( provisioning_host=provisioning_host, registration_id=registration_id, id_scope=id_scope, symmetric_key=symmetric_key, ) properties = {"House": "Gryffindor", "Muggle-Born": "False"} wizard_a = Wizard("Harry", "Potter", properties) provisioning_device_client.provisioning_payload = wizard_a registration_result = provisioning_device_client.register() # The result can be directly printed to view the important details. print(registration_result) # Individual attributes can be seen as well print("The status was :-") print(registration_result.status) print("The etag is :-")