huawei-wingle-4g
Version:
This is a module NodeJS allowing to drive Huawei Wingle 4G. This module can :
143 lines • 7.15 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const log4js_1 = __importDefault(require("log4js"));
const StringUtils_1 = require("../../utils/StringUtils");
class default_1 {
constructor(login) {
this.login = login;
this.connection = login.getConnnection();
this.logger = log4js_1.default.getLogger(StringUtils_1.substringAfter(__filename, 'huawei-wingle-4g'));
}
activeLog(activeLog) {
this.logger.level = activeLog ? 'debug' : 'OFF';
this.login.activeLog(activeLog);
}
getStatistics() {
return __awaiter(this, void 0, void 0, function* () {
yield this.connection.openHomePage();
yield this.login.login();
const monthlyDataUsedDocument = yield this.connectMonthlyDataUsed();
const monthlyDataUsed = yield this.getMonthlyDataUsed(monthlyDataUsedDocument);
const totalDataUsed = yield this.getTotalDataUsed(monthlyDataUsed.duration, monthlyDataUsed.used);
const lastClearTime = yield this.getLastClearTime(monthlyDataUsedDocument);
return { monthlyDataUsed, totalDataUsed, lastClearTime };
});
}
connectMonthlyDataUsed() {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.connection.get('/api/monitoring/month_statistics');
return response.document;
});
}
getMonthlyDataUsed(document) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
const rawDuration = (_a = document.querySelector('MonthDuration')) === null || _a === void 0 ? void 0 : _a.textContent;
;
if (!rawDuration) {
throw new Error('Unable to retrieve monthly duration');
}
const duration = +rawDuration * 1000;
this.logger.debug(`Monthly duration : ${duration}`);
const rawDownload = (_b = document.querySelector('CurrentMonthDownload')) === null || _b === void 0 ? void 0 : _b.textContent;
if (!rawDownload) {
throw new Error('Unable to retrieve monthly data downloaded');
}
const download = +rawDownload;
this.logger.debug(`Monthly data downloaded : ${download}`);
const rawUpload = (_c = document.querySelector('CurrentMonthUpload')) === null || _c === void 0 ? void 0 : _c.textContent;
if (!rawUpload) {
throw new Error('Unable to retrieve monthly data uploaded');
}
const upload = +rawUpload;
this.logger.debug(`Monthly data uploaded : ${download}`);
const used = download + upload;
this.logger.debug(`Monthly data used : ${used}`);
const limit = yield this.getMonthlyDataLimit();
return { duration, used, limit };
});
}
getMonthlyDataLimit() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.connection.get('/api/monitoring/start_date');
const document = response.document;
const rawDataLimit = (_a = document.querySelector('DataLimit')) === null || _a === void 0 ? void 0 : _a.textContent;
if (!rawDataLimit) {
throw new Error('Unable to retrieve monthly limit data');
}
const matcher = /(\d+)([A-Z]+)/.exec(rawDataLimit);
if (matcher) {
let data = +matcher[1];
switch (matcher[2]) {
case 'GB':
return data * Math.pow(1024, 3);
case 'MB':
return data * Math.pow(1024, 2);
case 'KB':
return data * 1024;
}
}
throw new Error(`Unable to extract monthly data limit from : ${rawDataLimit}`);
});
}
getTotalDataUsed(monthDuration, monthDataUsed) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.connection.get('/api/monitoring/traffic-statistics');
const document = response.document;
const rawTotalConnectTime = (_a = document.querySelector('TotalConnectTime')) === null || _a === void 0 ? void 0 : _a.textContent;
if (!rawTotalConnectTime) {
throw new Error('Unable to retrieve total connect time');
}
let duration = +rawTotalConnectTime;
if (duration < monthDuration) {
duration = monthDuration;
}
this.logger.debug(`Total connect time : ${duration}`);
const rawTotalDownload = (_b = document.querySelector('TotalDownload')) === null || _b === void 0 ? void 0 : _b.textContent;
if (!rawTotalDownload) {
throw new Error('Unable to retrieve total data downloaded');
}
const rawTotalUpload = (_c = document.querySelector('TotalUpload')) === null || _c === void 0 ? void 0 : _c.textContent;
if (!rawTotalUpload) {
throw new Error('Unable to retriebe total data uploaded');
}
let used = +rawTotalDownload + +rawTotalUpload;
if (used < monthDataUsed) {
used = monthDataUsed;
}
this.logger.debug(`Total data used : ${used}`);
return { duration, used };
});
}
getLastClearTime(document) {
var _a;
const monthLastClearTime = (_a = document.querySelector('MonthLastClearTime')) === null || _a === void 0 ? void 0 : _a.textContent;
if (!monthLastClearTime) {
throw new Error('Unable to retrieve month last clear time');
}
const matcher = /(\d+)-(\d+)-(\d+)/.exec(monthLastClearTime);
if (matcher) {
const year = matcher[1];
const month = StringUtils_1.toTwo(+matcher[2]);
const date = StringUtils_1.toTwo(+matcher[3]);
return +new Date(`${year}-${month}-${date}T00:00:00.000Z`);
}
throw new Error(`Unable to retrieve lase clear time from : ${monthLastClearTime}`);
}
}
exports.default = default_1;
//# sourceMappingURL=StatisticsExtractor.js.map