| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521 |
- import 'package:flutter_test/flutter_test.dart';
- import 'package:mebarista/models/machine_state.dart';
- import 'package:mebarista/services/me_coffee_protocol.dart';
- void main() {
- group('MeCoffeeProtocol', () {
- test('parses tmp with timestamp field correctly', () {
- final state = MeCoffeeProtocol.parse(
- 'tmp 12345 10100 9850 0',
- MachineState(),
- );
- expect(state.targetTemperature, 101.0);
- expect(state.temperature, 98.5);
- });
- test('encodes schedule as seconds since midnight', () {
- final bytes = MeCoffeeProtocol.encodeSchedule(
- true,
- '07:30',
- true,
- '22:15',
- );
- final payload = String.fromCharCodes(bytes);
- expect(payload, contains('cmd set tmrwnbl 1\n'));
- expect(payload, contains('cmd set tmron 27000\n'));
- expect(payload, contains('cmd set tmrsnbl 1\n'));
- expect(payload, contains('cmd set tmroff 80100\n'));
- });
- test('parses schedule fields from cmd get', () {
- var state = MachineState();
- state = MeCoffeeProtocol.parse('cmd get tmrwnbl 1 OK', state);
- state = MeCoffeeProtocol.parse('cmd get tmron 27000 OK', state);
- state = MeCoffeeProtocol.parse('cmd get tmrsnbl 1 OK', state);
- state = MeCoffeeProtocol.parse('cmd get tmroff 80100 OK', state);
- expect(state.wakeupEnabled, isTrue);
- expect(state.wakeupTime, '07:30');
- expect(state.shutdownEnabled, isTrue);
- expect(state.shutdownTime, '22:15');
- });
- test('parses bare get format for legacy compatibility', () {
- final state = MeCoffeeProtocol.parse('get pivlv 1', MachineState());
- expect(state.valveClosedWhilePaused, isTrue);
- });
- test('parses pid power and heating state', () {
- final state = MeCoffeeProtocol.parse(
- 'pid 1000 2000 3000 4000',
- MachineState(),
- );
- expect(
- state.heatingPower,
- closeTo((1000 + 2000 + 3000 + 4000) / 65535.0, 0.00001),
- );
- expect(state.isHeating, isTrue);
- });
- for (final suffix in ['', ' OK']) {
- test('parses three-component PID frame with suffix "$suffix"', () {
- final state = MeCoffeeProtocol.parse(
- 'pid 1000 2000 3000$suffix',
- MachineState(),
- );
- expect(state.heatingPower, closeTo(6000 / 65535, 0.00001));
- expect(state.isHeating, isTrue);
- });
- test('parses four-component PID frame with suffix "$suffix"', () {
- final state = MeCoffeeProtocol.parse(
- 'pid 1000 2000 3000 4000$suffix',
- MachineState(),
- );
- expect(state.heatingPower, closeTo(10000 / 65535, 0.00001));
- });
- test('parses temperature without sensor 2 with suffix "$suffix"', () {
- final state = MeCoffeeProtocol.parse(
- 'tmp 12345 10100 9850$suffix',
- MachineState(sensor2Temperature: 97),
- );
- expect(state.temperature, 98.5);
- expect(state.targetTemperature, 101);
- expect(state.sensor2Temperature, 97);
- });
- }
- test('OK-only frame and missing numeric fields leave state unchanged', () {
- final original = MachineState(temperature: 96, heatingPower: 0.4);
- for (final message in ['OK', 'pid 1000 2000 OK', 'tmp 1 10100 OK']) {
- expect(MeCoffeeProtocol.parse(message, original), same(original));
- }
- });
- test('clamps pid heating power to 1.0', () {
- final state = MeCoffeeProtocol.parse(
- 'pid 65535 65535 65535 65535',
- MachineState(),
- );
- expect(state.heatingPower, 1.0);
- expect(state.isHeating, isTrue);
- });
- test('parses shot timer start and stop transitions', () {
- final started = MeCoffeeProtocol.parse('sht 0 0', MachineState());
- final stopped = MeCoffeeProtocol.parse('sht 0 25000', started);
- expect(started.isBrewing, isTrue);
- expect(started.shotTimer, 0);
- expect(stopped.isBrewing, isFalse);
- 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('steam markers switch the heater without ending steam', () {
- 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, 1.0);
- // Boiler reached tmpstm: heater off, steam switch still on.
- final atSteamTemp = MeCoffeeProtocol.parse('pid 0 0 0 0 OK', steaming);
- expect(atSteamTemp.isSteaming, isTrue);
- expect(atSteamTemp.isHeating, isFalse);
- expect(atSteamTemp.heatingPower, 0);
- // Below tmpstm again: V10 reports 65536, not 65535.
- final reheating = MeCoffeeProtocol.parse(
- 'pid 65536 0 0 0 OK',
- atSteamTemp,
- );
- expect(reheating.isSteaming, isTrue);
- expect(reheating.isHeating, isTrue);
- expect(reheating.heatingPower, 1.0);
- // Release: the setpoint in tmp returns to tmpsp.
- final released = MeCoffeeProtocol.parse(
- 'tmp 9 10100 11990 0 - 619 100 128 128 OK',
- MeCoffeeProtocol.parse('pid 0 0 0 0 OK', reheating),
- );
- expect(released.isSteaming, isFalse);
- final resumed = MeCoffeeProtocol.parse('pid 2200 1258 0 0 OK', steaming);
- expect(resumed.isSteaming, isFalse);
- expect(resumed.heatingPower, closeTo(3458 / 65535, 1e-9));
- });
- test('tmp setpoint equal to tmpstm marks steam without a marker', () {
- final state = MeCoffeeProtocol.parse(
- 'tmp 7 12500 12785 0 - 619 100 128 128 OK',
- MachineState(),
- );
- expect(state.isSteaming, isTrue);
- expect(
- MeCoffeeProtocol.parse('pid 0 0 0 0 OK', MachineState()).isSteaming,
- isFalse,
- );
- });
- 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();
- state = MeCoffeeProtocol.parse('cmd get tmpsp 10100 OK', state);
- state = MeCoffeeProtocol.parse('cmd get tmpstm 12500 OK', state);
- state = MeCoffeeProtocol.parse('cmd get pd1i 35 OK', state);
- state = MeCoffeeProtocol.parse('cmd get pd1imm 32768 OK', state);
- state = MeCoffeeProtocol.parse('cmd get pd1imx 65536 OK', state);
- state = MeCoffeeProtocol.parse('cmd get pivlv 1 OK', state);
- expect(state.brewTemperature, 101.0);
- expect(state.targetTemperature, 101.0);
- expect(state.steamTemperature, 125.0);
- expect(state.pidI, 0.35);
- expect(state.pidIWindDownMin, closeTo(50.0, 0.01));
- expect(state.pidIWindDownMax, 100.0);
- expect(state.valveClosedWhilePaused, isTrue);
- });
- test('parses firmware grinder counter key and set echoes', () {
- var state = MachineState();
- state = MeCoffeeProtocol.parse('cmd get cntgrndr 42 OK', state);
- expect(state.grinderCounter, 42);
- state = MeCoffeeProtocol.parse('cmd set s_tmpsp 9350 OK', state);
- expect(state.brewTemperature, 93.5);
- final rejected = MeCoffeeProtocol.parse('cmd set NOT OK', state);
- expect(rejected.brewTemperature, 93.5);
- });
- test('normalizes parsed schedule seconds to HH:mm', () {
- var state = MachineState();
- state = MeCoffeeProtocol.parse('cmd get tmron -60 OK', state);
- state = MeCoffeeProtocol.parse('cmd get tmroff 90000 OK', state);
- expect(state.wakeupTime, '23:59');
- expect(state.shutdownTime, '01:00');
- });
- test('encodeSet applies correct scaling rules', () {
- expect(MeCoffeeProtocol.encodeSet('tmpsp', 93.5), 'cmd set tmpsp 9350\n');
- expect(MeCoffeeProtocol.encodeSet('pd1i', 0.32), 'cmd set pd1i 32\n');
- expect(
- MeCoffeeProtocol.encodeSet('pistrt', 3.4),
- 'cmd set pistrt 3400\n',
- );
- expect(
- MeCoffeeProtocol.encodeSet('pd1imm', 12.5),
- 'cmd set pd1imm 8192\n',
- );
- });
- test('single-key writes preserve schedule, bool and V4 units', () {
- expect(MeCoffeeProtocol.encodeSet('pinbl', true), 'cmd set pinbl 1\n');
- expect(
- MeCoffeeProtocol.encodeSet('tmron', '07:30'),
- 'cmd set tmron 27000\n',
- );
- expect(
- MeCoffeeProtocol.encodeSet('pd1i', 3.0, isLegacyV4: true),
- 'cmd set pd1i 3\n',
- );
- expect(
- MeCoffeeProtocol.encodeSet('pd1imx', 3750.0, isLegacyV4: true),
- 'cmd set pd1imx 3750\n',
- );
- expect(
- MeCoffeeProtocol.encodeSet('pistrt', 3.0, isLegacyV4: true),
- 'cmd set pistrt 3\n',
- );
- });
- test('encodePreinfusion includes all preinfusion keys', () {
- final payload = String.fromCharCodes(
- MeCoffeeProtocol.encodePreinfusion(true, 3.2, 2.8, true),
- );
- expect(payload, contains('cmd set pinbl 1\n'));
- expect(payload, contains('cmd set pistrt 3200\n'));
- expect(payload, contains('cmd set piprd 2800\n'));
- expect(payload, contains('cmd set pivlv 1\n'));
- });
- test('invalid input leaves state unchanged', () {
- final original = MachineState(
- temperature: 96.2,
- targetTemperature: 100.0,
- );
- final result = MeCoffeeProtocol.parse('tmp invalid line', original);
- expect(result.temperature, 96.2);
- expect(result.targetTemperature, 100.0);
- });
- test('nonfinite temperature frames cannot count as fresh telemetry', () {
- final original = MachineState(temperature: 96.2, targetTemperature: 100);
- for (final command in ['tmp', 'T']) {
- for (final value in ['NaN', 'Infinity', '-Infinity']) {
- for (final message in [
- '$command $value 10000 9600 0',
- '$command 123 $value 9600 0',
- '$command 123 10000 $value 0',
- '$command 123 10000 9600 $value',
- ]) {
- expect(MeCoffeeProtocol.parse(message, original), same(original));
- }
- }
- }
- });
- test('uname containing V4 sets isLegacyV4Firmware flag', () {
- final state = MeCoffeeProtocol.parse(
- 'cmd uname meCoffee V4 OK',
- MachineState(),
- );
- expect(state.isLegacyV4Firmware, isTrue);
- });
- test('uname without V4 leaves isLegacyV4Firmware false', () {
- final state = MeCoffeeProtocol.parse(
- 'cmd uname meCoffee_v5 OK',
- MachineState(),
- );
- expect(state.isLegacyV4Firmware, isFalse);
- });
- test('pistrt decodes with scale 1 for V4 firmware', () {
- final v4State = MachineState(isLegacyV4Firmware: true);
- final result = MeCoffeeProtocol.parse('cmd get pistrt 3 OK', v4State);
- expect(result.preinfusionTime, 3.0); // scale=1: 3/1=3
- });
- test('pistrt decodes with scale 1000 for new firmware', () {
- final result = MeCoffeeProtocol.parse(
- 'cmd get pistrt 3000 OK',
- MachineState(),
- );
- expect(result.preinfusionTime, 3.0); // scale=1000: 3000/1000=3
- });
- test('encodePreinfusion uses scale 1 for V4 firmware', () {
- final payload = String.fromCharCodes(
- MeCoffeeProtocol.encodePreinfusion(
- true,
- 3.0,
- 2.0,
- false,
- isLegacyV4: true,
- ),
- );
- expect(payload, contains('cmd set pistrt 3\n'));
- expect(payload, contains('cmd set piprd 2\n'));
- });
- for (final identity in [
- 'cmd uname meCoffee V4 OK',
- 'cmd uname meCoffee V4',
- 'uname meCoffee V4 OK',
- 'cmd uname meCoffee_v4.1 OK',
- ]) {
- test('recognizes legacy identity "$identity"', () {
- final state = MeCoffeeProtocol.parse(identity, MachineState());
- expect(state.isLegacyV4Firmware, isTrue);
- expect(state.firmwareVersion, isNot(contains('OK')));
- });
- }
- test('identity does not mistake V40 for V4 or erase an empty reply', () {
- final original = MachineState(
- isLegacyV4Firmware: true,
- firmwareVersion: 'meCoffee V4',
- );
- for (final identity in ['cmd uname OK', 'uname OK']) {
- expect(MeCoffeeProtocol.parse(identity, original), same(original));
- }
- final modern = MeCoffeeProtocol.parse(
- 'cmd uname meCoffee V40 OK',
- original,
- );
- expect(modern.isLegacyV4Firmware, isFalse);
- expect(modern.firmwareVersion, 'meCoffee V40');
- });
- for (final legacy in [false, true]) {
- test('PID read/write scales match ${legacy ? 'V4' : 'modern'}', () {
- final initial = MachineState(isLegacyV4Firmware: legacy);
- final rawI = legacy ? 3 : 30;
- for (final prefix in ['get', 'cmd get', 'cmd set']) {
- final parsed = MeCoffeeProtocol.parse(
- '$prefix pd1i $rawI OK',
- initial,
- );
- expect(parsed.pidI, legacy ? 3 : 0.3);
- }
- final payload = String.fromCharCodes(
- MeCoffeeProtocol.encodePID(
- legacy ? 25 : 20,
- legacy ? 3 : 0.3,
- 128,
- isLegacyV4: legacy,
- ),
- );
- expect(
- payload,
- 'cmd set pd1p ${legacy ? 25 : 20}\n'
- 'cmd set pd1i $rawI\ncmd set pd1d 128\n',
- );
- });
- test('preinfusion read/write matches ${legacy ? 'V4' : 'modern'}', () {
- final initial = MachineState(isLegacyV4Firmware: legacy);
- final raw = legacy ? 10 : 10000;
- var parsed = MeCoffeeProtocol.parse('get pistrt $raw', initial);
- parsed = MeCoffeeProtocol.parse('get piprd $raw', parsed);
- expect(parsed.preinfusionTime, 10);
- expect(parsed.preinfusionPauseTime, 10);
- final payload = String.fromCharCodes(
- MeCoffeeProtocol.encodePreinfusion(
- true,
- 10,
- 10,
- false,
- isLegacyV4: legacy,
- ),
- );
- expect(
- payload,
- 'cmd set pinbl 1\ncmd set pistrt $raw\n'
- 'cmd set piprd $raw\ncmd set pivlv 0\n',
- );
- });
- }
- test('explicit parse V4 flag works before identity is stored', () {
- final result = MeCoffeeProtocol.parse(
- 'get pd1i 3',
- MachineState(),
- isLegacyV4: true,
- );
- expect(result.pidI, 3);
- });
- test('V4 maximum winddown stays in direct units without percent clamp', () {
- final initial = MachineState(isLegacyV4Firmware: true);
- for (final value in [0, 3750, 6000, 6250]) {
- expect(
- MeCoffeeProtocol.parse('get pd1imx $value', initial).pidIWindDownMax,
- value,
- );
- }
- final payload = String.fromCharCodes(
- MeCoffeeProtocol.encodePIDLimits(42, 3750, 5000, isLegacyV4: true),
- );
- expect(payload, 'cmd set pd1imx 3750\ncmd set pd1sz 5000\n');
- for (final key in ['pd1imn', 'pd1imm']) {
- expect(
- MeCoffeeProtocol.parse('get $key 32768', initial),
- same(initial),
- );
- }
- });
- test(
- 'modern minimum accepts XML and firmware spelling, sends firmware key',
- () {
- for (final key in ['pd1imm', 'pd1imn']) {
- expect(
- MeCoffeeProtocol.parse(
- 'get $key 32768',
- MachineState(),
- ).pidIWindDownMin,
- 50,
- );
- }
- expect(
- String.fromCharCodes(MeCoffeeProtocol.encodePIDLimits(25, 48, 1000)),
- 'cmd set pd1imn 16384\ncmd set pd1imx 31457\ncmd set pd1sz 1000\n',
- );
- // Raw values above 32000 are reset to defaults at controller startup,
- // and 65536 would store 0 in the 16-bit field.
- expect(
- String.fromCharCodes(MeCoffeeProtocol.encodePIDLimits(50, 100, 1000)),
- 'cmd set pd1imn 32000\ncmd set pd1imx 32000\ncmd set pd1sz 1000\n',
- );
- },
- );
- test('settings ranges preserve direct P, D and percentage units', () {
- var state = MachineState();
- for (final entry in {'pd1p': 100, 'pd1d': 256, 'tmppap': 100}.entries) {
- state = MeCoffeeProtocol.parse(
- 'cmd get ${entry.key} ${entry.value}',
- state,
- );
- }
- expect(state.pidP, 100);
- expect(state.pidD, 256);
- expect(state.proActivePercentage, 100);
- });
- test(
- 'unknown and nonfinite settings do not alter state or guess scaling',
- () {
- final initial = MachineState();
- for (final message in [
- 'get pd1unknown 3',
- 'get pd1i NaN',
- 'get pd1imx Infinity',
- ]) {
- expect(MeCoffeeProtocol.parse(message, initial), same(initial));
- }
- expect(
- MeCoffeeProtocol.encodeSet('tmpunknown', 3.0),
- 'cmd set tmpunknown 3\n',
- );
- expect(
- MeCoffeeProtocol.encodeSet('piunknown', 3.0),
- 'cmd set piunknown 3\n',
- );
- },
- );
- });
- }
|