2014-11-21 21:24:33 +01:00
|
|
|
// stl includes
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
// Qt includes
|
|
|
|
#include <QRgb>
|
|
|
|
|
|
|
|
// protoserver includes
|
|
|
|
#include "protoserver/ProtoConnection.h"
|
|
|
|
|
|
|
|
ProtoConnection::ProtoConnection(const std::string & a) :
|
|
|
|
_socket(),
|
2016-02-25 15:21:25 +01:00
|
|
|
_skipReply(false),
|
|
|
|
_prevSocketState(QAbstractSocket::UnconnectedState)
|
2014-11-21 21:24:33 +01:00
|
|
|
{
|
|
|
|
QString address(a.c_str());
|
|
|
|
QStringList parts = address.split(":");
|
|
|
|
if (parts.size() != 2)
|
|
|
|
{
|
2016-03-23 17:40:34 +01:00
|
|
|
throw std::runtime_error(QString("PROTOCONNECTION ERROR: Wrong address: Unable to parse address (%1)").arg(address).toStdString());
|
2014-11-21 21:24:33 +01:00
|
|
|
}
|
|
|
|
_host = parts[0];
|
|
|
|
|
|
|
|
bool ok;
|
|
|
|
_port = parts[1].toUShort(&ok);
|
|
|
|
if (!ok)
|
|
|
|
{
|
2016-03-23 17:40:34 +01:00
|
|
|
throw std::runtime_error(QString("PROTOCONNECTION ERROR: Wrong port: Unable to parse the port number (%1)").arg(parts[1]).toStdString());
|
2014-11-21 21:24:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// try to connect to host
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cout << "PROTOCONNECTION INFO: Connecting to Hyperion: " << _host.toStdString() << ":" << _port << std::endl;
|
2014-11-21 21:24:33 +01:00
|
|
|
connectToHost();
|
2016-02-25 15:21:25 +01:00
|
|
|
|
|
|
|
// start the connection timer
|
|
|
|
_timer.setInterval(5000);
|
|
|
|
_timer.setSingleShot(false);
|
|
|
|
|
|
|
|
connect(&_timer,SIGNAL(timeout()), this, SLOT(connectToHost()) );
|
|
|
|
_timer.start();
|
2014-11-21 21:24:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ProtoConnection::~ProtoConnection()
|
|
|
|
{
|
2016-02-25 15:21:25 +01:00
|
|
|
_timer.stop();
|
2014-11-21 21:24:33 +01:00
|
|
|
_socket.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtoConnection::setSkipReply(bool skip)
|
|
|
|
{
|
|
|
|
_skipReply = skip;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtoConnection::setColor(const ColorRgb & color, int priority, int duration)
|
|
|
|
{
|
|
|
|
proto::HyperionRequest request;
|
|
|
|
request.set_command(proto::HyperionRequest::COLOR);
|
|
|
|
proto::ColorRequest * colorRequest = request.MutableExtension(proto::ColorRequest::colorRequest);
|
|
|
|
colorRequest->set_rgbcolor((color.red << 16) | (color.green << 8) | color.blue);
|
|
|
|
colorRequest->set_priority(priority);
|
|
|
|
colorRequest->set_duration(duration);
|
|
|
|
|
|
|
|
// send command message
|
|
|
|
sendMessage(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtoConnection::setImage(const Image<ColorRgb> &image, int priority, int duration)
|
|
|
|
{
|
|
|
|
proto::HyperionRequest request;
|
|
|
|
request.set_command(proto::HyperionRequest::IMAGE);
|
|
|
|
proto::ImageRequest * imageRequest = request.MutableExtension(proto::ImageRequest::imageRequest);
|
|
|
|
imageRequest->set_imagedata(image.memptr(), image.width() * image.height() * 3);
|
|
|
|
imageRequest->set_imagewidth(image.width());
|
|
|
|
imageRequest->set_imageheight(image.height());
|
|
|
|
imageRequest->set_priority(priority);
|
|
|
|
imageRequest->set_duration(duration);
|
|
|
|
|
|
|
|
// send command message
|
|
|
|
sendMessage(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtoConnection::clear(int priority)
|
|
|
|
{
|
|
|
|
proto::HyperionRequest request;
|
|
|
|
request.set_command(proto::HyperionRequest::CLEAR);
|
|
|
|
proto::ClearRequest * clearRequest = request.MutableExtension(proto::ClearRequest::clearRequest);
|
|
|
|
clearRequest->set_priority(priority);
|
|
|
|
|
|
|
|
// send command message
|
|
|
|
sendMessage(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtoConnection::clearAll()
|
|
|
|
{
|
|
|
|
proto::HyperionRequest request;
|
|
|
|
request.set_command(proto::HyperionRequest::CLEARALL);
|
|
|
|
|
|
|
|
// send command message
|
|
|
|
sendMessage(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtoConnection::connectToHost()
|
|
|
|
{
|
2016-02-25 15:21:25 +01:00
|
|
|
// try connection only when
|
|
|
|
if (_socket.state() == QAbstractSocket::UnconnectedState)
|
|
|
|
{
|
|
|
|
_socket.connectToHost(_host, _port);
|
|
|
|
//_socket.waitForConnected(1000);
|
2014-11-21 21:24:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtoConnection::sendMessage(const proto::HyperionRequest &message)
|
|
|
|
{
|
2016-02-25 15:21:25 +01:00
|
|
|
// print out connection message only when state is changed
|
|
|
|
if (_socket.state() != _prevSocketState )
|
2014-11-21 21:24:33 +01:00
|
|
|
{
|
2016-02-25 15:21:25 +01:00
|
|
|
switch (_socket.state() )
|
|
|
|
{
|
|
|
|
case QAbstractSocket::UnconnectedState:
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cout << "PROTOCONNECTION INFO: No connection to Hyperion: " << _host.toStdString() << ":" << _port << std::endl;
|
2016-02-25 15:21:25 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case QAbstractSocket::ConnectedState:
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cout << "PROTOCONNECTION INFO: Connected to Hyperion: " << _host.toStdString() << ":" << _port << std::endl;
|
2016-02-25 15:21:25 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
//std::cout << "Connecting to Hyperion: " << _host.toStdString() << ":" << _port << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_prevSocketState = _socket.state();
|
2014-11-21 21:24:33 +01:00
|
|
|
}
|
|
|
|
|
2016-02-25 15:21:25 +01:00
|
|
|
|
2014-11-21 21:24:33 +01:00
|
|
|
if (_socket.state() != QAbstractSocket::ConnectedState)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only get here if we are connected
|
|
|
|
|
|
|
|
// serialize message (FastWriter already appends a newline)
|
|
|
|
std::string serializedMessage = message.SerializeAsString();
|
|
|
|
|
|
|
|
int length = serializedMessage.size();
|
|
|
|
const uint8_t header[] = {
|
|
|
|
uint8_t((length >> 24) & 0xFF),
|
|
|
|
uint8_t((length >> 16) & 0xFF),
|
|
|
|
uint8_t((length >> 8) & 0xFF),
|
|
|
|
uint8_t((length ) & 0xFF)};
|
|
|
|
|
|
|
|
// write message
|
|
|
|
int count = 0;
|
|
|
|
count += _socket.write(reinterpret_cast<const char *>(header), 4);
|
|
|
|
count += _socket.write(reinterpret_cast<const char *>(serializedMessage.data()), length);
|
|
|
|
if (!_socket.waitForBytesWritten())
|
|
|
|
{
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cerr << "PROTOCONNECTION ERROR: Error while writing data to host" << std::endl;
|
2014-11-21 21:24:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_skipReply)
|
|
|
|
{
|
|
|
|
// read reply data
|
|
|
|
QByteArray serializedReply;
|
|
|
|
length = -1;
|
|
|
|
while (length < 0 && serializedReply.size() < length+4)
|
|
|
|
{
|
|
|
|
// receive reply
|
|
|
|
if (!_socket.waitForReadyRead())
|
|
|
|
{
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cerr << "PROTOCONNECTION ERROR: Error while reading data from host" << std::endl;
|
2014-11-21 21:24:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
serializedReply += _socket.readAll();
|
|
|
|
|
|
|
|
if (length < 0 && serializedReply.size() >= 4)
|
|
|
|
{
|
|
|
|
// read the message size
|
|
|
|
length =
|
|
|
|
((serializedReply[0]<<24) & 0xFF000000) |
|
|
|
|
((serializedReply[1]<<16) & 0x00FF0000) |
|
|
|
|
((serializedReply[2]<< 8) & 0x0000FF00) |
|
|
|
|
((serializedReply[3] ) & 0x000000FF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse reply data
|
|
|
|
proto::HyperionReply reply;
|
|
|
|
reply.ParseFromArray(serializedReply.constData()+4, length);
|
|
|
|
|
|
|
|
// parse reply message
|
|
|
|
parseReply(reply);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProtoConnection::parseReply(const proto::HyperionReply &reply)
|
|
|
|
{
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
if (!reply.success())
|
|
|
|
{
|
|
|
|
if (reply.has_error())
|
|
|
|
{
|
2016-03-23 17:40:34 +01:00
|
|
|
throw std::runtime_error("PROTOCONNECTION ERROR: " + reply.error());
|
2014-11-21 21:24:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-23 17:40:34 +01:00
|
|
|
throw std::runtime_error("PROTOCONNECTION ERROR: No error info");
|
2014-11-21 21:24:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|