hisma_console_monitor 0.1.1+1 hisma_console_monitor: ^0.1.1+1 copied to clipboard
Simple state machine monitor implementation that writes to the console for the Hisma package.
example/hisma_console_monitor_example.dart
// ignore_for_file: avoid_print
import 'package:hisma/hisma.dart';
import 'package:hisma_console_monitor/hisma_console_monitor.dart';
enum S { on, off }
enum E { turnOn, turnOff }
enum T { toOn, toOff }
StateMachine<S, E, T> createLightMachine({
RegionList<S, E, T>? regions,
}) =>
StateMachine<S, E, T>(
name: 'lightMachine',
events: E.values,
initialStateId: S.off,
states: {
S.off: State(
etm: {
E.turnOn: [T.toOn],
},
onEntry: Action(
description: 'Turning off.',
action: (machine, dynamic parameter) async => print('OFF'),
),
),
S.on: State(
etm: {
E.turnOff: [T.toOff],
},
regions: regions,
onEntry: Action(
description: 'Turning on.',
action: (machine, dynamic parameter) async => print('ON'),
),
),
},
transitions: {
T.toOn: Transition(to: S.on),
T.toOff: Transition(to: S.off),
},
);
final lightMachine = createLightMachine();
Future<void> play() async {
while (true) {
await Future<void>.delayed(const Duration(seconds: 1));
await lightMachine.fire(E.turnOn);
await Future<void>.delayed(const Duration(seconds: 1));
await lightMachine.fire(E.turnOff);
}
}
Future<void> main() async {
StateMachine.monitorCreators = [
(machine) => ConsoleMonitor(machine),
];
await lightMachine.start();
play();
}