Updated dependencies. Also changed the way a controller object is created

This commit is contained in:
Glenn de Haan
2023-01-02 17:28:42 +01:00
parent 19e3d7e04c
commit 93c9ccaccb
3 changed files with 248 additions and 209 deletions

View File

@@ -16,13 +16,6 @@ const config = {
}
};
/**
* Create new UniFi controller object
*
* @type {Controller}
*/
const controller = new unifi.Controller({host: config.unifi.ip, port: config.unifi.port, site: config.unifi.siteID, sslverify: false});
/**
* Exports the UniFi voucher function
*
@@ -31,6 +24,16 @@ const controller = new unifi.Controller({host: config.unifi.ip, port: config.uni
*/
module.exports = (type) => {
return new Promise((resolve) => {
/**
* Create new UniFi controller object
*
* @type {Controller}
*/
const controller = new unifi.Controller({host: config.unifi.ip, port: config.unifi.port, site: config.unifi.siteID, sslverify: false});
/**
* Login and create a voucher
*/
controller.login(config.unifi.username, config.unifi.password).then(() => {
controller.getSitesStats().then(() => {
controller.createVouchers(type.expiration, 1, type.usage === 1 ? 1 : 0, null, typeof type.upload !== "undefined" ? type.upload : null, typeof type.download !== "undefined" ? type.download : null, typeof type.megabytes !== "undefined" ? type.megabytes : null).then((voucher_data) => {
@@ -38,18 +41,22 @@ module.exports = (type) => {
const voucher = `${[voucher_data_complete[0].code.slice(0, 5), '-', voucher_data_complete[0].code.slice(5)].join('')}`;
resolve(voucher);
}).catch((e) => {
console.log('Error while getting voucher!');
console.log(e);
process.exit(1);
});
}).catch((e) => {
console.log('Error while creating voucher!');
console.log(e);
process.exit(1);
});
}).catch((e) => {
console.log('Error while getting site stats!');
console.log(e);
process.exit(1);
});
}).catch((e) => {
console.log('Error while logging in!');
console.log(e);
process.exit(1);
});