def _main(): api = DeviceDirectoryAPI() # Pretty print all the registered queries for idx, e in enumerate(api.list_queries(limit=5)): print(e) # Create a new query new_query = api.add_query("test_filter", {'device_id': { '$eq': str(uuid.uuid4()) }}) print("\nCreated new query: %r" % (new_query.name)) # Delete same query api.delete_query(new_query.id) print("Deleted newly created query") # Create more complex query print("Creating complex query") new_c_query = api.add_query( "complex_test_query %s" % _id_generator(), { 'device_id': { '$eq': str(uuid.uuid4()) }, 'auto_update': { '$eq': True }, 'state': { '$eq': 'bootstrapped' }, 'device_class': { '$eq': 'embedded' }, 'serial_number': { '$eq': '1234' }, 'vendor_id': { '$eq': 'Arm' }, 'description': { '$eq': 'Loreum ipsum' }, 'device_name': { '$eq': 'DeviceName' }, 'custom_attributes': { 'customA': { '$eq': 'SomethingA' }, 'customB': { '$eq': 'Something B' } } }) # Manually get it gf = api.get_query(new_c_query.id) print("Got query %r using 'get'" % gf.name) # Update the query new_filter_dict = gf.filter new_filter_dict['serial_number']['$eq'] = '12345' updated_gf = api.update_query(query_id=gf.id, name=gf.name, filter=new_filter_dict) # Check it was successful assert updated_gf.filter['serial_number']['$eq'] == '12345' print("Updated query with new serial number") # Find device using query object print("Find devices that are matching the created query") devicesResponse = api.list_devices(filters=updated_gf.filter) # Print all devices that are matching provided query print(list(devicesResponse)) # And delete that too api.delete_query(new_c_query.id) print("Deleted complex query")
def _main(): update_api = UpdateAPI() # Read manifest from first argument on command line if len(sys.argv) < 2: raise ValueError("No manifest filename found on command line") filename = os.path.abspath(sys.argv[1]) # Upload manifest mobj = update_api.add_firmware_manifest( name="Auto manifest %s" % _rand_id(), datafile=filename, description="Manifest uploaded using Mbed Cloud SDK") print("Successfully uploaded manifest %r\n\tURL: %s\nProperties:" % (mobj.name, mobj.url)) # List all manifests currently uploaded mresp = update_api.list_firmware_manifests(limit=10) _print_manifests(mresp) # List all filters device_api = DeviceDirectoryAPI() fresp = device_api.list_queries() header = "Current filters:" print("\n%s\n%s" % (header, "-" * len(header))) if fresp.count() == 0: print("No filters created. Please create one to apply the update on.") sys.exit(1) filters = [f for idx, f in enumerate(fresp)] print("\n".join(["\t- %s" % (f.name) for f in filters])) selected_query = random.choice(filters) print("Randomly chose %r for applying update" % (selected_query.name)) # Create update campaign. For this step we need three pieces of information: # 1. The name of the update campaign (we auto-generate this) # 2. What manifest to use for running the update # 3. What devices we should apply the updat on (i.e. the filter to use) campaign_name = _rand_id() print( "\nCreating campaign %r using:\n\t- Manifest ID: %r\n\t- Filter ID: %r" % (campaign_name, mobj.id, selected_query.id)) cobj = update_api.add_campaign(name=campaign_name, manifest_id=mobj.id, device_filter=selected_query.filter) print("Campaign successfully created. Current state: %r" % (cobj.state)) # # List the current campaigns # header = "Update campaigns" print("\n%s\n%s" % (header, "-" * len(header))) for idx, c in enumerate(update_api.list_campaigns()): print("\t- %s (State: %r)" % (c.name, c.state)) # # Start the update campaign we've created. # print("\n** Starting the update campign **") # By default a new campaign is created with the 'draft' status. We can manually start it. new_cobj = update_api.start_campaign(cobj) print( "Campaign successfully started. Current state: %r. Checking updates.." % (new_cobj.state)) countdown = 10 while countdown > 0: c = update_api.get_campaign(new_cobj.id) print("[%d/10] Current state: %r (Finished: %s)" % (countdown, c.state, c.finished_at)) countdown -= 1 # # Cleanup. # print("\n** Deleting update campaign and manifest **") update_api.delete_campaign(new_cobj.id) update_api.delete_firmware_manifest(mobj.id)