Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgraded Result across the project #48

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/src/core/ds_client.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:hmi_core/hmi_core.dart';
import 'package:hmi_core/hmi_core_result_new.dart';

///
abstract class DsClient {
Expand Down Expand Up @@ -29,20 +30,20 @@ abstract class DsClient {
/// то они прийдут в текущем активном подключении
/// в потоке Stream<DsDataPoint> stream
/// В качестве результата Result<bool> получает результат записи в socket
Future<Result<bool>> send(
Future<ResultF<void>> send(
DsCommand dsCommand,
);
///
/// Делает запрос на S7 DataServer что бы получить все точки данных
/// что бы сервер прочитал и прислал значения всех точек в потоке.
/// Данные не ждем, они прийдут в потоке
Future<Result<bool>> requestAll();
Future<ResultF<void>> requestAll();
///
/// Делает запрос на S7 DataServer в виде списка имен точек данных
/// что бы сервер прочитал и прислал значения запрошенных точек в потоке
/// В качестве результата Result<bool> получает результат чтения из S7
/// данные не ждем, они прийдут в потоке
Future<Result<bool>> requestNamed(List<String> names);
Future<ResultF<void>> requestNamed(List<String> names);
///
/// Returns number of live subscriptions
int get subscriptionsCount;
Expand Down Expand Up @@ -84,13 +85,13 @@ abstract class DsClient {
/// то они прийдут в текущем активном подключении
/// в потоке Stream<DsDataPoint> stream
/// В качестве результата Result<bool> получает результат записи в socket
Result<bool> sendEmulated(
ResultF<void> sendEmulated(
DsCommand dsCommand,
);
///
/// Делает запрос на S7 DataServer в виде списка имен точек данных
/// что бы сервер прочитал и прислал значения запрошенных точек в потоке
/// В качестве результата Result<bool> получает результат чтения из S7
/// данные не ждем, они прийдут в потоке
Result<bool> requestNamedEmulated(List<String> names);
ResultF<void> requestNamedEmulated(List<String> names);
}
13 changes: 7 additions & 6 deletions lib/src/core/ds_client_real.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

Check notice on line 1 in lib/src/core/ds_client_real.dart

View workflow job for this annotation

GitHub Actions / coverage

Good coverage level

34.05%

import 'package:hmi_core/hmi_core.dart';
import 'package:hmi_core/hmi_core_result_new.dart';
import 'package:hmi_networking/src/core/ds_client.dart';
import 'package:hmi_networking/src/core/ds_client_connection_listener.dart';
import 'package:hmi_networking/src/protocols/custom_protocol_line.dart';
Expand Down Expand Up @@ -249,7 +250,7 @@
/// в потоке Stream<DsDataPoint> stream
/// В качестве результата Result<bool> получает результат записи в socket
@override
Future<Result<bool>> send(
Future<ResultF<void>> send(
DsCommand dsCommand,
) {
return _line.send(dsCommand);
Expand All @@ -259,15 +260,15 @@
/// что бы сервер прочитал и прислал значения всех точек в потоке.
/// Данные не ждем, они прийдут в потоке
@override
Future<Result<bool>> requestAll() {
Future<ResultF<void>> requestAll() {
return _line.requestAll();
}
///
/// Делает запрос на S7 DataServer в виде списка имен точек данных
/// что бы сервер прочитал и прислал значения запрошенных точек в потоке.
/// Данные не ждем, они прийдут в потоке
@override
Future<Result<bool>> requestNamed(List<String> names) {
Future<ResultF<void>> requestNamed(List<String> names) {
return send(DsCommand(
dsClass: DsDataClass.requestList,
type: DsDataType.bool,
Expand All @@ -279,7 +280,7 @@
}
///
/// Останавливаем цикл обработки входящего потока данных от S7 DataServer
Future cancel() {
Future<void> cancel() {
// TODO _dsClientConnectionListener must be released
// _dsClientConnectionListener.close();
return _line.close();
Expand Down Expand Up @@ -320,7 +321,7 @@
/// в потоке Stream<DsDataPoint> stream
/// В качестве результата Result<bool> получает результат записи в socket
@override
Result<bool> sendEmulated(
ResultF<void> sendEmulated(
DsCommand dsCommand,
) {
throw Failure.unexpected(
Expand All @@ -334,7 +335,7 @@
/// В качестве результата Result<bool> получает результат чтения из S7
/// данные не ждем, они прийдут в потоке
@override
Result<bool> requestNamedEmulated(List<String> names) {
ResultF<void> requestNamedEmulated(List<String> names) {
throw Failure.unexpected(
message: '[$DsClientReal.requestNamedEmulated] method not implemented, used only for emulation in the test mode',
stackTrace: StackTrace.current,
Expand Down
45 changes: 20 additions & 25 deletions lib/src/core/ds_line_socket.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import 'dart:async';

Check notice on line 1 in lib/src/core/ds_line_socket.dart

View workflow job for this annotation

GitHub Actions / coverage

Good coverage level

82.75%
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:hmi_core/hmi_core.dart';
import 'package:hmi_core/hmi_core_result_new.dart';
import 'package:hmi_networking/src/core/line_socket.dart';

///
class DsLineSocket implements LineSocket{
class DsLineSocket implements LineSocket {
static final _log = const Log('DsLineSocket')..level = LogLevel.info;
bool _isActive = false;
bool _isConnected = false;
Expand Down Expand Up @@ -105,54 +106,48 @@
}
//
@override
Result<bool> requestAll() {
ResultF<void> requestAll() {
_controller.add(
_buildConnectionStatus(_isConnected),
);
return const Result(data: true);
return const Ok(null);
}
//
@override
Future<Result<bool>> send(List<int> data) async {
Future<ResultF<void>> send(List<int> data) async {
final socket = _socket;
// TODO Better implementation of this feature to be released
if(socket == null) {
_log.debug('[.send] failed, socket was: $socket');
await Future.delayed(const Duration(milliseconds: 100));
return Future.value(
Result(
error: Failure.connection(
message: 'Not connected',
stackTrace: StackTrace.current,
),
return Err(
Failure.connection(
message: 'Not connected',
stackTrace: StackTrace.current,
),
);
} else {
_log.debug('[.send] event: $data');
try {
if (_isConnected) {
socket.add(data);
return Future.value(const Result(data: true));
return const Ok(null);
}
return Future.value(
Result(
error: Failure(
message: 'Ошибка в методе $runtimeType.send: socket is not connected',
stackTrace: StackTrace.current,
),
return Err(
Failure(
message: 'Ошибка в методе $runtimeType.send: socket is not connected',
stackTrace: StackTrace.current,
),
);
} catch (error) {
_log.debug('[.send] error: $error');
await _closeSocket(socket);
return Future.value(
Result(
error: Failure.connection(
message: '$error',
stackTrace: StackTrace.current,
),
return Err(
Failure.connection(
message: '$error',
stackTrace: StackTrace.current,
),
);
);
}
}
}
Expand All @@ -167,7 +162,7 @@
}
//
@override
Future close() async {
Future<void> close() async {
_cancel = true;
await _closeSocket(_socket);
await _controller.close();
Expand Down
9 changes: 5 additions & 4 deletions lib/src/core/ds_send.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

Check notice on line 1 in lib/src/core/ds_send.dart

View workflow job for this annotation

GitHub Actions / coverage

Good coverage level

100.00%
import 'package:hmi_core/hmi_core.dart';
import 'package:hmi_core/hmi_core_result_new.dart';
import 'package:hmi_networking/hmi_networking.dart';

///
Expand Down Expand Up @@ -32,7 +33,7 @@
_response = response,
_responseTimeout = responseTimeout;
///
Future<Result<DsDataPoint<T>>> exec(T value) {
Future<ResultF<DsDataPoint<T>>> exec(T value) {
_dsClient.send(DsCommand(
dsClass: DsDataClass.commonCmd,
type: _types[T],
Expand All @@ -44,11 +45,11 @@
final response = _response;
return _dsClient.stream<T>(response ?? _pointName.name)
.first
.then((value) => Result(data: value))
.then<ResultF<DsDataPoint<T>>>((value) => Ok(value))
.timeout(
Duration(seconds: _responseTimeout),
onTimeout: () => Result<DsDataPoint<T>>(
error: Failure(
onTimeout: () => Err(
Failure(
message: 'Ошибка в методе $runtimeType.exec: Timeout exceeded ($_responseTimeout sec) on stream(${response ?? _pointName.name})',
stackTrace: StackTrace.current,
),
Expand Down
9 changes: 4 additions & 5 deletions lib/src/core/line_socket.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

import 'dart:typed_data';

import 'package:hmi_core/hmi_core.dart';
import 'package:hmi_core/hmi_core_result_new.dart';

///
/// Listen socket, send to socket,
Expand All @@ -14,11 +13,11 @@ abstract class LineSocket {
/// stream of all data coming from the socket
Stream<Uint8List> get stream;
///
Result<bool> requestAll();
ResultF<void> requestAll();
/// Sending data to the socket, not blocking, buffered
Future<Result<bool>> send(List<int> data);
Future<ResultF<void>> send(List<int> data);
/// Just closes the socket
///
/// NOTE: Writes may be buffered, and may not be flushed by a call to close(). To flush all buffered writes, call flush() before calling close().
Future close();
Future<void> close();
}
8 changes: 4 additions & 4 deletions lib/src/protocols/custom_protocol_line.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'dart:async';
import 'package:hmi_core/hmi_core.dart';
import 'package:hmi_core/hmi_core_result_new.dart';
///
abstract class CustomProtocolLine {
///
Expand All @@ -10,13 +10,13 @@ abstract class CustomProtocolLine {
///
/// Requests all possible information from subordinated devices,
/// as well as diagnostics
Future<Result<bool>> requestAll();
Future<ResultF<void>> requestAll();
///
/// TODO Documentation description to be writen
Future<Result<bool>> send(
Future<ResultF<void>> send(
DsCommand dsCommand,
);
///
/// TODO Documentation description to be writen
Future close();
Future<void> close();
}
7 changes: 4 additions & 3 deletions lib/src/protocols/jsd_line.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'dart:async';

Check notice on line 1 in lib/src/protocols/jsd_line.dart

View workflow job for this annotation

GitHub Actions / coverage

Good coverage level

89.88%
import 'dart:convert';
import 'dart:typed_data';
import 'package:hmi_core/hmi_core.dart';
import 'package:hmi_core/hmi_core_result_new.dart';
import 'package:hmi_networking/src/core/line_socket.dart';
import 'package:hmi_networking/src/protocols/custom_protocol_line.dart';
///
Expand Down Expand Up @@ -127,7 +128,7 @@
Stream<DsDataPoint> get stream => _lineSocket.stream.transform(_dataPointTransformer);
//
@override
Future<Result<bool>> send(
Future<ResultF<void>> send(
DsCommand dsCommand,
) {
_log.debug('[$JdsLine.send] dsCommand: $dsCommand');
Expand All @@ -136,13 +137,13 @@
}
//
@override
Future close() => _lineSocket.close();
Future<void> close() => _lineSocket.close();
//
@override
bool get isConnected => _lineSocket.isConnected;
//
@override
Future<Result<bool>> requestAll() {
Future<ResultF<void>> requestAll() {
_lineSocket.requestAll();
return send(DsCommand(
dsClass: DsDataClass.requestAll,
Expand Down
3 changes: 2 additions & 1 deletion test/unit/core/ds_line_socket/ds_line_socket_send_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/hmi_core_log.dart';
import 'package:hmi_core/hmi_core_result_new.dart';
import 'package:hmi_networking/hmi_networking.dart';

void main() {
Expand Down Expand Up @@ -55,7 +56,7 @@ void main() {
log.debug('sending: ${utf8.decode(entry.value)}');
final result = await lineSocket.send(entry.value);
log.debug('result: $result');
expect(result.hasData, true);
expect(result is Ok, true);
await Future.delayed(Duration(milliseconds: entry.key));
}
await Future.delayed(const Duration(milliseconds: 1000));
Expand Down
Loading
Loading