huawei-wingle-4g
Version:
This is a module NodeJS allowing to drive Huawei Wingle 4G. This module can :
86 lines • 4.01 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 Connection_1 = __importDefault(require("../../connection/Connection"));
const DataPlan_1 = require("../../model/statistics/DataPlan");
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);
}
updateDataPlan(dataPlan) {
return __awaiter(this, void 0, void 0, function* () {
yield this.connection.openHomePage();
yield this.login.login();
const parameters = this.buildParameters(dataPlan);
const response = yield this.connection.post('/api/monitoring/start_date', parameters);
if (Connection_1.default.isSuccess(response)) {
this.logger.debug('Updating data plan done with success');
}
else {
throw new Error('Unable to update data plan');
}
});
}
buildParameters(dataPlan) {
const startDate = this.getStartDate(dataPlan);
const dataLimit = this.getDataLimit(dataPlan);
const threshold = this.getThreshold(dataPlan);
return `<?xml version: "1.0" encoding="UTF-8"?><request><StartDay>${startDate}</StartDay><DataLimit>${dataLimit}</DataLimit><MonthThreshold>${threshold}</MonthThreshold><SetMonthData>1</SetMonthData></request>`;
}
getStartDate(dataPlan) {
const startDate = dataPlan.startDate;
if (!this.isPositiveInteger(startDate) || startDate > 31) {
throw new Error('Start date in data plan parameters must be between 1 and 31. 1 and 31 included');
}
return StringUtils_1.toTwo(startDate);
}
getDataLimit(dataPlan) {
let unit = '';
switch (dataPlan.monthlyDataPlan.unit) {
case DataPlan_1.Unit.MG:
unit = 'MG';
break;
case DataPlan_1.Unit.GB:
unit = 'GB';
break;
default:
throw new Error(`Unable to determinate data unit from : ${dataPlan.monthlyDataPlan.unit}`);
}
const volume = dataPlan.monthlyDataPlan.volume;
if (!this.isPositiveInteger(volume)) {
throw new Error('Monthly data volume must be a positive integer number');
}
return `${dataPlan.monthlyDataPlan.volume}${unit}`;
}
getThreshold(dataPlan) {
const threshold = dataPlan.threshold;
if (!this.isPositiveInteger(threshold) || threshold > 100) {
throw new Error('Threshold must be a positive integer between 1 and 100. 1 and 100 included');
}
return threshold;
}
isPositiveInteger(number) {
return number > 0 && !`${number}`.includes('.');
}
}
exports.default = default_1;
//# sourceMappingURL=DataPlanning.js.map