Skip to content

Commit

Permalink
JdsServiceStartupOnReconnect | updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Minyewoo committed Apr 2, 2024
1 parent ec71ab7 commit 862c138
Showing 1 changed file with 35 additions and 34 deletions.
69 changes: 35 additions & 34 deletions lib/src/core/jds_service/jds_service_startup_on_reconnect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ import 'package:hmi_core/hmi_core.dart';
import 'package:hmi_core/hmi_core_result_new.dart';
import 'package:hmi_networking/src/core/jds_service/jds_service.dart';
import 'package:hmi_networking/src/core/jds_service/jds_service_startup.dart';
import 'package:hmi_networking/src/core/non_repetitive_stream.dart';
///
/// [JdsService] cache update sequence.
class JdsServiceStartupOnReconnect {
static const _log = Log('JdsServiceStartupOnReconnect');
final Stream<DsDataPoint<int>> _connectionStatuses;
final JdsServiceStartup _startup;
bool _isConnected;
bool _isStartupCompleted = true;
StreamSubscription<bool>? _connectionSubscription;
bool _isConnected = true;
bool _isStartupCompleted = false;
StreamSubscription? _connectionSubscription;
///
/// [JdsService] cache update sequence.
JdsServiceStartupOnReconnect({
Expand All @@ -24,39 +23,41 @@ class JdsServiceStartupOnReconnect {
_startup = startup,
_isConnected = initialConnectionStatus;
///
void run() {
_connectionSubscription ??= NonRepetitiveStream(
stream: _connectionStatuses.map(
(point) {
switch(point) {
case DsDataPoint<int>(
value: final connectionStatus,
status: DsStatus.ok,
cot: DsCot.inf,
):
final isConnected = connectionStatus == DsStatus.ok.value;
_isConnected = isConnected;
return isConnected;
case _:
return false;
}
},
),
).stream
.where((_) => _isStartupCompleted)
.listen((_) async {
while(_isConnected) {
_log.info('Entering startup loop...');
_isStartupCompleted = false;
if(await _startup.run() case Ok()) {
_log.info('Startup completed! Exiting startup loop...');
_isStartupCompleted = true;
break;
void run() async {
_connectionSubscription = _connectionStatuses.listen((point) {
_log.info('ConnectionStatus: status: ${point.status}');
if (point.status == DsStatus.ok) {
_log.info('ConnectionStatus: value : ${point.value} (${DsStatus.ok.value})');
final connectionStatus = '${point.value}';
if (connectionStatus == '${DsStatus.ok.value}') {
_log.info('ConnectionStatus: is connected!');
_isConnected = true;
} else {
_log.info('ConnectionStatus: is not connected.');
_isConnected = false;
_isStartupCompleted = false;
}
_log.info('Startup failed! Starting next startup attempt...');
_isStartupCompleted = true;
} else {
_log.info('ConnectionStatus: is not connected.');
_isConnected = false;
_isStartupCompleted = false;
}
});
_log.info('Entering startup loop...');
while (true) {
_log.info('_isConnected: $_isConnected');
_log.info('_isStartupCompleted: $_isStartupCompleted');
if (_isConnected && !_isStartupCompleted) {
_log.info('Starting up...');
final result = await _startup.run();
_isStartupCompleted = switch (result) {
Ok() => true,
Err() => false,
};
_log.info('Startup completed, result: $result');
}
await Future.delayed(const Duration(seconds: 1));
}
}
///
Future<void> dispose() async {
Expand Down

0 comments on commit 862c138

Please sign in to comment.