Considerations for the Sample Code

The sample code in this guide is written under the following rules. Rewrite the code accordingly when you incorporate it into your web app.

const and let

In the sample code, TypeScript variables are declared as const and their value cannot be changed later.

If you need to change their values in your app, rewrite the code to declare the variables as let to avoid errors.

Explicit declaration of types in TypeScript

In the sample code, variable types used in TypeScript are basically written under the following rules.

  • When you can implicitly declare variables with type inference, the sample code omits variable types. Such variables include argument values passed to APIs.

  • When you cannot know variable types without checking the reference documentation, the sample code explicitly indicates type information even though it is redundant as a notation. Such variables include those of returned values from APIs and arguments of callback functions. You can omit those variable declarations when you implement the web app.

Promises and callbacks

The Thing-IF SDK supports both of promises and callback functions as implementation methods for non-blocking APIs.

The sample code in this guide basically uses promises. If you use callback functions with the Thing-IF SDK APIs, see the API specification in the JSDoc.

For example, you can rewrite the promise code in Executing Commands using a callback function as below.

Promise

  • const actions = [
      {"turnPower":{"power":true}},
      {"setPresetTemperature":{"presetTemperature":25}},
      {"setFanSpeed":{"fanSpeed":5}}
    ];
    const commandRequest = new ThingIF.PostCommandRequest("AirConditioner-Demo", 1, actions);
    api.postNewCommand(commandRequest).then((command: ThingIF.Command) => {
      // Do something.
    }).catch((error: ThingIF.ThingIFError) => {
      // Handle the error.
    });
  • var actions = [
      {"turnPower":{"power":true}},
      {"setPresetTemperature":{"presetTemperature":25}},
      {"setFanSpeed":{"fanSpeed":5}}
    ];
    var commandRequest = new ThingIF.PostCommandRequest("AirConditioner-Demo", 1, actions);
    api.postNewCommand(commandRequest).then(
      function(command) {
        // Do something.
      }
    ).catch(
      function(error) {
        // Handle the error.
      }
    );

Callback function

  • const actions = [
      {"turnPower":{"power":true}},
      {"setPresetTemperature":{"presetTemperature":25}},
      {"setFanSpeed":{"fanSpeed":5}}
    ];
    const commandRequest = new ThingIF.PostCommandRequest("AirConditioner-Demo", 1, actions);
    api.postNewCommand(commandRequest, (error: Error, command: ThingIF.Command) => {
      if (error) {
        // Handle the error.
        return;
      }
      // Do something.
    });
  • var actions = [
      {"turnPower":{"power":true}},
      {"setPresetTemperature":{"presetTemperature":25}},
      {"setFanSpeed":{"fanSpeed":5}}
    ];
    var commandRequest = new ThingIF.PostCommandRequest("AirConditioner-Demo", 1, actions);
    api.postNewCommand(commandRequest, function (error, command) {
      if (error) {
        // Handle the error.
        return;
      }
      // Do something.
    });

For implementing promises, see Use Promise in the tutorial and Using Promises in the Implementation Guidelines section for the Kii Cloud SDK.