me_coffee_protocol_test.dart 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. import 'package:flutter_test/flutter_test.dart';
  2. import 'package:mebarista/models/machine_state.dart';
  3. import 'package:mebarista/services/me_coffee_protocol.dart';
  4. void main() {
  5. group('MeCoffeeProtocol', () {
  6. test('parses tmp with timestamp field correctly', () {
  7. final state = MeCoffeeProtocol.parse(
  8. 'tmp 12345 10100 9850 0',
  9. MachineState(),
  10. );
  11. expect(state.targetTemperature, 101.0);
  12. expect(state.temperature, 98.5);
  13. });
  14. test('encodes schedule as seconds since midnight', () {
  15. final bytes = MeCoffeeProtocol.encodeSchedule(
  16. true,
  17. '07:30',
  18. true,
  19. '22:15',
  20. );
  21. final payload = String.fromCharCodes(bytes);
  22. expect(payload, contains('cmd set tmrwnbl 1\n'));
  23. expect(payload, contains('cmd set tmron 27000\n'));
  24. expect(payload, contains('cmd set tmrsnbl 1\n'));
  25. expect(payload, contains('cmd set tmroff 80100\n'));
  26. });
  27. test('parses schedule fields from cmd get', () {
  28. var state = MachineState();
  29. state = MeCoffeeProtocol.parse('cmd get tmrwnbl 1 OK', state);
  30. state = MeCoffeeProtocol.parse('cmd get tmron 27000 OK', state);
  31. state = MeCoffeeProtocol.parse('cmd get tmrsnbl 1 OK', state);
  32. state = MeCoffeeProtocol.parse('cmd get tmroff 80100 OK', state);
  33. expect(state.wakeupEnabled, isTrue);
  34. expect(state.wakeupTime, '07:30');
  35. expect(state.shutdownEnabled, isTrue);
  36. expect(state.shutdownTime, '22:15');
  37. });
  38. test('parses bare get format for legacy compatibility', () {
  39. final state = MeCoffeeProtocol.parse('get pivlv 1', MachineState());
  40. expect(state.valveClosedWhilePaused, isTrue);
  41. });
  42. test('parses pid power and heating state', () {
  43. final state = MeCoffeeProtocol.parse(
  44. 'pid 1000 2000 3000 4000',
  45. MachineState(),
  46. );
  47. expect(
  48. state.heatingPower,
  49. closeTo((1000 + 2000 + 3000 + 4000) / 65535.0, 0.00001),
  50. );
  51. expect(state.isHeating, isTrue);
  52. });
  53. for (final suffix in ['', ' OK']) {
  54. test('parses three-component PID frame with suffix "$suffix"', () {
  55. final state = MeCoffeeProtocol.parse(
  56. 'pid 1000 2000 3000$suffix',
  57. MachineState(),
  58. );
  59. expect(state.heatingPower, closeTo(6000 / 65535, 0.00001));
  60. expect(state.isHeating, isTrue);
  61. });
  62. test('parses four-component PID frame with suffix "$suffix"', () {
  63. final state = MeCoffeeProtocol.parse(
  64. 'pid 1000 2000 3000 4000$suffix',
  65. MachineState(),
  66. );
  67. expect(state.heatingPower, closeTo(10000 / 65535, 0.00001));
  68. });
  69. test('parses temperature without sensor 2 with suffix "$suffix"', () {
  70. final state = MeCoffeeProtocol.parse(
  71. 'tmp 12345 10100 9850$suffix',
  72. MachineState(sensor2Temperature: 97),
  73. );
  74. expect(state.temperature, 98.5);
  75. expect(state.targetTemperature, 101);
  76. expect(state.sensor2Temperature, 97);
  77. });
  78. }
  79. test('OK-only frame and missing numeric fields leave state unchanged', () {
  80. final original = MachineState(temperature: 96, heatingPower: 0.4);
  81. for (final message in ['OK', 'pid 1000 2000 OK', 'tmp 1 10100 OK']) {
  82. expect(MeCoffeeProtocol.parse(message, original), same(original));
  83. }
  84. });
  85. test('clamps pid heating power to 1.0', () {
  86. final state = MeCoffeeProtocol.parse(
  87. 'pid 65535 65535 65535 65535',
  88. MachineState(),
  89. );
  90. expect(state.heatingPower, 1.0);
  91. expect(state.isHeating, isTrue);
  92. });
  93. test('parses shot timer start and stop transitions', () {
  94. final started = MeCoffeeProtocol.parse('sht 0 0', MachineState());
  95. final stopped = MeCoffeeProtocol.parse('sht 0 25000', started);
  96. expect(started.isBrewing, isTrue);
  97. expect(started.shotTimer, 0);
  98. expect(stopped.isBrewing, isFalse);
  99. expect(stopped.shotTimer, 25);
  100. });
  101. test('keeps the shtmx cutoff duration when the release line follows', () {
  102. // V10 simulation, shtmx 2 with a 4 s shot signal.
  103. var state = MachineState();
  104. for (final line in [
  105. 'sht 4 4823 OK', // stray line before the start
  106. 'sht 4 0 OK',
  107. 'sht 6 2001 OK', // cutoff
  108. 'sht 8 3971 OK', // signal released
  109. ]) {
  110. state = MeCoffeeProtocol.parse(line, state);
  111. if (line == 'sht 4 4823 OK') {
  112. expect(state.isBrewing, isFalse);
  113. expect(state.shotTimer, 0);
  114. }
  115. }
  116. expect(state.isBrewing, isFalse);
  117. expect(state.shotTimer, 2);
  118. });
  119. test('steam markers switch the heater without ending steam', () {
  120. final heating = MachineState(heatingPower: 0.3, isHeating: true);
  121. final steaming = MeCoffeeProtocol.parse('pid 65535 0 0 0 OK', heating);
  122. expect(steaming.isSteaming, isTrue);
  123. expect(steaming.isHeating, isTrue);
  124. expect(steaming.heatingPower, 1.0);
  125. // Boiler reached tmpstm: heater off, steam switch still on.
  126. final atSteamTemp = MeCoffeeProtocol.parse('pid 0 0 0 0 OK', steaming);
  127. expect(atSteamTemp.isSteaming, isTrue);
  128. expect(atSteamTemp.isHeating, isFalse);
  129. expect(atSteamTemp.heatingPower, 0);
  130. // Below tmpstm again: V10 reports 65536, not 65535.
  131. final reheating = MeCoffeeProtocol.parse(
  132. 'pid 65536 0 0 0 OK',
  133. atSteamTemp,
  134. );
  135. expect(reheating.isSteaming, isTrue);
  136. expect(reheating.isHeating, isTrue);
  137. expect(reheating.heatingPower, 1.0);
  138. // Release: the setpoint in tmp returns to tmpsp.
  139. final released = MeCoffeeProtocol.parse(
  140. 'tmp 9 10100 11990 0 - 619 100 128 128 OK',
  141. MeCoffeeProtocol.parse('pid 0 0 0 0 OK', reheating),
  142. );
  143. expect(released.isSteaming, isFalse);
  144. final resumed = MeCoffeeProtocol.parse('pid 2200 1258 0 0 OK', steaming);
  145. expect(resumed.isSteaming, isFalse);
  146. expect(resumed.heatingPower, closeTo(3458 / 65535, 1e-9));
  147. });
  148. test('tmp setpoint equal to tmpstm marks steam without a marker', () {
  149. final state = MeCoffeeProtocol.parse(
  150. 'tmp 7 12500 12785 0 - 619 100 128 128 OK',
  151. MachineState(),
  152. );
  153. expect(state.isSteaming, isTrue);
  154. expect(
  155. MeCoffeeProtocol.parse('pid 0 0 0 0 OK', MachineState()).isSteaming,
  156. isFalse,
  157. );
  158. });
  159. test('V4 three-field pid 65535 0 0 is still full power', () {
  160. final state = MeCoffeeProtocol.parse('pid 65535 0 0', MachineState());
  161. expect(state.isSteaming, isFalse);
  162. expect(state.heatingPower, 1.0);
  163. });
  164. test('parses scaled cmd get values for key-value settings', () {
  165. var state = MachineState();
  166. state = MeCoffeeProtocol.parse('cmd get tmpsp 10100 OK', state);
  167. state = MeCoffeeProtocol.parse('cmd get tmpstm 12500 OK', state);
  168. state = MeCoffeeProtocol.parse('cmd get pd1i 35 OK', state);
  169. state = MeCoffeeProtocol.parse('cmd get pd1imm 32768 OK', state);
  170. state = MeCoffeeProtocol.parse('cmd get pd1imx 65536 OK', state);
  171. state = MeCoffeeProtocol.parse('cmd get pivlv 1 OK', state);
  172. expect(state.brewTemperature, 101.0);
  173. expect(state.targetTemperature, 101.0);
  174. expect(state.steamTemperature, 125.0);
  175. expect(state.pidI, 0.35);
  176. expect(state.pidIWindDownMin, closeTo(50.0, 0.01));
  177. expect(state.pidIWindDownMax, 100.0);
  178. expect(state.valveClosedWhilePaused, isTrue);
  179. });
  180. test('parses firmware grinder counter key and set echoes', () {
  181. var state = MachineState();
  182. state = MeCoffeeProtocol.parse('cmd get cntgrndr 42 OK', state);
  183. expect(state.grinderCounter, 42);
  184. state = MeCoffeeProtocol.parse('cmd set s_tmpsp 9350 OK', state);
  185. expect(state.brewTemperature, 93.5);
  186. final rejected = MeCoffeeProtocol.parse('cmd set NOT OK', state);
  187. expect(rejected.brewTemperature, 93.5);
  188. });
  189. test('normalizes parsed schedule seconds to HH:mm', () {
  190. var state = MachineState();
  191. state = MeCoffeeProtocol.parse('cmd get tmron -60 OK', state);
  192. state = MeCoffeeProtocol.parse('cmd get tmroff 90000 OK', state);
  193. expect(state.wakeupTime, '23:59');
  194. expect(state.shutdownTime, '01:00');
  195. });
  196. test('encodeSet applies correct scaling rules', () {
  197. expect(MeCoffeeProtocol.encodeSet('tmpsp', 93.5), 'cmd set tmpsp 9350\n');
  198. expect(MeCoffeeProtocol.encodeSet('pd1i', 0.32), 'cmd set pd1i 32\n');
  199. expect(
  200. MeCoffeeProtocol.encodeSet('pistrt', 3.4),
  201. 'cmd set pistrt 3400\n',
  202. );
  203. expect(
  204. MeCoffeeProtocol.encodeSet('pd1imm', 12.5),
  205. 'cmd set pd1imm 8192\n',
  206. );
  207. });
  208. test('single-key writes preserve schedule, bool and V4 units', () {
  209. expect(MeCoffeeProtocol.encodeSet('pinbl', true), 'cmd set pinbl 1\n');
  210. expect(
  211. MeCoffeeProtocol.encodeSet('tmron', '07:30'),
  212. 'cmd set tmron 27000\n',
  213. );
  214. expect(
  215. MeCoffeeProtocol.encodeSet('pd1i', 3.0, isLegacyV4: true),
  216. 'cmd set pd1i 3\n',
  217. );
  218. expect(
  219. MeCoffeeProtocol.encodeSet('pd1imx', 3750.0, isLegacyV4: true),
  220. 'cmd set pd1imx 3750\n',
  221. );
  222. expect(
  223. MeCoffeeProtocol.encodeSet('pistrt', 3.0, isLegacyV4: true),
  224. 'cmd set pistrt 3\n',
  225. );
  226. });
  227. test('encodePreinfusion includes all preinfusion keys', () {
  228. final payload = String.fromCharCodes(
  229. MeCoffeeProtocol.encodePreinfusion(true, 3.2, 2.8, true),
  230. );
  231. expect(payload, contains('cmd set pinbl 1\n'));
  232. expect(payload, contains('cmd set pistrt 3200\n'));
  233. expect(payload, contains('cmd set piprd 2800\n'));
  234. expect(payload, contains('cmd set pivlv 1\n'));
  235. });
  236. test('invalid input leaves state unchanged', () {
  237. final original = MachineState(
  238. temperature: 96.2,
  239. targetTemperature: 100.0,
  240. );
  241. final result = MeCoffeeProtocol.parse('tmp invalid line', original);
  242. expect(result.temperature, 96.2);
  243. expect(result.targetTemperature, 100.0);
  244. });
  245. test('nonfinite temperature frames cannot count as fresh telemetry', () {
  246. final original = MachineState(temperature: 96.2, targetTemperature: 100);
  247. for (final command in ['tmp', 'T']) {
  248. for (final value in ['NaN', 'Infinity', '-Infinity']) {
  249. for (final message in [
  250. '$command $value 10000 9600 0',
  251. '$command 123 $value 9600 0',
  252. '$command 123 10000 $value 0',
  253. '$command 123 10000 9600 $value',
  254. ]) {
  255. expect(MeCoffeeProtocol.parse(message, original), same(original));
  256. }
  257. }
  258. }
  259. });
  260. test('uname containing V4 sets isLegacyV4Firmware flag', () {
  261. final state = MeCoffeeProtocol.parse(
  262. 'cmd uname meCoffee V4 OK',
  263. MachineState(),
  264. );
  265. expect(state.isLegacyV4Firmware, isTrue);
  266. });
  267. test('uname without V4 leaves isLegacyV4Firmware false', () {
  268. final state = MeCoffeeProtocol.parse(
  269. 'cmd uname meCoffee_v5 OK',
  270. MachineState(),
  271. );
  272. expect(state.isLegacyV4Firmware, isFalse);
  273. });
  274. test('pistrt decodes with scale 1 for V4 firmware', () {
  275. final v4State = MachineState(isLegacyV4Firmware: true);
  276. final result = MeCoffeeProtocol.parse('cmd get pistrt 3 OK', v4State);
  277. expect(result.preinfusionTime, 3.0); // scale=1: 3/1=3
  278. });
  279. test('pistrt decodes with scale 1000 for new firmware', () {
  280. final result = MeCoffeeProtocol.parse(
  281. 'cmd get pistrt 3000 OK',
  282. MachineState(),
  283. );
  284. expect(result.preinfusionTime, 3.0); // scale=1000: 3000/1000=3
  285. });
  286. test('encodePreinfusion uses scale 1 for V4 firmware', () {
  287. final payload = String.fromCharCodes(
  288. MeCoffeeProtocol.encodePreinfusion(
  289. true,
  290. 3.0,
  291. 2.0,
  292. false,
  293. isLegacyV4: true,
  294. ),
  295. );
  296. expect(payload, contains('cmd set pistrt 3\n'));
  297. expect(payload, contains('cmd set piprd 2\n'));
  298. });
  299. for (final identity in [
  300. 'cmd uname meCoffee V4 OK',
  301. 'cmd uname meCoffee V4',
  302. 'uname meCoffee V4 OK',
  303. 'cmd uname meCoffee_v4.1 OK',
  304. ]) {
  305. test('recognizes legacy identity "$identity"', () {
  306. final state = MeCoffeeProtocol.parse(identity, MachineState());
  307. expect(state.isLegacyV4Firmware, isTrue);
  308. expect(state.firmwareVersion, isNot(contains('OK')));
  309. });
  310. }
  311. test('identity does not mistake V40 for V4 or erase an empty reply', () {
  312. final original = MachineState(
  313. isLegacyV4Firmware: true,
  314. firmwareVersion: 'meCoffee V4',
  315. );
  316. for (final identity in ['cmd uname OK', 'uname OK']) {
  317. expect(MeCoffeeProtocol.parse(identity, original), same(original));
  318. }
  319. final modern = MeCoffeeProtocol.parse(
  320. 'cmd uname meCoffee V40 OK',
  321. original,
  322. );
  323. expect(modern.isLegacyV4Firmware, isFalse);
  324. expect(modern.firmwareVersion, 'meCoffee V40');
  325. });
  326. for (final legacy in [false, true]) {
  327. test('PID read/write scales match ${legacy ? 'V4' : 'modern'}', () {
  328. final initial = MachineState(isLegacyV4Firmware: legacy);
  329. final rawI = legacy ? 3 : 30;
  330. for (final prefix in ['get', 'cmd get', 'cmd set']) {
  331. final parsed = MeCoffeeProtocol.parse(
  332. '$prefix pd1i $rawI OK',
  333. initial,
  334. );
  335. expect(parsed.pidI, legacy ? 3 : 0.3);
  336. }
  337. final payload = String.fromCharCodes(
  338. MeCoffeeProtocol.encodePID(
  339. legacy ? 25 : 20,
  340. legacy ? 3 : 0.3,
  341. 128,
  342. isLegacyV4: legacy,
  343. ),
  344. );
  345. expect(
  346. payload,
  347. 'cmd set pd1p ${legacy ? 25 : 20}\n'
  348. 'cmd set pd1i $rawI\ncmd set pd1d 128\n',
  349. );
  350. });
  351. test('preinfusion read/write matches ${legacy ? 'V4' : 'modern'}', () {
  352. final initial = MachineState(isLegacyV4Firmware: legacy);
  353. final raw = legacy ? 10 : 10000;
  354. var parsed = MeCoffeeProtocol.parse('get pistrt $raw', initial);
  355. parsed = MeCoffeeProtocol.parse('get piprd $raw', parsed);
  356. expect(parsed.preinfusionTime, 10);
  357. expect(parsed.preinfusionPauseTime, 10);
  358. final payload = String.fromCharCodes(
  359. MeCoffeeProtocol.encodePreinfusion(
  360. true,
  361. 10,
  362. 10,
  363. false,
  364. isLegacyV4: legacy,
  365. ),
  366. );
  367. expect(
  368. payload,
  369. 'cmd set pinbl 1\ncmd set pistrt $raw\n'
  370. 'cmd set piprd $raw\ncmd set pivlv 0\n',
  371. );
  372. });
  373. }
  374. test('explicit parse V4 flag works before identity is stored', () {
  375. final result = MeCoffeeProtocol.parse(
  376. 'get pd1i 3',
  377. MachineState(),
  378. isLegacyV4: true,
  379. );
  380. expect(result.pidI, 3);
  381. });
  382. test('V4 maximum winddown stays in direct units without percent clamp', () {
  383. final initial = MachineState(isLegacyV4Firmware: true);
  384. for (final value in [0, 3750, 6000, 6250]) {
  385. expect(
  386. MeCoffeeProtocol.parse('get pd1imx $value', initial).pidIWindDownMax,
  387. value,
  388. );
  389. }
  390. final payload = String.fromCharCodes(
  391. MeCoffeeProtocol.encodePIDLimits(42, 3750, 5000, isLegacyV4: true),
  392. );
  393. expect(payload, 'cmd set pd1imx 3750\ncmd set pd1sz 5000\n');
  394. for (final key in ['pd1imn', 'pd1imm']) {
  395. expect(
  396. MeCoffeeProtocol.parse('get $key 32768', initial),
  397. same(initial),
  398. );
  399. }
  400. });
  401. test(
  402. 'modern minimum accepts XML and firmware spelling, sends firmware key',
  403. () {
  404. for (final key in ['pd1imm', 'pd1imn']) {
  405. expect(
  406. MeCoffeeProtocol.parse(
  407. 'get $key 32768',
  408. MachineState(),
  409. ).pidIWindDownMin,
  410. 50,
  411. );
  412. }
  413. expect(
  414. String.fromCharCodes(MeCoffeeProtocol.encodePIDLimits(25, 48, 1000)),
  415. 'cmd set pd1imn 16384\ncmd set pd1imx 31457\ncmd set pd1sz 1000\n',
  416. );
  417. // Raw values above 32000 are reset to defaults at controller startup,
  418. // and 65536 would store 0 in the 16-bit field.
  419. expect(
  420. String.fromCharCodes(MeCoffeeProtocol.encodePIDLimits(50, 100, 1000)),
  421. 'cmd set pd1imn 32000\ncmd set pd1imx 32000\ncmd set pd1sz 1000\n',
  422. );
  423. },
  424. );
  425. test('settings ranges preserve direct P, D and percentage units', () {
  426. var state = MachineState();
  427. for (final entry in {'pd1p': 100, 'pd1d': 256, 'tmppap': 100}.entries) {
  428. state = MeCoffeeProtocol.parse(
  429. 'cmd get ${entry.key} ${entry.value}',
  430. state,
  431. );
  432. }
  433. expect(state.pidP, 100);
  434. expect(state.pidD, 256);
  435. expect(state.proActivePercentage, 100);
  436. });
  437. test(
  438. 'unknown and nonfinite settings do not alter state or guess scaling',
  439. () {
  440. final initial = MachineState();
  441. for (final message in [
  442. 'get pd1unknown 3',
  443. 'get pd1i NaN',
  444. 'get pd1imx Infinity',
  445. ]) {
  446. expect(MeCoffeeProtocol.parse(message, initial), same(initial));
  447. }
  448. expect(
  449. MeCoffeeProtocol.encodeSet('tmpunknown', 3.0),
  450. 'cmd set tmpunknown 3\n',
  451. );
  452. expect(
  453. MeCoffeeProtocol.encodeSet('piunknown', 3.0),
  454. 'cmd set piunknown 3\n',
  455. );
  456. },
  457. );
  458. });
  459. }