-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgrpc_client.cpp
More file actions
144 lines (123 loc) · 5.83 KB
/
grpc_client.cpp
File metadata and controls
144 lines (123 loc) · 5.83 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
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
#include "mach/grpc_client.hpp"
#include <iostream>
#include <thread>
#include <chrono>
#include <functional>
#include <spdlog/spdlog.h>
#include <unordered_map>
#include "absl/flags/flag.h"
#include "messages.pb.h"
#include "mach/sequences/sequence_manager.hpp"
#include "mach/executor.hpp"
#include "mach/device/device_manager.hpp"
#include "mach/executor.hpp"
#include "mach/thread_safe_queue.hpp"
#include "mach/sequences/open_action.hpp"
#include "mach/sequences/close_action.hpp"
#include "mach/broadcast.hpp"
ABSL_FLAG(std::string, ip, "localhost:50051", "Server address");
namespace mach {
static ThreadSafeQueue<std::pair<long, std::unordered_map<std::string, double>>> sensorQueue;
void ServiceClient::StartSensorDataStream() {
grpc::ClientContext context;
proto::Response response;
std::unique_ptr<grpc::ClientWriter<proto::SensorData>> stream(stub_->SensorDataStream(&context, &response));
spdlog::info("MACH: Started sensor data stream.");
while (true) {
try {
std::pair<long, std::unordered_map<std::string, double>> data = sensorQueue.pop();
proto::SensorData sensorData;
sensorData.set_timestamp(data.first);
for (const auto& [key, value] : data.second) {
sensorData.mutable_values()->insert({key, static_cast<float>(value)});
}
stream->Write(sensorData);
// spdlog::info("MACH: Sent sensor data.");
} catch (const std::exception& ex) {
spdlog::info("MACH: Error sending data: '{}'.", std::string(ex.what()));
}
}
}
void ServiceClient::StartCommandStream() {
// Data we are sending to the server.
proto::DeviceInformation info;
// Add existing devices and sequences to device information.
DeviceManager& deviceManager = DeviceManager::getInstance();
std::vector<std::shared_ptr<Sensor>> sensors = deviceManager.getSensors();
for (const auto& sensor : sensors) {
mach::proto::Device device;
device.set_pin(sensor->getLabJackPin());
device.set_type(proto::Device_DeviceType_SENSOR);
device.set_location(proto::Device_DeviceLocation_GSE);
info.mutable_devices()->insert({sensor->getName(), device});
}
std::vector<std::string> sequences = SequenceManager::getInstance().getSequenceNames();
for (const auto &sequence : sequences) {
info.add_sequences(sequence);
}
// Context for the client. It could be used to convey extra information to
// the server and/or tweak certain RPC behaviors.
grpc::ClientContext context;
// The actual RPC.
std::unique_ptr<grpc::ClientReader<proto::SequenceCommand>> reader(stub_->CommandStream(&context, info));
spdlog::info("MACH: Sent device information to server. Listening for commands.");
proto::SequenceCommand sequence;
while (reader->Read(&sequence)) {
spdlog::info("MACH: Received sequence command: {}", sequence.sequence());
// Commands:
// valve:V##:open
// valve:V##:close
// sequence:###
std::string type = sequence.sequence().substr(0, sequence.sequence().find(":"));
if (type == "valve") {
std::string valveName = sequence.sequence().substr(sequence.sequence().find(":") + 1, sequence.sequence().find(":", sequence.sequence().find(":") + 1) - sequence.sequence().find(":") - 1);
std::transform(valveName.begin(), valveName.end(), valveName.begin(), ::toupper);
std::string command = sequence.sequence().substr(sequence.sequence().find(":", sequence.sequence().find(":") + 1) + 1);
std::shared_ptr<Valve> valve = DeviceManager::getInstance().getValve(valveName);
if (valve == nullptr) {
spdlog::warn("MACH: Bad valve command for '{}'", valveName);
} else if (command == "open") {
valve->open();
} else if (command == "close") {
valve->close();
} else {
spdlog::warn("MACH: Unknown valve command '{}', ignoring!", command);
}
} else if (type == "sequence") {
std::string sequenceName = sequence.sequence().substr(sequence.sequence().find(":") + 1);
Executor::getInstance().executeSequence(sequenceName);
} else {
spdlog::warn("MACH: Unknown command type '{}', ignoring!", type);
}
}
spdlog::info("MACH: GRPC client finished.");
}
static void startCommandStream(std::string target) {
spdlog::info("MACH: Starting GRPC client for command stream on '{}'", target);
ServiceClient serviceClient(grpc::CreateChannel(target, grpc::InsecureChannelCredentials()));
serviceClient.StartCommandStream();
}
static void startSensorDataStream(std::string target) {
spdlog::info("MACH: Starting GRPC client for sensor data on '{}'", target);
ServiceClient serviceClient(grpc::CreateChannel(target, grpc::InsecureChannelCredentials()));
serviceClient.StartSensorDataStream();
}
void grpcStartClient(int argc, char **argv) {
// Instantiate the client. It requires a channel, out of which the actual RPCs
// are created. This channel models a connection to an endpoint specified by
// the argument "--target=" which is the only expected argument.
std::string target_str = absl::GetFlag(FLAGS_ip);
std::thread(startCommandStream, target_str).detach();
std::thread(startSensorDataStream, target_str).detach();
// Start broadcasting the server to the network.
startBroadcastingServer();
}
void addSensorData(std::unordered_map<std::string, double> data) {
// Stop data queue from growing indefinitely.
if (sensorQueue.size() > 100) {
sensorQueue.pop();
}
long timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
sensorQueue.push(std::make_pair(timestamp, data));
}
} // namespace mach