From fbedee41cc0f6933980edf50361c949f6c5dbd9c Mon Sep 17 00:00:00 2001 From: Jack Greenlee Date: Sun, 14 Apr 2024 00:22:30 -0400 Subject: [PATCH] during bluetooth_ble matching, convert major and minor to hexadecimal The background/bluetooth_ble data type has "major" and "minor" as decimal integers. (https://github.com/e-mission/e-mission-docs/issues/1062#issuecomment-2052278631) 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.) --- www/js/survey/inputMatcher.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/www/js/survey/inputMatcher.ts b/www/js/survey/inputMatcher.ts index fdfb25579..a02f8ff76 100644 --- a/www/js/survey/inputMatcher.ts +++ b/www/js/survey/inputMatcher.ts @@ -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) { @@ -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;