rxjs-obd
Version:
RxJS Implementation for OBD (On Board Diagnostics) of vehicles via ELM 327 connections.
50 lines • 1.55 kB
JavaScript
import * as net from 'net';
import { EMPTY, Subject } from 'rxjs';
/**
* The wrapper net.Socket connection.
*/
export class NodeOBDConnection {
/**
* Open connection to OBD Reader.
* @returns the observable of opening connection.
*/
open(config) {
const { port, host } = config;
const subject = new Subject();
this.socket = net.connect(port, host, () => {
this.data$ = new Subject();
this.socket.on('data', (input) => this.data$.next(input.toString('utf8')));
subject.complete();
});
this.socket.on('error', (error) => subject.error(error));
return subject.asObservable();
}
/**
* Send a command to OBD Reader.
* @returns the observable of send operation.
* The events emitted are ignored, wait for complete (successful) or error (failure).
*/
send(command) {
this.socket.write(command);
return EMPTY;
}
/**
* Listener for data received from OBD Reader.
* @returns Observable of data received from OBD Reader.
*/
onData() {
return this.data$.asObservable();
}
/**
* Close and dispose resources of OBD Reader connection.
* @returns the observable of close operation.
* The events emitted are ignored, wait for complete (successful) or error (failure).
*/
close() {
this.data$.complete();
this.socket.end();
this.socket.destroy();
return EMPTY;
}
}
//# sourceMappingURL=NodeOBDConnection.js.map