This document is intended for developers who understand panel miniapp development, panel miniapp, and product functions. If you have questions about the above knowledge, learn the following prerequisite knowledge. Skip this document if you have mastered the mentioned knowledge.
A panel is a product that represents an IoT smart device in the Smart Life app. Before you create a product, you shall understand the definition of the panel and the relationship among the panel, product, and device.
The electrical socket template is developed based on SDM(Smart Device Model)
. For more information about SDM
, see Smart Device Model.
Product name: Universal electrical socket
The electrical socket provides the basic function of sockets, such as on/off control, and other functions such as countdown timer disabling, electricity adding, current update, and power. The electrical socket development tutorial helps you develop a panel miniapp to control sockets.
switch_1
by tapping on the button in the middle of the homepage.countdown_1
. If no countdown timer is configured, no reminder is displayed.send and report (read-write)
data points except switch_1
and countdown_1
.The electrical socket template must have the following data points:
switch_1,
countdown_1,
dp function | dpid | code | type | mode | property |
Switch | 1 | switch_1 | Boolean | Send and report (read-write) | - |
Countdown | 9 | countdown_1 | Value | Send and report (read-write) | Value range: 0–86400, interval: 1, multiple: 0, unit: s |
Electricity | 17 | add_ele | Value | Send and report (read-write) | Value range: 0–50000, interval: 100, multiple: 3, unit: kwh |
Current | 18 | cur_current | Value | Send and report (read-write) | Value range: 0–80000, interval: 1, multiple: 0, unit: mA |
Power | 19 | cur_power | Value | Report only (read-only) | Value range: 0–200000, interval: 1, multiple: 1, unit: W |
Voltage | 20 | cur_voltage | Value | Report only (read-only) | Value range: 0–5000, interval: 1, multiple: 1, unit: V |
The data points required by the homepage are listed in the table above.
Create an electricity socket, define the data points, and configure the data points in the panel.
🎉 By now, an electrical socket named Public Socket is created.
Log in to Tuya MiniApp Developer Platform to create a panel miniapp.
Select the created panel miniapp in Tuya MiniApp Developer Platform and click Development to enter the Development page. In the Cloud Capabilities section, find MiniApp Device Logging Capability and MiniApp Chart Capability and click Subscribe Resource Bundle.
By now, the initialization of the development of a panel miniapp is completed. The introduction to the project directory is as follows.
├── src
│ ├── app.config.ts # Configurations generated automatically.
│ ├── app.tsx # App root component.
│ ├── components # Component directory.
│ ├── constant # Constant directory.
│ ├── devices # Smart device model directory.
│ │ ├── index.ts # Smart device model definition and export.
│ │ └── schema.ts # Description of the DP of the current smart device, which can be generated automatically by IDE.
│ ├── global.config.ts
│ ├── hooks # Custom hooks directory.
│ ├── i18n # Multilingual directory.
│ ├── pages # Page directory.
│ └── routes.config.ts # Route configuration.
│─── typings #Definition directory of business types.
│ └── sdm.d.ts #Definition files of smart device type.
Generate SDM schema
for the project to view src/devices/schema.ts
.
export const defaultSchema = [
{
attr: 1024,
canTrigger: true,
code: 'switch_1',
defaultRecommend: false,
editPermission: true,
executable: true,
extContent: '',
iconname: 'icon-dp_power2',
id: 1,
mode: 'rw',
name: 'Switch 1',
property: {
type: 'bool',
},
type: 'obj',
},
{
attr: 1120,
canTrigger: false,
code: 'countdown_1',
defaultRecommend: false,
editPermission: true,
executable: false,
extContent: '',
iconname: 'icon-dp_time2',
id: 9,
mode: 'rw',
name: 'Switch 1 countdown',
property: {
unit: 's',
min: 0,
max: 86400,
scale: 0,
step: 1,
type: 'value',
},
type: 'obj',
},
{
attr: 1120,
canTrigger: false,
code: 'add_ele',
defaultRecommend: false,
editPermission: true,
executable: false,
extContent: '{"trigger":"direct"}',
iconname: 'icon-battery',
id: 17,
mode: 'rw',
name: 'Electricity adding',
property: {
unit: 'kwh',
min: 0,
max: 50000,
scale: 3,
step: 100,
type: 'value',
},
type: 'obj',
},
{
attr: 1088,
canTrigger: true,
code: 'cur_current',
defaultRecommend: false,
editPermission: true,
executable: false,
extContent: '',
iconname: 'icon-Ele',
id: 18,
mode: 'ro',
name: 'Current',
property: {
unit: 'mA',
min: 0,
max: 80000,
scale: 0,
step: 1,
type: 'value',
},
type: 'obj',
},
{
attr: 1088,
canTrigger: true,
code: 'cur_power',
defaultRecommend: false,
editPermission: true,
executable: false,
extContent: '',
iconname: 'icon-dp_tool',
id: 19,
mode: 'ro',
name: 'Power',
property: {
unit: 'W',
min: 0,
max: 200000,
scale: 1,
step: 1,
type: 'value',
},
type: 'obj',
},
{
attr: 1088,
canTrigger: true,
code: 'cur_voltage',
defaultRecommend: false,
editPermission: true,
executable: false,
extContent: '',
iconname: 'icon-a_function_turbo',
id: 20,
mode: 'ro',
name: 'Voltage',
property: {
unit: 'V',
min: 0,
max: 5000,
scale: 1,
step: 1,
type: 'value',
},
type: 'obj',
},
];
switch_1
by tapping on the button in the middle of the homepageon/off button
component to the homepage.switch_1
.on/off button
component to report the DP value of the on and off events.switch_1
. If yes, pass this attribute to the component.useProps
to get the value of dp
sent in real time.Example:import React from 'react';
import { View } from '@ray-js/ray';
import { useProps } from '@ray-js/sdm-react';
export default function () {
const switch_1 = useProps((dpState) => dpState.switch_1);
return (
<View style={{ flex: 1 }}>
<View>switch_1: {switch_1}</View>
</View>
);
}
useActions
to get the action reported by dp
.Example:import React from 'react';
import { View } from '@ray-js/ray';
import { useActions } from '@ray-js/sdm-react';
export default function () {
const actions = useActions();
return (
<View style={{ flex: 1 }}>
<Button
onClick={() => {
actions.switch_1.set(false);
}}
>
change action
</Button>
</View>
);
}
import React from 'react';
import clsx from 'clsx';
import { View } from '@ray-js/ray';
import { DpBooleanAction } from '@tuya-miniapp/sdm';
import { useProps, useActions } from '@ray-js/sdm-react';
import { Icon } from '@/components/icon';
import { icons } from '@/res';
import styles from './index.module.less';
export interface Props {
dpCode: string;
}
export const PowerButton = React.memo<Props>(props => {
const { dpCode } = props;
const value = useProps(dpState => dpState[dpCode]);
const actions = useActions();
if (!dpCode || typeof value !== 'boolean') {
return null;
}
const action = actions[dpCode] as DpBooleanAction;
return (
<View className={styles['power-button']} onClick={action.toggle}>
<View
className={clsx(styles['power-button-content'], {
[`${styles['power-button-off']}`]: !value,
})}
/>
<Icon
className={styles['power-button-icon']}
d={icons.power}
size="34px"
fill={value ? '#ef550d' : '#ffffff'}
/>
</View>
);
});
dp
of the virtual panel. Tap the implemented component and view the IDE control panel status to verify the product functions.Display the reminder text based on the on/off status, clock format (12-hour or 24-hour) of the mobile phone, and the value of countdown_1
. If no countdown timer is configured, no reminder is displayed.
Create the countdown reminder component and control the displayed content based on the on/off status of switch_1
and the value of countdown_1
.
Implement countdown reminder component.
Display the countdown dialog box by tapping Countdown 1 at the bottom of the homepage to set the countdown timer. If a countdown timer is configured, this operation allows for disabling the countdown; otherwise, the dialog box is displayed.
countdown_1
.useActions
to report the status of countdown_1
.Implement countdown dialog box component.Enter the Electric Statistics page by tapping Statistics at the bottom of the homepage to check the current current, voltage, and power and get statistical data.
To get the statistics API, see Statistics.
Enter the Setting page by tapping Setting at the bottom of the homepage to set all send and report (read-write)
data points except switch_1
and countdown_1
.
send and report (read-write)
data points on the Setting page.useDevice
to get all values of dp
.import { useDevice } from '@ray-js/sdm-react';
const { devInfo } = useDevice();
// Get all data points.
const dpSchemas = devInfo.schema;
dp
.Implement setting page.Enter the Switch Logs page by tapping Switch Logs on the device page. To get the log reporting API, see getAnalyticsLogsStatusLog.