When you develop with Thing Specification Language (TSL) models, devices do not need to manage MQTT topic subscriptions. Instead, you can call TSL-related APIs to report properties, listen for services, and report events.
Report device properties
| API prototype | device#postProps(params, [callback]) |
| Description | Report properties |
| Parameters | - params: The property parameters. This is an Object. - callback - res: The reply message from the server. |
Example code:
// Report device properties.
device.postProps({
LightSwitch: 0
}, (res) => {
console.log(res);
});
The preceding code example reports a property named LightSwitch with a value of 0. For more information, see the complete reference code.
Set properties
You can call device.onProps() to listen for property settings from the cloud.
| API prototype | device#onProps(function(cmd)) |
| Description | Listens for property settings from the cloud. |
| Parameters | - function: The callback function that is called when a command is received. - cmd: The command sent from the server. |
The following code shows an example of a received message (the content of the cmd parameter):
{
method: 'thing.service.property.set',
id: '802031359',
params: { LightSwitch: 1 },
version: '1.0.0'
}
The following code shows an example of how to process the setting for the switch property of a light:
// Listen for property setting service messages from the cloud. The example code is for a light.
device.onProps((cmd)=>{
console.log('>>>onProps',cmd); // Print the complete property setting message.
for(var key in cmd.params){
if(key=='LightSwitch'){ // Check if the LightSwitch property is being set.
console.log('set property ',key);
// The example code saves the property set by the cloud locally. For actual product development, you must modify this to turn the light on or off.
lightState = cmd.params.LightSwitch;
// After the local settings are configured, report the updated status to the cloud.
// Note: After the cloud sends the command, the property value in the cloud does not change. The cloud waits for a property report from the device.
device.postProps({'LightSwitch': lightState});
}
}
})
Listen for service invocation messages from the cloud
| API prototype | device#onService(seviceIdentifier, [callback]) |
| Description | Listener Service Settings |
| Description | - serviceIdentifier: The service ID. This is a string. - callback |
The res parameter is returned by the server.
The reply function is used to respond to the service. You can respond synchronously or asynchronously.
The following code shows an example of how to process a service invocation:
// The example service is an adder. When the cloud service is invoked, it provides x and y and returns their sum.
function addFunc(x,y){
let err;
if(x==undefined || y==undefined){
err = 'x or y invail value';
return {err,code:10001} // Format encapsulation for an input parameter fault.
}
// Note that a JSON object is returned. The result is encapsulated in data.
return {
data:{
z:x+y // z is the output parameter defined in the service.
},
code:200
}
}
// Subscribe to the add_async service. The add_async service is defined on the product.
device.onService('add_async', function (res,reply) {
console.log('add_async called,res:',res);
const { params:{x,y}={}} = res; // Get the service parameters.
const result = addFunc(x,y); // Call addFunc, which formats the response data.
console.log('result',result);
reply(result); // Return the processing result.
});
Report events
| API prototype | device#postEvent(eventIdentifier, params, [callback]) |
| Description | Event reporting |
| Parameters | - eventIdentifier: The event ID. This is a String. - params: The event parameters. This is an Object. - callback - err: A fault, such as a timeout. - res: The reply message from the server. |
The following code shows an example of how to report an event with the ID eventIdentifier1:
device.postEvent('eventIdentifier1', {
// key1 is a parameter of the event 'eventIdentifier1'.
key1: 'value1'
});
You can view the complete code example.