def iothub_registrymanager_sample_run(): try: # RegistryManager iothub_registry_manager = IoTHubRegistryManager(CONNECTION_STRING) # CreateDevice primary_key = "aaabbbcccdddeeefffggghhhiiijjjkkklllmmmnnnoo" secondary_key = "111222333444555666777888999000aaabbbcccdddee" auth_method = IoTHubRegistryManagerAuthMethod.SHARED_PRIVATE_KEY new_device = iothub_registry_manager.create_device(DEVICE_ID, primary_key, secondary_key, auth_method) print_device_info("CreateDevice", new_device) # GetDevice iothub_device = iothub_registry_manager.get_device(DEVICE_ID) print_device_info("GetDevice", iothub_device) # UpdateDevice primary_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" secondary_key = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" status = IoTHubDeviceStatus.DISABLED auth_method = IoTHubRegistryManagerAuthMethod.SHARED_PRIVATE_KEY iothub_registry_manager.update_device(DEVICE_ID, primary_key, secondary_key, status, auth_method) updated_device = iothub_registry_manager.get_device(DEVICE_ID) print_device_info("UpdateDevice", updated_device) # DeleteDevice print ( "DeleteDevice" ) iothub_registry_manager.delete_device(DEVICE_ID) print ( "" ) # GetDeviceList print ( "GetDeviceList" ) number_of_devices = 3 dev_list = iothub_registry_manager.get_device_list(number_of_devices) number_of_devices = len(dev_list) print ( "Number of devices : {0}".format(number_of_devices) ) for device in range(0, number_of_devices): title = "Device " + str(device) print_device_info(title, dev_list[device]) print ( "" ) # GetStatistics iothub_registry_statistics = iothub_registry_manager.get_statistics() print ( "GetStatistics" ) print ( "Total device count : {0}".format(iothub_registry_statistics.totalDeviceCount) ) print ( "Enabled device count : {0}".format(iothub_registry_statistics.enabledDeviceCount) ) print ( "Disabled device count : {0}".format(iothub_registry_statistics.disabledDeviceCount) ) print ( "" ) except IoTHubError as iothub_error: print ( "Unexpected error {0}".format(iothub_error) ) return except KeyboardInterrupt: print ( "IoTHubRegistryManager sample stopped" )
def iothub_registrymanager_sample_run(): try: # RegistryManager iothub_registry_manager = IoTHubRegistryManager(CONNECTION_STRING) # CreateDevice primary_key = "aaabbbcccdddeeefffggghhhiiijjjkkklllmmmn3344" secondary_key = "111222333444555666777888999000aaabbbcccdddee" auth_method = IoTHubRegistryManagerAuthMethod.SHARED_PRIVATE_KEY print("auth_method", auth_method) new_device = iothub_registry_manager.create_device( DEVICE_ID, primary_key, secondary_key, auth_method) print(new_device) print_device_info("CreateDevice", new_device) # GetDevice iothub_device = iothub_registry_manager.get_device(DEVICE_ID) print_device_info("GetDevice", iothub_device) # UpdateDevice #primary_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" #secondary_key = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" #status = IoTHubDeviceStatus.DISABLED #auth_method = IoTHubRegistryManagerAuthMethod.SHARED_PRIVATE_KEY #iothub_registry_manager.update_device(DEVICE_ID, primary_key, secondary_key, status, auth_method) #updated_device = iothub_registry_manager.get_device(DEVICE_ID) #print_device_info("UpdateDevice", updated_device) # DeleteDevice #print ( "DeleteDevice" ) #iothub_registry_manager.delete_device(DEVICE_ID) #print ( "" ) # GetDeviceList print("GetDeviceList") #number_of_devices = 3 dev_list = iothub_registry_manager.get_device_list(number_of_devices) number_of_devices = len(dev_list) print("Number of devices : {0}".format( number_of_devices)) for device in range(0, number_of_devices): title = "Device " + str(device) print_device_info(title, dev_list[device]) print("") # GetStatistics #iothub_registry_statistics = iothub_registry_manager.get_statistics() #print ( "GetStatistics" ) #print ( "Total device count : {0}".format(iothub_registry_statistics.totalDeviceCount) ) #print ( "Enabled device count : {0}".format(iothub_registry_statistics.enabledDeviceCount) ) #print ( "Disabled device count : {0}".format(iothub_registry_statistics.disabledDeviceCount) ) #print ( "" ) except IoTHubError as iothub_error: print("Unexpected error {0}".format(iothub_error)) return except KeyboardInterrupt: print("IoTHubRegistryManager sample stopped")
def get_device(device_id): try: print "hello" device_id = str(device_id) CONNECTION_STRING = "HostName=ttpliot.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=xBmNYVmuqN9sVZYQprYKvpv/2kv9GX1CdiSb5W+5KBc=" iothub_registry_manager = IoTHubRegistryManager(CONNECTION_STRING) iothub_device = iothub_registry_manager.get_device(device_id) print "iothub_device.deviceId", iothub_device.deviceId return iothub_device.deviceId except IoTHubError as iothub_error: print("Unexpected error {0}".format(iothub_error)) return None
def run_e2e_registrymanager(iothub_connection_string): try: # prepare device_id = generate_device_name() assert isinstance(device_id, str), 'Invalid type returned!' ########################################################################### # IoTHubRegistryManager # prepare # act iothub_registry_manager = IoTHubRegistryManager(iothub_connection_string) # verify assert isinstance(iothub_registry_manager, IoTHubRegistryManager), 'Invalid type returned!' assert iothub_registry_manager != None, "iothub_registry_manager is NULL" ########################################################################### print ( "IoTHubRegistryManager is created successfully" ) ########################################################################### # create_device # prepare primary_key = "" secondary_key = "" auth_method = IoTHubRegistryManagerAuthMethod.SHARED_PRIVATE_KEY # act new_device = iothub_registry_manager.create_device(device_id, primary_key, secondary_key, auth_method) # verify assert isinstance(new_device, IoTHubDevice), 'Invalid type returned!' assert new_device != None, "new_device is NULL" assert new_device.primaryKey != None, "new_device.primaryKey is NULL" assert new_device.primaryKey != "", "new_device.primaryKey is empty" assert new_device.secondaryKey != None, "new_device.secondaryKey is NULL" assert new_device.secondaryKey != "", "new_device.secondaryKey is empty" ########################################################################### print_device_info("CreateDevice", new_device) ########################################################################### # get_device # prepare # act iothub_device = iothub_registry_manager.get_device(device_id) # verify assert isinstance(iothub_device, IoTHubDevice), 'Invalid type returned!' assert iothub_device != None, "iothub_device is NULL" assert iothub_device.primaryKey != None, "iothub_device.primaryKey is NULL" assert iothub_device.primaryKey != "", "iothub_device.primaryKey is empty" assert iothub_device.secondaryKey != None, "iothub_device.secondaryKey is NULL" assert iothub_device.secondaryKey != "", "iothub_device.secondaryKey is empty" ########################################################################### print_device_info("GetDevice", iothub_device) ########################################################################### # update_device # prepare primary_key = ''.join([random.choice(string.ascii_letters + string.digits) for n in range(44)]) secondary_key = ''.join([random.choice(string.ascii_letters + string.digits) for n in range(44)]) status = IoTHubDeviceStatus.DISABLED auth_method = IoTHubRegistryManagerAuthMethod.SHARED_PRIVATE_KEY # act iothub_registry_manager.update_device(device_id, primary_key, secondary_key, status, auth_method) updated_device = iothub_registry_manager.get_device(device_id) # verify assert isinstance(updated_device, IoTHubDevice), 'Invalid type returned!' assert updated_device != None, "updated_device is NULL" assert updated_device.primaryKey == primary_key, "updated_device.primaryKey is not updated" assert updated_device.secondaryKey == secondary_key, "updated_device.secondaryKey is not updated" assert updated_device.authMethod == auth_method, "updated_device.authMethod is not updated" assert updated_device.status == status, "updated_device.status is not updated" ########################################################################### print_device_info("UpdateDevice", updated_device) ########################################################################### # get_device_list # prepare req_number_of_devices = 10 # act device_list = iothub_registry_manager.get_device_list(req_number_of_devices) # verify assert device_list != None, "device_list is NULL" number_of_devices = len(device_list) assert number_of_devices != None, "device_list is NULL" assert number_of_devices > 0, "number_of_devices is incorrect" ########################################################################### print ( "Number of devices : {0}".format(number_of_devices) ) ########################################################################### # get_statistics # prepare # act iothub_registry_statistics = iothub_registry_manager.get_statistics() # verify assert iothub_registry_statistics.totalDeviceCount >= 0, "iothub_registry_statistics.totalDeviceCount is incorrect" sum_device_count = iothub_registry_statistics.enabledDeviceCount + iothub_registry_statistics.disabledDeviceCount assert sum_device_count >= 0, "iothub_registry_statistics.totalDeviceCount is incorrect" ########################################################################### print ( "GetStatistics" ) print ( "Total device count : {0}".format(iothub_registry_statistics.totalDeviceCount) ) print ( "Enabled device count : {0}".format(iothub_registry_statistics.enabledDeviceCount) ) print ( "Disabled device count : {0}".format(iothub_registry_statistics.disabledDeviceCount) ) retval = 0 except Exception as e: print ( "" ) print ("run_e2e_devicetwin() failed with exception: {0}".format(e)) retval = 1 finally: ########################################################################### # delete_device # prepare # act iothub_registry_manager.delete_device(device_id) # verify ########################################################################### return retval