2016-05-26 23:44:27 +02:00
|
|
|
// Local-Hyperion includes
|
|
|
|
#include "LedDeviceAtmoOrb.h"
|
2020-07-12 20:27:56 +02:00
|
|
|
#include <utils/QStringUtils.h>
|
2016-05-26 23:44:27 +02:00
|
|
|
|
|
|
|
// qt includes
|
2020-06-28 23:05:32 +02:00
|
|
|
#include <QUdpSocket>
|
|
|
|
|
|
|
|
const quint16 MULTICAST_GROUPL_DEFAULT_PORT = 49692;
|
|
|
|
const int LEDS_DEFAULT_NUMBER = 24;
|
2016-05-26 23:44:27 +02:00
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
LedDeviceAtmoOrb::LedDeviceAtmoOrb(const QJsonObject &deviceConfig)
|
|
|
|
: LedDevice()
|
|
|
|
, _udpSocket (nullptr)
|
2020-06-28 23:05:32 +02:00
|
|
|
, _multiCastGroupPort (MULTICAST_GROUPL_DEFAULT_PORT)
|
|
|
|
, _joinedMulticastgroup (false)
|
2020-02-10 15:21:58 +01:00
|
|
|
, _useOrbSmoothing (false)
|
|
|
|
, _transitiontime (0)
|
|
|
|
, _skipSmoothingDiff (0)
|
2020-06-28 23:05:32 +02:00
|
|
|
, _numLeds (LEDS_DEFAULT_NUMBER)
|
2020-02-10 15:21:58 +01:00
|
|
|
|
2017-03-04 22:17:42 +01:00
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
_devConfig = deviceConfig;
|
2020-07-12 20:27:56 +02:00
|
|
|
_isDeviceReady = false;
|
|
|
|
|
|
|
|
_activeDeviceType = deviceConfig["type"].toString("UNSPECIFIED").toLower();
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
LedDevice* LedDeviceAtmoOrb::construct(const QJsonObject &deviceConfig)
|
|
|
|
{
|
|
|
|
return new LedDeviceAtmoOrb(deviceConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
LedDeviceAtmoOrb::~LedDeviceAtmoOrb()
|
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
if ( _udpSocket != nullptr )
|
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
delete _udpSocket;
|
2020-06-28 23:05:32 +02:00
|
|
|
}
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LedDeviceAtmoOrb::init(const QJsonObject &deviceConfig)
|
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
bool isInitOK = false;
|
2020-02-10 15:21:58 +01:00
|
|
|
|
2020-06-28 23:05:32 +02:00
|
|
|
if ( LedDevice::init(deviceConfig) )
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
_multicastGroup = deviceConfig["output"].toString().toStdString().c_str();
|
|
|
|
_useOrbSmoothing = deviceConfig["useOrbSmoothing"].toBool(false);
|
|
|
|
_transitiontime = deviceConfig["transitiontime"].toInt(0);
|
|
|
|
_skipSmoothingDiff = deviceConfig["skipSmoothingDiff"].toInt(0);
|
2020-06-28 23:05:32 +02:00
|
|
|
_multiCastGroupPort = static_cast<quint16>(deviceConfig["port"].toInt(MULTICAST_GROUPL_DEFAULT_PORT));
|
|
|
|
_numLeds = deviceConfig["numLeds"].toInt(LEDS_DEFAULT_NUMBER);
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
QStringList orbIds = QStringUtils::split(deviceConfig["orbIds"].toString().simplified().remove(" "),",", QStringUtils::SplitBehavior::SkipEmptyParts);
|
2020-02-10 15:21:58 +01:00
|
|
|
_orbIds.clear();
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
for (auto & id_str : orbIds)
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
|
|
|
bool ok;
|
|
|
|
int id = id_str.toInt(&ok);
|
|
|
|
if (ok)
|
2020-06-28 23:05:32 +02:00
|
|
|
{
|
|
|
|
if ( id < 1 || id > 255 )
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
Warning(_log, "Skip orb id '%d'. IDs must be in range 1-255", id);
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
2020-06-28 23:05:32 +02:00
|
|
|
else
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
_orbIds.append(id);
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
2020-06-28 23:05:32 +02:00
|
|
|
}
|
2020-02-10 15:21:58 +01:00
|
|
|
else
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
Error(_log, "orb id '%s' is not a number", QSTRING_CSTR(id_str));
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
if ( _orbIds.empty() )
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
|
|
|
this->setInError("No valid OrbIds found!");
|
|
|
|
isInitOK = false;
|
|
|
|
}
|
2020-06-28 23:05:32 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
_udpSocket = new QUdpSocket(this);
|
|
|
|
isInitOK = true;
|
|
|
|
}
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
|
|
|
return isInitOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LedDeviceAtmoOrb::open()
|
2016-08-23 20:07:12 +02:00
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
int retval = -1;
|
2020-07-12 20:27:56 +02:00
|
|
|
_isDeviceReady = false;
|
2020-02-10 15:21:58 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// Try to bind the UDP-Socket
|
|
|
|
if ( _udpSocket != nullptr )
|
2016-08-23 20:07:12 +02:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
_groupAddress = QHostAddress(_multicastGroup);
|
|
|
|
if ( !_udpSocket->bind(QHostAddress::AnyIPv4, _multiCastGroupPort, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint) )
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
QString errortext = QString ("(%1) %2, MulticastGroup: (%3)").arg(_udpSocket->error()).arg(_udpSocket->errorString(), _multicastGroup);
|
|
|
|
this->setInError( errortext );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_joinedMulticastgroup = _udpSocket->joinMulticastGroup(_groupAddress);
|
|
|
|
if ( !_joinedMulticastgroup )
|
2020-06-28 23:05:32 +02:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
QString errortext = QString ("(%1) %2, MulticastGroup: (%3)").arg(_udpSocket->error()).arg(_udpSocket->errorString(), _multicastGroup);
|
2020-06-28 23:05:32 +02:00
|
|
|
this->setInError( errortext );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
// Everything is OK, device is ready
|
|
|
|
_isDeviceReady = true;
|
|
|
|
retval = 0;
|
2020-06-28 23:05:32 +02:00
|
|
|
}
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
2020-06-28 23:05:32 +02:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
int LedDeviceAtmoOrb::close()
|
2020-06-28 23:05:32 +02:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
int retval = 0;
|
|
|
|
_isDeviceReady = false;
|
2020-06-28 23:05:32 +02:00
|
|
|
|
|
|
|
if ( _udpSocket != nullptr )
|
|
|
|
{
|
|
|
|
// Test, if device requires closing
|
|
|
|
if ( _udpSocket->isOpen() )
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
Debug(_log,"Close UDP-device: %s", QSTRING_CSTR( this->getActiveDeviceType() ) );
|
|
|
|
_udpSocket->close();
|
|
|
|
// Everything is OK -> device is closed
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
2016-08-23 20:07:12 +02:00
|
|
|
}
|
2020-07-12 20:27:56 +02:00
|
|
|
return retval;
|
2016-08-23 20:07:12 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
int LedDeviceAtmoOrb::write(const std::vector <ColorRgb> &ledValues)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
// If not in multicast group return
|
2020-06-28 23:05:32 +02:00
|
|
|
if (!_joinedMulticastgroup)
|
2016-08-14 10:46:44 +02:00
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command options:
|
|
|
|
//
|
|
|
|
// 1 = force off
|
|
|
|
// 2 = use lamp smoothing and validate by Orb ID
|
|
|
|
// 4 = validate by Orb ID
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
// When setting _useOrbSmoothing = true it's recommended to disable Hyperion's own smoothing as it will conflict (double smoothing)
|
2016-05-26 23:44:27 +02:00
|
|
|
int commandType = 4;
|
2016-08-14 10:46:44 +02:00
|
|
|
if(_useOrbSmoothing)
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
|
|
|
commandType = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate through colors and set Orb color
|
|
|
|
// Start off with idx 1 as 0 is reserved for controlling all orbs at once
|
2020-02-10 15:21:58 +01:00
|
|
|
int idx = 1;
|
2016-05-26 23:44:27 +02:00
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
for (const ColorRgb &color : ledValues)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
// Retrieve last send colors
|
|
|
|
int lastRed = lastColorRedMap[idx];
|
|
|
|
int lastGreen = lastColorGreenMap[idx];
|
|
|
|
int lastBlue = lastColorBlueMap[idx];
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
// If color difference is higher than _skipSmoothingDiff than we skip Orb smoothing (if enabled) and send it right away
|
|
|
|
if ((_skipSmoothingDiff != 0 && _useOrbSmoothing) && (abs(color.red - lastRed) >= _skipSmoothingDiff || abs(color.blue - lastBlue) >= _skipSmoothingDiff ||
|
|
|
|
abs(color.green - lastGreen) >= _skipSmoothingDiff))
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
|
|
|
// Skip Orb smoothing when using (command type 4)
|
2017-03-04 22:17:42 +01:00
|
|
|
for (int i = 0; i < _orbIds.size(); i++)
|
2016-08-14 10:46:44 +02:00
|
|
|
{
|
|
|
|
if (_orbIds[i] == idx)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
setColor(idx, color, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-14 10:46:44 +02:00
|
|
|
else
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
// Send color
|
2017-03-04 22:17:42 +01:00
|
|
|
for (int i = 0; i < _orbIds.size(); i++)
|
2016-08-14 10:46:44 +02:00
|
|
|
{
|
|
|
|
if (_orbIds[i] == idx)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
setColor(idx, color, commandType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store last colors send for light id
|
2016-08-14 10:46:44 +02:00
|
|
|
lastColorRedMap[idx] = color.red;
|
2016-05-26 23:44:27 +02:00
|
|
|
lastColorGreenMap[idx] = color.green;
|
2016-08-14 10:46:44 +02:00
|
|
|
lastColorBlueMap[idx] = color.blue;
|
2016-05-26 23:44:27 +02:00
|
|
|
|
|
|
|
// Next light id.
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
void LedDeviceAtmoOrb::setColor(int orbId, const ColorRgb &color, int commandType)
|
2016-08-14 10:46:44 +02:00
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
QByteArray bytes;
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
// 5 bytes command-header + 3 bytes color information
|
|
|
|
bytes.resize(5 + 3);
|
2016-05-26 23:44:27 +02:00
|
|
|
bytes.fill('\0');
|
|
|
|
|
|
|
|
// Command identifier: C0FFEE
|
2020-06-28 23:05:32 +02:00
|
|
|
bytes[0] = static_cast<char>(0xC0);
|
|
|
|
bytes[1] = static_cast<char>(0xFF);
|
|
|
|
bytes[2] = static_cast<char>(0xEE);
|
2016-05-26 23:44:27 +02:00
|
|
|
|
|
|
|
// Command type
|
2020-06-28 23:05:32 +02:00
|
|
|
bytes[3] = static_cast<char>(commandType);
|
2016-05-26 23:44:27 +02:00
|
|
|
|
|
|
|
// Orb ID
|
2020-06-28 23:05:32 +02:00
|
|
|
bytes[4] = static_cast<char>(orbId);
|
2016-05-26 23:44:27 +02:00
|
|
|
|
|
|
|
// RED / GREEN / BLUE
|
2020-06-28 23:05:32 +02:00
|
|
|
bytes[5] = static_cast<char>(color.red);
|
|
|
|
bytes[6] = static_cast<char>(color.green);
|
|
|
|
bytes[7] = static_cast<char>(color.blue);
|
|
|
|
|
|
|
|
//std::cout << "Orb [" << orbId << "] Cmd [" << bytes.toHex(':').toStdString() <<"]"<< std::endl;
|
2016-05-26 23:44:27 +02:00
|
|
|
|
|
|
|
sendCommand(bytes);
|
|
|
|
}
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
void LedDeviceAtmoOrb::sendCommand(const QByteArray &bytes)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
QByteArray datagram = bytes;
|
2016-08-14 10:46:44 +02:00
|
|
|
_udpSocket->writeDatagram(datagram.data(), datagram.size(), _groupAddress, _multiCastGroupPort);
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|