Getting Started with EnOS SDKs

EnOS provides SDKs to support offline application development. The EnOS SDKs assembles API request URL, generates signature, parses response, and optimizes application performance.

This learning module aims to help you learn how to prepare the development environment and use EnOS SDKs to call EnOS APIs through the example of EnOS Service SDKs.

Before You Start

Ensure you have resources on EnOS that you are authorized to access. The resources can be devices, data, events, user accounts, and so on. So before you start using EnOS APIs, you typically have finished connecting your devices and data into EnOS, and have a user account that has proper access policies assigned through EnOS IAM function.

To use EnOS SDKs, a service account (accessKey and secretKey) is required. The service account is generated by registering an application in the EnOS Console. Before calling an API that requires Write access, ensure that your SA account has the required authority. For more information, see API Access Authorization.

Installing EnOS SDKs

The system environment requirements for EnOS SDKs are as follows:

  • For Java, install Java JDK SE 8 and Maven 3 (recommended).
  • For Python, intall Python 2.7.10 or above version, and pip is required.
  • For C, install CMake (downloading CMake from https://cmake.org/).

You can obtain the EnOS SDKs through the following methods:

  • For Java, include the project as a dependency in your Maven project. For Python, install SDKs through PIP.
  • Download the source code by cloning the GitHub repository and build on your machine.

From Maven (as a dependency)

This is the recommended method of including the EnOS Java SDKs in your project.
  1. Navigate to http://search.maven.org, search for com.envisioniot.enos and take note of the latest version number (or the version number of whichever version of the sdk you want to use).

  2. In your main pom.xml file, add the SDK for Java as a dependency (take the EnOS Service SDK for Java as example):

    <dependency>
       <groupId>com.envisioniot</groupId>
       <artifactId>enos-api</artifactId>
       <version>2.3.5</version>
       <!--You might need to change the version number as you need.-->
    </dependency>
    

Install from PIP

Use the following command to install EnOS SDKs for Python (take the EnOS Service SDK for Python as example):

pip install enos-api-sdk-python

Building from Source Code

Get a copy of the SDKs from the master branch of the corresponding GitHub repository (take the EnOS Service SDK for Java as example):

git clone https://github.com/EnvisionIot/enos-api-sdk-java.git

After you have obtained a copy of the source, you can install the SDK.

Example. Using EnOS Service SDK for Java

Take the following major steps to use EnOS Service SDK for Java to call an API.

  1. Retrieve service account and secret, EnOSDefaultClient.

    String enosApiUrl = "xxx";
    String accessKey = "xxx";
    String secretKey = "xxx";
    String orgId = "xxx";
    int connectTimeout = 5000; //Set connect timeout as 5000 milliseconds
    int readTimeout = 5000;    //Set read timeout as 5000 milliseconds
    EnOSDefaultClient client = new EnOSDefaultClient(enosApiUrl, accessKey,secretKey,connectTimeout,readTimeout);
    
  2. Build and execute the request. Take the createProduct API as an example:

    Product product = new Product();
    product.setProductName("xxx");
    product.setProductDesc("xxx");
    product.setModelId("xxx");
    product.setDataType(1);
    product.setNodeType(0);
    product.setAuthType(0);
    CreateProductRequest request = new CreateProductRequest(orgId, product);
    
  3. Send the request and get the response.

    try {
         EnOSResponse<Product> response = client.execute(request);
        }
    catch (EnOSApiException e) {
        e.printStackTrace();
        }
    
  4. When executing the request, the SDK will generate the signature using the request parameters, include the signature in the request URL as well, and send the request to the API server. After the request is processed, parses the response by calling response.getXXX().

    System.out.println("data: " + response.getData());
    System.out.println("status: " + response.getStatus());
    System.out.println("requestId: " + response.getRequestId());
    System.out.println("msg: " + response.getMsg());
    System.out.println("submsg: " + response.getSubmsg());
    

Code Sample

The following sample code is for calling the createProduct API (creating a product) with EnOS API SDK:

import com.envisioniot.enos.enosapi.api.request.connectservice.CreateProductRequest;
import com.envisioniot.enos.enosapi.api.resource.connectservice.Product;
import com.envisioniot.enos.enosapi.common.exception.EnOSApiException;
import com.envisioniot.enos.enosapi.common.response.EnOSResponse;
import com.envisioniot.enos.enosapi.sdk.client.EnOSDefaultClient;

public class demo {
    public static void main(String[] args) {
        String enosApiUrl = "xxx";
        String accessKey = "xxx";
        String secretKey = "xxx";
        String orgId = "xxx";
        int connectTimeout = 5000;
        int readTimeout = 5000;

        Product product = new Product();
        product.setProductName("xxx");
        product.setProductDesc("xxx");
        product.setModelId("xxx");
        product.setDataType(1);
        product.setNodeType(0);
        product.setAuthType(0);

        EnOSDefaultClient client = new EnOSDefaultClient(enosApiUrl, accessKey, secretKey, connectTimeout, readTimeout);
        CreateProductRequest request = new CreateProductRequest(orgId, product);

        try {
            EnOSResponse<Product> response = client.execute(request);
            System.out.println("data: " + response.getData());
            System.out.println("status: " + response.getStatus());
            System.out.println("requestId: " + response.getRequestId());
            System.out.println("msg: " + response.getMsg());
            System.out.println("submsg: " + response.getSubmsg());
        }
        catch (EnOSApiException e) {
            e.printStackTrace();
        }
    }
}