huawei-wingle-4g
Version:
This is a module NodeJS allowing to drive Huawei Wingle 4G. This module can :
88 lines • 4.11 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 NetworkExtractor_1 = __importDefault(require("./NetworkExtractor"));
const NetworkStatus_1 = require("../../model/home/NetworkStatus");
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(true);
}
connect() {
return __awaiter(this, void 0, void 0, function* () {
yield this.connection.openHomePage();
if (yield this.isConnected()) {
this.logger.debug(`Already connected, no need to connect`);
return;
}
yield this.login.login();
if (yield this.switchDataMobile(true)) {
this.logger.debug('Connecting data mobile with success');
return;
}
throw new Error('Unable to connect data mobile');
});
}
disconnect() {
return __awaiter(this, void 0, void 0, function* () {
yield this.connection.openHomePage();
if (!(yield this.isConnected())) {
this.logger.debug(`Already disconnected, no need to disconnect`);
return;
}
yield this.login.login();
if (yield this.switchDataMobile(false)) {
this.logger.debug('Disconnecting data mobile with success');
}
else {
throw new Error('Unable to disconnect data mobile');
}
});
}
isConnected() {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.connection.get('/api/monitoring/status');
const document = response.document;
const state = yield NetworkExtractor_1.default.getNetworkStatus(document);
switch (state) {
case NetworkStatus_1.NetworkStatus.CONNECTED:
case NetworkStatus_1.NetworkStatus.CONNECTING:
return true;
case NetworkStatus_1.NetworkStatus.DISCONNECTED:
case NetworkStatus_1.NetworkStatus.DISCONNECTING:
return false;
default:
throw new Error(`Unable to determinate if is is connected or not from state : ${state}`);
}
});
}
switchDataMobile(isToConnect) {
return __awaiter(this, void 0, void 0, function* () {
const dataSwitch = isToConnect ? 1 : 0;
const parameters = `<?xml version: "1.0" encoding="UTF-8"?><request><dataswitch>${dataSwitch}</dataswitch></request>`;
const response = yield this.connection.post('/api/dialup/mobile-dataswitch', parameters);
return Connection_1.default.isSuccess(response);
});
}
}
exports.default = default_1;
//# sourceMappingURL=DataMobile.js.map