In today’s API economy, everything is about getting to market faster and gaining the first-movers advantage. Multiple updates and code releases daily, put pressure on brands and products to deliver value at the speed-of-the market. So, when your code release is behind schedule, no one wants to get bogged down in policy and management details. Nor does anyone want to deal with deployment hiccups when your product reputation is on the line and the competition is breathing down your neck.

Esper is really useful when you need to manage device deployments for your business. But, at the same time, managing and deploying devices securely in the field can be really time consuming and often places an additional strain on your team’s resources. Esper is building tools to solve those pain points and empower developers to fully control device deployments while making them more efficient and seamless.

If you are already registered with Esper and are familiar with our Esper Console for managing devices, you know what we are talking about! If you don’t have an account yet, try it out. We’re going to take a look at a business scenario and see how you can use Esper Dev Tools to automate your device deployments.

Assuming that you already have an Esper account and have used it for device deployments, let’s consider the following situation.

Business Scenario: Grocery Store Inventory Management

Imagine that you are an inventory management company for a grocery store chain in your region. Your company has deployed close to 1,000 mobile inventory devices across these stores using Esper’s powerful platform. So far so good! But, now you’ve received a call from one of the stores in a panic that the Calculator app is not showing up. The Calculator app is a critical part of your field worker use case and cannot be ignored. You know that the calculator is present in your Android OS image, but somehow it just wasn’t enabled in your device template. You currently can’t do it from Esper Console since it supports limited group actions, noting the Esper team has already slated new capabilities for future releases. So, let’s workout the solution for how you can use Esper APIs to solve this problem.

Esper provides APIs for whitelisting system apps in a device such as a portable inventory tablet. So, what this means is that we just need to fix it for our group by using a small script. In this case, we’re going to use Python and the Esper SDK for python to communicate with Esper APIs. The entry point to the script should look like this:

python whitelist_script.py  

or 

python whitelist_script.py 8029f9cb-272c-4a70-954b-ec96d2063384 com.android.calculator

which means enabling the calculator functionality on all the devices in a group (represented by the group id). We can map this workflow to the following  Esper API actions:

  1. Organizing Your API Configuration
  2. Get all the Devices in a Group
  3. Check if the Calculator App is Whitelisted
  4. Send Device Command to Whitelist the App
  5. Wiring it up!

Let’s talk about each of these items in detail…

Organizing Your API Configuration

You’ll need to configure the SDK with an API key and your Esper domain url, similar to the example below

1
2
3
4
5
import esperclient
configuration = esperclient.Configuration()
configuration.host = 'https://foo-api.esper.cloud/api'
configuration.api_key['Authorization'= 'YOUR_API_KEY'
configuration.api_key_prefix['Authorization'= 'Bearer'

Where foo is your endpoint name. Also, keep your enterprise ID handy for use later.

Get all the Devices in a Group

For this, we can use the Device API and filter using the group ID for your deployment. We also need to filter out inactive devices as device command can only be fired on active devices. Once the script is ready, later on you can use it on the currently inactive devices when they are active again. Below is the function to get devices in a group:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def get_devices_in_group(group_id):
    api_instance = esperclient.DeviceApi(esperclient.ApiClient(configuration))
    limit = 1000  # replace this with your preferred number of results to return per page
   
    device_list = []
    try:
        api_response = api_instance.get_all_devices(enterprise_idgroup=group_idlimit=limit)
        if len(api_response.results> 0:
            for device in api_response.results:
                if device.status == 1:  # Check for active devices only
                    device_list.append(device)
    except ApiException as e:
        print("Exception when calling DeviceApi->get_all_devices: %s\n" % e)
    return device_list

Check if the Calculator App is Whitelisted

Esper provides an API to list all the apps on a device, both system and installed apps. We can apply a filter for the package name and whitelist to check if the app is already whitelisted or not. Below is a small function which returns false if the app package is not whitelisted on a device.

1
2
3
4
5
6
7
8
9
def is_package_whitelisted(device_idpackage_name):
    api_instance = esperclient.DeviceApi(esperclient.ApiClient(configuration))
    api_response = None
    try:
        api_response = api_instance.get_device_apps(enterprise_iddevice_idpackage_name=package_namewhitelisted=False)
    except ApiException as e:
        print("Exception when calling DeviceApi->get_device_apps: %s\n" % e)
    
    return False if api_response.count == 1 else True

Send Device Command to Whitelist the App

Now we can use the device commands API to send the  ADD_TO_WHITELIST command to enable the calculator app package on a device. Below is the function required to perform this task: 

1
2
3
4
5
6
7
8
9
10
def run_whitelist_command(devicepackage_name):
    api_instance = esperclient.CommandsApi(esperclient.ApiClient(configuration))
    command_args = esperclient.CommandArgs(package_name=package_name)
    command = esperclient.CommandRequest(command='ADD_TO_WHITELIST'command_args=command_args)
    try:
        api_response = api_instance.run_command(enterprise_iddevice.idcommand)
        if api_response.id is not None:
            print('Whitelist command fired successfully on ' +  device.device_name + ' for ' + package_name)
    except ApiException as e:
        print("Exception when calling CommandsApi->run_command: %s\n" % e)

Wiring it up!

The main components of our application are ready now. Let’s close this out by wiring it up, so everything works in sync. Below is the how the entry point of an application will look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import sys
import esperclient
import time
from esperclient.rest import ApiException
if __name__ == "__main__":
    main(sys.argv[1:])
def main(argv):
    try:
        group_id = argv[0]
    except IndexError:
        print('Error: group id is required')
        sys.exit(1)
    try:
        package_name = argv[1]
    except IndexError:
        print('Error: package name is required')
        sys.exit(1)   
    whitelist_package_in_group(group_idpackage_name)
def whitelist_package_in_group(group_idpackage_name):
    # Fetch all devices in group
    group_devices = get_devices_in_group(group_id)
    if len(group_devices> 0:
        print("Currently active devices in group: ")
        print([device.device_name for device in group_devices])
    for device in group_devices:
        if not is_package_whitelisted(device.idpackage_name):
            try:
                run_whitelist_command(devicepackage_name)
            except Exception as e:
                print('Could not fire command on ' + device.device_name + ": " + str(e))
            time.sleep(1)
        else:
            print('Warning: Package already whitelisted or not present on device: ' + device.device_name)

And now you can run the script using the following command:

python whitelist_script.py 8029f9cb-272c-4a70-954b-ec96d2063384 com.android.calculator

Where 8029f9cb-272c-4a70-954b-ec96d2063384 is the group id for the deployment. From where do you get the group id? You can use the device group api for that, see our api reference here.

Final Thoughts

This grocery store inventory management case is just one example among many of how you can use Esper to automate your device deployment workflow. The ability to carry out operations on a group of devices at one time is a really powerful way to leverage the rich set of Esper APIs. 

You can also use the above template to fill gaps in your use cases as well as develop new ones; this can involve uninstalling apps from a group, disabling an app, enabling new user features, etc. We recommend you visit API and SDK documentation to learn more about how you can leverage Esper’s rich set of tools and documentation to make your device deployments more efficient across the lifecycle of your organization.

SignUp With Esper
3