@trickfilm400/cs141-ups-mangement-card
Version:
Node.JS API for CS141 Management Card for UPS Systems
67 lines (66 loc) • 2.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CS141 = void 0;
const axios_1 = require("axios");
const https = require("https");
class CS141 {
constructor(host, username, password) {
this.host = host;
this.username = username;
this.password = password;
this.axios = axios_1.default.create({
baseURL: 'https://' + this.host,
httpsAgent: new https.Agent({ rejectUnauthorized: false }),
});
}
request() {
return new Promise((resolve, reject) => {
const requestHeader = new axios_1.AxiosHeaders();
if (this.headers && this.headers['set-cookie'])
requestHeader['cookie'] = this.headers['set-cookie'].join(';');
this.axios
.get(this.host + '/api/devices/ups/report', {
headers: requestHeader,
})
.then((res) => {
resolve(res);
})
.catch(reject);
});
}
handleRequest() {
return new Promise((resolve, reject) => {
this.request()
.then((request) => {
resolve(request.data);
})
.catch((e) => {
if (e.code === 'ERR_BAD_REQUEST') {
this.login()
.then((login) => {
this.headers = login.headers;
this.request().then((d) => resolve(d.data), reject);
})
.catch(reject);
}
});
});
}
login() {
return new Promise((resolve, reject) => {
this.axios
.post(this.host + '/api/login', {
userName: this.username,
password: this.password,
})
.then((res) => {
if (res.status == 200)
resolve(res);
else
reject(new Error('No 200 HTTP Status message'));
})
.catch(reject);
});
}
}
exports.CS141 = CS141;