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.

Relationship learning

A panel is a product that represents an IoT smart device in the Tuya 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 strip lights template is developed based on SDM (Smart Device Model). For more information about SDM, see Smart Device Model.

Concepts

Product name: Dreamcolor strip lights (RGB)

Product introduction

The strip lights support cool white light (C), cool and warm light (CW), colored light (RGB), cool white and colored light (RGBC), and white and colored light (RGBCW) modes based on the data points of the white light mode and multicolor light mode.

Definition:

Requirement prototype

Features

The strip lights template must have the following data points.

switch_led,
work_mode,
paint_colour_data,
dreamlight_scene_mode,
dreamlightmic_music_data,
light_pixel,
lightpixel_number_set,

The strip lights template has the following available data points that can be selected.

countdown,
light_length,

Switch

Parameter

Value

dpid

20

code

switch_led

type

Boolean

mode

Send and report (read-write)

Working mode

Parameter

Value

dpid

21

code

work_mode

type

Enum

mode

Send and report (read-write)

property

Enum values: white, colour, scene, music

Countdown

Parameter

Value

dpid

26

code

countdown

type

value

mode

Send and report (read-write)

property

{ "unit": "s", "min": 0, "max": 86400, "scale": 0, "step": 1, "type": "value"}

Strip lights length

Parameter

Value

dpid

46

code

light_length

type

value

mode

Report only (read-only)

property

{"unit": "cm", "min": 1, "max": 10000, "scale": 0, "step": 1, "type": "value" }

Strip lights bead number

Parameter

Value

dpid

47

code

light_pixel

type

value

mode

Report only (read-only)

property

{ "min": 1, "max": 1024, "scale": 0, "step": 1, "type": "value" }

Bead/Length setting

Parameter

Value

dpid

53

code

lightpixel_number_set

type

value

mode

Send and report (read-write)

property

{ "min": 1, "max": 1000, "scale": 0, "step": 1, "type": "value" }

The data points with a data type other than Raw are listed above. For data points with the data type of Raw, see the specific protocol.

Create dreamcolor strip lights, define data points, and configure the data points in the panel.

Log in to Tuya Developer Platform and create dreamcolor strip lights.

  1. Go to Product > Development and click Create on the Product Development page.
  2. On the Standard Category tab, select Lighting. In the Select a product step, select Dreamcolor Strip Light.
  3. Select a smart mode and an RGB solution and complete the product information. For example, enter PublicLamp for Product Name.
  4. Click Create.

Note: This template only supports dreamcolor RGB strip lights. For other modes, configure the product according to the logic.

πŸŽ‰ By now, dreamcolor strip lights named PublicLamp are created.

Create panel miniapp on Tuya MiniApp Developer Platform

Log in to Tuya MiniApp Developer Platform to create a panel miniapp.

For more information, see Panel MiniApp > Create panel miniapp.

Pull and run template project

Panel template repository: Repositories.

  1. Pull the project.
    git clone https://github.com/Tuya-Community/tuya-ray-materials.git
    
  2. Select the strip lights template, install the dependencies, and start the project.
    cd ./template/PublicPanelStripLamp
    
  3. Open IDE and click Import.
  4. Enter the project path for import and associate the project with the created panel miniapp and product.
  5. Associate the device, and IDE automatically installs dependencies and creates the project.

Project directory

By now, the initialization of the development of a panel miniapp is completed. The introduction to the project directory is as follows.

β”œβ”€β”€ public
β”‚ β”œβ”€β”€ images # Static resources of the project.
β”œβ”€β”€ src
β”‚ β”œβ”€β”€ app.config.ts # Configurations generated automatically.
β”‚ β”œβ”€β”€ app.tsx # App root component.
β”‚ β”œβ”€β”€ components # Component directory.
β”‚ β”œβ”€β”€ constant # Constant directory.
β”‚ β”œβ”€β”€ containers # Aggregated component directory.
β”‚ β”œβ”€β”€ devices # Smart device model directory.
β”‚ β”‚ β”œβ”€β”€ index.ts # Smart device model definition and export.
β”‚ β”‚ β”œβ”€β”€ protocols # Declaration definition of the DP required by the device.
β”‚ β”‚ β”œβ”€β”€β”€β”€β”€parses # Complex protocol definition of the DP required by the device.
β”‚ β”‚ └── 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.

Requirement implementation

Generate SDM schema in IDE for project

Generate SDM schema for the project by referring to src/devices/schema.ts.

export const defaultSchema = [
	{
		attr: 641,
		canTrigger: true,
		code: "switch_led",
		defaultRecommend: true,
		editPermission: false,
		executable: true,
		extContent: "",
		iconname: "icon-dp_power",
		id: 20,
		mode: "rw",
		name: "Switch",
		property: {
			type: "bool",
		},
		type: "obj",
	},
	{
		attr: 640,
		canTrigger: true,
		code: "work_mode",
		defaultRecommend: true,
		editPermission: false,
		executable: true,
		extContent: "",
		iconname: "icon-dp_mode",
		id: 21,
		mode: "rw",
		name: "Mode",
		property: {
			range: ["white", "colour", "scene", "music"],
			type: "enum",
		},
		type: "obj",
	},
	// ...
] as const;

Homepage: Multicolor light adjustment

Requirement analysis

  1. Add the on/off button component to the homepage.
    • Display different button icons according to the value sent by switch_led.
    • Tap the on/off button component to report the DP value of the on and off events.
  2. Check whether the DP identifier of the on/off button is switch_led. If yes, pass this attribute to the component.
  3. Use useProps to get the value of the data point sent in real time.Example:
    import React from "react";
    import { View } from "@ray-js/ray";
    import { useProps } from "@ray-js/panel-sdk";
    
    export default function () {
    	const power = useProps((props) => props.switch_led);
    	return (
         <View style={{ flex: 1 }}>
             <View>switch_led: {power}</View>
         </View>
    	);
    }
    
  4. Use useActions to send the data point. For more information about how to use SDM to send data points, see useActions.

Example:

import React from "react";
import { View } from "@ray-js/ray";
import { useActions } from "@ray-js/panel-sdk";

export default function () {
	const handleTogglePower = () => {
		actions.switch_led.toggle({ throttle: 300 });
	};
	return (
		<View style={{ flex: 1 }}>
			<Button img={power} onClick={handleTogglePower} />
		</View>
	);
}

On/Off button component implementation

Implement the on/off button component based on the analysis. For more information, see ControlBar.

import React from "react";
import res from "@/res";
import { View, Image } from "@ray-js/ray";
import dpCodes from "@/config/dpCodes";
import { Button } from "@/components";
import { useActions, useProps } from "@ray-js/panel-sdk";
import styles from "./index.module.less";

export const PowerButton = () => {
	const power = useProps((props) => props.switch_led);
	const actions = useActions();
	const handleTogglePower = () => {
		actions.switch_led.toggle({ throttle: 300 });
	};
	return (
		<View className={styles.container}>
			<Image className={styles.bg} mode="aspectFill" src={bottom_dark} />
			<Button
				img={power}
				onClick={handleTogglePower}
				imgClassName={styles.powerBtn}
				className={styles.powerBox}
			/>
		</View>
	);
};

Other