@@ -38,143 +38,135 @@ ProtoConnection::~ProtoConnection()
38
38
_socket.close ();
39
39
}
40
40
41
- void ProtoConnection::setColor (std::vector<QColor> colors , int priority, int duration)
41
+ void ProtoConnection::setColor (const ColorRgb & color , int priority, int duration)
42
42
{
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);
58
49
59
50
// send command message
60
- Json::Value reply = sendMessage (command );
51
+ proto::HyperionReply reply = sendMessage (request );
61
52
62
53
// parse reply message
63
54
parseReply (reply);
64
55
}
65
56
66
57
void ProtoConnection::setImage (const Image<ColorRgb> &image, int priority, int duration)
67
58
{
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);
83
67
84
68
// send command message
85
- Json::Value reply = sendMessage (command );
69
+ proto::HyperionReply reply = sendMessage (request );
86
70
87
71
// parse reply message
88
- parseReply (reply);
72
+ // parseReply(reply);
89
73
}
90
74
91
75
void ProtoConnection::clear (int priority)
92
76
{
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);
99
81
100
82
// send command message
101
- Json::Value reply = sendMessage (command );
83
+ proto::HyperionReply reply = sendMessage (request );
102
84
103
85
// parse reply message
104
86
parseReply (reply);
105
87
}
106
88
107
89
void ProtoConnection::clearAll ()
108
90
{
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);
114
93
115
94
// send command message
116
- Json::Value reply = sendMessage (command );
95
+ proto::HyperionReply reply = sendMessage (request );
117
96
118
97
// parse reply message
119
98
parseReply (reply);
120
99
}
121
100
122
- Json::Value ProtoConnection::sendMessage (const Json::Value & message)
101
+ proto::HyperionReply ProtoConnection::sendMessage (const proto::HyperionRequest & message)
123
102
{
124
103
// 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 )};
126
112
127
113
// 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);
129
117
if (!_socket.waitForBytesWritten ())
130
118
{
131
119
throw std::runtime_error (" Error while writing data to host" );
132
120
}
133
121
122
+ /*
134
123
// read reply data
135
124
QByteArray serializedReply;
136
- while (!serializedReply.contains (' \n ' ))
125
+ length = -1;
126
+ while (serializedReply.size() != length)
137
127
{
128
+ std::cout << length << std::endl;
138
129
// receive reply
139
130
if (!_socket.waitForReadyRead())
140
131
{
141
132
throw std::runtime_error("Error while reading data from host");
142
133
}
143
134
144
135
serializedReply += _socket.readAll();
145
- }
146
- int bytes = serializedReply.indexOf (' \n ' ) + 1 ; // Find the end of message
147
136
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
+ }
154
146
}
155
147
148
+ std::cout << length << std::endl;
149
+ */
150
+ // parse reply data
151
+ proto::HyperionReply reply;
152
+ // reply.ParseFromArray(serializedReply.constData()+4, length);
156
153
return reply;
157
154
}
158
155
159
- bool ProtoConnection::parseReply (const Json::Value &reply)
156
+ bool ProtoConnection::parseReply (const proto::HyperionReply &reply)
160
157
{
161
158
bool success = false ;
162
- std::string reason = " No error info" ;
163
159
164
- try
160
+ if (!reply. success ())
165
161
{
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
+ }
178
170
}
179
171
180
172
return success;
0 commit comments