elm327
Version:
Node.js/TypeScript library for ELM327 OBD2 adapters over USB, Bluetooth and WiFi
76 lines • 2.34 kB
TypeScript
import { EventEmitter } from 'events';
/**
* Represents a pending request waiting for a response.
* Similar to OpenXC's ResponseReceiver pattern.
*/
export interface PendingRequest {
id: string;
command: string;
resolve: (response: string) => void;
reject: (error: Error) => void;
timer: NodeJS.Timeout;
buffer: string[];
timestamp: number;
matchFn?: (response: string) => boolean;
}
/**
* Matches incoming responses to pending requests.
* Supports matching by command pattern or custom matching function.
*/
export declare class ResponseMatcher extends EventEmitter {
private pendingRequests;
private requestCounter;
private destroyed;
/**
* Adds a new pending request and returns its ID.
* If the matcher is destroyed (connection lost), rejects immediately.
*/
addRequest(command: string, timeout: number, matchFn?: (response: string) => boolean): {
id: string;
promise: Promise<string>;
};
/**
* Handles incoming data from the adapter.
* Attempts to match the data to a pending request.
* The data should include the '>' prompt for proper detection.
*/
handleData(data: string): void;
/**
* Checks if the response appears to be complete.
* Looks for the '>' prompt and valid response pattern.
*/
private isCompleteResponse;
/**
* Resolves a pending request with the given response.
*/
private resolveRequest;
/**
* Rejects all pending requests with the given error.
* Sets destroyed state to prevent new requests.
*/
rejectAll(error: Error): void;
/**
* Marks the matcher as destroyed (connection lost).
* Future addRequest calls will be rejected immediately.
*/
destroy(): void;
/**
* Resets the destroyed state (for reconnection).
* Also resets the request counter to avoid overflow.
*/
reset(): void;
/**
* Gets the number of pending requests.
*/
get pendingCount(): number;
/**
* Removes and rejects a specific request.
*/
cancelRequest(id: string): void;
/**
* Clears the buffer of the first pending request.
* Call this before sending a new command to avoid residual data.
*/
clearBuffer(): void;
}
//# sourceMappingURL=response-matcher.d.ts.map