ts-ssp
Version:
NodeJS library to work with coin and bill acceptors under SSP protocol. Written in Typescript.
154 lines • 5.18 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const events_1 = require("events");
const serialport_1 = __importDefault(require("serialport"));
const SSPCommands_1 = __importDefault(require("./SSPCommands"));
const SSPEvents_1 = require("./SSPEvents");
const SSPParser_1 = __importDefault(require("./SSPParser/SSPParser"));
const utils_1 = require("./utils");
class SSP extends events_1.EventEmitter {
constructor(type, options) {
super();
/**
* For additional info refer to `Transport Layer` section of SSP_Manual.pdf
*/
this.handleData = (buffer) => {
if (!this.commands) {
this.emit("error", new Error("Commands are not initialized"));
return;
}
try {
const eventToEmit = SSPEvents_1.getEventFromBuffer(buffer, this.commands);
if (!eventToEmit) {
return;
}
this.emit(...eventToEmit);
}
catch (e) {
console.error(e);
}
};
this.type = type;
this.options = Object.assign({ baudRate: 9600, dataBits: 8, stopbits: 2, parity: "none", sspID: 0, sequence: 0x80 }, options);
}
exec(commandName, ...args) {
if (!this.commands) {
throw new Error("Comamnds are not avalible");
}
return this.commands.exec(commandName, ...args);
}
isOpened() {
return (this.socket && this.socket.isOpen) || false;
}
isEnabled() {
return Boolean(this.pollTimeout) || false;
}
async enable() {
if (!this.commands) {
throw new Error("Commands are not initialised");
}
const poll = () => {
this.pollTimeout = setTimeout(() => {
if (!this.commands) {
throw new Error("Commands are not initialised");
}
void this.commands.exec("poll");
poll();
}, 400);
};
await this.commands.exec("enable");
poll();
}
async disable() {
if (this.pollTimeout) {
clearTimeout(this.pollTimeout);
}
if (!this.commands) {
throw new Error("Commands are not initialised");
}
await this.commands.exec("disable");
}
async reset() {
if (this.pollTimeout) {
clearTimeout(this.pollTimeout);
}
if (!this.commands) {
throw new Error("Commands are not initialised");
}
await this.commands.exec("reset");
if (!this.socket) {
return;
}
this.socket.close();
}
async open() {
if (this.socket) {
throw new Error("Connection is already opened");
}
this.socket = new serialport_1.default(this.options.device, {
baudRate: this.options.baudRate,
dataBits: this.options.dataBits,
stopBits: this.options.stopbits,
parity: this.options.parity,
autoOpen: false,
});
this.commands = new SSPCommands_1.default(this.socket, this.type, this.options.sspID, this.options.sequence);
const parser = this.socket.pipe(new SSPParser_1.default());
this.socket.on("close", () => {
this.emit("close");
});
this.socket.on("error", (err) => {
this.emit("error", err);
});
parser.on("data", this.handleData);
const openAsync = (socket) => new Promise((resolve, reject) => {
socket.open((err) => {
if (err) {
reject(err);
return;
}
resolve();
return;
});
});
await openAsync(this.socket);
const low = this.options.currencies.reduce((p, c, i) => {
return c === 1 ? p + Math.pow(2, i) : p;
}, 0);
//wait a bit for port buffer to empty
await utils_1.sleep(100);
if (!this.commands) {
throw new Error("Commands are not created");
}
await this.commands.exec("sync");
await this.commands.exec("enable_higher_protocol");
await this.commands.exec("set_channel_inhibits", low, 0x00);
this.emit("start");
}
async close() {
if (!this.socket) {
throw new Error("Connection is not opened");
}
if (this.isEnabled()) {
await this.disable();
}
const closeAsync = (socket) => new Promise((resolve, reject) => {
socket.close((err) => {
if (err) {
reject(err);
return;
}
resolve();
return;
});
});
await closeAsync(this.socket);
this.socket = undefined;
this.emit("close");
}
}
exports.default = SSP;
//# sourceMappingURL=SSP.js.map