UNPKG

minitel.ts

Version:

A port to typescript and extension of https://github.com/cquest/pynitel

117 lines (116 loc) • 4.57 kB
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()); }); }; import { SerialPort } from 'serialport'; import EventEmitter from 'events'; import 'colors'; import MinitelTSRouter from './router.js'; import MinitelTSRead from './read.js'; import MinitelTSWrite from './write.js'; import MinitelTSState from './state.js'; export default class MinitelTS extends EventEmitter { constructor() { super(); this.router = new MinitelTSRouter(); this.input = new MinitelTSRead(this); this.output = new MinitelTSWrite(this); this.colors = { noir: 0, rouge: 1, vert: 2, jaune: 3, bleu: 4, magenta: 5, cyan: 6, blanc: 7 }; this.store = MinitelTSState({ name: 'store' }); } init(path = undefined, baudRate = undefined) { if (!path && !process.env.MINITEL_PATH) { console.error('No path defined!'); process.exit(1); } const baud = baudRate || parseInt(process.env.MINITEL_BAUDRATE || '1200'); return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { var _a; yield this.openConnection(path, 1200); console.log('Connected to Minitel at 1200 baud rate'.green); if (baud === 4800) { console.log('Switching to 4800 baud rate...'.gray); this.send("\x1b\x3a\x6b\x76"); (_a = this.port) === null || _a === void 0 ? void 0 : _a.close(); yield this.openConnection(path, baud); console.log('Connected to Minitel at 4800 baud rate'.green); resolve(); } else { resolve(); } })); } openConnection(path, baudRate) { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { this.port = new SerialPort({ path: path || process.env.MINITEL_PATH || '', baudRate, dataBits: 7, parity: 'even', stopBits: 1, rtscts: false }, (err) => { if (err) { console.error('Error opening port:', err.message); reject(err); } }); this.port.on('open', () => { resolve(); }); this.port.on('data', (data) => { this.emit('data', data); }); })); } send(data) { if (!this.port) { console.error('Port not open'); return; } this.port.write(data, (err) => { if (err) console.error('Error writing to port:', err.message); }); } sendChr(ascii) { this.send(String.fromCharCode(ascii)); } sendEsc(data) { this.sendChr(27); // ESC character this.send(data); } bip() { if (process.env.MINITEL_SILENT !== 'true') { this.sendChr(7); // Beep sound } } scroll(enable = true) { this.sendEsc(enable ? '\x3A\x6A\x43' : '\x3A\x6B\x43'); // Enable/disable scrolling } loop() { return __awaiter(this, arguments, void 0, function* (routeDir = 'app', initialRoute = 'index', initialStore = {}) { console.log('Minitel loop started…'); yield this.router.loadRoutes(routeDir, initialRoute); if (this.router.current === null) { console.error(`No route found at ${routeDir}!`); return; } this.output.cls(); while (true) { const { store, setStore } = this.store; setStore(Object.assign(Object.assign({}, initialStore), { loop: store.loop ? store.loop + 1 : 1 })); yield this.router.current.loop(this, this.router.current); } }); } }