fixed main and added RunningMode

This commit is contained in:
Simon Stürz 2015-01-10 15:37:31 +01:00 committed by Michael Zanetti
parent 1529706561
commit 8eaa447cb1
8 changed files with 105 additions and 44 deletions

View File

@ -27,6 +27,13 @@
instantiate, set up and connect all the other components. instantiate, set up and connect all the other components.
*/ */
/*! \enum GuhCore::RunningMode
\value RunningModeApplication
Guh runns as application.
\value RunningModeService
Guh is started as service (daemon).
*/
/*! \fn void GuhCore::eventTriggered(const Event &event); /*! \fn void GuhCore::eventTriggered(const Event &event);
This signal is emitted when an \a event happend. This signal is emitted when an \a event happend.
*/ */
@ -92,6 +99,18 @@ void GuhCore::destroy()
s_instance = 0; s_instance = 0;
} }
/*! Returns the runningMode of this instance. */
GuhCore::RunningMode GuhCore::runningMode() const
{
return m_runningMode;
}
/*! Set the \a runningMode of this instance. */
void GuhCore::setRunningMode(const GuhCore::RunningMode &runningMode)
{
m_runningMode = runningMode;
}
/*! Calls the metheod DeviceManager::plugins(). /*! Calls the metheod DeviceManager::plugins().
* \sa DeviceManager::plugins(), */ * \sa DeviceManager::plugins(), */
QList<DevicePlugin *> GuhCore::plugins() const QList<DevicePlugin *> GuhCore::plugins() const

View File

@ -37,12 +37,20 @@ class GuhCore : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
enum RunningMode {
RunningModeApplication,
RunningModeService
};
static GuhCore* instance(); static GuhCore* instance();
~GuhCore(); ~GuhCore();
// Used for testing // Used for testing
void destroy(); void destroy();
RunningMode runningMode() const;
void setRunningMode(const RunningMode &runningMode);
QList<DevicePlugin *> plugins() const; QList<DevicePlugin *> plugins() const;
DeviceManager::DeviceError setPluginConfig(const PluginId &pluginId, const ParamList &params); DeviceManager::DeviceError setPluginConfig(const PluginId &pluginId, const ParamList &params);
@ -89,6 +97,7 @@ private:
DeviceManager* deviceManager() const; DeviceManager* deviceManager() const;
explicit GuhCore(QObject *parent = 0); explicit GuhCore(QObject *parent = 0);
static GuhCore *s_instance; static GuhCore *s_instance;
RunningMode m_runningMode;
JsonRPCServer *m_jsonServer; JsonRPCServer *m_jsonServer;
DeviceManager *m_deviceManager; DeviceManager *m_deviceManager;

View File

@ -1,8 +1,34 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* This file is part of guh. *
* *
* Guh is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, version 2 of the License. *
* *
* Guh is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with guh. If not, see <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <unistd.h>
#include "guhservice.h" #include "guhservice.h"
GuhService::GuhService(int argc, char **argv): GuhService::GuhService(int argc, char **argv):
QtService<QCoreApplication>(argc, argv, "guh daemon") QtService<QCoreApplication>(argc, argv, "guh daemon")
{ {
qDebug() << "guhd started as daemon.";
application()->setOrganizationName("guh");
application()->setApplicationName("guhd");
application()->setApplicationVersion(GUH_VERSION_STRING);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
setServiceDescription("guh daemon"); setServiceDescription("guh daemon");
setServiceFlags(QtServiceBase::CanBeSuspended); setServiceFlags(QtServiceBase::CanBeSuspended);
} }
@ -13,5 +39,5 @@ GuhService::~GuhService()
void GuhService::start() void GuhService::start()
{ {
GuhCore::instance(); GuhCore::instance()->setRunningMode(GuhCore::RunningModeService);
} }

View File

@ -1,3 +1,21 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* This file is part of guh. *
* *
* Guh is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, version 2 of the License. *
* *
* Guh is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with guh. If not, see <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef GUHSERVICE_H #ifndef GUHSERVICE_H
#define GUHSERVICE_H #define GUHSERVICE_H

View File

