UNPKG

rxjs-obd

Version:

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

53 lines 1.75 kB
import { interval, Observable, Subject } from 'rxjs'; import { defaultOBDWifiConfig } from '../connection'; import { supportedPIDs } from '../internal/operators/supportedPIDs'; import { OBDData } from '../model'; import { OBDEvent } from '../model/OBDEvent'; export const fromOBD = (config = defaultOBDWifiConfig) => new Observable((subscriber) => { /** * The connection connection to ELM 327 OBD Reader. */ const { pullingInterval, connection } = config; /** * Last OBD Data emitted. */ let data = new OBDData(); /** * Flag of indicate that a pull loop is running. */ let running = false; /** * Internal pulling (interval) subscription. */ let subscription; /** * Subject to observe the pluck moment. */ const subject = new Subject(); subject.subscribe((value) => { data = Object.assign({}, value); running = false; }); const dispatchNext = (id, subscriber) => { if (!running) { running = true; subscriber.next(new OBDEvent(id, connection, subject, Object.assign({}, data))); } }; connection.open(config).subscribe({ error: (error) => subscriber.error(error), complete: () => { // connected subscription = interval(pullingInterval).subscribe((id) => dispatchNext(id, subscriber)); } }); return () => { if (!!connection) { connection.close(); } if (!!subscription) { subscription.unsubscribe(); } }; }).pipe(supportedPIDs('00'), supportedPIDs('20'), supportedPIDs('40'), supportedPIDs('60'), supportedPIDs('80'), supportedPIDs('A0')); //# sourceMappingURL=fromOBD.js.map