added guhd.conf

add settings to tcpserver
This commit is contained in:
Simon Stürz 2015-07-07 17:35:27 +02:00 committed by Michael Zanetti
parent 38afa00b30
commit 40847fd153
8 changed files with 143 additions and 43 deletions

8
data/config/guhd.conf Normal file
View File

@ -0,0 +1,8 @@
[JSONRPC]
port=12345
interfaces="lo","all"
ip="IPv4", "IPv6"
[GPIO]
rf433rx=27
rf433tx=22

1
debian/guhd.install vendored
View File

@ -1,2 +1,3 @@
usr/bin/guhd
data/init/* /etc/init.d
data/config/guhd.conf /etc/guh/guhd.conf

View File

@ -188,7 +188,7 @@ void GuhSettings::remove(const QString &key)
void GuhSettings::setValue(const QString &key, const QVariant &value)
{
Q_ASSERT_X(m_role != GuhSettings::SettingsRoleGlobal, "GuhSettings", "Bad settings implementation. The global settings file is read only.");
Q_ASSERT_X(m_role != GuhSettings::SettingsRoleGlobal, "GuhSettings", "Bad settings usage. The global settings file should be read only.");
m_settings->setValue(key, value);
}

View File

@ -51,6 +51,7 @@
#include "radio433.h"
#include "loggingcategories.h"
#include "guhsettings.h"
#include <QFileInfo>
@ -58,11 +59,16 @@
Radio433::Radio433(QObject *parent) :
QObject(parent)
{
#ifdef GPIO433
m_receiver = new Radio433Receiver(this,27);
m_transmitter = new Radio433Trasmitter(this,22);
GuhSettings settings(GuhSettings::SettingsRoleGlobal);
qCDebug(dcHardware) << "Loading GPIO settings from:" << settings.fileName();
settings.beginGroup("GPIO");
int receiverGpioNumber = settings.value("rf433rx",27).toInt();
int transmitterGpioNumber = settings.value("rf433tx",22).toInt();
settings.endGroup();
m_receiver = new Radio433Receiver(this, receiverGpioNumber);
m_transmitter = new Radio433Trasmitter(this, transmitterGpioNumber);
connect(m_receiver, &Radio433Receiver::readingChanged, this, &Radio433::readingChanged);
connect(m_receiver, &Radio433Receiver::dataReceived, this, &Radio433::dataReceived);
#endif

View File

@ -38,7 +38,7 @@ SOURCES += plugin/device.cpp \
types/ruleactionparam.cpp \
types/statedescriptor.cpp \
loggingcategories.cpp \
guhsettings.cpp
guhsettings.cpp \
HEADERS += plugin/device.h \
plugin/deviceclass.h \
@ -72,5 +72,5 @@ HEADERS += plugin/device.h \
types/statedescriptor.h \
typeutils.h \
loggingcategories.h \
guhsettings.h
guhsettings.h \

View File

@ -86,6 +86,7 @@ int main(int argc, char *argv[])
parser.addOption(foregroundOption);
QString debugDescription = QString("Debug categories to enable. Prefix with \"No\" to disable. Warnings from all categories will be printed unless explicitly muted with \"NoWarnings\". \n\nCategories are:");
// create sorted loggingFiler list
QStringList sortedFilterList = QStringList(s_loggingFilters.keys());
sortedFilterList.sort();

View File

@ -31,32 +31,35 @@ namespace guhserver {
TcpServer::TcpServer(QObject *parent) :
QObject(parent)
{
qCDebug(dcConnection) << "----------------------------";
qCDebug(dcConnection) << "network interfaces:";
foreach(const QNetworkInterface &interface, QNetworkInterface::allInterfaces()){
qCDebug(dcConnection) << " -------------------------";
qCDebug(dcConnection) << " name :" << interface.name();
if(!interface.addressEntries().isEmpty()){
qCDebug(dcConnection) << " ip :" << interface.addressEntries().first().ip().toString();
}
qCDebug(dcConnection) << " mac : " << interface.hardwareAddress();
}
qCDebug(dcConnection) << "----------------------------";
// Timer for scanning network interfaces ever 5 seconds
// Note: QNetworkConfigurationManager does not work on embedded platforms
m_timer = new QTimer(this);
m_timer->setInterval(5000);
connect (m_timer, &QTimer::timeout, this, &TcpServer::onTimeout);
// load settings
bool ok;
// load JSON-RPC server settings
GuhSettings settings(GuhSettings::SettingsRoleGlobal);
qCDebug(dcConnection) << "Loading TCP server settings from:" << settings.fileName();
settings.beginGroup("JSONRPC");
// TODO: handle interfaces in settings (enable just localhost ecc...)
// load port
m_port = settings.value("port", 1234).toUInt();
uint port = settings.value("port", 1234).toUInt(&ok);
settings.endGroup();
if(ok){
m_port = port;
// load interfaces
QStringList interfaceList = settings.value("interfaces", QStringList("all")).toStringList();
if (interfaceList.contains("all")) {
m_networkInterfaces = QNetworkInterface::allInterfaces();
} else {
m_port = 1234;
foreach (const QNetworkInterface &interface, QNetworkInterface::allInterfaces()) {
if (interfaceList.contains(interface.name())) {
m_networkInterfaces.append(interface);
}
}
}
// load IP versions (IPv4, IPv6 or any)
m_ipVersions = settings.value("ip", QStringList("any")).toStringList();
settings.endGroup();
}
void TcpServer::sendData(const QList<QUuid> &clients, const QByteArray &data)
@ -66,6 +69,7 @@ void TcpServer::sendData(const QList<QUuid> &clients, const QByteArray &data)
}
}
void TcpServer::sendData(const QUuid &clientId, const QByteArray &data)
{
QTcpSocket *client = m_clientList.value(clientId);
@ -98,11 +102,11 @@ void TcpServer::readPackage()
QTcpSocket *client = qobject_cast<QTcpSocket*>(sender());
qCDebug(dcConnection) << "data comming from" << client->peerAddress().toString();
QByteArray message;
while(client->canReadLine()){
while (client->canReadLine()) {
QByteArray dataLine = client->readLine();
qCDebug(dcConnection) << "line in:" << dataLine;
message.append(dataLine);
if(dataLine.endsWith('\n')){
if (dataLine.endsWith('\n')) {
emit dataAvailable(m_clientList.key(client), message);
message.clear();
}
@ -117,37 +121,106 @@ void TcpServer::slotClientDisconnected()
m_clientList.take(clientId)->deleteLater();
}
void TcpServer::onTimeout()
{
// // check all networkinterfaces
// bool ipV4 = m_ipVersions.contains("IPv4");
// bool ipV6 = m_ipVersions.contains("IPv6");
// if (m_ipVersions.contains("any")) {
// ipV4 = ipV6 = true;
// }
// QList<QHostAddress *> m_serversToCreate;
// QList<QTcpServer *> m_serversToDelete;
// // check all available interfaces
// foreach (const QNetworkInterface &interface, m_networkInterfaces) {
// QList<QNetworkAddressEntry> addresseEntries = interface.addressEntries();
// QList<QHostAddress> addresses;
// foreach (QNetworkAddressEntry entry, addresseEntries) {
// if (ipV4 && entry.ip().protocol() == QAbstractSocket::IPv4Protocol) {
// addresses.append(entry.ip());
// }
// if (ipV6 && entry.ip().protocol() == QAbstractSocket::IPv6Protocol) {
// addresses.append(entry.ip());
// }
// }
// // check each host address of this interface
// foreach (QTcpServer *s, m_serverList) {
// if (!addresses.contains(s->serverAddress()))
// return;
// }
// }
}
bool TcpServer::startServer()
{
// Listen on all Networkinterfaces
foreach(const QHostAddress &address, QNetworkInterface::allAddresses()){
QTcpServer *server = new QTcpServer(this);
if(server->listen(address, m_port)) {
qCDebug(dcConnection) << "JSON-RPC server listening on" << address.toString() << ":" << m_port;
connect(server, SIGNAL(newConnection()), SLOT(newClientConnected()));
m_serverList.insert(QUuid::createUuid(), server);
} else {
qCWarning(dcConnection) << "can not listening to" << address.toString() << ":" << m_port;
delete server;
qCDebug(dcConnection) << "----------------------------";
qCDebug(dcConnection) << "JSON-RPC server listening on:";
qCDebug(dcConnection) << "----------------------------";
bool ipV4 = m_ipVersions.contains("IPv4");
bool ipV6 = m_ipVersions.contains("IPv6");
if (m_ipVersions.contains("any")) {
ipV4 = ipV6 = true;
}
foreach (const QNetworkInterface &interface, m_networkInterfaces) {
QList<QNetworkAddressEntry> addresseEntries = interface.addressEntries();
QList<QHostAddress> addresses;
foreach (QNetworkAddressEntry entry, addresseEntries) {
if (ipV4 && entry.ip().protocol() == QAbstractSocket::IPv4Protocol) {
addresses.append(entry.ip());
}
if (ipV6 && entry.ip().protocol() == QAbstractSocket::IPv6Protocol) {
addresses.append(entry.ip());
}
}
foreach(const QHostAddress &address, addresses){
QTcpServer *server = new QTcpServer(this);
if(server->listen(address, m_port)) {
qCDebug(dcConnection) << "\tname |" << interface.name();
qCDebug(dcConnection) << "\tip |" << address.toString();
qCDebug(dcConnection) << "\tport |" << m_port;
qCDebug(dcConnection) << "\tmac |" << interface.hardwareAddress();
qCDebug(dcConnection) << "\t-----+-------------------";
connect(server, SIGNAL(newConnection()), SLOT(newClientConnected()));
m_serverList.insert(QUuid::createUuid(), server);
} else {
qCWarning(dcConnection) << "ERROR: can not listen to" << interface.name() << address.toString() << m_port;
delete server;
}
}
}
if(m_serverList.empty()){
if (m_serverList.empty())
return false;
}
return true;
}
bool TcpServer::stopServer()
{
// Listen on all Networkinterfaces
foreach(QTcpServer *server, m_serverList){
foreach (QTcpServer *server, m_serverList) {
qCDebug(dcConnection) << "close server " << server->serverAddress().toString();
server->close();
delete server;
}
if(!m_serverList.empty()){
if (!m_serverList.empty())
return false;
}
return true;
}

View File

@ -23,10 +23,11 @@
#define TCPSERVER_H
#include <QObject>
#include <QNetworkInterface>
#include <QTcpServer>
#include <QTcpSocket>
#include <QNetworkInterface>
#include <QUuid>
#include <QTimer>
namespace guhserver {
@ -40,9 +41,18 @@ public:
void sendData(const QList<QUuid> &clients, const QByteArray &data);
private:
QTimer *m_timer;
QHash<QUuid, QTcpServer*> m_serverList;
QHash<QUuid, QTcpSocket*> m_clientList;
uint m_port;
QList<QNetworkInterface> m_networkInterfaces;
QStringList m_ipVersions;
// QList<QHostAddress> serversToCreate();
// QList<QHostAddress> serversToRemove();
signals:
void clientConnected(const QUuid &clientId);
@ -53,6 +63,7 @@ private slots:
void newClientConnected();
void readPackage();
void slotClientDisconnected();
void onTimeout();
public slots:
bool startServer();