sim800
Version:
A modern and opiniated module for SIM800 GSM modems ( SIM800 / SIM800L ).
71 lines • 3.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.newSmsSubscriberFactory = void 0;
const cmgr_command_1 = require("../classes/cmgr-command");
const node_pdu_1 = require("node-pdu");
const cmgd_command_1 = require("../classes/cmgd-command");
const newSmsSubscriberFactory = (client, logger) => async (data) => {
var _a;
try {
if (data.startsWith('+CMTI: "')) {
// extract the storage from the +CMTI "XX" message
const simIndex = parseInt(data.split(',')[1], 10);
const result = (await client.send(new cmgr_command_1.CmgrCommand(simIndex), { raw: true }));
const sms = (0, node_pdu_1.parse)(result[1]);
if (sms instanceof node_pdu_1.Deliver) {
if (sms.getParts().every((part) => part.header === null)) {
(_a = logger.verbose) === null || _a === void 0 ? void 0 : _a.call(logger, 'incoming SMS is single part, emitting');
client.eventEmitter.emit('incoming-sms', {
number: sms.address.phone,
text: sms.data.getText(),
date: new Date(),
});
}
else {
// SMS is Multipart, should be only 1 part per sms though
sms.getParts().forEach((part) => {
if (part.header) {
// we try to find an existing incoming SMS
const header = part.header.toJSON();
const existingRef = client.incomingSms.find((sms) => (sms.carrierId = header.POINTER || -1));
if (existingRef) {
existingRef.parts.push({
carrierIndex: header.CURRENT,
simIndex,
text: part.text,
});
existingRef.text += part.text;
// Complete detection
if (existingRef.parts.length === existingRef.length) {
// EMIT INCOMING SMS, DELETE PARTS AND SPLICE THE BUFFER
client.eventEmitter.emit('incoming-sms', { number: existingRef.number, text: existingRef.text, date: new Date() }, new Date());
if (!client.preventWipe) {
existingRef.parts.forEach((part) => client.send(new cmgd_command_1.CmgdCommand(part.simIndex)));
}
// deleting from buffer
client.incomingSms.splice(client.incomingSms.findIndex((sms) => existingRef.carrierId === sms.carrierId));
}
}
else {
client.incomingSms.push({
carrierId: header.POINTER,
length: header.SEGMENTS,
number: sms.address.phone,
text: part.text,
parts: [{ carrierIndex: header.CURRENT, simIndex, text: part.text }],
});
}
}
});
}
}
}
}
catch (error) {
if (error instanceof Error) {
logger.error(`Incoming SMS Error: ${error.message}`);
}
}
};
exports.newSmsSubscriberFactory = newSmsSubscriberFactory;
//# sourceMappingURL=new-sms-subscriber.js.map