Bläddra i källkod

Handle V10 steam marker and duplicate shot-end lines

Thomas Hondema 1 månad sedan
förälder
incheckning
b975b2ad19

+ 4 - 0
lib/main.dart

@@ -211,6 +211,8 @@ class MyHomePage extends ConsumerWidget {
                               child: LinearProgressIndicator(
                                 value: machineState.telemetryStale
                                     ? 0
+                                    : machineState.isSteaming
+                                    ? null
                                     : machineState.heatingPower,
                                 backgroundColor: Colors.grey.withValues(
                                   alpha: 0.2,
@@ -224,6 +226,8 @@ class MyHomePage extends ConsumerWidget {
                           Text(
                             machineState.telemetryStale
                                 ? 'Unavailable'
+                                : machineState.isSteaming
+                                ? 'Steam'
                                 : '${(machineState.heatingPower * 100).toStringAsFixed(0)}%',
                             style: const TextStyle(
                               fontSize: 12,

+ 8 - 0
lib/models/machine_state.dart

@@ -6,6 +6,11 @@ class MachineState {
   final double targetTemperature;
   final bool isHeating;
   final bool isBrewing;
+
+  /// Steam input active (V9/V10 `pid 65535 0 0 0` marker). The firmware
+  /// switches the boiler on/off around the steam setpoint and sends no PID
+  /// output, so [heatingPower] is unknown while this is set.
+  final bool isSteaming;
   final int shotTimer;
   final String connectionStatus;
   final String? commandError;
@@ -84,6 +89,7 @@ class MachineState {
     this.targetTemperature = 101.0,
     this.isHeating = false,
     this.isBrewing = false,
+    this.isSteaming = false,
     this.shotTimer = 0,
     this.connectionStatus = 'Disconnected',
     this.commandError,
@@ -139,6 +145,7 @@ class MachineState {
     double? targetTemperature,
     bool? isHeating,
     bool? isBrewing,
+    bool? isSteaming,
     int? shotTimer,
     String? connectionStatus,
     String? commandError,
@@ -195,6 +202,7 @@ class MachineState {
       targetTemperature: targetTemperature ?? this.targetTemperature,
       isHeating: isHeating ?? this.isHeating,
       isBrewing: isBrewing ?? this.isBrewing,
+      isSteaming: isSteaming ?? this.isSteaming,
       shotTimer: shotTimer ?? this.shotTimer,
       connectionStatus: connectionStatus ?? this.connectionStatus,
       commandError: clearCommandError

+ 4 - 0
lib/services/bluetooth_service.dart

@@ -77,6 +77,7 @@ class MeCoffeeService extends StateNotifier<MachineState> {
       _commandReady &&
       !state.isBrewing &&
       !state.isHeating &&
+      !state.isSteaming &&
       _lastPidAt != null &&
       DateTime.now().difference(_lastPidAt!) < freshnessTimeout &&
       !state.telemetryStale &&
@@ -147,6 +148,7 @@ class MeCoffeeService extends StateNotifier<MachineState> {
         telemetryStale: true,
         isHeating: false,
         heatingPower: 0,
+        isSteaming: false,
         isBrewing: false,
       );
       _log(DiagnosticDirection.event, 'Temperature telemetry timed out');
@@ -1249,6 +1251,7 @@ class MeCoffeeService extends StateNotifier<MachineState> {
         clearLastTelemetry: true,
         isHeating: false,
         heatingPower: 0,
+        isSteaming: false,
       );
     }
   }
@@ -1342,6 +1345,7 @@ class MeCoffeeService extends StateNotifier<MachineState> {
           parsed = parsed.copyWith(
             isHeating: false,
             heatingPower: 0,
+            isSteaming: false,
             isBrewing: false,
           );
         }

+ 18 - 0
lib/services/me_coffee_protocol.dart

@@ -61,10 +61,23 @@ class MeCoffeeProtocol {
         int d = int.parse(parts[3]);
         int f = parts.length >= 5 ? int.parse(parts[4]) : 0;
 
+        // V9/V10 send the fixed line `pid 65535 0 0 0` when the steam input
+        // becomes active. It is an event marker, not controller output: the
+        // boiler is then switched fully on or off around the steam setpoint
+        // and no further `pid` lines arrive until steam ends.
+        if (parts.length == 5 && p == 65535 && i == 0 && d == 0 && f == 0) {
+          return currentState.copyWith(
+            isSteaming: true,
+            isHeating: true,
+            heatingPower: 0,
+          );
+        }
+
         double totalPower = (p + i + d + f) / 65535.0;
         return currentState.copyWith(
           heatingPower: totalPower.clamp(0.0, 1.0),
           isHeating: totalPower > 0,
+          isSteaming: false,
         );
       } catch (_) {}
     }
@@ -76,6 +89,11 @@ class MeCoffeeProtocol {
         int shottimeMs = int.parse(parts[2]);
         if (shottimeMs == 0) {
           return currentState.copyWith(isBrewing: true, shotTimer: 0);
+        } else if (!currentState.isBrewing) {
+          // V9/V10 send a second end line when the input is released after a
+          // `shtmx` cutoff, and can send a stray `sht <s> <uptime-ms>` before
+          // the first start line. Keep the duration of the shot that ended.
+          return currentState.copyWith();
         } else {
           return currentState.copyWith(
             isBrewing: false,

+ 45 - 0
test/services/me_coffee_protocol_test.dart

@@ -118,6 +118,51 @@ void main() {
       expect(stopped.shotTimer, 25);
     });
 
+    test('keeps the shtmx cutoff duration when the release line follows', () {
+      // V10 simulation, shtmx 2 with a 4 s shot signal.
+      var state = MachineState();
+      for (final line in [
+        'sht 4 4823 OK', // stray line before the start
+        'sht 4 0 OK',
+        'sht 6 2001 OK', // cutoff
+        'sht 8 3971 OK', // signal released
+      ]) {
+        state = MeCoffeeProtocol.parse(line, state);
+        if (line == 'sht 4 4823 OK') {
+          expect(state.isBrewing, isFalse);
+          expect(state.shotTimer, 0);
+        }
+      }
+
+      expect(state.isBrewing, isFalse);
+      expect(state.shotTimer, 2);
+    });
+
+    test('treats pid 65535 0 0 0 as the steam marker, not full power', () {
+      final heating = MachineState(heatingPower: 0.3, isHeating: true);
+      final steaming = MeCoffeeProtocol.parse('pid 65535 0 0 0 OK', heating);
+
+      expect(steaming.isSteaming, isTrue);
+      expect(steaming.isHeating, isTrue);
+      expect(steaming.heatingPower, 0);
+
+      final released = MeCoffeeProtocol.parse('pid 0 0 0 0 OK', steaming);
+      expect(released.isSteaming, isFalse);
+      expect(released.isHeating, isFalse);
+      expect(released.heatingPower, 0);
+
+      final resumed = MeCoffeeProtocol.parse('pid 2200 1258 0 0 OK', steaming);
+      expect(resumed.isSteaming, isFalse);
+      expect(resumed.heatingPower, closeTo(3458 / 65535, 1e-9));
+    });
+
+    test('V4 three-field pid 65535 0 0 is still full power', () {
+      final state = MeCoffeeProtocol.parse('pid 65535 0 0', MachineState());
+
+      expect(state.isSteaming, isFalse);
+      expect(state.heatingPower, 1.0);
+    });
+
     test('parses scaled cmd get values for key-value settings', () {
       var state = MachineState();
 

+ 33 - 0
test/widget_test.dart

@@ -87,6 +87,39 @@ void main() {
     await tester.pumpWidget(const SizedBox());
   });
 
+  testWidgets('dashboard shows Steam instead of a PID percentage', (
+    tester,
+  ) async {
+    final service = DashboardService();
+    await tester.pump();
+    service.showState(
+      MachineState(
+        connectionStatus: 'Connected',
+        temperature: 120,
+        lastTelemetryAt: DateTime(2026, 10, 6),
+        telemetryStale: false,
+        isHeating: true,
+        isSteaming: true,
+      ),
+    );
+    await tester.pumpWidget(
+      ProviderScope(
+        overrides: [bluetoothServiceProvider.overrideWith((ref) => service)],
+        child: const MyApp(),
+      ),
+    );
+    await tester.pump(const Duration(milliseconds: 100));
+    expect(find.text('Steam'), findsOneWidget);
+    expect(find.text('0%'), findsNothing);
+    expect(
+      tester
+          .widget<LinearProgressIndicator>(find.byType(LinearProgressIndicator))
+          .value,
+      isNull,
+    );
+    await tester.pumpWidget(const SizedBox());
+  });
+
   testWidgets('dashboard diagnostics button opens local logs', (tester) async {
     await tester.pumpWidget(const ProviderScope(child: MyApp()));
     await tester.pumpAndSettle();