Skip to content

Commit fc94232

Browse files
authored
fix: sciencelab instance mgmt (#2627)
1 parent 07a6a29 commit fc94232

File tree

4 files changed

+51
-31
lines changed

4 files changed

+51
-31
lines changed

lib/others/science_lab_common.dart

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,40 @@ import 'package:pslab/communication/science_lab.dart';
33
import 'package:pslab/others/logger_service.dart';
44

55
class ScienceLabCommon {
6-
static late ScienceLab scienceLab;
7-
bool connected = false;
6+
static late ScienceLab _scienceLab;
7+
static late CommunicationHandler communicationHandler;
88

9-
ScienceLabCommon._privateConstructor();
10-
11-
static final ScienceLabCommon _instance =
12-
ScienceLabCommon._privateConstructor();
9+
ScienceLabCommon(CommunicationHandler communicationHandler) {
10+
communicationHandler = communicationHandler;
11+
_scienceLab = ScienceLab(communicationHandler);
12+
}
1313

14-
factory ScienceLabCommon() => _instance;
14+
ScienceLab getScienceLab() {
15+
return _scienceLab;
16+
}
1517

16-
Future<bool> openDevice(CommunicationHandler communicationHandler) async {
17-
scienceLab = ScienceLab(communicationHandler);
18-
await scienceLab.connect();
19-
if (!scienceLab.isConnected()) {
18+
Future<bool> openDevice() async {
19+
await _scienceLab.connect();
20+
if (!_scienceLab.isConnected()) {
2021
logger.d("Error in connection");
2122
return false;
2223
}
23-
connected = true;
2424
return true;
2525
}
26+
27+
Future<void> initialize() {
28+
return communicationHandler.initialize();
29+
}
30+
31+
void setConnected() {
32+
communicationHandler.connected = true;
33+
}
34+
35+
bool isConnected() {
36+
return communicationHandler.isConnected();
37+
}
38+
39+
bool isDeviceFound() {
40+
return communicationHandler.isDeviceFound();
41+
}
2642
}

lib/providers/board_state_provider.dart

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
import 'package:flutter/cupertino.dart';
2-
import 'package:get_it/get_it.dart';
3-
import 'package:pslab/communication/handler/base.dart';
2+
import 'package:pslab/communication/science_lab.dart';
43
import 'package:pslab/others/logger_service.dart';
4+
import 'package:pslab/providers/locator.dart';
55
import 'package:usb_serial/usb_serial.dart';
66

7-
import '../others/science_lab_common.dart';
7+
import 'package:pslab/others/science_lab_common.dart';
88

99
class BoardStateProvider extends ChangeNotifier {
1010
bool initialisationStatus = false;
1111
bool pslabIsConnected = false;
1212
bool hasPermission = false;
13-
late CommunicationHandler communicationHandler;
1413
late ScienceLabCommon scienceLabCommon;
1514
String pslabVersionID = 'Not Connected';
1615

16+
BoardStateProvider() {
17+
scienceLabCommon = getIt.get<ScienceLabCommon>();
18+
}
19+
1720
Future<void> initialize() async {
18-
scienceLabCommon = ScienceLabCommon();
19-
communicationHandler = GetIt.instance.get<CommunicationHandler>();
20-
await communicationHandler.initialize();
21-
pslabIsConnected = await scienceLabCommon.openDevice(communicationHandler);
21+
await scienceLabCommon.initialize();
22+
pslabIsConnected = await scienceLabCommon.openDevice();
2223
setPSLabVersionIDs();
2324
UsbSerial.usbEventStream?.listen((UsbEvent usbEvent) async {
2425
if (usbEvent.event == UsbEvent.ACTION_USB_ATTACHED) {
2526
if (await attemptToConnectPSLab()) {
26-
pslabIsConnected =
27-
await scienceLabCommon.openDevice(communicationHandler);
27+
pslabIsConnected = await scienceLabCommon.openDevice();
2828
setPSLabVersionIDs();
2929
}
3030
} else if (usbEvent.event == UsbEvent.ACTION_USB_DETACHED) {
31-
communicationHandler.connected = false;
31+
scienceLabCommon.setConnected();
3232
pslabIsConnected = false;
3333
pslabVersionID = 'Not Connected';
3434
notifyListeners();
@@ -37,18 +37,16 @@ class BoardStateProvider extends ChangeNotifier {
3737
}
3838

3939
Future<void> setPSLabVersionIDs() async {
40-
pslabVersionID = await ScienceLabCommon.scienceLab.getVersion();
40+
pslabVersionID = await getIt.get<ScienceLab>().getVersion();
4141
notifyListeners();
4242
}
4343

4444
Future<bool> attemptToConnectPSLab() async {
45-
scienceLabCommon = ScienceLabCommon();
46-
if (communicationHandler.isConnected()) {
45+
if (scienceLabCommon.isConnected()) {
4746
logger.d("Device Connected Successfully");
4847
} else {
49-
communicationHandler = CommunicationHandler();
50-
await communicationHandler.initialize();
51-
if (communicationHandler.isDeviceFound()) {
48+
await scienceLabCommon.initialize();
49+
if (scienceLabCommon.isDeviceFound()) {
5250
return true;
5351
}
5452
}

lib/providers/locator.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import 'package:get_it/get_it.dart';
44
import 'package:pslab/communication/handler/android_comms_handler.dart';
55
import 'package:pslab/communication/handler/base.dart';
66
import 'package:pslab/communication/handler/ios_comms_handler.dart';
7+
import 'package:pslab/communication/science_lab.dart';
8+
import 'package:pslab/others/science_lab_common.dart';
79
import 'package:pslab/providers/board_state_provider.dart';
810

911
final GetIt getIt = GetIt.instance;
@@ -16,5 +18,9 @@ void setupLocator() {
1618
return IosNoOpCommunicationHandler();
1719
}
1820
});
21+
getIt.registerLazySingleton<ScienceLabCommon>(
22+
() => ScienceLabCommon(getIt.get<CommunicationHandler>()));
23+
getIt.registerLazySingleton<ScienceLab>(
24+
() => getIt.get<ScienceLabCommon>().getScienceLab());
1925
getIt.registerLazySingleton<BoardStateProvider>(() => BoardStateProvider());
2026
}

lib/providers/oscilloscope_state_provider.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'package:data/data.dart';
66
import 'package:fl_chart/fl_chart.dart';
77
import 'package:flutter/material.dart';
88
import 'package:pslab/others/logger_service.dart';
9-
import 'package:pslab/others/science_lab_common.dart';
9+
import 'package:pslab/providers/locator.dart';
1010

1111
import '../communication/analytics_class.dart';
1212
import '../communication/science_lab.dart';
@@ -118,7 +118,7 @@ class OscilloscopeStateProvider extends ChangeNotifier {
118118
_channelIndexMap[CHANNEL.ch3.toString()] = 3;
119119
_channelIndexMap[CHANNEL.mic.toString()] = 4;
120120

121-
_scienceLab = ScienceLabCommon.scienceLab;
121+
_scienceLab = getIt.get<ScienceLab>();
122122
triggerChannel = CHANNEL.ch1.toString();
123123
_trigger = 0;
124124
timebase = 875;

0 commit comments

Comments
 (0)