Last Updated on : 2025-05-30 01:56:03download
import { OpenSaasSDK } from ‘xxx/openSaasSdk’
OpenSaasSDK.set({client_id, secret, access_token, refresh_token})
OpenSaasSDK.request({
data: {
action: "***.***",
vsersion: "1.0",
params: {"***":{}}
}
});
action | version | description |
---|---|---|
mqtt.getMqttConfig | 1.0 | Obtain MQTT connection configuration. |
device.listDevices | 1.0 | Obtain the device list. |
device.getDeviceStatus | 1.0 | Obtain the latest device status. |
device.batchQuerySwitchStatus | 1.0 | Query device switch status in batches. |
device.getDeviceFunctions | 1.0 | Obtain the function set supported by a device. |
device.getDeviceSpecifications | 1.0 | Obtain device specifications. |
device.controlDevice | 1.0 | Issue commands to a device. |
device.controlDeviceSwitch | 1.0 | Control a device switch. |
device.controlInfraredDevice | 1.0 | Control an infrared device. |
device.getInfraredStatus | 1.0 | Obtain the latest status of an infrared device. |
scene.listScenes | 1.0 | Obtain the scene list. |
scene.triggerScene | 1.0 | Trigger a scene. |
You can use this method to obtain MQTT connection configuration.
action | version |
---|---|
mqtt.getMqttConfig | 1.0 |
Parameter | Type | Description |
---|---|---|
connect_id | String | Unique ID of a device. (Optional. We recommend that you set this parameter.) |
Parameter | Type | Description |
---|---|---|
code | Integer | Response code.Null indicates success. |
success | Boolean | Request result. true: success false: failure |
msg | String | Exception message.It is null when the request result is success. |
result | Object | MQTT connection configuration. |
Parameters in result
Parameter | Type | Description |
---|---|---|
client_id | String | ID of the client connected to MQTT. The value a unique and constant mapping generated based on the user account and unique_id. One client ID can be used for both publishing and subscription. |
expire_time | Integer | Expiration time of the current configuration.When the current configuration is invalid, all connections are broken. |
username | String | User name in an MQTT connection. The value is a unique and constant mapping generated based on the user account. |
password | String | Password in an MQTT connection.This parameter remains unchanged before the expiration time. |
sink_topic | Map | MQTT topic corresponding to device control messages delivered by a user. The format is cloud/token/out/{dev_id}. In the format, {dev_id} indicates the ID of the controlled IoT device. |
source_topic | Map | MQTT topic corresponding to messages pushed from a device to the account. Multiple client IDs can be used to obtain connections. |
url | String | MQTT connection URL, which contains the protocol, IP address, and port number. |
OpenSaasSDK.request({
data: {
action: "mqtt.getMqttConfig",
vsersion: "1.0",
params: {
"connect_id": "******"
},
}
});
{
"result": {
"client_id": "******",
"expire_time": 7200,
"username": "******",
"password": "******",
"sink_topic":{
"device":"cloud/token/out/{device_id}"
},
"source_topic":{
"device":"cloud/token/in/22e37df6e53bc8******"
},
"url": "wss://m1.tuyacn.com:443/mqtt"
},
"success": true
}
{
"code":500,
"msg":"system error,please contact the admin",
"success":false
}
You can use this method to obtain the device list.
action | version |
---|---|
device.listDevices | 1.0 |
Parameter | Type | Description |
---|---|---|
code | Integer | Response code.Null indicates success. |
success | Boolean | Request result. true: success false: failure |
msg | String | Exception message.It is null when the request result is success. |
result | Array | Device list. |
Parameters in result
Parameter | Type | Description |
---|---|---|
device_id | String | Unique ID of a device. |
product_id | String | Unique ID of a product. |
device_name | String | Device name. |
category_name | String | Category name. |
category_code | String | Category code. |
sub | Boolean | Sub device. |
online | Boolean | Indicates whether a deivce is online. |
position | String | Location of a device. |
dp_count | Integer | Number of data points. |
OpenSaasSDK.request({
data: {
action: "device.listDevices",
vsersion: "1.0"
}
});
{
"result": [
{
"category_code": "ckqdkg",
"category_name": "",
"device_id": "6c423606c35******",
"device_name": "Zigbee low-frequency card power switch",
"dp_count": 0,
"online": true,
"position": "Living room",
"product_id": "iqsefpek",
"sub": false
}
],
"success": true
}
{
"code":500,
"msg":"system error,please contact the admin",
"success":false
}
You can use this method to obtain the latest device status.
action | version |
---|---|
device.getDeviceStatus | 1.0 |
Parameter | Type | Description |
---|---|---|
device_id | String | Unique ID of a device. |
Parameter | Type | Description |
---|---|---|
code | Integer | Response code.Null indicates success. |
success | Boolean | Request result. true: success false: failure |
msg | String | Exception message.It is null when the request result is success. |
result | Array | Device status list. |
Parameters in result
Parameter | Type | Description |
---|---|---|
code | String | Status code. |
value | String | Status value. |
OpenSaasSDK.request({
data: {
action: "device.getDeviceStatus",
vsersion: "1.0",
params: {
"device_id": "6c760b4d9668******_switch_2"
}
}
});
{
"result": [
{
"code": "switch",
"value": "true"
}
],
"success": true
}
{
"code":500,
"msg":"system error,please contact the admin",
"success":false
}
You can use this method to query device switch status in batches.
action | version |
---|---|
device.batchQuerySwitchStatus | 1.0 |
Parameter | Type | Description |
---|---|---|
device_ids | List |
List of unique IDs of devices. |
Parameter | Type | Description |
---|---|---|
code | Integer | Response code.Null indicates success. |
success | Boolean | Request result. true: success false: failure |
msg | String | Exception message.It is null when the request result is success. |
result | Array | List of device switch statuses. |
Parameters in result
Parameter | Type | Description |
---|---|---|
device_id | String | Unique ID of a device. |
status | Boolean | Switch status. |
OpenSaasSDK.request({
data: {
action: "device.batchQuerySwitchStatus",
vsersion: "1.0",
params: {
"device_id": "6c760b4d9668******_switch_2"
}
}
});
{
"result": [
{
"device_id": "6c760b4d9668******_switch_2",
"status": true
}
],
"success": true
}
{
"code":500,
"msg":"system error,please contact the admin",
"success":false
}
You can use this method to obtain the function set supported by a device.
action | version |
---|---|
device.getDeviceFunctions | 1.0 |
Parameter | Type | Description |
---|---|---|
device_id | String | Unique ID of a device. |
Parameter | Type | Description |
---|---|---|
code | Integer | Response code.Null indicates success. |
success | Boolean | Request result. true: success false: failure |
msg | String | Exception message.It is null when the request result is success. |
result | Object | Result object. |
Parameters in result
Parameter | Type | Description |
---|---|---|
category_code | String | Category code. |
functions | Array | List of function sets. |
Parameters in result.functions
Parameter | Type | Description |
---|---|---|
code | String | Function code. |
type | String | Function type. |
values | String | Parameter value range. |
name | String | Function name. |
desc | String | Function description. |
OpenSaasSDK.request({
data: {
action: "device.getDeviceFunctions",
vsersion: "1.0",
params: {
"device_id": "6c760b4d96688******_switch_2"
}
}
});
{
"result": {
"category": "kg",
"functions": [
{
"name": "Switch",
"desc": "Main switch. true: all on. false: all off.",
"code": "switch",
"type": "Boolean",
"values": "{}"
}
]
},
"success": true
}
{
"code":500,
"msg":"system error,please contact the admin",
"success":false
}
You can use this method to obtain device specifications.
action | version |
---|---|
device.getDeviceSpecifications | 1.0 |
Parameter | Type | Description |
---|---|---|
device_id | String | Unique ID of a device. |
Parameter | Type | Description |
---|---|---|
code | Integer | Response code.Null indicates success. |
success | Boolean | Request result. true: success false: failure |
msg | String | Exception message.It is null when the request result is success. |
result | Object | Result object. |
Parameters in result
Parameter | Type | Description |
---|---|---|
category_code | String | Category code. |
functions | Array | List of function sets. |
status | Array | List of status sets. |
Parameters in result.functions
Parameter | Type | Description |
---|---|---|
code | String | Function code. |
type | String | Function type. |
values | String | Parameter value range. |
name | String | Function name. |
desc | String | Function description. |
Parameters in result.status
Parameter | Type | Description |
---|---|---|
code | String | Status code. |
type | String | Status type. |
values | String | Parameter value range. |
OpenSaasSDK.request({
data: {
action: "device.getDeviceSpecifications",
vsersion: "1.0",
params: {
"device_id": "6c760b4d96688******_switch_2"
}
}
});
{
"result": {
"category": "kg",
"functions": [
{
"name": "Switch",
"desc": "Main switch. true: all on. false: all off.",
"code": "switch",
"type": "Boolean",
"values": "{}"
}
],
"status":[
{
"code":"switch_1",
"type":"Boolean",
"values":"{}"
}
]
},
"success": true
}
{
"code":500,
"msg":"system error,please contact the admin",
"success":false
}
You can use this method to issue commands to a device based on the unique ID of the device.
action | version |
---|---|
device.controlDevice | 1.0 |
Parameter | Type | Description |
---|---|---|
device_id | String | Unique ID of a device. |
commands | Array | Instruction set. |
Parameters in commands
Parameter | Type | Description |
---|---|---|
code | String | Command code. |
value | Object | Command value. |
Parameter | Type | Description |
---|---|---|
code | Integer | Response code.Null indicates success. |
success | Boolean | Request result. true: success false: failure |
msg | String | Exception message.It is null when the request result is success. |
result | Boolean | Operation object. |
OpenSaasSDK.request({
data: {
action: "device.getDeviceSpecifications",
vsersion: "1.0",
params: {
"device_id": "6c760b4d96688******_switch_2",
"commands": [
{
"code": "switch_1",
"value": false
}
]
}
}
});
{
"result": true,
"success": true
}
{
"code":500,
"msg":"system error,please contact the admin",
"success":false
}
You can use this method to control a device switch based on the unique ID of the device.
action | version |
---|---|
device.controlDeviceSwitch | 1.0 |
Parameter | Type | Description |
---|---|---|
device_id | String | Unique ID of a device. |
value | Boolean | Device switch. The values are as follows: true: on false: off |
Parameter | Type | Description |
---|---|---|
code | Integer | Response code.Null indicates success. |
success | Boolean | Request result. true: success false: failure |
msg | String | Exception message.It is null when the request result is success. |
result | Boolean | Operation object. |
OpenSaasSDK.request({
data: {
action: "device.controlDeviceSwitch",
vsersion: "1.0",
params: {
"device_id": "6c760b4d96688******_switch_2",
"value": true
}
}
});
{
"result": true,
"success": true
}
{
"code":500,
"msg":"system error,please contact the admin",
"success":false
}
You can use this method to control an infrared device based on the unique ID of the device.
action | version |
---|---|
device.controlInfraredDevice | 1.0 |
Parameter | Type | Description |
---|---|---|
infrared_id | String | Unique ID of an infrared device. |
standard_key | String | Standard key of an infrared device. |
Parameter | Type | Description |
---|---|---|
code | Integer | Response code.Null indicates success. |
success | Boolean | Request result. true: success false: failure |
msg | String | Exception message.It is null when the request result is success. |
result | Boolean | Operation object. |
OpenSaasSDK.request({
data: {
action: "device.controlInfraredDevice",
vsersion: "1.0",
params: {
"infrared_id": "6c760b4d966882******",
"standard_key": "PowerOn"
}
}
});
{
"result": true,
"success": true
}
{
"code":500,
"msg":"system error,please contact the admin",
"success":false
}
You can use this method to obtain the latest status of an infrared device based on the unique ID of the device.
action | version |
---|---|
device.getInfraredStatus | 1.0 |
Parameter | Type | Description |
---|---|---|
infrared_id | String | Unique ID of an infrared device. |
Parameter | Type | Description |
---|---|---|
code | Integer | Response code.Null indicates success. |
success | Boolean | Request result. true: success false: failure |
msg | String | Exception message.It is null when the request result is success. |
result | Object | Result Object |
OpenSaasSDK.request({
data: {
action: "device.getInfraredStatus",
vsersion: "1.0",
params: {
"infrared_id": "6c760b4d966882******"
}
}
});
{
"result": {
"mode": "4",
"temp": "22",
"power": "1",
"wind": "2"
},
"success": true
}
{
"code":500,
"msg":"system error,please contact the admin",
"success":false
}
You can use this method to obtain the scene list.
action | version |
---|---|
scene.listScenes | 1.0 |
Parameter | Type | Description |
---|---|---|
code | Integer | Response code.Null indicates success. |
success | Boolean | Request result. true: success false: failure |
msg | String | Exception message.It is null when the request result is success. |
result | Array | Scene list. |
Parameters in result
Parameter | Type | Description |
---|---|---|
scene_id | String | Unique ID of a scene. |
name | String | Scene name. |
OpenSaasSDK.request({
data: {
action: "scene.listScenes",
vsersion: "1.0"
}
});
{
"result": [
{
"scene_id": "*******",
"name": "Turn on all lights"
}
],
"success": true
}
{
"code":500,
"msg":"system error,please contact the admin",
"success":false
}
You can use this method to trigger a scene based on the unique ID of the scene.
action | version |
---|---|
scene.triggerScene | 1.0 |
Parameter | Type | Description |
---|---|---|
scene_id | String | Unique ID of a scene. |
Parameter | Type | Description |
---|---|---|
code | Integer | Response code.Null indicates success. |
success | Boolean | Request result. true: success false: failure |
msg | String | Exception message.It is null when the request result is success. |
result | Boolean | Operation result. |
OpenSaasSDK.request({
data: {
action: "scene.triggerScene",
vsersion: "1.0",
params: {
"scene_id": "6c760******f"
}
}
});
{
"result": true,
"success": true
}
{
"code":500,
"msg":"system error,please contact the admin",
"success":false
}
import { SaasMqtt } from 'xxx/openSaasSdk'
Method description: You can use this method to establish an MQTT connection.
Method: SaasMqtt.connectMqtt(config)
Parameter | Type | Description |
---|---|---|
config | Object | MQTT connection configuration. |
Parameters in config
Parameter | Type | Description |
---|---|---|
client_id | String | ID of the client connected to MQTT. The value a unique and constant mapping generated based on the user account and unique_id. One client ID can be used for both publishing and subscription. |
expire_time | String | Expiration time of the current configuration.When the current configuration is invalid, all connections are broken. |
username | String | User name in an MQTT connection. The value is a unique and constant mapping generated based on the user account. |
password | String | Password in an MQTT connection.This parameter remains unchanged before the expiration time. |
sink_topic | Map | MQTT topic corresponding to device control messages delivered by a user. The format is cloud/token/out/{dev_id}. In the format, {dev_id} indicates the ID of the controlled IoT device. |
source_topic | Map | MQTT topic corresponding to messages pushed from a device to the account. Multiple client IDs can be used to obtain connections. |
url | String | MQTT connection URL, which contains the protocol, IP address, and port number. |
async function () {
const { result } = await OpenSaasSDK.getgetMqttConfig()
SaasMqtt.connectMqtt(result)
}
Method description: You can use this method to listen to MQTT instances.
Method: SaasMqtt.on(event, fn)
Parameter | Type | Description |
---|---|---|
event | Object | Name of a listening event. |
fn | Function | Function for handling a listening event. |
Parameters in event
Parameter | Type | Description |
---|---|---|
close | String | Connection closure. |
disconnect | String | Disconnection. |
offline | String | Going offline. |
error | String | Error. |
message | String | Message receiving. |
Special Case
When the listening event is message, three callback parameters are available for the fn callback function.
Parameters in event
Parameter | Type | Description |
---|---|---|
topic | String | Message queue topic. |
message | String | Message body. |
packet | String | Packet. |
SaasMqtt.on('message', (topic, message) => {
console.log(message)
});
We recommend that you assign the MQTT object to a global variable and configure the message body to process the message push events of the corresponding device.
MQTT disconnection often occurs due to the WebSocket mechanism of WeChat. Therefore, you need to add the following code to the onShow lifecycle listening function in the app.js file:
// Obtain MQTT connection configuration (-config).
SassMqtt.connectMqtt(config)
SassMqtt.on(‘close’, (errMsg) => {
// Obtain the MQTT connection configuration.
SassMqtt.connectMqtt(config)
console.log(‘errorMsg: mqttClose’, errMsg)
})
SassMqtt.on(‘error, (errMsg) => {
// Obtain the MQTT connection configuration.
SassMqtt.connectMqtt(config)
console.log(‘errorMsg: mqttError, errMsg)
})
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback