@tuya-miniapp/sdm
v1.0.5
Published
Smart Device Model
Downloads
4
Readme
English | 简体中文
@tuya-miniapp/sdm
Smart Device Model
Installation
$ npm install @tuya-miniapp/sdm
# or
$ yarn add @tuya-miniapp/sdm
# or
$ pnpm add @tuya-miniapp/sdm
Usage
import { SmartDeviceModel } from '@tuya-miniapp/sdm';
// SmartDevices comes from typings/sdm.d.ts,negligible for non Typescript developers
export const devices = {} as SmartDevices;
SmartDeviceModel.init<SmartDeviceSchema>().then(device => {
/**
* It is recommended that the name of the smart device be used as the key name for assignment
*/
devices.robot = device;
});
devices.robot.model.actions.power.toggle()
Ray / React Project
// src/app.tsx
import React from 'react';
import 'ray';
import '@/i18n';
import { kit } from '@ray-js/panel-sdk';
import { SdmProvider } from '@ray-js/sdm-react';
import { devices } from '@/devices';
const { initPanelEnvironment } = kit;
interface Props {
children: React.ReactNode;
}
initPanelEnvironment({ useDefaultOffline: true });
export default class App extends React.Component<Props> {
onLaunch() {
console.info('=== App onLaunch');
}
render() {
return (
<SdmProvider value={devices.robot}>{this.props.children}</SdmProvider>
);
}
}
// src/pages/home/index.tsx
import React from 'react';
import { Button, View } from '@ray-js/components';
import { devices } from '@/devices';
export default function Home() {
const device = devices.robot;
return (
<View>
<Button onClick={() => device.model.actions.power.toggle()}>
Click me toggle power
</Button>
</View>
);
}