Skip to content

Sending requests

Once you are connected to an IoTize tap device, you can perform requests.

Available commands are organized into multiple services. See the list of services and functions.

There are two ways to send requests to your Tap:

  • Service API that provides a developer friendly interface
  • Client API for a very basic a low level interface

Service API

Service API is a wrapper on the IoTize client that list availables services and and handle requestbuilding and binary parsing of response.

If you want to send raw requests, you can use the client directly (see Client API)

Accessing service object

import { IoTizeDevice } from "@iotize/device-client.js/device";
import { BundleService } from "@iotize/device-client.js/device/service/bundle-service";
import { TargetService } from "@iotize/device-client.js/device/service/target-service";
import { FirmwareService } from "@iotize/device-client.js/device/service/firwmare-service";

IoTizeDevice device = ...;

let bundleService: BundleService = device.service.bundle;
let targetService: TargetService = device.services.target;
let firmwareService: FirmwareService = device.service.firmware;
// ...
import com.iotize.android.device.device.impl.IoTizeDevice;
import com.iotize.android.device.device.api.service.definition.BundleService;
import com.iotize.android.device.device.api.service.definition.FirmwareService;
import com.iotize.android.device.device.api.service.definition.TargetService;

IoTizeDevice device = // ... ;

// Access different service:
BundleService bundleService = device.service.bundle;
TargetService targetService = device.service.target;
FirmwareService firmwareService = device.service.firmware;
// ...

Performing calls

You can either perform synchronous our asynchronous call to the tap device.

Synchronously

// Only available if you are in a ASYNC function
let response: Response<string> = await device.service.device.getSerialNumber();
Call<String> call = device.service.device.getSerialNumber();
Response<String> response = call.execute();

Asynchronously

Asynchronous calls with callbacks:

let response: Promise<Response<string>> = device.service.device.getSerialNumber();

responsePromise
    .then((response: Response<string>) => {
        console.log('Device response:', response);
    })
    .catch((error) => {
        console.error('An error occured:', error);
    })
Call<String> call = device.services.device.getSerialNumber();
commandCall.enqueue(new ICallback<String>() {
    @Override
    public void onResponse(Call<String> call, Response<String> response) {
        // Do what you need here
    }

    @Override
    public void onFailure(Call<String> call, Throwable throwable) {
        Log.e("yourlogtag", "Error occured: " + throwable.getMessage(), throwable);
    }
});

Client API

Command format

IoTize LWM2M commands are composed of - a header composed of - A command type (GET, PUT or POST) - Command path represented as a string with <Object ID>/<Object Instance ID>/<Resource ID> - a payload as bytes

Building a command

Commands are modelized by a Command Object. You can use helper methods to instantiate it according to the request type your want to use.

import { Command } from '@iotize/device-client.js/client/impl/request';
// Get command
let command = Command.GET("/1/2/3")
// Put command
let command = Command.PUT("/1/2/3")
// Post command
let command = Command.POST("/1/2/3")
import com.iotize.android.device.api.client.request.Command;

// Get command
Command command = Command.GET("/1/2/3")
// Put command
Command command = Command.PUT("/1/2/3")
// Post command
Command command = Command.POST("/1/2/3")
// Get command
let command = IoTizeDeviceClient.Client.Command.GET("/1/2/3");
// Put command
let command = IoTizeDeviceClient.Client.Command.PUT("/1/2/3")
// Post command
let command = IoTizeDeviceClient.Client.Command.POST("/1/2/3")

If resource id is null omit it (meaning it's a singleton object)

// Get command with resource id null
let command = Command.GET("/1//3")
// Get command with resource id null
Command command = Command.GET("/1//3")

You can also add a command payload with the second parameter (optional):

let data = new Uint8Array([1,2,3]);
let command = Command.POST("/1/2/3", data)
byte[] data = // Your data...
Command command = Command.POST("/1/2/3", data)

Sending command

You can use client implementation:

import { Command } from "@iotize/device-client.js/client/impl/request";
import { DefaultIoTizeClient } from "@iotize/device-client.js/client/impl";
import { Response } from '@iotize/device-client.js/client/impl/response';

let protocol: ComProtocol = // ...;
let client = new DefaultIoTizeClient.create(protocol);

client
    .connect()
    .then(function() {
        // Command to read tap device serial number
        let command: Command = Command.GET("/1024//2");
        return client.send(command);
    })
    .then(function(response: Reponse<Uint8Array>){
        console.log(`Response: ${response}`);
    });
import com.iotize.android.communication.client.impl.IoTizeClient;
import com.iotize.android.device.api.client.request.Command;

ComProtocol protocol = // your communication protocol;
IoTize client = new IoTizeClient(protocol);
client.connect();

// Command to read tap device serial number
Command command = Command.GET("/1024//2");
byte[] response = client.send(command);

Handling Response

A Response is composed of

  • result code: See the list of result code returns by a tap device.
  • payload (optionaly): encoded data (bytes)

A Response can be configured with a Converter class to decode bytes into the corresponding type.

If you are using the Service API, Response are preconfigured with the proper Converter instance.

We suppose in the following examples that we have a response for the getSerialNumber command which return a ASCII string.

import { Response } from "@iotize/device-client.js/client/impl/response"

let response: Response<string> = //...;

if (response.isSuccess()){
    // If you are using Service API
    let serialNumber = response.body();

    // Or if you build request with the client, you can give the converter type
    let serialNumber = resonse.body(StringConverter.instance());
}
else {
    // Handle invalid response
    console.error(`Response error: ${response.codeRet()}`);
}
import com.iotize.android.device.api.client.response.Response;

Response<String> response = // ...;

// Check if command was successful
if (response.isSuccessful()){
    String serialNumber = response.body();
    Log.i("yourlogtag", "Serial number is : " + serialNumber);
}
else{
    // If command is not successful we can check error code to know why
    Log.w("yourtag", "Iotize error code: " + response.codeRet());
}

You can also access raw body (without any decoding)

let body: Uint8Array = response.rawBody();
byte[] body = response.rawBody();