Skip to content

Commit

Permalink
Pass logger to BleService constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
chetstone committed Oct 23, 2021
1 parent a7af729 commit 27a5b57
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 83 deletions.
56 changes: 0 additions & 56 deletions ios/Podfile

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"rr": "adb shell input text \"RR\"",
"test": "jest --watch",
"build": "cd android && ./gradlew assembleRelease",
"lint": "eslint src",
"lint": "eslint --ext .tsx --ext .ts --ext .js --ext .jsx src",
"deploy": "./dev_scripts/deploy.sh",
"export": "./dev_scripts/export.sh",
"uninstall": "./dev_scripts/uninstall.sh",
Expand All @@ -37,7 +37,7 @@
"preset": "react-native"
},
"resolutions": {
"react-devtools-core": "4.6.0"
"react-devtools-core": "4.13.1"
},
"lint-staged": {
"*.{json,css,md}": [
Expand Down
33 changes: 18 additions & 15 deletions src/bluetooth/BleService.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { BleManager } from 'react-native-ble-plx';
import { Buffer } from 'buffer';
import { BLUETOOTH } from './constants';
import LoggerService from '../utilities/logging';

const bufferFromBase64 = base64 => Buffer.from(base64, 'base64');
const stringFromBase64 = base64 => bufferFromBase64(base64).toString('utf-8');
const base64FromString = string => Buffer.from(string, 'utf-8').toString('base64');

const logger = LoggerService.createLogger('BleService');

/**
* Interface for interacting with a native Ble Manager for interacting
* with BlueMaestro temperature sensors, specifically.
Expand All @@ -28,10 +25,16 @@ const logger = LoggerService.createLogger('BleService');
*
*/
class BleService {
constructor(manager = new BleManager()) {
constructor(
manager = new BleManager(),
logger = {
info: () => {},
}
) {
this.manager = manager;
this.logger = logger;

logger.info('BleService constructor', { manager });
this.logger.info('BleService constructor', { manager });
}

setManager = manager => {
Expand All @@ -47,7 +50,7 @@ class BleService {
* @param {String} macAddress
*/
connectToDevice = async macAddress => {
logger.info('connectToDevice', { macAddress });
this.logger.info('connectToDevice', { macAddress });
return this.manager.connectToDevice(macAddress);
};

Expand All @@ -60,13 +63,13 @@ class BleService {
* @param {String} macAddress
*/
connectAndDiscoverServices = async macAddress => {
logger.info('connectAndDiscoverServices', { macAddress });
this.logger.info('connectAndDiscoverServices', { macAddress });
// without the cancel & reconnect further commands
// were sometimes returning an error: "BleError: Device [mac address] was disconnected"
// Note: adding the option `{ autoConnect: true }` prevents the sensors from
// connecting sometimes :shrug:
const deviceIsConnected = await this.manager.isDeviceConnected(macAddress);
logger.info('deviceIsConnected', { deviceIsConnected });
this.logger.info('deviceIsConnected', { deviceIsConnected });
if (deviceIsConnected) {
await this.manager.cancelDeviceConnection(macAddress);
}
Expand All @@ -75,7 +78,7 @@ class BleService {

await this.manager.discoverAllServicesAndCharacteristicsForDevice(macAddress);

logger.info('Discovered all services and characteristics for device', { macAddress });
this.logger.info('Discovered all services and characteristics for device', { macAddress });
return device;
};

Expand Down Expand Up @@ -242,9 +245,9 @@ class BleService {
*/
downloadLogs = async macAddress => {
await this.connectAndDiscoverServices(macAddress);
logger.info('Download logs connected and discovered services', { macAddress });
this.logger.info('Download logs connected and discovered services', { macAddress });
return this.writeAndMonitor(macAddress, BLUETOOTH.COMMANDS.DOWNLOAD, data => {
logger.info('Write and monitor found some data!', { data });
this.logger.info('Write and monitor found some data!', { data });
const buffer = Buffer.concat(data.slice(1).map(datum => bufferFromBase64(datum)));

const ind = buffer.findIndex(
Expand Down Expand Up @@ -284,7 +287,7 @@ class BleService {
*
* Connects with and blinks a sensors LED light.
*
* Returns a promise which resolves to a string 'ok'
* Returns a promise which resolves to a boolean
*
* @param {String} macAddress
*/
Expand Down Expand Up @@ -371,7 +374,7 @@ class BleService {
* @param {Error} error
*/
downloadLogsWithRetries = async (macAddress, retriesLeft, error) => {
logger.info('Starting to download logs', { macAddress, retriesLeft, error });
this.logger.info('Starting to download logs', { macAddress, retriesLeft, error });
if (!retriesLeft) throw error;

return this.downloadLogs(macAddress).catch(err =>
Expand Down Expand Up @@ -413,9 +416,9 @@ class BleService {

let BleServiceInstance;

export const getBleServiceInstance = manager => {
export const getBleServiceInstance = (manager, logger) => {
if (!BleServiceInstance) {
BleServiceInstance = new BleService(manager);
BleServiceInstance = new BleService(manager, logger);
}

return BleServiceInstance;
Expand Down
4 changes: 3 additions & 1 deletion src/mSupplyMobileApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { connect } from 'react-redux';
import { BluetoothStatus } from 'react-native-bluetooth-status';
import { AppState, View } from 'react-native';
import { Scheduler } from 'sussol-utilities';
import { BleManager } from 'react-native-ble-plx';

import Settings from './settings/MobileAppSettings';
import Database from './database/BaseDatabase';
Expand Down Expand Up @@ -55,6 +56,7 @@ import { UtilService } from './database/utilities/utilService';
import { SensorDownloadActions } from './actions/Bluetooth/SensorDownloadActions';
import BreachManager from './bluetooth/BreachManager';
import { selectIsPassivelyDownloadingTemps } from './selectors/Bluetooth/sensorDownload';
import LoggerService from './utilities/logging';

const BLUETOOTH_SYNC_INTERVAL = 60 * 1000; // 1 minute in milliseconds.
const AUTHENTICATION_INTERVAL = 10 * 60 * 1000; // 10 minutes in milliseconds.
Expand All @@ -69,7 +71,7 @@ BreachManager(new VaccineDataAccess(UIDatabase), new UtilService());
console.log('Emulator detected - Init Dev BleManager');
BleService(new DevBleManager());
} else {
BleService();
BleService(new BleManager(), LoggerService.createLogger('BleService'));
}
})();

Expand Down
1 change: 1 addition & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ LogBox.ignoreLogs([
'Setting a timer',
'componentWillMount',
'Non-serializable values were found in the navigation state',
'Require cycle',
]);

AppRegistry.registerComponent(appName, () => App);
10 changes: 1 addition & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8579,15 +8579,7 @@ react-deep-force-update@^1.0.0:
resolved "https://registry.yarnpkg.com/react-deep-force-update/-/react-deep-force-update-1.1.2.tgz#3d2ae45c2c9040cbb1772be52f8ea1ade6ca2ee1"
integrity sha512-WUSQJ4P/wWcusaH+zZmbECOk7H5N2pOIl0vzheeornkIMhu+qrNdGFm0bDZLCb0hSF0jf/kH1SgkNGfBdTc4wA==

[email protected], [email protected], react-devtools-core@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/react-devtools-core/-/react-devtools-core-4.6.0.tgz#2443b3c6fac78b801702af188abc6d83d56224e6"
integrity sha512-sjR3KC5VvGV7X6vzR3OTutPT5VeBcSKwoIXUwihpl1Nb4dkmweEbzCTPx2PYMVAqc+NZ5tPGhqBzXV+iGg5CNA==
dependencies:
shell-quote "^1.6.1"
ws "^7"

react-devtools-core@^4.13.1:
[email protected], react-devtools-core@^4.13.1, react-devtools-core@^4.6.0:
version "4.13.1"
resolved "https://registry.yarnpkg.com/react-devtools-core/-/react-devtools-core-4.13.1.tgz#84e1b51a8cb2034e1e2276851ace7874cbb8f083"
integrity sha512-+N7vZgjQWqkPe/q7yltXOi20U3Zy4WdXax9IcLCNB4nWsUxLrkVF0Mqbsr3h4m/j0dA4046QpJOnlwdWVQuiFw==
Expand Down

0 comments on commit 27a5b57

Please sign in to comment.