Usage
Create SDM
The following directory contains all the files required for this chapter. You can also directly develop based on the public-sdm sample project.
src
devices
- index.ts
- schema.ts
pages
home
- index.tsx
- app.tsx
typings
- sdm.d.ts
Generating Smart Device Model
This example file is located at devices/index.ts, and it is used to generate intelligent device models for use in subsequent business projects.
Defining Smart Device Schema File
This example file is located at src/devices/schema.ts and is used to define the DP (Data Point) function descriptions of the intelligent device. It enables development based on the current intelligent device's functional definitions while adhering to TypeScript type constraints in subsequent business projects.
You can inject it into the project through the SDM tab in the Tuya MiniApp IDE device tool, as shown in the figure below.
Please ensure to add the as const assertion to the schema in order to enable proper type inference.
Global Declaration of Smart Device Types
This example file is located at typings/sdm.d.ts and is used to globally declare type definitions related to SDM. It allows developers to easily access TS type hints for smart device-related Hooks in their projects. Non-TypeScript developers may ignore this type definition file.
Please note that this step must be performed after the completion of the smart device descriptor file in the previous step, otherwise it may result in failed TS type inference.
Use SDM
Using SdmProvider in the Root Component
This example file is located at src/app.tsx, where the SdmProvider is used in the root component. It will wait for the initialization of the provided device to complete before rendering the child components. This ensures that the accompanying SDM hooks can be used in any page component thereafter.
Controlling Devices through SDM
This example file is located at src/pages/home/index.tsx. It controls the on/off state of smart devices and retrieves real-time values using the accompanying useActions hooks.
Multi-Device
To manage multiple devices in the same Mini Program, use the Multi-Device Management capability: inject SmartDevicesManager via SdmProvider's deviceManager and use multi-device Hooks (useDevices, useDevicesProps, useDevicesActions, etc.). See Multi-Device Management - Usage and Multi-Device Management - Hooks.