-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCmdDispatcher.cpp
145 lines (127 loc) · 3.24 KB
/
CmdDispatcher.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
135
136
137
138
139
140
141
142
143
144
145
#include "CmdDispatcher.h"
#include <stdio.h>
#include <sstream>
CmdDispatcher::CmdDispatcher( ILED *led ) : pLED_( led ) {
cmd_map_["set-led-state"] = &CmdDispatcher::set_state;
cmd_map_["get-led-state"] = &CmdDispatcher::get_state;
cmd_map_["set-led-color"] = &CmdDispatcher::set_color;
cmd_map_["get-led-color"] = &CmdDispatcher::get_color;
cmd_map_["set-led-rate"] = &CmdDispatcher::set_rate;
cmd_map_["get-led-rate"] = &CmdDispatcher::get_rate;
}
std::string CmdDispatcher::set_state( std::vector< std::string > & prm ) {
std::string status;
LED_STATE state;
bool bad_prm = false;
if ( prm.size() != 2 )
return "BAD COMMAND";
if ( std::string::npos != prm[1].find( "on" ) ) {
state = ON;
} else if ( std::string::npos != prm[1].find( "off" ) ) {
state = OFF;
} else {
status = "FAILED";
bad_prm = true;
}
if ( !bad_prm && pLED_->set_state( state ) ) {
status = "OK";
} else {
status = "FAILED";
}
return status;
}
std::string CmdDispatcher::get_state( std::vector< std::string > & prm ) {
std::string status, res;
int state = pLED_->get_state();
if ( state < 0 ) {
status = "FAILED";
} else {
status = "OK";
if ( state == OFF ) {
res = "off";
} else if ( state == ON ) {
res = "on";
}
}
return std::string( status + " " + res );
}
std::string CmdDispatcher::set_color( std::vector< std::string > & prm ) {
std::string status;
LED_COLOR color;
bool bad_prm = false;
if ( prm.size() != 2 )
return "BAD COMMAND";
if ( prm[1] == "red" ) {
color = RED;
} else if ( prm[1] == "green" ) {
color = GREEN;
} else if ( prm[1] == "blue" ) {
color = BLUE;
} else {
status = "FAILED";
bad_prm = true;
}
if ( !bad_prm && pLED_->set_color( color ) ) {
status = "OK";
} else {
status = "FAILED";
}
return status;
}
std::string CmdDispatcher::get_color( std::vector< std::string > & prm ) {
std::string status, res;
int color = pLED_->get_color();
if ( color < 0 ) {
status = "FAILED";
} else {
status = "OK";
if ( color == RED ) {
res = "red";
} else if ( color == GREEN ) {
res = "green";
} else if ( color == BLUE ) {
res = "blue";
}
}
return std::string( status + " " + res );
}
std::string CmdDispatcher::set_rate( std::vector< std::string > & prm ) {
std::string status;
int rate;
if ( prm.size() != 2 )
return "BAD COMMAND";
sscanf( prm[1].c_str(), "%d", &rate );
if ( pLED_->set_rate( rate ) ) {
status = "OK";
} else {
status = "FAILED";
}
return status;
}
std::string CmdDispatcher::get_rate( std::vector< std::string > & prm ) {
std::string status, res;
int rate = pLED_->get_rate();
std::stringstream ss;
ss << rate;
if ( rate < 0 ) {
status = "FAILED";
} else {
status = "OK";
res = ss.str();
}
return std::string( status + " " + res );
}
std::string CmdDispatcher::dispatch( std::string & cmd ) {
std::vector< std::string > cmd_parts;
std::size_t lpos = 0, rpos = 0;
while ( rpos != std::string::npos ) {
lpos = cmd.find_first_not_of( ' ', rpos );
rpos = cmd.find_first_of( ' ', lpos );
if ( lpos != std::string::npos ) {
cmd_parts.push_back( cmd.substr( lpos, rpos - lpos ) );
}
}
if ( cmd_parts.size() == 0 || cmd_map_[ cmd_parts[ 0 ] ] == NULL )
return "UNKNOWN COMMAND";
return ( this->*cmd_map_[ cmd_parts[ 0 ] ] ) ( cmd_parts );
}