From 463c2a5b642087e3901c995f8a57d4287d802067 Mon Sep 17 00:00:00 2001 From: "K. Shankari" Date: Sat, 13 Apr 2024 14:20:29 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=A1=20Switch=20the=20simulation=20code?= =?UTF-8?q?=20in=20the=20UI=20to=20use=20the=20mock=20objects?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In https://github.com/e-mission/e-mission-data-collection/pull/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 ``` --- www/js/bluetooth/BluetoothScanPage.tsx | 49 +++++++++++++------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/www/js/bluetooth/BluetoothScanPage.tsx b/www/js/bluetooth/BluetoothScanPage.tsx index d855c47c3..a4e5bda9e 100644 --- a/www/js/bluetooth/BluetoothScanPage.tsx +++ b/www/js/bluetooth/BluetoothScanPage.tsx @@ -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'); } @@ -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 @@ -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; @@ -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() {