I am trying to fetch data from an api from different endpoints(ip adresses) using await statement. It seems to work for some but not others. However, when I use "then" (the commented code" statements it works for all.
The purpose is to save a .txt log file. The result is the same on the saved .txt file and on the console logging as well
Any help is appreciated. Thanks!
Here is my code that's fetching and logging data.
class Status {
async doFetch(api, ep) {
// api.status.get('SystemUnit ProductId')
// .then(res => {
// console.log(res)
// this.acc += res + '\r\n'
// }).then( () => {
// api.status.get('UserInterface ContactInfo Name')
// .then(name => {
// console.log(name)
// this.acc += name + '\r\n';
// })
// })
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const PRODUCT_ID = await api.status.get('SystemUnit ProductId');
const SYSTEM_NAME = await api.status.get('UserInterface ContactInfo Name');
const SOFTWARE_VERSION = await api.status.get('SystemUnit Software Version');
const MAC_ADDRESS = await api.status.get('Network 1 Ethernet MacAddress');
const TEMPERATURE = await api.status.get('SystemUnit Hardware Temperature');
const SIP_Registration_Status = await api.status.get('SIP Registration 1 Status');
const SIP_Proxy_Address = await api.status.get('SIP Proxy 1 Address');
const SIP_Registration_URI = await api.status.get('SIP Registration 1 URI');
await timeout(500);
return`Product ID: ${await PRODUCT_ID}\r\n
System name: ${await SYSTEM_NAME}\r\n
IP address: ${ep}\r\n
Software version: ${await SOFTWARE_VERSION}\r\n
MAC address: ${await MAC_ADDRESS}\r\n
Temperature: ${await TEMPERATURE}˚\r\n
Sip registration status: ${await SIP_Registration_Status}\r\n
Sip proxy address: ${await SIP_Proxy_Address}\r\n
Sip registration URL: ${await SIP_Registration_URI}\r\n\r\n
`;
}
}
(function() {
const server = new Server(1000);
logs.config(FILE_NAME, DIR);
let api;
let ep;
const toRun = (body) => {
api = body.api;
ep = body.endpoint
const s = new Status();
if (ep !== '' && ep !== undefined)
s.doFetch(api, ep)
.then(acc => {
console.log(acc)
logs.buildString(acc);
})
.catch(err => {
if (err.code !== 3)
console.error(err);
});
}
server.runCommand(toRun, logs.save)
})()