[Share]Introduction to Control Device Management in TuyaOS
I. Basic Introduction
The Central Control SDK provides interfaces to obtain a list of devices under the home that comply with standard commands and to control the devices.
II. Usage Process
1. Obtain Device List
You can obtain the device list using the interface tuya_get_dev_brief_info_list provided by the TuyaOS Central Control SDK. Note that the acquired device list has allocated some memory and needs to be released using tuya_dev_brief_info_list_free.
2. Obtain Room List
With TuyaOS Central Control SDK, you can use the interface tuya_get_room_info_list to get the room list. Note that the acquired room list has allocated some memory and needs to be released using tuya_room_info_list_free.
The data of every room is more or less as follows:
You can distinguish rooms based on the id. The device information of each room is stored in the dev_list, which contains the dev_id of the device in that room. For further details about the device, refer to the device details retrieved from tuya_get_dev_brief_info_list.
3. Obtain Shortcut Switch List
Using the TuyaOS Central Control SDK, the interface tuya_get_shortcut_switch_list can be used to obtain the shortcut list. Note that the acquired shortcut list has allocated some memory and needs to be released using tuya_shortcut_switch_list_free.
4. Control & Status Retrieval for Devices
1. Load Devices
Before using a device, we need to load it using tuya_home_ctrl_dev_open. This function internally prepares some device information and consumes some memory. The precise amount of memory consumed depends on the device's dp. When memory is tight, consider liberating the memory using tuya_home_ctrl_dev_close after use. Further, this function supports multiple instances – for a single device, multiple calls to tuya_home_ctrl_dev_open will retain only one piece of preparatory information. Any subsequent control (including but not limited to APP, the device itself, UI, voice) will synchronously push data to various tuya_home_ctrl_dev_open registered report_cb.function instances.
2. Unload Devices
Once you're done using a device, it can be unloaded using tuya_home_ctrl_dev_close.
3. Obtain Device Control Command
The SDK provides a tuya_home_ctrl_xxx.h file for various categories, which gives an exhaustive list of commands that can control this category. However, not all devices support all commands. In reality, you must use tuya_home_ctrl_dev_get_all_dp to find the actual set of commands that a device supports. Note that the memory allocated to the output parameter dp_sets of tuya_home_ctrl_dev_get_all_dp, even though managed by the SDK and freed when tuya_home_ctrl_dev_close is invoked, should be stored somewhere by the application to guard against changing instructions that may alter the internal command address, potentially causing program execution errors. Generally, when instructions change, the dps_update_cb.function callback is invoked. In earlier versions, you had to close all instances with tuya_home_ctrl_dev_close then reopen them with tuya_home_ctrl_dev_open to get the new instructions. However, more recent SDK versions allow you to retrieve the new instructions directly by calling tuya_home_ctrl_dev_get_all_dp again, but this is a required operation. If you don't call tuya_home_ctrl_dev_get_all_dp or reopen the device, any further control attempts will return an error.
4. Proactively Obtain Values of Device Instructions
You can use tuya_home_ctrl_dev_get_dp_value to get the value of a single instruction, or you can use tuya_home_ctrl_dev_get_all_dp to get all instruction values and then match the value you need.
5. Control Device Instructions
You can use tuya_home_ctrl_dev_set_dp_value to control a single instruction of a device. This instruction must be within the range fetched by the tuya_home_ctrl_dev_get_all_dp and its type should match.
6. Proactively Report after Device Instruction Value Change
When a device is controlled by any (including but not limited to APP, the device itself, UI, voice), the SDK will make a notification through the report_cb.function callback registered by tuya_home_ctrl_dev_open.
###