Skip to content

Commit 463c2a5

Browse files
committed
🤡 Switch the simulation code in the UI to use the mock objects
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 ```
1 parent 7cb3882 commit 463c2a5

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

www/js/bluetooth/BluetoothScanPage.tsx

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,14 @@ const BluetoothScanPage = ({ ...props }: any) => {
9797
in_range: status,
9898
},
9999
}));
100-
// using putMessage instead of putSensorData as a temporary hack for now
101-
// since putSensorData is not exposed through javascript
102100
let { monitorResult: _, in_range: _, ...noResultDevice } = sampleBLEDevices[uuid];
103-
window['cordova']?.plugins?.BEMUserCache.putMessage('background/bluetooth_ble', {
104-
eventType: status ? 'REGION_ENTER' : 'REGION_EXIT',
105-
ts: Date.now() / 1000, // our convention is to have timestamps in seconds
106-
uuid: uuid,
107-
...noResultDevice, // gives us uuid, major, minor
108-
});
101+
window['cordova']?.plugins?.BEMDataCollection.mockBLEObjects(
102+
status ? 'REGION_ENTER' : 'REGION_EXIT',
103+
uuid,
104+
undefined,
105+
undefined,
106+
1,
107+
);
109108
if (!status) {
110109
forceTransition('BLE_BEACON_LOST');
111110
}
@@ -131,13 +130,13 @@ const BluetoothScanPage = ({ ...props }: any) => {
131130
} = sampleBLEDevices[uuid];
132131
let parsedResult = JSON.parse(result);
133132
parsedResult.beacons.forEach((beacon) => {
134-
window['cordova']?.plugins?.BEMUserCache.putMessage('background/bluetooth_ble', {
135-
eventType: 'RANGE_UPDATE',
136-
ts: Date.now() / 1000, // our convention is to have timestamps in seconds
137-
uuid: uuid,
138-
...noResultDevice, // gives us uuid, major, minor
139-
...beacon, // gives us proximity, accuracy and rssi
140-
});
133+
window['cordova']?.plugins?.BEMDataCollection.mockBLEObjects(
134+
'RANGE_UPDATE',
135+
uuid,
136+
beacon.major,
137+
beacon.minor,
138+
5,
139+
);
141140
});
142141
// we only check for the transition on "real" callbacks to avoid excessive
143142
// spurious callbacks on android
@@ -147,17 +146,17 @@ const BluetoothScanPage = ({ ...props }: any) => {
147146
// (last param)
148147
let nowSec = DateTime.now().toUnixInteger();
149148
let tq = { key: 'write_ts', startTs: nowSec - 5 * 60, endTs: nowSec };
150-
let readBLEReadingsPromise = window['cordova']?.plugins?.BEMUserCache.getMessagesForInterval(
151-
'background/bluetooth_ble',
152-
tq,
153-
false,
154-
);
149+
let readBLEReadingsPromise = window[
150+
'cordova'
151+
]?.plugins?.BEMUserCache.getSensorDataForInterval('background/bluetooth_ble', tq, false);
155152
readBLEReadingsPromise.then((bleResponses) => {
156-
let lastThreeResponses = bleResponses.slice(0, 3);
157-
if (!lastThreeResponses.every((x) => x.eventType == 'RANGE_UPDATE')) {
153+
// we add 5 entries at a time, so if we want 3 button presses,
154+
// we really want 15 entries
155+
let lastFifteenResponses = bleResponses.slice(0, 15);
156+
if (!lastFifteenResponses.every((x) => x.eventType == 'RANGE_UPDATE')) {
158157
console.log(
159158
'Last three entries ' +
160-
lastThreeResponses.map((x) => x.eventType) +
159+
lastFifteenResponses.map((x) => x.eventType) +
161160
' are not all RANGE_UPDATE, skipping transition',
162161
);
163162
return;
@@ -168,7 +167,9 @@ const BluetoothScanPage = ({ ...props }: any) => {
168167
}
169168
}
170169

171-
async function simulateLocation(state: String) {}
170+
async function simulateLocation(state: String) {
171+
forceTransition(state);
172+
}
172173

173174
// BLE LOGIC
174175
async function startBeaconScanning() {

0 commit comments

Comments
 (0)