Skip to content

Commit

Permalink
during bluetooth_ble matching, convert major and minor to hexadecimal
Browse files Browse the repository at this point in the history
The background/bluetooth_ble data type has "major" and "minor" as decimal integers. (e-mission/e-mission-docs#1062 (comment))
But in the vehicle_identities spec, we have major:minor pairs as hexadecimal strings. So we will need to convert.

decimalToHex handles this and allows us to add padding, ensuring the converted major and minor are always 4 hex characters.
(so 1 becomes "0001", 255 becomes "00ff", etc.)
  • Loading branch information
JGreenlee committed Apr 14, 2024
1 parent bff97ae commit fbedee4
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion www/js/survey/inputMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,19 @@ function getBleScansForTimelineEntry(
return bleScans.filter((scan) => validBleScanForTimelineEntry(tlEntry, scan));
}

/**
* @description Convert a decimal number to a hexadecimal string, with optional padding
* @example decimalToHex(245) => 'f5'
* @example decimalToHex(245, 4) => '00f5'
*/
function decimalToHex(d: string | number, padding?: number) {
let hex = Number(d).toString(16);
while (hex.length < (padding || 0)) {
hex = '0' + hex;
}
return hex;
}

export function mapBleScansToTimelineEntries(allEntries: TimelineEntry[], appConfig: AppConfig) {
const timelineBleMap = {};
for (const tlEntry of allEntries) {
Expand All @@ -390,7 +403,9 @@ export function mapBleScansToTimelineEntries(allEntries: TimelineEntry[], appCon
// count the number of occurrences of each major:minor pair
const majorMinorCounts = {};
matches.forEach((match) => {
const majorMinor = match.data.major + ':' + match.data.minor;
const major = decimalToHex(match.data.major, 4);
const minor = decimalToHex(match.data.minor, 4);
const majorMinor = major + ':' + minor;
majorMinorCounts[majorMinor] = majorMinorCounts[majorMinor]
? majorMinorCounts[majorMinor] + 1
: 1;
Expand Down

0 comments on commit fbedee4

Please sign in to comment.