ThingIFAPI

ThingIFAPI

ThingIFAPI represent an API instance to access Thing-IF APIs for a specified target

Constructor

new ThingIFAPI(owner, token, app, targetopt)

Parameters:
Name Type Attributes Description
owner string Specify who uses the ThingIFAPI
token string A token can access Thing-IF APIs, which can be admin token or token of a registered kii user.
app App App instance of existing Kii App. You must create a app in Kii developer portal
target TypedID <optional>
TypedID for a specified target.

Members

app

App instance about Kii App.

target

TypeID of target.

token

Access token.

Methods

deleteTrigger(triggerID, functionopt) → {Promise}

Delete a specified trigger. **Note**: Please onboard first, or provide a target when constructor ThingIFAPI. Otherwise, error will be returned.
Parameters:
Name Type Attributes Description
triggerID string ID of trigger.
function onCompletion <optional>
Callback function when completed
Returns:
promise object
Type
Promise
Example
api.deleteTrigger("Trigger ID").then(function(deletedTriggerID) {
  // Do something
}).catch(function(err){
  // Error handling
});

enableTrigger(triggerID, enable, functionopt) → {Promise}

Enable/Disable a specified trigger. **Note**: Please onboard first, or provide a target when constructor ThingIFAPI. Otherwise, error will be returned.
Parameters:
Name Type Attributes Description
triggerID string ID of trigger.
enable boolean True to enable, otherwise, disable the trigger.
function onCompletion <optional>
Callback function when completed
Returns:
promise object
Type
Promise
Example
api.enableTrigger("Trigger ID", true).then(function(trigger) {
  // Do something
}).catch(function(err){
  // Error handling
});

getCommand(commandID, functionopt) → {Promise}

Retrieve command with specified ID. **Note**: Please onboard first, or provide a target when constructor ThingIFAPI. Otherwise, error will be returned.
Parameters:
Name Type Attributes Description
commandID string Command ID to retrieve.
function onCompletion <optional>
Callback function when completed
Returns:
promise object
Type
Promise
Example
api.getCommand("CommandID").then(function(command) {
  // Do something
}).catch(function(err){
  // Error handling
});

getState(functionopt) → {Promise}

Get State of specified target. **Note**: Please onboard first, or provide a target when constructor ThingIFAPI. Otherwise, error will be returned.
Parameters:
Name Type Attributes Description
function onCompletion <optional>
Callback function when completed
Returns:
promise object
Type
Promise
Example
api.getState().then(function(state) {
  // Do something
}).catch(function(err){
  // Error handling
});

getTrigger(triggerID, functionopt) → {Promise}

Retrieve trigger. **Note**: Please onboard first, or provide a target when constructor ThingIFAPI. Otherwise, error will be returned.
Parameters:
Name Type Attributes Description
triggerID string ID of trigger.
function onCompletion <optional>
Callback function when completed
Returns:
promise object
Type
Promise
Example
api.getTrigger("TriggerID").then(function(trigger) {
  // Do something
}).catch(function(err){
  // Error handling
});

getVendorThingID(functionopt) → {Promise}

Get vendorThingID of specified target **Note**: Please onboard first, or provide a target when constructor ThingIFAPI. Otherwise, error will be returned.
Parameters:
Name Type Attributes Description
function onCompletion <optional>
Callback function when completed
Returns:
promise object
Type
Promise
Example
api.getVendorThingID().then(function(vendorThingID) {
  // Do something
}).catch(function(err){
  // Error handling
});

installFCM(installationRegistrationID, development, functionopt) → {Promise}

Register the id issued by Firebase Cloud Message to Kii cloud for kii user.
Parameters:
Name Type Attributes Description
installationRegistrationID string The ID of registration that identifies the installation externally.
development boolean Indicates if the installation is for development or production environment.
function onCompletion <optional>
Callback function when completed.
Returns:
promise object.
Type
Promise
Example
api.installFCM("Registration ID", false).then(function(installationID) {
  // Do something
}).catch(function(err){
  // Error handling
});

listCommands(listOptions, functionopt) → {Promise}

Retrieve commands. **Note**: Please onboard first, or provide a target when constructor ThingIFAPI. Otherwise, error will be returned.
Parameters:
Name Type Attributes Description
listOptions Object Options to retrieve commands.
function onCompletion <optional>
Callback function when completed
Returns:
promise object
Type
Promise
Example
api.listCommands().then(function(queryResult) {
  if (queryResult.hasNext) {
    // Handle more results
  }
  // Do something
  var commands = queryResult.results;
}).catch(function(err){
  // Error handling
});

listServerCodeExecutionResults(triggerID, listOptions, functionopt) → {Promise}

