-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLI.cpp
169 lines (144 loc) · 4.05 KB
/
CLI.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "CLI.h"
#include "string"
#include "iostream"
#include <sstream>
#include "Valve_Parser.h"
#include "Networking/client.h"
#include "Networking/server.h"
#include <utility>
#include <tuple>
#include "jsoncpp/json/json.h"
using namespace std;
//todo clean this up
string server_ip = "10.10.10.2";
CLI::CLI()
{
Valve_Parser parser("./valves.txt");
m_valves = parser.get_valves();
mclient.post(server_ip, 8000, "{\"identity\": \"vcb1\", \"server-port\": \"8000\"}");
}
list<string> CLI::parse(string input)
{
char delim = ' ';
list<string> tokens = split(input, delim);
return tokens;
}
void CLI::status(list<string> message)
{
for(auto it = m_valves.begin();
it != m_valves.end();
it++)
{
string open_status = ( (*it)->is_open() ? "open" : "closed");
string normal_open = ( (*it)->get_normal_open() ?
"normal open" : "normal closed");
//name, open/closed, normal_open, portnum
cout << (*it)->get_name() << "\t" << open_status << "\t" \
<< normal_open << "\t" << (*it)->get_port_number() << endl;
}
}
void CLI::open_valve(list<string> message)
{
if(message.size() != 2)
cout << "malformed open-valve command." << endl;
string cmd = message.front();
message.pop_front();
string valve_name = message.front();
for( auto it = m_valves.begin(); it != m_valves.end(); it++)
{
if( (*it)->get_name() == valve_name)
{
(*it)->open_valve();
stringstream ss;
ss << "{ \"identity\": \"vcb1\", ";
ss << "\"valve-name\": \"" << (*it)->get_name() << "\", ";
ss << "\"valve-status\": \"open\"}";
mclient.post(server_ip, 8000, ss.str());
return;
}
}
cout << "we dont have a valve of that name." << endl;
}
void CLI::close_valve(list<string> message)
{
if(message.size() != 2)
cout << "malformed close-valve command." << endl;
string cmd = message.front();
message.pop_front();
string valve_name = message.front();
for( auto it = m_valves.begin(); it != m_valves.end(); it++)
{
if( (*it)->get_name() == valve_name)
{
(*it)->close_valve();
stringstream ss;
ss << "{ \"identity\": \"vcb1\", ";
ss << "\"valve-name\": \"" << (*it)->get_name() << "\", ";
ss << "\"valve-status\": \"closed\"}";
mclient.post(server_ip, 8000, ss.str());
return;
}
}
cout << "we dont have a valve of that name." << endl;
}
void CLI::route(list<string> message)
{
string cmd = *message.begin();
if(cmd == "status")
status(message);
if(cmd == "open-valve")
open_valve(message);
if(cmd == "close-valve")
close_valve(message);
}
list<string> CLI::split(string str, char delim)
{
stringstream ss;
ss.str(str);
string token;
list<string> l;
while(getline(ss, token, delim))
l.push_back(token);
return l;
}
void CLI::parse_payload(string s)
{
Json::Value root;
stringstream ss;
ss << s;
ss >> root;
list<string> cmd_list;
string cmd = root["cmd"].asString();
if(cmd != "")
cmd_list.push_back(cmd);
string arg = root["arg0"].asString();
//TODO: dont hardcode the arg parsing.
//this will most likely break if things arent hardcoded
//precisely
if(arg != "")
cmd_list.push_back(arg);
arg = root["arg1"].asString();
if(arg != "")
cmd_list.push_back(arg);
route(cmd_list);
}
void CLI::loop()
{
string input;
list<string> message;
string path;
string method;
string payload;
while(true)
{
tie(method, path, payload) = mserver.read_request(8000);
//cout << "Method\t" << method << endl;
//cout << "Path:\t" << path << endl;
//cout << "Payload:\t" << payload << endl;
if(method == "")
continue;
if(payload != "")
parse_payload(payload);
mclient.get("10.10.10.2", 8000, "/");
}
}