From 3e9577762d1d5e37f3ccaf801f27dca658377f27 Mon Sep 17 00:00:00 2001 From: Paul Kepinski Date: Mon, 4 Nov 2024 08:38:06 +0100 Subject: [PATCH] Add SaneDev implementation Fixes #15 --- packages/sane/example/main.dart | 8 +- packages/sane/lib/sane.dart | 1 + packages/sane/lib/src/sane_dev.dart | 213 ++++++++++++++++++++++++++++ 3 files changed, 220 insertions(+), 2 deletions(-) create mode 100644 packages/sane/lib/src/sane_dev.dart diff --git a/packages/sane/example/main.dart b/packages/sane/example/main.dart index 4e92837..340b880 100644 --- a/packages/sane/example/main.dart +++ b/packages/sane/example/main.dart @@ -6,15 +6,19 @@ import 'dart:typed_data'; import 'package:sane/sane.dart'; void main(List args) async { - final sane = SaneIsolate(sane: Sane()); + final sane = SaneIsolate(sane: SaneDev()); await sane.spawn(); await sane.init(); - final devices = await sane.getDevices(localOnly: true); + final devices = await sane.getDevices(localOnly: false); for (final device in devices) { print('Device found: ${device.name}'); } + if (devices.isEmpty) { + print('No device found'); + return; + } final handle = await sane.openDevice(devices.first); diff --git a/packages/sane/lib/sane.dart b/packages/sane/lib/sane.dart index 1545f85..7769469 100644 --- a/packages/sane/lib/sane.dart +++ b/packages/sane/lib/sane.dart @@ -2,6 +2,7 @@ library; export 'src/exceptions.dart'; export 'src/sane.dart'; +export 'src/sane_dev.dart'; export 'src/sane_isolate.dart'; export 'src/structures.dart'; export 'src/utils.dart'; diff --git a/packages/sane/lib/src/sane_dev.dart b/packages/sane/lib/src/sane_dev.dart new file mode 100644 index 0000000..8438e01 --- /dev/null +++ b/packages/sane/lib/src/sane_dev.dart @@ -0,0 +1,213 @@ +import 'dart:typed_data'; + +import 'package:sane/sane.dart'; + +class SaneDev implements Sane { + @override + Future cancel(SaneHandle handle) { + return Future.delayed(const Duration(seconds: 1), () { + print('sane_cancel()'); + }); + } + + @override + Future close(SaneHandle handle) { + return Future.delayed(const Duration(seconds: 1), () { + print('sane_close()'); + }); + } + + @override + Future> controlBoolOption({ + required SaneHandle handle, + required int index, + required SaneAction action, + bool? value, + }) { + return Future.delayed(const Duration(seconds: 1), () { + print('sane_controlBoolOption()'); + return SaneOptionResult(result: value ?? true, infos: []); + }); + } + + @override + Future> controlButtonOption({ + required SaneHandle handle, + required int index, + }) { + return Future.delayed(const Duration(seconds: 1), () { + print('sane_controlButtonOption()'); + return SaneOptionResult(result: null, infos: []); + }); + } + + @override + Future> controlFixedOption({ + required SaneHandle handle, + required int index, + required SaneAction action, + double? value, + }) { + return Future.delayed(const Duration(seconds: 1), () { + print('sane_controlFixedOption()'); + return SaneOptionResult(result: value ?? .1, infos: []); + }); + } + + @override + Future> controlIntOption({ + required SaneHandle handle, + required int index, + required SaneAction action, + int? value, + }) { + return Future.delayed(const Duration(seconds: 1), () { + print('sane_controlIntOption()'); + return SaneOptionResult(result: value ?? 1, infos: []); + }); + } + + @override + Future> controlStringOption({ + required SaneHandle handle, + required int index, + required SaneAction action, + String? value, + }) { + return Future.delayed(const Duration(seconds: 1), () { + print('sane_controlStringOption()'); + return SaneOptionResult(result: value ?? 'value', infos: []); + }); + } + + @override + Future exit() { + return Future(() { + print('sane_exit()'); + }); + } + + @override + Future> getAllOptionDescriptors( + SaneHandle handle, + ) { + return Future.delayed(const Duration(seconds: 1), () { + print('sane_getAllOptionDescriptors()'); + return [ + SaneOptionDescriptor( + index: 0, + name: 'name', + title: 'title', + desc: 'desc', + type: SaneOptionValueType.int, + unit: SaneOptionUnit.none, + size: 1, + capabilities: [], + constraint: null, + ), + ]; + }); + } + + @override + Future> getDevices({ + required bool localOnly, + }) { + return Future.delayed(const Duration(seconds: 1), () { + print('sane_getDevices()'); + return [ + for (var i = 0; i < 3; i++) + SaneDevice( + name: 'name $i', + vendor: 'Vendor$i', + model: 'Model$i', + type: 'Type$i', + ), + ]; + }); + } + + @override + Future getOptionDescriptor( + SaneHandle handle, + int index, + ) { + return Future.delayed(const Duration(seconds: 1), () { + print('sane_getOptionDescriptor()'); + return SaneOptionDescriptor( + index: index, + name: 'name', + title: 'title', + desc: 'desc', + type: SaneOptionValueType.int, + unit: SaneOptionUnit.none, + size: 1, + capabilities: [], + constraint: null, + ); + }); + } + + @override + Future getParameters(SaneHandle handle) { + return Future.delayed(const Duration(seconds: 1), () { + print('sane_getParameters()'); + return SaneParameters( + format: SaneFrameFormat.gray, + lastFrame: true, + bytesPerLine: 800, + pixelsPerLine: 100, + lines: 100, + depth: 8, + ); + }); + } + + @override + Future init({ + AuthCallback? authCallback, + }) { + return Future(() { + print('sane_init()'); + return 1; + }); + } + + @override + Future open(String deviceName) { + return Future.delayed(const Duration(seconds: 1), () { + print('sane_open()'); + return SaneHandle(deviceName: deviceName); + }); + } + + @override + Future openDevice(SaneDevice device) { + return Future.delayed(const Duration(seconds: 1), () { + print('sane_openDevice()'); + return SaneHandle(deviceName: device.name); + }); + } + + @override + Future read(SaneHandle handle, int bufferSize) { + return Future.delayed(const Duration(seconds: 1), () { + print('sane_read()'); + return Uint8List.fromList([]); + }); + } + + @override + Future setIOMode(SaneHandle handle, SaneIOMode mode) { + return Future.delayed(const Duration(seconds: 1), () { + print('sane_setIOMode()'); + }); + } + + @override + Future start(SaneHandle handle) { + return Future.delayed(const Duration(seconds: 1), () { + print('sane_start()'); + }); + } +}