forked from trashvin/Tutorial_UsingStateless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatelessLightSwitch.cs
More file actions
42 lines (30 loc) · 1.06 KB
/
Copy pathStatelessLightSwitch.cs
File metadata and controls
42 lines (30 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using Stateless;
namespace Lightswitch
{
public class StatelessLightSwitch : ALightSwitch
{
enum Trigger { TOGGLE };
StateMachine<ALightSwitch.State, Trigger> _machine;
public StatelessLightSwitch(string name, bool initialState = false, bool enforceTimeConstraint = false)
: base(name, initialState, enforceTimeConstraint)
{
_time = new TimeSpan(0, 0, 0);
_machine = new StateMachine<State, Trigger>(() => CurrentState, s => CurrentState = s);
_machine.Configure(State.ON)
.Permit(Trigger.TOGGLE, State.OFF);
_machine.Configure(State.OFF)
.PermitIf(Trigger.TOGGLE, State.ON, () => IsLightNeeded(), "Toggle allowed")
.PermitReentryIf(Trigger.TOGGLE, () => !IsLightNeeded(), "Toggle not allowed");
}
public override void ToggleSwitch()
{
_machine.Fire(Trigger.TOGGLE);
Console.WriteLine("Switch is " + CurrentState.ToString());
}
public string ShowStateMachine()
{
return _machine.ToDotGraph();
}
}
}