import new codebase

pull/1/head
Michael Zanetti 2013-12-30 16:10:03 +01:00
parent 424fa8f9d9
commit f481333dee
18 changed files with 484 additions and 0 deletions

15
CMakeLists.txt Normal file
View File

@ -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)

14
libhive/CMakeLists.txt Normal file
View File

@ -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)

12
libhive/device.cpp Normal file
View File

@ -0,0 +1,12 @@
#include "device.h"
Device::Device(QObject *parent):
QObject(parent)
{
}
QUuid Device::id() const
{
return m_id;
}

21
libhive/device.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef DEVICE_H
#define DEVICE_H
#include <QObject>
#include <QUuid>
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

18
libhive/deviceclass.cpp Normal file
View File

@ -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;
}

22
libhive/deviceclass.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef DEVICECLASS_H
#define DEVICECLASS_H
#include <QObject>
#include <QUuid>
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

12
libhive/deviceplugin.cpp Normal file
View File

@ -0,0 +1,12 @@
#include "deviceplugin.h"
DevicePlugin::~DevicePlugin()
{
}
DevicePlugin::DevicePlugin(QObject *parent)
{
}

18
libhive/deviceplugin.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef DEVICEPLUGIN_H
#define DEVICEPLUGIN_H
#include "deviceclass.h"
#include <QObject>
class DevicePlugin: public QObject
{
Q_OBJECT
public:
DevicePlugin(QObject *parent = 0);
virtual ~DevicePlugin();
virtual QList<DeviceClass> supportedDevices() const = 0;
};
#endif

17
server/CMakeLists.txt Normal file
View File

@ -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)

50
server/hivecore.cpp Normal file
View File

@ -0,0 +1,50 @@
#include "hivecore.h"
#include "jsonrpcserver.h"
#include "device.h"
#include "deviceclass.h"
#include <QDebug>
HiveCore* HiveCore::s_instance = 0;
HiveCore *HiveCore::instance()
{
if (!s_instance) {
s_instance = new HiveCore();
}
return s_instance;
}
QList<Device *> 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();
}

44
server/hivecore.h Normal file
View File

@ -0,0 +1,44 @@
#ifndef HIVECORE_H
#define HIVECORE_H
#include <QObject>
//#include "server.h"
//#include "devicemanager.h"
//#include <jsonhandler.h>
//#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<Device*> 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<DeviceClass*> m_supportedDevices;
QList<Device*> m_devices;
};
#endif // HIVECORE_H

41
server/jsonrpcserver.cpp Normal file
View File

@ -0,0 +1,41 @@
#include "jsonrpcserver.h"
#include "tcpserver.h"
#include <QJsonDocument>
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;
// {<device>: "name", <method>: "doBla", <id>: "int", <command> { <name>: "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;
// }
}

23
server/jsonrpcserver.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef JSONRPCSERVER_H
#define JSONRPCSERVER_H
#include <QObject>
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

11
server/main.cpp Normal file
View File

@ -0,0 +1,11 @@
#include <QCoreApplication>
#include <hivecore.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
HiveCore::instance();
return a.exec();
}

10
server/radio433.cpp Normal file
View File

@ -0,0 +1,10 @@
#include "radio433.h"
Radio433::Radio433(QObject *parent)
{
}
void Radio433::sendData(QList<int> rawData)
{
}

20
server/radio433.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef RADIO433_H
#define RADIO433_h
#include <QObject>
class Radio433: public QObject
{
Q_OBJECT
public:
Radio433(QObject *parent = 0);
public:
void sendData(QList<int> rawData);
signals:
void dataReceived(QList<int> rawData);
};
#endif

100
server/tcpserver.cpp Normal file
View File

@ -0,0 +1,100 @@
#include "tcpserver.h"
#include <QDebug>
#include <QJsonDocument>
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<QTcpServer*>(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<QTcpSocket*>(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<QTcpSocket*>(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);
}
}

36
server/tcpserver.h Normal file
View File

@ -0,0 +1,36 @@
#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <QObject>
#include <QNetworkInterface>
#include <QTcpServer>
#include <QTcpSocket>
class TcpServer : public QObject
{
Q_OBJECT
public:
explicit TcpServer(QObject *parent = 0);
private:
QList<QTcpServer*> m_serverList;
QList<QTcpSocket*> 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