@ -17,53 +17,40 @@
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <QCoreApplication> #include <QCoreApplication>
#include <QCommandLineParser>
#include <QtPlugin> #include <QtPlugin>
#include <unistd.h>
#include "guhcore.h" #include "guhcore.h"
#include "guhservice.h" #include "guhservice.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QStringList arguments; QCoreApplication application(argc, argv);
for (int i = 0; i < argc; ++i) { application.setOrganizationName("guh");
arguments.append(QString(argv[i])); application.setApplicationName("guhd");
application.setApplicationVersion(GUH_VERSION_STRING);
QCommandLineParser parser;
parser.addHelpOption();
parser.addVersionOption();
QString description = QString("\nguh ( /[guːh]/ ) is an open source home automation server, which allows to\n"
"control a lot of different devices from many different manufacturers.\n"
"guhd %1 (C) 2014-2015 guh\n"
"Released under the GNU GENERAL PUBLIC LICENSE Version 2.").arg(GUH_VERSION_STRING);
parser.setApplicationDescription(description);
QCommandLineOption foregroundOption(QStringList() << "n" << "no-daemon", QCoreApplication::translate("main", "Run guhd in the foreground, not as daemon."));
parser.addOption(foregroundOption);
parser.process(application);
bool startForeground = parser.isSet(foregroundOption);
if (startForeground) {
GuhCore::instance()->setRunningMode(GuhCore::RunningModeApplication);
return application.exec();
} }
if (arguments.contains("-h") || arguments.contains("--help")) { GuhService service(argc, argv);
qDebug() << "guhd" << GUH_VERSION_STRING << "(C) 2014-2015 guh" ; return service.exec();
qDebug() << "Released under the GNU GENERAL PUBLIC LICENSE Version 2";
qDebug() << "";
qDebug() << "guh (/[guːh]/ - pronounced German and sounds like \"goo\") is an open source";
qDebug() << "home automation server, which allows to control a lot of different devices ";
qDebug() << "from many different manufacturers.";
qDebug() << "";
qDebug() << "options:";
qDebug() << " -h, --help print this help message";
qDebug() << " -v, --version print version";
qDebug() << " -e, --executable start guh as application, not as daemon";
qDebug() << "";
exit(0);
}
if (arguments.contains("-v") || arguments.contains("--version")) {
qDebug() << "guhd" << GUH_VERSION_STRING;
exit(0);
}
if (!arguments.contains("-e") && !arguments.contains("--executable")) {
qDebug() << "guhd started as daemon.";
GuhService service(argc, argv);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
return service.exec();
}
QCoreApplication a(argc, argv);
qDebug() << "guhd started as executable.";
a.setOrganizationName("guh");
a.setApplicationName("guhd");
GuhCore::instance();
return a.exec();
} }

View File

@ -1005,7 +1005,7 @@ void QtServiceBase::processCommand(int /*code*/)
\b{Note:} On Unix systems, this class relies on facilities \b{Note:} On Unix systems, this class relies on facilities
provided by the QtNetwork module, provided as part of the provided by the QtNetwork module, provided as part of the
\l{Qt Open Source Edition} and certain \l{Qt Commercial Editions}. Qt Open Source Edition and certain Qt Commercial Editions.
The QtService class functionality is inherited from QtServiceBase, The QtService class functionality is inherited from QtServiceBase,
but in addition the QtService class binds an instance of but in addition the QtService class binds an instance of

View File

@ -39,7 +39,9 @@ TcpServer::TcpServer(QObject *parent) :
// load settings // load settings
bool ok; bool ok;
QSettings settings("/etc/guh/guhd.conf"); QSettings settings("/etc/guh/guhd.conf");
settings.beginGroup("JSON-RPC Server"); settings.beginGroup("JSONRPC");
// TODO: handle interfaces in settings (enable just localhost ecc...)
uint port = settings.value("port", 1234).toUInt(&ok); uint port = settings.value("port", 1234).toUInt(&ok);
settings.endGroup(); settings.endGroup();

View File

@ -3,7 +3,7 @@ if test -z "$results"; then
exit 0 exit 0
else else
echo "*** License check failed. Offending files:" echo "*** License check failed. Offending files:"
licensecheck -r -c '\.(cpp|h)$' $1 | grep -v "GPL (v2)" | grep -v "GENERATED" licensecheck -r -c '\.(cpp|h)$' $1 | grep -v "GPL (v2)" | grep -v "BSD" | grep -v "GENERATED"
exit 1 exit 1
fi fi