Skip to content

Commit

Permalink
made logger public to enable overriding of the loglevel and filters
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisMoschina committed Jan 20, 2025
1 parent 2016bfd commit 87769b1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 44 deletions.
6 changes: 3 additions & 3 deletions lib/open_earable_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export 'src/models/capabilities/storage_path_audio_player.dart';

part 'src/constants.dart';

Logger _logger = Logger();
Logger logger = Logger();

class WearableManager {
static final WearableManager _instance = WearableManager._internal();
Expand Down Expand Up @@ -70,12 +70,12 @@ class WearableManager {
for (WearableFactory wearableFactory in _wearableFactories) {
wearableFactory.bleManager = _bleManager;
wearableFactory.disconnectNotifier = disconnectNotifier;
_logger.t("checking factory: $wearableFactory");
logger.t("checking factory: $wearableFactory");
if (await wearableFactory.matches(device, connectionResult.$2)) {
Wearable wearable = await wearableFactory.createFromDevice(device);
return wearable;
} else {
_logger.d("'$wearableFactory' does not support '$device'");
logger.d("'$wearableFactory' does not support '$device'");
}
}
throw Exception('Device is currently not supported');
Expand Down
27 changes: 13 additions & 14 deletions lib/src/models/devices/open_earable_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const String _deviceParseInfoServiceUuid =
const String _deviceParseInfoCharacteristicUuid =
"caa25cb8-7e1b-44f2-adc9-e8c06c9ced43";

Logger _logger = Logger();

class OpenEarableFactory extends WearableFactory {
final _v1Regex = RegExp(r'^1\.\d+\.\d+$');
Expand All @@ -27,13 +26,13 @@ class OpenEarableFactory extends WearableFactory {
@override
Future<bool> matches(DiscoveredDevice device, List<BleService> services) async {
if (!services.any((service) => service.uuid == _deviceInfoServiceUuid)) {
_logger.d("'$device' has no service matching '$_deviceInfoServiceUuid'");
logger.d("'$device' has no service matching '$_deviceInfoServiceUuid'");
return false;
}
String firmwareVersion = await _getFirmwareVersion(device);
_logger.d("Firmware Version: '$firmwareVersion'");
logger.d("Firmware Version: '$firmwareVersion'");

_logger.t("matches V2: ${_v2Regex.hasMatch(firmwareVersion)}");
logger.t("matches V2: ${_v2Regex.hasMatch(firmwareVersion)}");

return _v1Regex.hasMatch(firmwareVersion) || _v2Regex.hasMatch(firmwareVersion);
}
Expand Down Expand Up @@ -77,7 +76,7 @@ class OpenEarableFactory extends WearableFactory {
serviceId: _deviceInfoServiceUuid,
characteristicId: _deviceFirmwareVersionCharacteristicUuid,
);
_logger.d("Raw Firmware Version: $softwareGenerationBytes");
logger.d("Raw Firmware Version: $softwareGenerationBytes");
int firstZeroIndex = softwareGenerationBytes.indexOf(0);
if (firstZeroIndex != -1) {
softwareGenerationBytes = softwareGenerationBytes.sublist(0, firstZeroIndex);
Expand All @@ -94,9 +93,9 @@ class OpenEarableFactory extends WearableFactory {
serviceId: _deviceParseInfoServiceUuid,
characteristicId: _deviceParseInfoCharacteristicUuid,
);
_logger.d("Read raw parse info: $sensorParseSchemeData");
logger.d("Read raw parse info: $sensorParseSchemeData");
Map<String, Object> parseInfo = _parseSchemeCharacteristic(sensorParseSchemeData);
_logger.i("Found the following info about parsing: $parseInfo");
logger.i("Found the following info about parsing: $parseInfo");

OpenEarableSensorManager sensorManager = OpenEarableSensorManager(
bleManager: bleManager!,
Expand All @@ -105,10 +104,10 @@ class OpenEarableFactory extends WearableFactory {

for (String sensorName in parseInfo.keys) {
Map<String, Object> sensorDetail = parseInfo[sensorName] as Map<String, Object>;
_logger.t("sensor detail: $sensorDetail");
logger.t("sensor detail: $sensorDetail");

Map<String, Object> componentsMap = sensorDetail['Components'] as Map<String, Object>;
_logger.t("components: $componentsMap");
logger.t("components: $componentsMap");

sensorConfigurations.add(
_OpenEarableSensorConfiguration(
Expand All @@ -121,7 +120,7 @@ class OpenEarableFactory extends WearableFactory {
for (String groupName in componentsMap.keys) {

Map<String, Object> groupDetail = componentsMap[groupName] as Map<String, Object>;
_logger.t("group detail: $groupDetail");
logger.t("group detail: $groupDetail");
List<(String, String)> axisDetails = groupDetail.entries.map((axis) {
Map<String, Object> v = axis.value as Map<String, Object>;
return (axis.key, v['unit'] as String);
Expand All @@ -141,8 +140,8 @@ class OpenEarableFactory extends WearableFactory {
}
}

_logger.d("Created sensors: $sensors");
_logger.d("Created sensor configurations: $sensorConfigurations");
logger.d("Created sensors: $sensors");
logger.d("Created sensor configurations: $sensorConfigurations");

return (sensors, sensorConfigurations);
}
Expand Down Expand Up @@ -290,9 +289,9 @@ class _OpenEarableSensor extends Sensor {
_dataSubscription?.cancel();
_dataSubscription = _sensorManager.subscribeToSensorData(_sensorId).listen((data) {
int timestamp = data["timestamp"];
_logger.t("SensorData: $data");
logger.t("SensorData: $data");

_logger.t("componentData of $componentName: ${data[componentName]}");
logger.t("componentData of $componentName: ${data[componentName]}");

List<double> values = [];
for (var entry in (data[componentName] as Map).entries) {
Expand Down
39 changes: 12 additions & 27 deletions lib/src/models/devices/open_earable_v2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,8 @@ import 'dart:async';
import 'dart:math';
import 'dart:typed_data';

import 'package:logger/logger.dart';
import 'package:open_earable_flutter/src/models/capabilities/battery_service.dart';
import 'package:open_earable_flutter/src/models/capabilities/status_led.dart';

import '../capabilities/device_firmware_version.dart';
import '../capabilities/device_hardware_version.dart';
import '../capabilities/device_identifier.dart';
import '../capabilities/rgb_led.dart';
import '../capabilities/sensor.dart';
import '../capabilities/sensor_configuration.dart';
import '../capabilities/sensor_configuration_manager.dart';
import '../capabilities/sensor_manager.dart';
import 'package:open_earable_flutter/open_earable_flutter.dart';
import '../../managers/ble_manager.dart';
import 'discovered_device.dart';
import 'wearable.dart';

const String _batteryServiceUuid = "180F";
const String _batteryLevelCharacteristicUuid = "2A19";
Expand All @@ -38,8 +25,6 @@ const String _deviceFirmwareVersionCharacteristicUuid =
const String _deviceHardwareVersionCharacteristicUuid =
"45622513-6468-465a-b141-0b9b0f96b468";

Logger _logger = Logger();

class OpenEarableV2 extends Wearable
implements
SensorManager,
Expand Down Expand Up @@ -162,7 +147,7 @@ class OpenEarableV2 extends Wearable
characteristicId: _batteryLevelCharacteristicUuid,
);

_logger.t("Battery level bytes: $batteryLevelList");
logger.t("Battery level bytes: $batteryLevelList");

if (batteryLevelList.length != 1) {
throw StateError('Battery level characteristic expected 1 value, but got ${batteryLevelList.length}');
Expand All @@ -179,7 +164,7 @@ class OpenEarableV2 extends Wearable
characteristicId: _batteryEnergyStatusCharacteristicUuid,
);

_logger.t("Battery energy status bytes: $energyStatusList");
logger.t("Battery energy status bytes: $energyStatusList");

if (energyStatusList.length != 7) {
throw StateError('Battery energy status characteristic expected 7 values, but got ${energyStatusList.length}');
Expand All @@ -200,7 +185,7 @@ class OpenEarableV2 extends Wearable
chargeRate: chargeRate,
);

_logger.d('Battery energy status: $batteryEnergyStatus');
logger.d('Battery energy status: $batteryEnergyStatus');

return batteryEnergyStatus;
}
Expand All @@ -212,7 +197,7 @@ class OpenEarableV2 extends Wearable
if (mantissa >= 0x800) {
mantissa = -((0x1000) - mantissa);
}
_logger.t("Exponent: $exponent, Mantissa: $mantissa");
logger.t("Exponent: $exponent, Mantissa: $mantissa");
double result = mantissa.toDouble() * pow(10.0, exponent.toDouble());
return result;
}
Expand All @@ -225,7 +210,7 @@ class OpenEarableV2 extends Wearable
characteristicId: _batteryHealthStatusCharacteristicUuid,
);

_logger.t("Battery health status bytes: $healthStatusList");
logger.t("Battery health status bytes: $healthStatusList");

if (healthStatusList.length != 5) {
throw StateError('Battery health status characteristic expected 5 values, but got ${healthStatusList.length}');
Expand All @@ -241,7 +226,7 @@ class OpenEarableV2 extends Wearable
currentTemperature: currentTemperature,
);

_logger.d('Battery health status: $batteryHealthStatus');
logger.d('Battery health status: $batteryHealthStatus');

return batteryHealthStatus;
}
Expand All @@ -255,7 +240,7 @@ class OpenEarableV2 extends Wearable
);

int powerState = (powerStateList[1] << 8) | powerStateList[2];
_logger.d("Battery power status bits: ${powerState.toRadixString(2)}");
logger.d("Battery power status bits: ${powerState.toRadixString(2)}");

bool batteryPresent = powerState >> 15 & 0x1 != 0;

Expand Down Expand Up @@ -298,7 +283,7 @@ class OpenEarableV2 extends Wearable
chargingFaultReason: chargingFaultReason,
);

_logger.d('Battery power status: $batteryPowerStatus');
logger.d('Battery power status: $batteryPowerStatus');

return batteryPowerStatus;
}
Expand All @@ -317,7 +302,7 @@ class OpenEarableV2 extends Wearable
try {
yield await readPowerStatus();
} catch (e) {
_logger.e('Error reading power status: $e');
logger.e('Error reading power status: $e');
}
await Future.delayed(const Duration(seconds: 5));
}
Expand All @@ -329,7 +314,7 @@ class OpenEarableV2 extends Wearable
try {
yield await readEnergyStatus();
} catch (e) {
_logger.e('Error reading energy status: $e');
logger.e('Error reading energy status: $e');
}
await Future.delayed(const Duration(seconds: 5));
}
Expand All @@ -341,7 +326,7 @@ class OpenEarableV2 extends Wearable
try {
yield await readHealthStatus();
} catch (e) {
_logger.e('Error reading health status: $e');
logger.e('Error reading health status: $e');
}
await Future.delayed(const Duration(seconds: 5));
}
Expand Down

0 comments on commit 87769b1

Please sign in to comment.