UNPKG

sim800

Version:

A modern and opiniated module for SIM800 GSM modems ( SIM800 / SIM800L ).

151 lines 5.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Sim800Command = void 0; const rxjs_1 = require("rxjs"); const sim800_command_state_enum_1 = require("../interfaces/sim800-command-state.enum"); class Sim800Command { constructor(input) { this.state = sim800_command_state_enum_1.Sim800CommandState.Created; this.completed$ = new rxjs_1.AsyncSubject(); this.ack = false; this.result = null; this.raw = []; // the raw stream of input received during the command handling this.error = null; this.pid = Math.floor(Math.random() * 1000000); this.command = input.command; this.arg = input.arg; this.timeoutMs = input.timeoutMs || 5000; this.completeWhen = input.completeWhen; this.errorWhen = input.errorWhen; this.observer = input.observer; this.isInput = input.isInput; this.expectedData = input.expectedData; if (!this.observer && !this.completeWhen) { throw new Error(`Either observer or completeWhen must be provided for "${this.command}" command`); } } async send(stream$, serial) { this.execute(stream$, serial); await (0, rxjs_1.lastValueFrom)(this.completed$); } isDataPartOfRunningCommand(data) { // If the command is expected to receive data listed in the expectedReturns array, then we should consider it as part of the command if (this.expectedData && this.isDataExpected(data)) { return true; } return (this.state !== sim800_command_state_enum_1.Sim800CommandState.Created && (data.includes(this.command) || this.handleCompletedWhen(data) || this.handleErrorWhen(data) || (this.isInput && data.startsWith('> ')))); } execute(stream$, serial) { if (this.observer) { this.subscription = stream$.pipe((0, rxjs_1.timeout)(this.timeoutMs), (0, rxjs_1.takeUntil)(this.completed$)).subscribe(this.observer); } // Auto advance state stream$ .pipe((0, rxjs_1.timeout)(this.timeoutMs), (0, rxjs_1.takeUntil)(this.completed$), (0, rxjs_1.catchError)((err) => { this.error = err; this.complete(sim800_command_state_enum_1.Sim800CommandState.Error); return ''; })) .subscribe((data) => { if (!this.expectedData && this.ack) { this.raw.push(data); } else if (this.expectedData) { // If we're expecting this data if (this.isDataExpected(data)) { this.raw.push(data); } } if (data.startsWith(this.command)) { this.ack = true; this.state = sim800_command_state_enum_1.Sim800CommandState.Acknowledged; } this.handleCompletedWhen(data); this.handleErrorWhen(data); }); if (this.isInput) { serial.write(`${this.command}${String.fromCharCode(26)}`); } else { if (this.arg) { serial.write(`${this.command}${this.arg}\n`); } else { serial.write(`${this.command}\n`); } } this.state = sim800_command_state_enum_1.Sim800CommandState.Transmitting; } complete(state = sim800_command_state_enum_1.Sim800CommandState.Done) { this.completed$.next(this.pid); this.completed$.complete(); this.state = state; // command is completed, we can unsubscribe from the stream if (this.subscription) { this.subscription.unsubscribe(); } } setResult(data) { this.result = data; this.state = sim800_command_state_enum_1.Sim800CommandState.Done; this.complete(); } setError(error) { this.error = error; this.state = sim800_command_state_enum_1.Sim800CommandState.Error; this.complete(); } handleCompletedWhen(data) { if (this.completeWhen) { if (typeof this.completeWhen === 'string') { if (data === this.completeWhen) { this.setResult(data); return true; } } else { if (this.completeWhen(data)) { this.setResult(data); return true; } } return false; } } handleErrorWhen(data) { if (this.errorWhen) { if (typeof this.errorWhen === 'string') { if (data === this.errorWhen) { this.setError(new Error(data)); return true; } } else { if (this.errorWhen(data)) { this.setError(new Error(data)); return true; } } } return false; } isDataExpected(data) { if (this.expectedData) { return this.expectedData.some((expected) => { if (typeof expected === 'string') { return data.startsWith(expected); } else { return expected(data); } }); } return true; } } exports.Sim800Command = Sim800Command; //# sourceMappingURL=sim800-command.js.map