IOT篇:涂鸦云开发

289 阅读1分钟

后端:IOT万能遥控


const { TuyaContext } = require('@tuya/tuya-connector-nodejs');

const context = new TuyaContext({
    baseUrl: 'https://openapi.tuyacn.com',
    accessKey: '****',
    secretKey: '****',
});

const remoteCtr = async () => {
    //定义设备id
    const device_id = "3444686684f3ebf4c2e3";
    //下发指令,万能遥控需要查询到,categoryId,remoteIndex
    //相关文档:[获取品类支持的品牌列表-云服务 API 参考-涂鸦开发者 (tuya.com)](https://developer.tuya.com/cn/docs/cloud/28754b7982?id=Kb3oe3q9ybzuw)
    const commands = await context.request({
        path: `/v2.0/infrareds/${device_id}/remotes/6ca57b27ee6d953b45fbyp/command`,
        method: 'POST',
        body: {
            "categoryId": 1,
            "remoteIndex": 5122,
            "key": "Power"
        }
    });
    if (!commands.success) {
        new Error();
    }
    console.log("执行结果:", commands);
    // return Promise.resolve(commands)
    return commands
};

module.exports = { remoteCtr }

//前端:调用该接口

//开关控制
const remoteCtr = async () => {
  const res = await axios.get("/api/remoteCtr")
  console.log(res)
}