Retrieve execution results of server code trigger. **Note**: Please onboard first, or provide a target when constructor ThingIFAPI. Otherwise, error will be returned.
Parameters:
Name Type Attributes Description
triggerID string ID of trigger.
listOptions Object Options to retrieve.
function onCompletion <optional>
Callback function when completed
Returns:
promise object
Type
Promise
Example
api.listServerCodeExecutionResults("Trigger ID").then(function(queryResult) {
  if (queryResult.hasNext) {
    // Handle more results
  }
  // Do something
  var executionResults = queryResult.results;
}).catch(function(err){
  // Error handling
});

listTriggers(listOptions, functionopt) → {Promise}

Retrive triggers. **Note**: Please onboard first, or provide a target when constructor ThingIFAPI. Otherwise, error will be returned.
Parameters:
Name Type Attributes Description
listOptions ListQueryOptions instance to ListQueryOptions.
function onCompletion <optional>
Callback function when completed
Returns:
promise object
Type
Promise
Example
api.listTriggers().then(function(queryResult) {
  if (queryResult.hasNext) {
    // Handle more results
  }
  // Do something
  var triggers = queryResult.results;
}).catch(function(err){
  // Error handling
});

onboardWithThingID(onboardRequest, functionopt) → {Promise}

Onboard Thing by thingID for the things already registered on Kii Cloud.
Parameters:
Name Type Attributes Description
onboardRequest Object Necessary fields when request onboarding
function onCompletion <optional>
Callback function when completed
Returns:
promise object
Type
Promise
Example
var owner = new ThingIF.TypedID(ThingIF.Types.User, "Your UserID");
var app = new ThingIF.App("Your AppID", "Your AppKey", ThingIF.Site.US);
var api = new ThingIF.ThingIFAPI(owner, "Your user's acess token", app);
var thingID = "Your thing's ID";
var password = "Your thing's password";
var request = new ThingIF.OnboardWithThingIDRequest(thingID, password, owner);
api.onboardWithThingID(request).then(function(result){
  // Do something
}).catch(function(err){
  // Error handling
});

onboardWithVendorThingID(onboardRequest, functionopt) → {Promise}

Onboard Thing by vendorThingID
Parameters:
Name Type Attributes Description
onboardRequest Object request body when request onboarding
function onCompletion <optional>
callback function when completed
Returns:
promise object
Type
Promise
Example
// Assume user is already exist and you have User ID and Access token.
var owner = new ThingIF.TypedID(ThingIF.Types.User, "Your UserID");
var app = new ThingIF.App("Your AppID", "Your AppKey", ThingIF.Site.US);
var api = new ThingIF.ThingIFAPI(owner, "Your user's acess token", app);
var vendorThingID = "Your thing's vendor thing ID";
var password = "Your thing's password";
var request = new ThingIF.OnboardWithVendorThingIDRequest(vendorThingID, password, owner);
api.onboardWithVendorThingID(request).then(function(result){
  // Do something
}).catch(function(err){
  // Error handling
});

patchCommandTrigger(triggerID, requestObject, functionopt) → {Promise}

Update a command trigger. **Note**: Please onboard first, or provide a target when constructor ThingIFAPI. Otherwise, error will be returned. When thing related to this ThingIFAPI instance meets condition described by predicate, A registered command sends to thing related to target. `target` property and commandTarget in requestObject must belong to same owner.
Parameters:
Name Type Attributes Description
triggerID string ID of trigger.
requestObject Object The fields of trigger to be updated.
function onCompletion <optional>
Callback function when completed
Returns:
promise object
Type
Promise
Example
var condition = new ThingIF.Condition(new ThingIF.Equals("power", "false"));
var statePredicate = new ThingIF.StatePredicate(condition, ThingIF.TriggersWhen.CONDITION_CHANGED);
// if commandTargetID can be different with api.target
var commandTargetID = new ThingIF.TypedID(ThingIF.Types.Thing, "another thing to receive command");
var triggerCommandObject = new ThingIF.TriggerCommandObject("led2", 2, [{setBrightness: {brightness:50}}], commandTargetID);
var request = new ThingIF.PatchCommandTriggerRequest(triggerCommandObject, statePredicate);
api.patchCommandTrigger("Trigger ID", request).then(function(trigger) {
  // Do something
}).catch(function(err){
  // Error handling
});

patchServerCodeTrigger(triggerID, requestObject, functionopt) → {Promise}

