diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..052f4c5f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 2.8.9) + +project(Hive C CXX) + +set(CMAKE_AUTOMOC ON) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-permissive -pedantic -Wall -Wextra -fPIC") + +include(FindPkgConfig) + +find_package(Qt5Core) + +add_subdirectory(libhive) +add_subdirectory(server) + diff --git a/libhive/CMakeLists.txt b/libhive/CMakeLists.txt new file mode 100644 index 00000000..08c9354b --- /dev/null +++ b/libhive/CMakeLists.txt @@ -0,0 +1,14 @@ +include_directories( + ${CMAKE_CURRENT_SOURCE_DIR} +) + +set(libhive_SRCS + device.cpp + deviceclass.cpp + deviceplugin.cpp +) + +add_library(libhive ${libhive_SRCS}) + +qt5_use_modules(libhive Network) + diff --git a/libhive/device.cpp b/libhive/device.cpp new file mode 100644 index 00000000..9d7d7381 --- /dev/null +++ b/libhive/device.cpp @@ -0,0 +1,12 @@ +#include "device.h" + +Device::Device(QObject *parent): + QObject(parent) +{ + +} + +QUuid Device::id() const +{ + return m_id; +} diff --git a/libhive/device.h b/libhive/device.h new file mode 100644 index 00000000..a719eb06 --- /dev/null +++ b/libhive/device.h @@ -0,0 +1,21 @@ +#ifndef DEVICE_H +#define DEVICE_H + +#include +#include + +class Device: public QObject +{ + Q_OBJECT + Q_PROPERTY(QUuid id READ id CONSTANT) + +public: + Device(QObject *parent = 0); + + QUuid id() const; + +private: + QUuid m_id; +}; + +#endif diff --git a/libhive/deviceclass.cpp b/libhive/deviceclass.cpp new file mode 100644 index 00000000..3bdcde2d --- /dev/null +++ b/libhive/deviceclass.cpp @@ -0,0 +1,18 @@ +#include "deviceclass.h" + +DeviceClass::DeviceClass(const QUuid &id, QObject *parent): + QObject(parent), + m_id(id) +{ + +} + +DeviceClass::~DeviceClass() +{ + +} + +QUuid DeviceClass::id() const +{ + return m_id; +} diff --git a/libhive/deviceclass.h b/libhive/deviceclass.h new file mode 100644 index 00000000..9b832f7d --- /dev/null +++ b/libhive/deviceclass.h @@ -0,0 +1,22 @@ +#ifndef DEVICECLASS_H +#define DEVICECLASS_H + +#include +#include + +class DeviceClass: public QObject +{ + Q_OBJECT + Q_PROPERTY(QUuid id READ id CONSTANT) + +public: + DeviceClass(const QUuid &id, QObject *parent = 0); + virtual ~DeviceClass(); + + virtual QUuid id() const; + +private: + QUuid m_id; +}; + +#endif diff --git a/libhive/deviceplugin.cpp b/libhive/deviceplugin.cpp new file mode 100644 index 00000000..c74acecc --- /dev/null +++ b/libhive/deviceplugin.cpp @@ -0,0 +1,12 @@ +#include "deviceplugin.h" + +DevicePlugin::~DevicePlugin() +{ + +} + + +DevicePlugin::DevicePlugin(QObject *parent) +{ + +} diff --git a/libhive/deviceplugin.h b/libhive/deviceplugin.h new file mode 100644 index 00000000..95d9e696 --- /dev/null +++ b/libhive/deviceplugin.h @@ -0,0 +1,18 @@ +#ifndef DEVICEPLUGIN_H +#define DEVICEPLUGIN_H + +#include "deviceclass.h" + +#include + +class DevicePlugin: public QObject +{ + Q_OBJECT +public: + DevicePlugin(QObject *parent = 0); + virtual ~DevicePlugin(); + + virtual QList supportedDevices() const = 0; +}; + +#endif diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt new file mode 100644 index 00000000..87d8ecd0 --- /dev/null +++ b/server/CMakeLists.txt @@ -0,0 +1,17 @@ +include_directories( + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_SOURCE_DIR}/libhive +) + +set(hive_SRCS + main.cpp + hivecore.cpp + jsonrpcserver.cpp + tcpserver.cpp + radio433.cpp +) + +add_executable(hive ${hive_SRCS}) +target_link_libraries(hive libhive) + +qt5_use_modules(hive Network) diff --git a/server/hivecore.cpp b/server/hivecore.cpp new file mode 100644 index 00000000..ee4e118b --- /dev/null +++ b/server/hivecore.cpp @@ -0,0 +1,50 @@ +#include "hivecore.h" +#include "jsonrpcserver.h" + +#include "device.h" +#include "deviceclass.h" + +#include + +HiveCore* HiveCore::s_instance = 0; + +HiveCore *HiveCore::instance() +{ + if (!s_instance) { + s_instance = new HiveCore(); + } + return s_instance; +} + +QList HiveCore::devices() const +{ + return m_devices; +} + +HiveCore::HiveCore(QObject *parent) : + QObject(parent) +{ + // create a fake device + m_devices.append(new Device(this)); + + + // start the server + m_jsonServer = new JsonRPCServer(this); + + + + +// // create 433.92 MHz sender +// m_sender = new RadioSender(this); +// m_sender->setFrequency(RadioSender::RF433MHz); +// m_sender->setLineCode(RadioSender::SWITCH); +// m_sender->setPulseLength(320); +//// //m_sender->sendBin("000000000000010101010001"); + +// // create 433.92 MHz receiver +// m_reciver = new RadioReciver(this); +// m_reciver->setFrequency(RadioReciver::RF433MHz); +// m_reciver->setPin(2); +// m_reciver->enableReceiver(); + +} diff --git a/server/hivecore.h b/server/hivecore.h new file mode 100644 index 00000000..27ae72a1 --- /dev/null +++ b/server/hivecore.h @@ -0,0 +1,44 @@ +#ifndef HIVECORE_H +#define HIVECORE_H + +#include +//#include "server.h" +//#include "devicemanager.h" +//#include +//#include "radio/radioreciver.h" +//#include "radio/radiosender.h" + +class JsonRPCServer; +class Device; +class DeviceClass; + +class HiveCore : public QObject +{ + Q_OBJECT +public: + static HiveCore* instance(); + + QList devices() const; + +private: + explicit HiveCore(QObject *parent = 0); + static HiveCore *s_instance; + + JsonRPCServer *m_jsonServer; + +// Server *m_server; +// RadioReciver *m_reciver; +// RadioSender *m_sender; +// DeviceManager *m_deviceManager; +// JsonHandler *m_jsonHandler; + +signals: + +public slots: + +private: + QList m_supportedDevices; + QList m_devices; +}; + +#endif // HIVECORE_H diff --git a/server/jsonrpcserver.cpp b/server/jsonrpcserver.cpp new file mode 100644 index 00000000..f2cc0a8d --- /dev/null +++ b/server/jsonrpcserver.cpp @@ -0,0 +1,41 @@ +#include "jsonrpcserver.h" + +#include "tcpserver.h" + +#include + +JsonRPCServer::JsonRPCServer(QObject *parent): + m_tcpServer(new TcpServer(this)) +{ + connect(m_tcpServer, &TcpServer::jsonDataAvailable, this, &JsonRPCServer::processData); + m_tcpServer->startServer(); +} + +void JsonRPCServer::processData(const QByteArray &jsonData) +{ + QJsonParseError error; + QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &error); + + if(error.error != QJsonParseError::NoError) { + qDebug() << "failed to parse data" << jsonData << ":" << error.errorString(); + } + qDebug() << "-------------------------\n" << jsonDoc.toJson(); + + QVariantMap command = jsonDoc.toVariant().toMap(); + QVariantMap params = jsonDoc.toVariant().toMap().value("params").toMap(); + + //DeviceJsonPlugin plugin; + // {: "name", : "doBla", : "int", { : "name", ... }} + +// if(command.value("device").toString() == m_device->deviceName()){ +// return m_device->process(command,params); +// } +// if(command.value("device").toString() == m_radio->deviceName()){ +// return m_radio->process(command,params); +// }else{ +// return NULL; +// } + +} + + diff --git a/server/jsonrpcserver.h b/server/jsonrpcserver.h new file mode 100644 index 00000000..89bb6763 --- /dev/null +++ b/server/jsonrpcserver.h @@ -0,0 +1,23 @@ +#ifndef JSONRPCSERVER_H +#define JSONRPCSERVER_H + +#include + +class TcpServer; + +class JsonRPCServer: public QObject +{ + Q_OBJECT +public: + JsonRPCServer(QObject *parent = 0); + +signals: + +private slots: + void processData(const QByteArray &jsonData); + +private: + TcpServer *m_tcpServer; +}; + +#endif diff --git a/server/main.cpp b/server/main.cpp new file mode 100644 index 00000000..692d95bd --- /dev/null +++ b/server/main.cpp @@ -0,0 +1,11 @@ +#include +#include + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + HiveCore::instance(); + + return a.exec(); +} diff --git a/server/radio433.cpp b/server/radio433.cpp new file mode 100644 index 00000000..f2185759 --- /dev/null +++ b/server/radio433.cpp @@ -0,0 +1,10 @@ +#include "radio433.h" + +Radio433::Radio433(QObject *parent) +{ + +} + +void Radio433::sendData(QList rawData) +{ +} diff --git a/server/radio433.h b/server/radio433.h new file mode 100644 index 00000000..2cb119ce --- /dev/null +++ b/server/radio433.h @@ -0,0 +1,20 @@ +#ifndef RADIO433_H +#define RADIO433_h + +#include + +class Radio433: public QObject +{ + Q_OBJECT + +public: + Radio433(QObject *parent = 0); + +public: + void sendData(QList rawData); + +signals: + void dataReceived(QList rawData); +}; + +#endif diff --git a/server/tcpserver.cpp b/server/tcpserver.cpp new file mode 100644 index 00000000..44f37617 --- /dev/null +++ b/server/tcpserver.cpp @@ -0,0 +1,100 @@ +#include "tcpserver.h" +#include +#include + +TcpServer::TcpServer(QObject *parent) : + QObject(parent) +{ + + qDebug() << "----------------------------"; + qDebug() << "network interfaces:"; + foreach(const QNetworkInterface &interface, QNetworkInterface::allInterfaces()){ + qDebug() << " -------------------------"; + qDebug() << " name:" << interface.name(); + qDebug() << " mac: " << interface.hardwareAddress(); + } + qDebug() << "----------------------------"; + +} + +void TcpServer::newClientConnected() +{ + // got a new client connected + QTcpServer *server = qobject_cast(sender()); + QTcpSocket *newConnection = server->nextPendingConnection(); + qDebug() << "new client connected:" << newConnection->peerAddress().toString(); + + // append the new client to the client list + m_clientList.append(newConnection); + + connect(newConnection, SIGNAL(readyRead()),this,SLOT(readPackage())); + connect(newConnection,SIGNAL(disconnected()),this,SLOT(clientDisconnected())); + +} + + +void TcpServer::readPackage() +{ + QTcpSocket *client = qobject_cast(sender()); + qDebug() << "-----------> data comming from" << client->peerAddress().toString(); + QByteArray message; + while(client->canReadLine()){ + QByteArray dataLine = client->readLine(); + qDebug() << "line in:" << dataLine; + message.append(dataLine); + if(dataLine.endsWith("}\r")){ + qDebug() << message; + emit jsonDataAvailable(message); + message.clear(); + } + } +} + +void TcpServer::clientDisconnected() +{ + QTcpSocket *client = qobject_cast(sender()); + qDebug() << "client disconnected:" << client->peerAddress().toString(); +} + +bool TcpServer::startServer() +{ + // Listen on all Networkinterfaces + foreach(const QHostAddress &address, QNetworkInterface::allAddresses()){ + QTcpServer *server = new QTcpServer(this); + if(server->listen(address, 1234)) { + qDebug() << "server listening on" << address.toString(); + connect(server, SIGNAL(newConnection()), SLOT(newClientConnected())); + m_serverList.append(server); + } else { + qDebug() << "ERROR: can not listening to" << address.toString(); + delete server; + } + } + if(m_serverList.empty()){ + return false; + } + return true; +} + +bool TcpServer::stopServer() +{ + // Listen on all Networkinterfaces + foreach(QTcpServer *server, m_serverList){ + qDebug() << "close server " << server->serverAddress().toString(); + server->close(); + delete server; + } + if(!m_serverList.empty()){ + return false; + } + return true; +} + +void TcpServer::sendToAll(QByteArray data) +{ + foreach(QTcpSocket *client,m_clientList){ + client->write(data); + } +} + + diff --git a/server/tcpserver.h b/server/tcpserver.h new file mode 100644 index 00000000..70f002e8 --- /dev/null +++ b/server/tcpserver.h @@ -0,0 +1,36 @@ +#ifndef TCPSERVER_H +#define TCPSERVER_H + +#include +#include +#include +#include + +class TcpServer : public QObject +{ + Q_OBJECT +public: + explicit TcpServer(QObject *parent = 0); + +private: + QList m_serverList; + QList m_clientList; + + +signals: + void jsonDataAvailable(const QByteArray &data); + +private slots: + void newClientConnected(); + void readPackage(); + void clientDisconnected(); + +public slots: + bool startServer(); + bool stopServer(); + void sendToAll(QByteArray data); + + +}; + +#endif // TCPSERVER_H