-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfakeledparser.cpp
More file actions
102 lines (97 loc) · 2.05 KB
/
Copy pathfakeledparser.cpp
File metadata and controls
102 lines (97 loc) · 2.05 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
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
#include "fakeledparser.h"
#include "fakeled.h"
#include <algorithm>
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstring>
#include <cstdlib> // EXIT_FAILURE
FakeledParser::FakeledParser(Fakeled *fakeled)
: _fakeled(fakeled)
{
}
void FakeledParser::PrintMessage(const std::string &message)
{
std::cout << message << std::endl;
}
bool FakeledParser::parse(const char* buffer, std::string &answer)
{
if(std::strncmp(buffer, "set-led-state", 13) == 0)
{
const std::string str = buffer + 14;
bool state = (std::strcmp(str.c_str(), "on") == 0) ? true : false;
if(_fakeled->setState(state) == 0)
{
answer = "OK\n";
}
else
{
answer = "FAILED\n";
}
return true;
}
else if(std::strcmp(buffer, "get-led-state\n") == 0)
{
bool state = false;
bool ret = _fakeled->getState(state);
answer = ret ? "OK " : "FAILED\n";
if(ret)
{
if(state)
{
answer += "on\n";
}
else
{
answer += "off\n";
}
}
return true;
}
else if(std::strncmp(buffer, "set-led-color", 13) == 0)
{
std::string str = buffer + 14;
str.erase(std::remove(str.begin(), str.end(), '\n'), str.end());
answer = (_fakeled->setColor(str) == 0) ? "OK\n" : "FAILED\n";
return true;
}
else if(std::strcmp(buffer, "get-led-color\n") == 0)
{
std::string color = "";
bool ret = _fakeled->getColor(color);
answer = ret ? "OK " : "FAILED\n";
if(ret)
{
answer += color;
answer += '\n';
}
return true;
}
else if(std::strncmp(buffer, "set-led-rate", 12) == 0)
{
const std::string str = buffer + 13;
float rate = atof(str.c_str());
answer = (_fakeled->setRate(rate) == 0) ? "OK\n" : "FAILED\n";
return true;
}
else if(std::strcmp(buffer, "get-led-rate\n") == 0)
{
float rate = 0.0;
bool ret = _fakeled->getRate(rate);
answer = ret ? "OK " : "FAILED\n";
if(ret)
{
char buffer[16];
sprintf(buffer, "%.1f", rate);
answer += buffer;
answer += '\n';
}
return true;
}
else
{
answer = "Wrong command format\n";
return false;
}
return true;
}