Update a servercode trigger. **Note**: Please onboard first, or provide a target when constructor ThingIFAPI. Otherwise, error will be returned.
Parameters:
Name Type Attributes Description
triggerID string ID of trigger.
requestObject Object The fields of trigger to be updated.
function onCompletion <optional>
Callback function when completed
Returns:
promise object
Type
Promise
Example
var serverCode = new ThingIF.ServerCode("function_name", null, null, {param1: "hoge"});
var condition = new ThingIF.Condition(new ThingIF.Equals("power", "false"));
var statePredicate = new ThingIF.StatePredicate(condition, ThingIF.TriggersWhen.CONDITION_CHANGED);
var request = new ThingIF.PatchServerCodeTriggerRequest(serverCode, statePredicate);
api.patchServerCodeTrigger("Trigger ID", request).then(function(trigger) {
  // Do something
}).catch(function(err){
  // Error handling
});

postCommandTrigger(requestObject, functionopt) → {Promise}

Post a new command trigger. **Note**: Please onboard first, or provide a target when constructor ThingIFAPI. Otherwise, error will be returned. When thing related to this ThingIFAPI instance meets condition described by predicate, A registered command sends to thing related to target. `target` property and commandTarget in requestObject must belong to same owner.
Parameters:
Name Type Attributes Description
requestObject Object Necessary fields for new command trigger. `_owner` property is used as IssuerID. So even if IssuerID is provided in requestObject, it will be ignored. If requestObject.command.targetID is not provide or null, `_target` property will be used by default.
function onCompletion <optional>
Callback function when completed
Returns:
promise object
Type
Promise
Example
// commandTargetID can be different with api.target.
var commandTargetID = new ThingIF.TypedID(ThingIF.Types.Thing, "another thing to receive command");
var condition = new ThingIF.Condition(new ThingIF.Equals("power", "false"));
var statePredicate = new ThingIF.StatePredicate(condition, ThingIF.TriggersWhen.CONDITION_CHANGED);
var triggerCommandObject = new ThingIF.TriggerCommandObject("Schema name", 1, [{turnPower: {power:true}}], commandTargetID);
var request = new ThingIF.PostCommandTriggerRequest(triggerCommandObject, statePredicate);
api.postCommandTrigger(request).then(function(trigger) {
  // Do something
}).catch(function(err){
  // Error handling
});

postNewCommand(command, functionopt) → {Promise}

Post a new command. **Note**: Please onboard first, or provide a target when constructor ThingIFAPI. Otherwise, error will be returned.
Parameters:
Name Type Attributes Description
command Object Necessary fields for new command
function onCompletion <optional>
Callback function when completed
Returns:
promise object
Type
Promise
Example
var request = new ThingIF.PostCommandRequest("led", 1, [{turnPower: {power:true}}]);
api.postNewCommand(request).then(function(command) {
  // Do something
}).catch(function(err){
  // Error handling
});

postServerCodeTrigger(requestObject, functionopt) → {Promise}

Post a new servercode trigger. **Note**: Please onboard first, or provide a target when constructor ThingIFAPI. Otherwise, error will be returned.
Parameters:
Name Type Attributes Description
requestObject PostServerCodeTriggerRequest Necessary fields for new servercode trigger.
function onCompletion <optional>
Callback function when completed
Returns:
promise object
Type
Promise
Example
var serverCode = new ThingIF.ServerCode("function_name", null, null, {param1: "hoge"});
var condition = new ThingIF.Condition(new ThingIF.Equals("power", "false"));
var statePredicate = new ThingIF.StatePredicate(condition, ThingIF.TriggersWhen.CONDITION_CHANGED);
var request = new ThingIF.ServerCodeTriggerRequest(serverCode, statePredicate);
api.postServerCodeTrigger(request).then(function(trigger) {
  // Do something
}).catch(function(err){
  // Error handling
});

uninstallPush(installationID, functionopt) → {Promise}

Unregister the push settings by the id(issued by KiiCloud) that is used for installation.
Parameters:
Name Type Attributes Description
installationID string The ID of the installation issued by KiiCloud.
function onCompletion <optional>
Callback function when completed
Returns:
promise object.
Type
Promise
Example
api.uninstallPush("Installation ID").then(function() {
  // Do something
}).catch(function(err){
  // Error handling
});

updateVendorThingID(newVendorThingID, newPassword, functionopt) → {Promise}

Update vendorThingID of specified target **Note**: Please onboard first, or provide a target when constructor ThingIFAPI. Otherwise, error will be returned.
Parameters:
Name Type Attributes Description
newVendorThingID string New vendorThingID of target to be updated.
newPassword string New password of target to be updated.
function onCompletion <optional>
Callback function when completed
Returns:
promise object
Type
Promise
Example
api.updateVendorThingID("New vendor thing ID", "New Password").then(function() {
  // Do something
}).catch(function(err){
  // Error handling
});