简体   繁体   English

Javascript,关闭多个设备

[英]Javascript, turn off multiple devices

I have rewritten my code so that I now have three lights which I turn off one by one. 我重写了代码,现在有了三盏灯,它们一一关掉。 Is there a way to place all three of them in a variable to just run the turn off command once? 有没有一种方法可以将它们全部放在一个变量中,以便只运行一次off命令?

// Turn off a bunch of lights

// Specify ID for a specific lamp
let KitchentableRight = await Homey.devices.getDevice({id: 'ef6bae9c-ec57-46ff-9011-905f90d16589'});
let KitchentableMiddle = await Homey.devices.getDevice({id: 'e29ab963-5139-4fdb-b4ec-ec25c103a62a'});
let KitchentableLeft = await Homey.devices.getDevice({id: '8b102bc6-00dd-4305-b111-f67a53ffd9d9'});

// Turn off tha lights based on their ID:s
KitchentableRight.setCapabilityValue('onoff', false);
KitchentableMiddle.setCapabilityValue('onoff', false);
KitchentableLeft.setCapabilityValue('onoff', false);

return true

to make the loop runs for each device it should be: 使每个设备的循环运行应该是:

 devices.forEach(device => {
       if(device.name) return false;
        console.log(device.name);
        device.setCapabilityValue('onoff', !device.state.onoff);
    })

if a is array of device's name, you can check if the device in a by: a.indexOf(device.name) != -1 如果a是设备名称的数组,则可以通过以下方式检查设备中的设备: a.indexOf(device.name) != -1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM