hilink-nodejs-sms
Version:
Node.js library to send sms using Huawei modems (hilink).
115 lines (114 loc) • 4.83 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 });
/*
author: kamil90k@hotmail.com (https://github.com/kamil90k)
tested on ->
Device name: E5573s-320
Hardware version: CL1E5573SM01
Software version: 21.326.62.00.264
Web UI version: 17.100.18.01.264
*/
const utils_1 = require("./utils");
const connector_1 = require("./connector");
const CredentialsProvider_1 = __importDefault(require("./CredentialsProvider"));
const common_1 = require("./common");
class HilinkSms {
constructor(config) {
this._smsQueue = [];
this._name = config.name;
this._parallelSmsCount = config.parallelSmsCount || common_1.DEFAULT_PARALLEL_SMS_COUNT;
this._connector = connector_1.connector(config.host, config.protocol, config.networkInterface);
this._logger = config.logger !== undefined
? config.logger
: utils_1.createLogger(!!config.silentLogs, config.name);
this._credentialsProvider = new CredentialsProvider_1.default(config.login, config.sha256password, this._connector, this._logger);
this._run().catch((e) => {
this._logger(`SMS provider fatal Error. ${e.message}`);
});
}
_run() {
return __awaiter(this, void 0, void 0, function* () {
while (this._smsQueue.length > 0) {
const chunks = this._smsQueue.splice(0, this._parallelSmsCount);
const parallelSms = chunks.map((sms) => __awaiter(this, void 0, void 0, function* () {
try {
yield this._sendSms(sms.message, sms.recipient);
sms.callback();
}
catch (e) {
sms.callback(e.message);
}
}));
yield Promise.all(parallelSms);
}
this._asyncTaskId = setTimeout(this._run.bind(this), common_1.DEFAULT_QUEUE_SLEEP_TIME);
});
}
_sendSms(message, recipient) {
return __awaiter(this, void 0, void 0, function* () {
const startTime = Date.now();
const credentials = yield this._getCredentials();
const response = yield this._connector.post('/api/sms/send-sms', utils_1.jsonToXmlString({
Index: -1,
Phones: { Phone: recipient },
Sca: '',
Content: message,
Length: message.length,
Reserved: 1,
Date: utils_1.getDate()
}), { cookie: credentials.sessionId, __RequestVerificationToken: credentials.token });
const responseJson = yield utils_1.xmlStringToJson(response.body);
if (responseJson.response !== 'OK') {
throw new Error(`${this._getDeviceName()} [SMS] Failed to send SMS.`);
}
this._logger(`Send SMS OK, ${Math.floor(Date.now() - startTime)}ms`);
});
}
;
_getCredentials() {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield this._credentialsProvider.getCredentials();
}
catch (e) {
throw new Error(`${this._getDeviceName()} ${e.message}.`);
}
});
}
_getDeviceName() {
return this._name !== undefined ? this._name : '';
}
sms(message, recipient) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
this._smsQueue.push({
message,
recipient,
callback: (err) => {
if (err !== undefined) {
return reject(err);
}
resolve();
}
});
});
});
}
destroy() {
this._credentialsProvider.destroy();
clearTimeout(this._asyncTaskId);
}
}
exports.default = HilinkSms;