Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit 1a1c70b

Browse files
committed
Switched json to proto
1 parent 8c4e0ef commit 1a1c70b

File tree

3 files changed

+72
-81
lines changed

3 files changed

+72
-81
lines changed

src/hyperion-v4l2/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ add_executable(hyperion-v4l2
4242

4343
target_link_libraries(hyperion-v4l2
4444
getoptPlusPlus
45-
jsoncpp
4645
hyperion-utils
4746
${PROTOBUF_LIBRARIES}
4847
pthread

src/hyperion-v4l2/ProtoConnection.cpp

Lines changed: 66 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -38,143 +38,135 @@ ProtoConnection::~ProtoConnection()
3838
_socket.close();
3939
}
4040

41-
void ProtoConnection::setColor(std::vector<QColor> colors, int priority, int duration)
41+
void ProtoConnection::setColor(const ColorRgb & color, int priority, int duration)
4242
{
43-
// create command
44-
Json::Value command;
45-
command["command"] = "color";
46-
command["priority"] = priority;
47-
Json::Value & rgbValue = command["color"];
48-
for (const QColor & color : colors)
49-
{
50-
rgbValue.append(color.red());
51-
rgbValue.append(color.green());
52-
rgbValue.append(color.blue());
53-
}
54-
if (duration > 0)
55-
{
56-
command["duration"] = duration;
57-
}
43+
proto::HyperionRequest request;
44+
request.set_command(proto::HyperionRequest::COLOR);
45+
proto::ColorRequest * colorRequest = request.MutableExtension(proto::ColorRequest::colorRequest);
46+
colorRequest->set_rgbcolor((color.red << 16) | (color.green << 8) | color.blue);
47+
colorRequest->set_priority(priority);
48+
colorRequest->set_duration(duration);
5849

5950
// send command message
60-
Json::Value reply = sendMessage(command);
51+
proto::HyperionReply reply = sendMessage(request);
6152

6253
// parse reply message
6354
parseReply(reply);
6455
}
6556

6657
void ProtoConnection::setImage(const Image<ColorRgb> &image, int priority, int duration)
6758
{
68-
// ensure the image has RGB888 format
69-
QByteArray binaryImage = QByteArray::fromRawData(reinterpret_cast<const char *>(image.memptr()), image.width() * image.height() * 3);
70-
const QByteArray base64Image = binaryImage.toBase64();
71-
72-
// create command
73-
Json::Value command;
74-
command["command"] = "image";
75-
command["priority"] = priority;
76-
command["imagewidth"] = image.width();
77-
command["imageheight"] = image.height();
78-
command["imagedata"] = std::string(base64Image.data(), base64Image.size());
79-
if (duration > 0)
80-
{
81-
command["duration"] = duration;
82-
}
59+
proto::HyperionRequest request;
60+
request.set_command(proto::HyperionRequest::IMAGE);
61+
proto::ImageRequest * imageRequest = request.MutableExtension(proto::ImageRequest::imageRequest);
62+
imageRequest->set_imagedata(image.memptr(), image.width() * image.height() * 3);
63+
imageRequest->set_imagewidth(image.width());
64+
imageRequest->set_imageheight(image.height());
65+
imageRequest->set_priority(priority);
66+
imageRequest->set_duration(duration);
8367

8468
// send command message
85-
Json::Value reply = sendMessage(command);
69+
proto::HyperionReply reply = sendMessage(request);
8670

8771
// parse reply message
88-
parseReply(reply);
72+
// parseReply(reply);
8973
}
9074

9175
void ProtoConnection::clear(int priority)
9276
{
93-
std::cout << "Clear priority channel " << priority << std::endl;
94-
95-
// create command
96-
Json::Value command;
97-
command["command"] = "clear";
98-
command["priority"] = priority;
77+
proto::HyperionRequest request;
78+
request.set_command(proto::HyperionRequest::CLEAR);
79+
proto::ClearRequest * clearRequest = request.MutableExtension(proto::ClearRequest::clearRequest);
80+
clearRequest->set_priority(priority);
9981

10082
// send command message
101-
Json::Value reply = sendMessage(command);
83+
proto::HyperionReply reply = sendMessage(request);
10284

10385
// parse reply message
10486
parseReply(reply);
10587
}
10688

10789
void ProtoConnection::clearAll()
10890
{
109-
std::cout << "Clear all priority channels" << std::endl;
110-
111-
// create command
112-
Json::Value command;
113-
command["command"] = "clearall";
91+
proto::HyperionRequest request;
92+
request.set_command(proto::HyperionRequest::CLEARALL);
11493

11594
// send command message
116-
Json::Value reply = sendMessage(command);
95+
proto::HyperionReply reply = sendMessage(request);
11796

11897
// parse reply message
11998
parseReply(reply);
12099
}
121100

122-
Json::Value ProtoConnection::sendMessage(const Json::Value & message)
101+
proto::HyperionReply ProtoConnection::sendMessage(const proto::HyperionRequest &message)
123102
{
124103
// serialize message (FastWriter already appends a newline)
125-
std::string serializedMessage = Json::FastWriter().write(message);
104+
std::string serializedMessage = message.SerializeAsString();
105+
106+
int length = serializedMessage.size();
107+
const uint8_t header[] = {
108+
uint8_t((length >> 24) & 0xFF),
109+
uint8_t((length >> 16) & 0xFF),
110+
uint8_t((length >> 8) & 0xFF),
111+
uint8_t((length ) & 0xFF)};
126112

127113
// write message
128-
_socket.write(serializedMessage.c_str());
114+
int count = 0;
115+
count += _socket.write(reinterpret_cast<const char *>(header), 4);
116+
count += _socket.write(reinterpret_cast<const char *>(serializedMessage.data()), length);
129117
if (!_socket.waitForBytesWritten())
130118
{
131119
throw std::runtime_error("Error while writing data to host");
132120
}
133121

122+
/*
134123
// read reply data
135124
QByteArray serializedReply;
136-
while (!serializedReply.contains('\n'))
125+
length = -1;
126+
while (serializedReply.size() != length)
137127
{
128+
std::cout << length << std::endl;
138129
// receive reply
139130
if (!_socket.waitForReadyRead())
140131
{
141132
throw std::runtime_error("Error while reading data from host");
142133
}
143134
144135
serializedReply += _socket.readAll();
145-
}
146-
int bytes = serializedReply.indexOf('\n') + 1; // Find the end of message
147136
148-
// parse reply data
149-
Json::Reader jsonReader;
150-
Json::Value reply;
151-
if (!jsonReader.parse(serializedReply.constData(), serializedReply.constData() + bytes, reply))
152-
{
153-
throw std::runtime_error("Error while parsing reply: invalid json");
137+
if (length < 0 && serializedReply.size() >= 4)
138+
{
139+
std::cout << (int) serializedReply[3] << std::endl;
140+
std::cout << (int) serializedReply[2] << std::endl;
141+
std::cout << (int) serializedReply[1] << std::endl;
142+
std::cout << (int) serializedReply[0] << std::endl;
143+
144+
length = (uint8_t(serializedReply[0]) << 24) | (uint8_t(serializedReply[1]) << 16) | (uint8_t(serializedReply[2]) << 8) | uint8_t(serializedReply[3]) ;
145+
}
154146
}
155147
148+
std::cout << length << std::endl;
149+
*/
150+
// parse reply data
151+
proto::HyperionReply reply;
152+
// reply.ParseFromArray(serializedReply.constData()+4, length);
156153
return reply;
157154
}
158155

159-
bool ProtoConnection::parseReply(const Json::Value &reply)
156+
bool ProtoConnection::parseReply(const proto::HyperionReply &reply)
160157
{
161158
bool success = false;
162-
std::string reason = "No error info";
163159

164-
try
160+
if (!reply.success())
165161
{
166-
success = reply.get("success", false).asBool();
167-
if (!success)
168-
reason = reply.get("error", reason).asString();
169-
}
170-
catch (const std::runtime_error &)
171-
{
172-
// Some json parsing error: ignore and set parsing error
173-
}
174-
175-
if (!success)
176-
{
177-
throw std::runtime_error("Error: " + reason);
162+
if (reply.has_error())
163+
{
164+
throw std::runtime_error("Error: " + reply.error());
165+
}
166+
else
167+
{
168+
throw std::runtime_error("Error: No error info");
169+
}
178170
}
179171

180172
return success;

src/hyperion-v4l2/ProtoConnection.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <utils/ColorRgb.h>
1515

1616
// jsoncpp includes
17-
#include <json/json.h>
17+
#include <message.pb.h>
1818

1919
///
2020
/// Connection class to setup an connection to the hyperion server and execute commands
@@ -41,7 +41,7 @@ class ProtoConnection
4141
/// @param priority The priority
4242
/// @param duration The duration in milliseconds
4343
///
44-
void setColor(std::vector<QColor> color, int priority, int duration);
44+
void setColor(const ColorRgb & color, int priority, int duration = 1);
4545

4646
///
4747
/// Set the leds according to the given image (assume the image is stretched to the display size)
@@ -50,7 +50,7 @@ class ProtoConnection
5050
/// @param priority The priority
5151
/// @param duration The duration in milliseconds
5252
///
53-
void setImage(const Image<ColorRgb> & image, int priority, int duration);
53+
void setImage(const Image<ColorRgb> & image, int priority, int duration = -1);
5454

5555
///
5656
/// Clear the given priority channel
@@ -66,13 +66,13 @@ class ProtoConnection
6666

6767
private:
6868
///
69-
/// Send a json command message and receive its reply
69+
/// Send a command message and receive its reply
7070
///
7171
/// @param message The message to send
7272
///
7373
/// @return The returned reply
7474
///
75-
Json::Value sendMessage(const Json::Value & message);
75+
proto::HyperionReply sendMessage(const proto::HyperionRequest & message);
7676

7777
///
7878
/// Parse a reply message
@@ -81,7 +81,7 @@ class ProtoConnection
8181
///
8282
/// @return true if the reply indicates success
8383
///
84-
bool parseReply(const Json::Value & reply);
84+
bool parseReply(const proto::HyperionReply & reply);
8585

8686
private:
8787
/// The TCP-Socket with the connection to the server

0 commit comments

Comments
 (0)