Skip to content

Commit

Permalink
Added signal strength and fixed timer is not properly configured
Browse files Browse the repository at this point in the history
  • Loading branch information
JanFellner committed Aug 16, 2024
1 parent 946a111 commit 4f24100
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
33 changes: 29 additions & 4 deletions src/OBDConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class OBDConnection {
public async connect(): Promise<true | string> {
try {
// Ensure the bluetooth obd2 token is reachable
const reachable = this.isBluetoothDeviceReachable(this.config.obd2MAC);
const reachable = this.isBluetoothDeviceReachable();
if (reachable !== true)
return reachable;

Expand Down Expand Up @@ -268,13 +268,12 @@ export class OBDConnection {
/**
* Validates if a certain bluetooth devices is reachable or not
*
* @param deviceMacAddress - the mac of the bluetooth decvice
* @returns true if the device is reachable or false if not.
*/
private isBluetoothDeviceReachable(deviceMacAddress: string): true | string {
private isBluetoothDeviceReachable(): true | string {
// Run the l2ping command to check whether the device is pingable
const options: ExecSyncOptionsWithStringEncoding = { stdio: "pipe", encoding: "ascii" };
const command = `sudo l2ping -c 1 -s 1 -f ${deviceMacAddress}`;
const command = `sudo l2ping -c 1 -s 1 -f ${this.config.obd2MAC}`;
try {
execSync(command, options);
theLogger.log(`${command} succeeded`);
Expand All @@ -284,4 +283,30 @@ export class OBDConnection {
return JSON.stringify((error as Error).message);
}
}

/**
* Get Bluetooth device signal strength
*
* @returns the signal strength as number or undefined on error
*/
public getBluetoothDeviceSignalStrength(): number | undefined {
// Run the l2ping command to check whether the device is pingable
const options: ExecSyncOptionsWithStringEncoding = { stdio: "pipe", encoding: "ascii" };
const command = `hcitool rssi ${this.config.obd2MAC}`;
try {
let dBmValue: number | undefined;
const result = execSync(command, options) as string;
const pos = result.lastIndexOf(": ");
if (pos !== -1) {
const dbValueString = result.substring(pos + 2);
dBmValue = parseInt(dbValueString, 10);
}
if (dBmValue === undefined)
theLogger.warn(`Could no read signal strength from ${command} - result was '${result}`);
return dBmValue;
} catch (error: unknown) {
theLogger.error(`${command} failed`);
return undefined;
}
}
}
32 changes: 24 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ let reentrant_protection = false;
let lastQuery = 0;
let errorCounter = 0;
let mainState = eMainState.disconnected;
let signalStrength: number | undefined;
let timeout: NodeJS.Timeout | undefined;

/**
Expand Down Expand Up @@ -111,8 +112,10 @@ async function querySOC(caller: string): Promise<boolean> {
} else
theLogger.error(connectResult);
}
if (bContinue)
if (bContinue) {
signalStrength = await theOBDConnection.getBluetoothDeviceSignalStrength();
bReturn = await readSOC(caller);
}
} catch (error) {
}
reentrant_protection = false;
Expand All @@ -124,8 +127,6 @@ async function querySOC(caller: string): Promise<boolean> {
* the global loop that keeps everything going
*/
async function mainLoop(): Promise<void> {
if (timeout)
clearTimeout(timeout);
let nextRun = 5000;
let nextState = eMainState.disconnected;
try {
Expand Down Expand Up @@ -170,9 +171,13 @@ async function mainLoop(): Promise<void> {
if (nextState === eMainState.connected_failed_to_read_soc && errorCounter <= 10)
logMessage += ` (${errorCounter}/10)`;
logMessage += "...";

theLogger.log(logMessage);
timeout = setTimeout(() => { void mainLoop(); }, 5000);

if (timeout) {
clearTimeout(timeout);
timeout = undefined;
}
timeout = setTimeout(() => { void mainLoop(); }, nextRun);
}

theWebServer.get("/soc_double", async (req: Request, res: Response) => {
Expand Down Expand Up @@ -245,9 +250,20 @@ theWebServer.get("/", async (req: Request, res: Response) => {
body += `<tr><td>SOC:</td><td>${theBatteryState.soc}%</td></tr>\n`;
body += `<tr><td>Last read:</td><td>${Helpers.getTimeString(theBatteryState.lastChanged, false)}</td></tr>\n`;
body += `<tr><td>OBD-Dongle:</td><td>${theConfig.obd2MAC} `;
if (theOBDConnection.isConnected())
body += `<span style="color: green; font-weight: bold;">connected</span>`;
else
if (theOBDConnection.isConnected()) {
let color = "gray";
if (typeof signalStrength === "number") {
if (signalStrength < 0 && signalStrength >= -30)
color = "green";
else if (signalStrength < -30 && signalStrength >= -60)
color = "yellow";
else if (signalStrength < -60 && signalStrength >= -90)
color = "orange";
else if (signalStrength < -90)
color = "red";
}
body += `<span style="color: green; font-weight: bold;">connected</span> (<span style="color: ${color}; font-weight: bold;">${signalStrength}</span> dBm)`;
} else
body += `<span style="color: red; font-weight: bold;">not connected</span>`;
body += "</td></tr>\n";
body += `<tr><td>Links:</td><td><a target="_blank" href="/soc_integer">/soc_integer</a>&nbsp;<a target="_blank" href="/soc_double">/soc_double</a>&nbsp;<a target="_blank" href="/distance">/distance</a></td></tr>\n`;
Expand Down

0 comments on commit 4f24100

Please sign in to comment.