UNPKG

rxjs-obd

Version:

RxJS Implementation for OBD (On Board Diagnostics) of vehicles via ELM 327 connections.

54 lines 2.16 kB
import { from, zip } from 'rxjs'; import { map, mergeMap, take } from 'rxjs/operators'; import { OuterSubscriber } from '../internal/OuterSubscriber'; import { isSupportedPID } from '../internal/utils'; import { obdReader } from '../operators/obdReader'; const OBD_NO_DATA = 'NO DATA'; const OBD_OUTPUT_DELIMITER = '\r'; export class OBDOuterSubscriber extends OuterSubscriber { constructor(destination) { super(destination); } /** * Send the command to OBD Reader and try to read the response. * @param event The OBD reader event loop. */ _next(event) { const self = this; const service01 = this.command().match(/(\d\d) ([0-9A-F][0-9A-F]) 1\r/); if (!service01 || isSupportedPID(event.data, service01[2])) { event.connection.send(this.command()).subscribe({ error: (error) => self.destination.error(error), complete: () => self.read(event) }); } else { this.destination.next(event); } } /** * Read one return from OBD, parse and update event. * @param event the OBD reader event loop. */ read(event) { event.connection.onData().pipe(mergeMap((data) => from(data.split(OBD_OUTPUT_DELIMITER))), obdReader(), take(1), map((result) => { return OBD_NO_DATA !== result[0] ? this.parse(result) : OBD_NO_DATA; })).subscribe((value) => { if (Array.isArray(this.field())) { const values = value; zip(from(this.field()), from(value)).subscribe(([field, _value]) => event.update(field, _value)); } else { const segment = this.field().match(/supportedPIDs\.(segment[0-9A-F][0-9A-F])/); if (segment) { event.supportedPIDs(segment[1], value); } else if (OBD_NO_DATA !== value) { event.update(this.field(), value); } } this.destination.next(event); }, (error) => this.destination.error(error)); } } //# sourceMappingURL=OBDOuterSubscriber.js.map