Skip to content

Commit

Permalink
only use "ranging" scans for BLE matching
Browse files Browse the repository at this point in the history
When matching we basically are trying to find the major:minor pair that was sensed the most frequently during the trip.
But should only consider RANGE_UPDATE events for this. The REGION_ENTER and REGION_EXIT events do not have major and minor defined, so we should exclude them for the purpose of matching.

Add a filter condition for this. Also updated function + variable names to make it clearer that we are only considering 'ranging' scans.

Note that before processing, the value of eventType is a string ('RANGE_UPDATE'). But the server changes this to an enum value where 'RANGE_UPDATE' -> 2. So we have to check for both.
  • Loading branch information
JGreenlee committed Apr 15, 2024
1 parent 0b6b844 commit 37fbb85
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
22 changes: 15 additions & 7 deletions www/js/survey/inputMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,19 @@ function validBleScanForTimelineEntry(tlEntry: TimelineEntry, bleScan: BEMData<B
return bleScan.data.ts >= entryStart && bleScan.data.ts <= entryEnd;
}

function getBleScansForTimelineEntry(
/**
* @description Get BLE scans that are of type RANGE_UPDATE and are within the time range of the timeline entry
*/
function getBleRangingScansForTimelineEntry(
tlEntry: TimelineEntry,
bleScans: BEMData<BluetoothBleData>[],
) {
return bleScans.filter((scan) => validBleScanForTimelineEntry(tlEntry, scan));
return bleScans.filter(
(scan) =>
/* RANGE_UPDATE is the string value, but the server uses an enum, so once processed it becomes 2 */
(scan.data.eventType == 'RANGE_UPDATE' || scan.data.eventType == 2) &&
validBleScanForTimelineEntry(tlEntry, scan),
);
}

/**
Expand All @@ -395,16 +403,16 @@ function decimalToHex(d: string | number, padding?: number) {
export function mapBleScansToTimelineEntries(allEntries: TimelineEntry[], appConfig: AppConfig) {
const timelineBleMap = {};
for (const tlEntry of allEntries) {
const matches = getBleScansForTimelineEntry(tlEntry, unprocessedBleScans);
if (!matches.length) {
const rangingScans = getBleRangingScansForTimelineEntry(tlEntry, unprocessedBleScans);
if (!rangingScans.length) {
continue;
}

// count the number of occurrences of each major:minor pair
const majorMinorCounts = {};
matches.forEach((match) => {
const major = decimalToHex(match.data.major, 4);
const minor = decimalToHex(match.data.minor, 4);
rangingScans.forEach((scan) => {
const major = decimalToHex(scan.data.major, 4);
const minor = decimalToHex(scan.data.minor, 4);
const majorMinor = major + ':' + minor;
majorMinorCounts[majorMinor] = majorMinorCounts[majorMinor]
? majorMinorCounts[majorMinor] + 1
Expand Down
2 changes: 1 addition & 1 deletion www/js/types/diaryTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export type UserInputEntry<T = UserInputData> = {

export type BluetoothBleData = {
ts: number;
eventType: 'REGION_ENTER' | 'REGION_EXIT' | 'RANGE_UPDATE';
eventType: 'REGION_ENTER' | 'REGION_EXIT' | 'RANGE_UPDATE' | number;
uuid: string;
major: number; // for our use case, missing for REGION_ENTER or REGION_EXIT
minor: number; // for our use case, missing for REGION_ENTER or REGION_EXIT
Expand Down

0 comments on commit 37fbb85

Please sign in to comment.