rxjs-obd
Version:
RxJS Implementation for OBD (On Board Diagnostics) of vehicles via ELM 327 connections.
71 lines • 2.23 kB
JavaScript
import * as _ from 'lodash';
import { OBDData } from './OBDData';
import * as fromFields from './OBDFields';
/**
* OBD Event that will be used in the internal part of the rxjs-obd stream.
*/
export class OBDEvent {
/**
* Constructor for OBD Events, only rxjs-obd observables should use it.
*
* @param _id The event identifier.
* @param _connection The connection to ELM 327 OBD Reader.
* @param _subject The subject to be notified only by pluckODBData from rxjs-obd.
* @param _data The previous ODB data (Optional).
*/
constructor(_id, _connection, _subject, _data = new OBDData()) {
this._id = _id;
this._connection = _connection;
this._subject = _subject;
this._data = _data;
this._data.timestamp = new Date().getTime();
}
/**
* Update the supported PIDs.
*
* @param segment The segment.
* @param value The value.
*/
supportedPIDs(segment, value) {
this._data.supportedPIDs = Object.assign({}, this._data.supportedPIDs, { [segment]: value });
}
/**
* Update a field of the public OBD Data.
* @param name The field name.
* @param value the new value of field.
*/
update(name, value) {
const field = fromFields[_.snakeCase(name).toUpperCase()];
const formatted = field.formatter ? field.formatter(field.unit, value) : `${value} ${field.unit}`;
const { code, label, unit } = field;
this._data[name] = { code, label, unit, value, formatted };
}
/**
* The subject to be notified only by pluckODBData from rxjs-obd.
*/
next(data) {
this._subject.next(data);
}
/**
* Return the event id.
* @returns the event id.
*/
get id() {
return this._id;
}
/**
* Return the internal connection connection to be used by rxjs-obd operators only.
* @returns The connection connection to ELM 327 OBD Reader.
*/
get connection() {
return this._connection;
}
/**
* Return the public data of the rxjs-obd observables.
* @returns The public data.
*/
get data() {
return this._data;
}
}
//# sourceMappingURL=OBDEvent.js.map