From 862c1384e1ad85bc018636a30226bed2340a9e53 Mon Sep 17 00:00:00 2001 From: Minyewoo Date: Tue, 2 Apr 2024 13:05:41 +0300 Subject: [PATCH] JdsServiceStartupOnReconnect | updated --- .../jds_service_startup_on_reconnect.dart | 69 ++++++++++--------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/lib/src/core/jds_service/jds_service_startup_on_reconnect.dart b/lib/src/core/jds_service/jds_service_startup_on_reconnect.dart index a6af80e..e805e27 100644 --- a/lib/src/core/jds_service/jds_service_startup_on_reconnect.dart +++ b/lib/src/core/jds_service/jds_service_startup_on_reconnect.dart @@ -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> _connectionStatuses; final JdsServiceStartup _startup; - bool _isConnected; - bool _isStartupCompleted = true; - StreamSubscription? _connectionSubscription; + bool _isConnected = true; + bool _isStartupCompleted = false; + StreamSubscription? _connectionSubscription; /// /// [JdsService] cache update sequence. JdsServiceStartupOnReconnect({ @@ -24,39 +23,41 @@ class JdsServiceStartupOnReconnect { _startup = startup, _isConnected = initialConnectionStatus; /// - void run() { - _connectionSubscription ??= NonRepetitiveStream( - stream: _connectionStatuses.map( - (point) { - switch(point) { - case DsDataPoint( - 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 dispose() async {