From 80e4779cd551cc7dfecb155efbed962e96afeff7 Mon Sep 17 00:00:00 2001 From: Jonas Bark Date: Wed, 4 Mar 2026 17:37:08 +0100 Subject: [PATCH] try script inline --- lib/utils/interpreter.dart | 16 ++- lib/widgets/device_script_drawer.dart | 164 +++++++++++++++++++++++++- 2 files changed, 176 insertions(+), 4 deletions(-) diff --git a/lib/utils/interpreter.dart b/lib/utils/interpreter.dart index 45e3603f..2b447297 100644 --- a/lib/utils/interpreter.dart +++ b/lib/utils/interpreter.dart @@ -194,9 +194,23 @@ class DeviceScriptService { return null; } + return runScriptSource( + source: source, + characteristicUuid: characteristicUuid, + data: data, + deviceTypeForLog: deviceType, + ); + } + + Future runScriptSource({ + required String source, + required String characteristicUuid, + required Uint8List data, + String? deviceTypeForLog, + }) async { core.connection.signalNotification( LogNotification( - 'Running custom script for device type "$deviceType" with characteristic $characteristicUuid input data: ${data.join()}', + 'Running custom script${deviceTypeForLog != null ? ' for device type "$deviceTypeForLog"' : ''} with characteristic $characteristicUuid input data: ${data.join()}', ), ); diff --git a/lib/widgets/device_script_drawer.dart b/lib/widgets/device_script_drawer.dart index 07feffbc..b58db558 100644 --- a/lib/widgets/device_script_drawer.dart +++ b/lib/widgets/device_script_drawer.dart @@ -2,6 +2,7 @@ import 'package:bike_control/utils/interpreter.dart'; import 'package:bike_control/widgets/ui/loading_widget.dart'; import 'package:bike_control/widgets/ui/small_progress_indicator.dart'; import 'package:bike_control/widgets/ui/toast.dart'; +import 'package:flutter/foundation.dart'; import 'package:shadcn_flutter/shadcn_flutter.dart'; class DeviceScriptDrawer extends StatefulWidget { @@ -15,22 +16,31 @@ class DeviceScriptDrawer extends StatefulWidget { class _DeviceScriptDrawerState extends State { final TextEditingController _controller = TextEditingController(); + final TextEditingController _tryCharacteristicController = TextEditingController(); + final TextEditingController _tryHexController = TextEditingController(text: '01 02 03'); bool _isLoading = true; bool _isSaving = false; bool _isDeleting = false; + bool _isTrying = false; bool _hasSavedScript = false; String? _validationError; + String? _tryOutputCharacteristic; + String? _tryOutputHex; + String? _tryError; @override void initState() { super.initState(); + _tryCharacteristicController.text = '00000000-0000-0000-0000-000000000000'; _loadScript(); } @override void dispose() { _controller.dispose(); + _tryCharacteristicController.dispose(); + _tryHexController.dispose(); super.dispose(); } @@ -115,6 +125,76 @@ class _DeviceScriptDrawerState extends State { buildToast(title: 'Script deleted for ${widget.deviceType}.'); } + Future _tryScript() async { + final characteristicUuid = _tryCharacteristicController.text.trim().toLowerCase(); + final hexInput = _tryHexController.text.trim(); + + setState(() { + _tryError = null; + _tryOutputCharacteristic = null; + _tryOutputHex = null; + }); + + if (characteristicUuid.isEmpty) { + setState(() { + _tryError = 'Characteristic UUID is required.'; + }); + return; + } + + try { + final data = _parseHexInput(hexInput); + final result = await DeviceScriptService.instance.runScriptSource( + source: _controller.text, + characteristicUuid: characteristicUuid, + data: data, + ); + + if (!mounted) { + return; + } + + setState(() { + _tryOutputCharacteristic = result.characteristicUuid; + _tryOutputHex = _toHex(result.data); + }); + } catch (e) { + if (!mounted) { + return; + } + setState(() { + _tryError = e.toString(); + }); + } + } + + Uint8List _parseHexInput(String input) { + final normalized = input + .replaceAll(RegExp(r'0x', caseSensitive: false), '') + .replaceAll(RegExp(r'[^0-9a-fA-F]'), ''); + + if (normalized.isEmpty) { + return Uint8List(0); + } + + if (normalized.length.isOdd) { + throw const FormatException('Hex input must have an even number of characters.'); + } + + final bytes = []; + for (var i = 0; i < normalized.length; i += 2) { + bytes.add(int.parse(normalized.substring(i, i + 2), radix: 16)); + } + return Uint8List.fromList(bytes); + } + + String _toHex(Uint8List bytes) { + if (bytes.isEmpty) { + return '(empty)'; + } + return bytes.map((b) => b.toRadixString(16).padLeft(2, '0')).join(' ').toUpperCase(); + } + @override Widget build(BuildContext context) { return SafeArea( @@ -147,6 +227,84 @@ class _DeviceScriptDrawerState extends State { placeholder: Text('Write your script here...'), ).inlineCode, ), + Container( + width: double.infinity, + padding: const EdgeInsets.all(10), + decoration: BoxDecoration( + color: Theme.of(context).colorScheme.card.withAlpha(180), + borderRadius: BorderRadius.circular(8), + ), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + spacing: 8, + children: [ + Text('Try Script', style: TextStyle(fontWeight: FontWeight.w600)), + Row( + spacing: 8, + children: [ + Expanded( + child: Column( + children: [ + TextField( + controller: _tryCharacteristicController, + placeholder: const Text('Characteristic UUID'), + hintText: 'Characteristic UUID', + ), + TextField( + controller: _tryHexController, + placeholder: const Text('Hex input (e.g. 01 FF 2A)'), + hintText: 'Hex input (e.g. 01 FF 2A)', + ), + ], + ), + ), + LoadingWidget( + onLoadCallback: (isLoading) { + if (!mounted) { + return; + } + setState(() { + _isTrying = isLoading; + }); + }, + futureCallback: _tryScript, + renderChild: (isLoading, tap) => OutlineButton( + onPressed: (_isLoading || _isSaving || _isDeleting) ? null : tap, + child: isLoading + ? const Row( + mainAxisSize: MainAxisSize.min, + spacing: 8, + children: [ + SmallProgressIndicator(), + Text('Trying...'), + ], + ) + : const Text('Try'), + ), + ), + ], + ), + if (_tryError != null) + Text( + _tryError!, + style: TextStyle(color: Theme.of(context).colorScheme.destructive, fontSize: 12), + ), + if (_tryOutputCharacteristic != null && _tryOutputHex != null) + Column( + crossAxisAlignment: CrossAxisAlignment.start, + spacing: 4, + children: [ + Text( + 'Output characteristic: $_tryOutputCharacteristic', + ).inlineCode, + Text( + 'Output data (hex): $_tryOutputHex', + ).inlineCode, + ], + ), + ], + ), + ), if (_validationError != null) Container( width: double.infinity, @@ -176,7 +334,7 @@ class _DeviceScriptDrawerState extends State { }, futureCallback: _deleteScript, renderChild: (isLoading, tap) => DestructiveButton( - onPressed: (_isLoading || _isSaving) ? null : tap, + onPressed: (_isLoading || _isSaving || _isTrying) ? null : tap, child: isLoading ? const Row( mainAxisSize: MainAxisSize.min, @@ -192,7 +350,7 @@ class _DeviceScriptDrawerState extends State { if (!_hasSavedScript) const SizedBox.shrink(), const Spacer(), OutlineButton( - onPressed: (_isSaving || _isDeleting) ? null : () => closeDrawer(context), + onPressed: (_isSaving || _isDeleting || _isTrying) ? null : () => closeDrawer(context), child: const Text('Cancel'), ), LoadingWidget( @@ -206,7 +364,7 @@ class _DeviceScriptDrawerState extends State { }, futureCallback: _saveScript, renderChild: (isLoading, tap) => PrimaryButton( - onPressed: (_isLoading || _isDeleting) ? null : tap, + onPressed: (_isLoading || _isDeleting || _isTrying) ? null : tap, child: isLoading ? const Row( mainAxisSize: MainAxisSize.min,