Skip to content

Commit

Permalink
🤡 Switch the simulation code in the UI to use the mock objects
Browse files Browse the repository at this point in the history
In e-mission/e-mission-data-collection#221
we have added a new method to the data collection interface that allows
us to create mock BLE objects and save them using the native methods.
This allows us to simulate the real world scenario more closely, by
saving from the native code. while still avoiding hacks to real code.

----------------------------------

Android:

```
04-13 13:34:28.204 13563 13563 I chromium:   "state": "CLRegionStateInside"
04-13 13:34:28.204 13563 13563 I chromium: }", source: https://localhost/plugins/cordova-plugin-em-unifiedlogger/www/unifiedlogger.js (49)
04-13 13:34:28.222 13563 13763 I System.out: About to execute query SELECT data FROM userCache WHERE key = 'background/bluetooth_ble' AND type = 'sensor-data' AND write_ts >= 1.713040168E9 AND write_ts <= 1.713040468E9 ORDER BY write_ts DESC
04-13 13:34:28.241 13563 13763 W PluginManager: THREAD WARNING: exec() call to UserCache.getSensorDataForInterval blocked the main thread for 20ms. Plugin should use CordovaInterface.getThreadPool().
04-13 13:34:28.253 13563 31932 D BuiltinUserCache: Added value for key background/bluetooth_ble at time 1.713040468221E9
04-13 13:34:28.273 13563 31932 D BuiltinUserCache: Added value for key background/bluetooth_ble at time 1.713040468254E9
04-13 13:34:28.319 13563 31932 D BuiltinUserCache: Added value for key background/bluetooth_ble at time 1.713040468276E9
04-13 13:34:28.351 13563 31932 D BuiltinUserCache: Added value for key background/bluetooth_ble at time 1.71304046832E9
04-13 13:34:28.371 13563 13563 I TripDiaryStateMachineRcvr: noarg constructor called
04-13 13:34:28.373 13563 31932 D BuiltinUserCache: Added value for key background/bluetooth_ble at time 1.713040468352E9
```

iOS

```
2024-04-13 13:38:22.029076-0700 emission[77938:10285250] DEBUG:[BLE] didRangeBeaconsInRegion
2024-04-13 13:38:22.029320-0700 emission[77938:10285250] DEBUG: [BLE] didRangeBeaconsInRegion
2024-04-13 13:38:22.031065-0700 emission[77938:10285250] DEBUG:{
}
2024-04-13 13:38:25.060123-0700 emission[77938:10285250] data has 174 bytes, str has size 174
2024-04-13 13:38:25.062087-0700 emission[77938:10285250] data has 174 bytes, str has size 174
2024-04-13 13:38:25.063593-0700 emission[77938:10285250] data has 173 bytes, str has size 173
2024-04-13 13:38:25.065085-0700 emission[77938:10285250] data has 175 bytes, str has size 175
2024-04-13 13:38:25.066255-0700 emission[77938:10285250] data has 175 bytes, str has size 175
2024-04-13 13:38:26.343114-0700 emission[77938:10285250] THREAD WARNING: ['DataCollection'] took '4307.931885' ms. Plugin should use a background thread.
2024-04-13 13:38:26.350811-0700 emission[77938:10285250] In TripDiaryStateMachine, received transition T_BLE_BEACON_FOUND in state STATE_ONGOING_TRIP
2024-04-13 13:38:26.350982-0700 emission[77938:10285250] DEBUG: In TripDiaryStateMachine, received transition T_BLE_BEACON_FOUND in state STATE_ONGOING_TRIP
2024-04-13 13:38:26.352531-0700 emission[77938:10285250] data has 92 bytes, str has size 92
2024-04-13 13:38:26.354445-0700 emission[77938:10285250] data has 69 bytes, str has size 69
2024-04-13 13:38:26.355964-0700 emission[77938:10285250] Got unexpected transition T_BLE_BEACON_FOUND in state STATE_ONGOING_TRIP, ignoring
2024-04-13 13:38:26.356106-0700 emission[77938:10285250] Ignoring silent push notification
```
  • Loading branch information
shankari committed Apr 13, 2024
1 parent 7cb3882 commit 463c2a5
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions www/js/bluetooth/BluetoothScanPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,14 @@ const BluetoothScanPage = ({ ...props }: any) => {
in_range: status,
},
}));
// using putMessage instead of putSensorData as a temporary hack for now
// since putSensorData is not exposed through javascript
let { monitorResult: _, in_range: _, ...noResultDevice } = sampleBLEDevices[uuid];
window['cordova']?.plugins?.BEMUserCache.putMessage('background/bluetooth_ble', {
eventType: status ? 'REGION_ENTER' : 'REGION_EXIT',
ts: Date.now() / 1000, // our convention is to have timestamps in seconds
uuid: uuid,
...noResultDevice, // gives us uuid, major, minor
});
window['cordova']?.plugins?.BEMDataCollection.mockBLEObjects(
status ? 'REGION_ENTER' : 'REGION_EXIT',
uuid,
undefined,
undefined,
1,
);
if (!status) {
forceTransition('BLE_BEACON_LOST');
}
Expand All @@ -131,13 +130,13 @@ const BluetoothScanPage = ({ ...props }: any) => {
} = sampleBLEDevices[uuid];
let parsedResult = JSON.parse(result);
parsedResult.beacons.forEach((beacon) => {
window['cordova']?.plugins?.BEMUserCache.putMessage('background/bluetooth_ble', {
eventType: 'RANGE_UPDATE',
ts: Date.now() / 1000, // our convention is to have timestamps in seconds
uuid: uuid,
...noResultDevice, // gives us uuid, major, minor
...beacon, // gives us proximity, accuracy and rssi
});
window['cordova']?.plugins?.BEMDataCollection.mockBLEObjects(
'RANGE_UPDATE',
uuid,
beacon.major,
beacon.minor,
5,
);
});
// we only check for the transition on "real" callbacks to avoid excessive
// spurious callbacks on android
Expand All @@ -147,17 +146,17 @@ const BluetoothScanPage = ({ ...props }: any) => {
// (last param)
let nowSec = DateTime.now().toUnixInteger();
let tq = { key: 'write_ts', startTs: nowSec - 5 * 60, endTs: nowSec };
let readBLEReadingsPromise = window['cordova']?.plugins?.BEMUserCache.getMessagesForInterval(
'background/bluetooth_ble',
tq,
false,
);
let readBLEReadingsPromise = window[
'cordova'
]?.plugins?.BEMUserCache.getSensorDataForInterval('background/bluetooth_ble', tq, false);
readBLEReadingsPromise.then((bleResponses) => {
let lastThreeResponses = bleResponses.slice(0, 3);
if (!lastThreeResponses.every((x) => x.eventType == 'RANGE_UPDATE')) {
// we add 5 entries at a time, so if we want 3 button presses,
// we really want 15 entries
let lastFifteenResponses = bleResponses.slice(0, 15);
if (!lastFifteenResponses.every((x) => x.eventType == 'RANGE_UPDATE')) {
console.log(
'Last three entries ' +
lastThreeResponses.map((x) => x.eventType) +
lastFifteenResponses.map((x) => x.eventType) +
' are not all RANGE_UPDATE, skipping transition',
);
return;
Expand All @@ -168,7 +167,9 @@ const BluetoothScanPage = ({ ...props }: any) => {
}
}

async function simulateLocation(state: String) {}
async function simulateLocation(state: String) {
forceTransition(state);
}

// BLE LOGIC
async function startBeaconScanning() {
Expand Down

0 comments on commit 463c2a5

Please sign in to comment.