elm327
Version:
Node.js/TypeScript library for ELM327 OBD2 adapters over USB, Bluetooth and WiFi
144 lines • 5.62 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.WifiConnection = void 0;
const net = __importStar(require("node:net"));
const connection_1 = require("./connection");
const errors_1 = require("./errors");
/**
* WiFi (TCP/IP) connection to an ELM327 adapter.
* WiFi adapters connect over TCP, typically at 192.168.0.10:35000.
*
* Updated to use ResponseMatcher for better request/response matching.
*/
class WifiConnection extends connection_1.OBD2Connection {
client = null;
host;
port;
lineEnding;
buffer = '';
constructor(config) {
super(config);
this.host = config.host || '192.168.0.10';
this.port = config.port
? typeof config.port === 'string'
? parseInt(config.port, 10)
: config.port
: 35000;
this.lineEnding = config.lineEnding || '\r';
}
async connect() {
return new Promise((resolve, reject) => {
this.client = new net.Socket();
this.client.setTimeout(this.timeout);
let settled = false;
this.client.connect(this.port, this.host, () => {
settled = true;
this.isConnected = true;
// Disable the default timeout after successful connect
this.client.setTimeout(0);
this.emit('connected');
resolve();
});
this.client.on('data', (data) => {
this.buffer += data.toString();
let idx;
while ((idx = this.buffer.indexOf('>')) !== -1) {
// Include the '>' prompt in the data passed to handleIncomingData
const raw = this.buffer.slice(0, idx + 1);
this.buffer = this.buffer.slice(idx + 1);
if (raw.trim().length > 0) {
// Send to ResponseMatcher for request matching (with '>' included)
this.handleIncomingData(raw);
// Also emit raw data event (without '>' for compatibility)
this.emit('data', raw.replace('>', '').trim());
}
}
});
this.client.on('error', (err) => {
const error = new errors_1.ConnectionError(`WiFi error: ${err.message}`);
this.rejectAllPending(error);
this.emit('error', err);
if (!settled) {
settled = true;
reject(new errors_1.ConnectionError(`Failed to connect: ${err.message}`));
}
});
this.client.on('timeout', () => {
const err = new errors_1.ConnectionError('Connection timeout');
this.rejectAllPending(err);
this.client?.destroy();
this.emit('error', err);
});
this.client.on('close', () => {
this.isConnected = false;
this.rejectAllPending(new errors_1.ConnectionError('Connection closed'));
this.emit('disconnected');
});
});
}
async sendRaw(data) {
if (!this.isConnected || !this.client) {
throw new errors_1.ConnectionError('Not connected to WiFi adapter');
}
return new Promise((resolve, reject) => {
this.client.write(`${data}${this.lineEnding}`, (err) => {
if (err) {
reject(new errors_1.ConnectionError(`Failed to send data: ${err.message}`));
}
else {
resolve();
}
});
});
}
async disconnect() {
this.rejectAllPending(new errors_1.ConnectionError('Disconnected manually'));
if (this.client) {
this.client.destroy();
this.client = null;
}
this.isConnected = false;
this.emit('disconnected');
}
isConnectionOpen() {
return this.isConnected && this.client !== null && !this.client.destroyed;
}
clearBuffer() {
this.buffer = '';
}
}
exports.WifiConnection = WifiConnection;
//# sourceMappingURL=wifi-connection.js.map