-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFSMTestHeadset.cpp
134 lines (103 loc) · 4.42 KB
/
FSMTestHeadset.cpp
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "FiniteStateMachine.h"
#include <iostream>
#include <cassert>
using namespace std;
class FSMTestHeadset : public FiniteStateMachine {
public:
FSMTestHeadset(Properties properties, States states, std::vector<FSMTransitionPtr> transitions, std::string startState) : FiniteStateMachine(properties, states, transitions, startState) {}
};
class SwitchInput : public SubjectImplementation {};
class FSMTestHeadsetStateOff : public FSMEmptyState {
public:
void entry(FiniteStateMachine* ctx) override {
std::cout << "Off entry:" << std::endl;
};
};
class FSMTestHeadsetStatePairing : public FSMEmptyState {
public:
void entry(FiniteStateMachine* ctx) override {
std::cout << "Pairing entry:" << std::endl;
ctx->setProperty("First Time On", false);
};
};
class FSMTestHeadsetStateConnected : public FSMEmptyState {
public:
void entry(FiniteStateMachine* ctx) override {
std::cout << "Connected entry:" << std::endl;
};
};
class FSMTestHeadsetStateSearching : public FSMEmptyState {
public:
void entry(FiniteStateMachine* ctx) override {
std::cout << "Searching entry:" << std::endl;
};
};
void testFiniteStateMachine() {
static MessageType firstTimeOn(true);
//auto firstTimeProp = Property::create("First Time On");
//firstTimeProp.second->setValue(firstTimeOn);
FiniteStateMachine::Properties props = {
{ Property::create("On/Off") },
{ Property::create("Pairing Button") },
{ Property::create("Paired With") },
{ Property::create("Volume") },
{ Property::create("Phone Volume") },
{ Property::create("Timer") },
{ Property::create("Connection") },
{ Property::create("First Time On", true) }
};
FiniteStateMachine::States states = {
FiniteStateMachine::create<FSMTestHeadsetStateOff>("Off"),
FiniteStateMachine::create<FSMTestHeadsetStatePairing>("Pairing"),
FiniteStateMachine::create<FSMTestHeadsetStateConnected>("Connected"),
FiniteStateMachine::create<FSMTestHeadsetStateSearching>("Searching")
};
FSMTransitionFn conditionalFn = [](FiniteStateMachine* ctx, const MessageType& message) {
bool firstTimeOn = false;
auto firstTimeOnProp = ctx->getProperty("First Time On");
firstTimeOnProp->getValue(firstTimeOn, firstTimeOn);
if (firstTimeOn == true) {
std::cout << "First Time On edge conditional" << std::endl;
return true;
}
return false;
};
FiniteStateMachine::Transitions transitions = {
std::make_shared<FSMStateTransition>("Off", "Pairing", "On/Off", conditionalFn), // except it needs to check "first time on"
std::make_shared<FSMStateTransition>("Off", "Searching", "On/Off", "on"),
std::make_shared<FSMStateTransition>("Pairing", "Off", "On/Off", "off"),
std::make_shared<FSMStateTransition>("Pairing", "Searching", "Timer", "timeout"),
std::make_shared<FSMStateTransition>("Pairing", "Searching", "Paired With"), // transition on any update of the property
std::make_shared<FSMStateTransition>("Searching", "Off", "Timer", "timeout"),
std::make_shared<FSMStateTransition>("Searching", "Connected", "Connection", "connected"),
std::make_shared<FSMStateTransition>("Searching", "Off", "On/Off", "off"),
std::make_shared<FSMStateTransition>("Searching", "Pairing", "Pairing Button"), // on any update (entry into Pairing sets button to false)
std::make_shared<FSMStateTransition>("Connected", "Off", "On/Off", "off"),
std::make_shared<FSMStateTransition>("Connected", "Pairing", "Pairing Button"),
std::make_shared<FSMStateTransition>("Connected", "Searching", "Connection", "disconnected"),
};
auto fsm = std::make_shared<FSMTestHeadset>(props, states, transitions, "Off");
auto graph = std::make_shared<FSMGraphAdapter>(fsm);
cout << "init" << endl;
assert(fsm->getState() == "Off");
fsm->setProperty("On/Off", "on");
assert(fsm->getState() == "Pairing");
fsm->setProperty("Paired With", "Mobile Phone");
assert(fsm->getState() == "Searching");
fsm->setProperty("On/Off", "off");
assert(fsm->getState() == "Off");
fsm->setProperty("On/Off", "on");
assert(fsm->getState() == "Searching");
fsm->setProperty("Connection", "connected");
assert(fsm->getState() == "Connected");
fsm->setProperty("Connection", "disconnected");
assert(fsm->getState() == "Searching");
fsm->setProperty("On/Off", "off");
assert(fsm->getState() == "Off");
cout << endl << "OK! **done**" << endl;
graph->show();
//auto onOffSwitch = std::make_shared<SwitchInput>();
//fsm.addInput(onOffSwitch);
//onOffSwitch->CreateMessage(" Switch On ");
//onOffSwitch->CreateMessage(" Switch Off ");
}