我正在进行API调用以使所有设备都连接到本地服务器。然后,我试图遍历所有连接的设备,并进行更多调用以获取一些数据。我将如何遍历所有设备,而不仅仅是阵列中的第一个设备?
这是我存储在设备变量中的响应
[{“ connected”:true,“ functions”:[“ SetGain”,“ SetIntegTime”,“ SetInterrupt”,“ Enable”,“ begin”],“ id”:“ 1f0039000547363339343638”,“ last_app”:null ,“ last_heard”:“ 2018-10-26T16:01:34.974Z”,“ last_ip_address”:“ :: ffff:10.10.10.5”,“名称”:“ actually-jail”,“ platform_id”:6,“ product_firmware_version “:65535,” product_id“:6,” status“:” normal“,” variables“:{” RawData“:” string“,” ColorTemp“:” string“,” Lux“:” string“,” SensorArray“ :“ string”,“ Gain”:“ string”,“ IntegraTime”:“ string”,“ TemperatureC”:“ double”,“ TemperatureF”:“ double”,“ TemperatureK”:“ double”,“ TemperatureR”:“ double“}},{” connected“:false,” functions“:null,” id“:” 3a003a001047047363333343437“,” last_app“:null,” status“:” normal“,” variables“:null}]
async function getDevices() {
let devices = await axios('http://10.10.10.1:8080/v1/devices/?access_token=687b5aee0b82f653c725570422dea5d37de6b65f')
return this.devices = (devices.data[0].id)};
async function getVariables() {
try {
const devices = await getDevices();
console.log(devices);
const RawDataPromise = axios(API + devices + '/RawData/?access_token=' + Token);
const ColorTempPromise = axios(API + devices + '/ColorTemp/?access_token=' + Token);
const LuxPromise = axios(API + devices + '/Lux/?access_token=' + Token);
const SensorArrayPromise = axios(API + devices + '/SensorArray/?access_token=' + Token);
const GainPromise = axios(API + devices + '/Gain/?access_token=' + Token);
const IntegraTimePromise = axios(API + devices + '/IntegraTime/?access_token=' + Token);
const TemperatureCPromise = axios(API + devices + '/TemperatureC/?access_token=' + Token);
const TemperatureFPromise = axios(API + devices + '/TemperatureF/?access_token=' + Token);
const TemperatureKPromise = axios(API + devices + '/TemperatureK/?access_token=' + Token);
const TemperatureRPromise = axios(API + devices + '/TemperatureR/?access_token=' + Token);
const [RawData, ColorTemp, Lux, SensorArray, Gain, IntegraTime, TemperatureC, TemperatureF, TemperatureK, TemperatureR] = await Promise.all([RawDataPromise, ColorTempPromise, LuxPromise, SensorArrayPromise,GainPromise,IntegraTimePromise,TemperatureCPromise,TemperatureFPromise,TemperatureKPromise,TemperatureRPromise]);
console.log(RawData.data.result, ColorTemp.data.result,Lux.data.result,SensorArray.data.result,Gain.data.result,IntegraTime.data.result,TemperatureC.data.result,TemperatureF.data.result,TemperatureK.data.result,TemperatureR.data.result);
} catch (e) {
console.error(e)
}
}
getVariables();
答案 0 :(得分:1)
创建一个包装器函数以从单个设备获取值,然后使用Promise.all(或Promise.map/如果使用bluebird,则每次调用)多次调用该函数;
async function getDevices() {
let devices = await axios('http://10.10.10.1:8080/v1/devices/?access_token=687b5aee0b82f653c725570422dea5d37de6b65f');
return this.devices = (devices.data)
};
async function getForOneDevice(device) {
// I've assumed here that 'devices' in your API calls was a typo, and was intended to be devices[0] (based on the question)
const RawDataPromise = axios(API + device + '/RawData/?access_token=' + Token);
const ColorTempPromise = axios(API + device + '/ColorTemp/?access_token=' + Token);
const LuxPromise = axios(API + device + '/Lux/?access_token=' + Token);
const SensorArrayPromise = axios(API + device + '/SensorArray/?access_token=' + Token);
const GainPromise = axios(API + devices + '/Gain/?access_token=' + Token);
const IntegraTimePromise = axios(API + device + '/IntegraTime/?access_token=' + Token);
const TemperatureCPromise = axios(API + device + '/TemperatureC/?access_token=' + Token);
const TemperatureFPromise = axios(API + device + '/TemperatureF/?access_token=' + Token);
const TemperatureKPromise = axios(API + device + '/TemperatureK/?access_token=' + Token);
const TemperatureRPromise = axios(API + device + '/TemperatureR/?access_token=' + Token);
return Promise.all([RawDataPromise, ColorTempPromise, LuxPromise, SensorArrayPromise,GainPromise,IntegraTimePromise,TemperatureCPromise,TemperatureFPromise,TemperatureKPromise,TemperatureRPromise]);
}
async function getVariables() {
try {
const devices = await getDevices();
console.log(devices);
const promises = [];
devices.forEach((device) => {
promises.push(getForOneDevice(device.id));
})
const allResults = Promise.all(promises);
} catch (e) {
console.error(e)
}
}
getVariables();