some more work on the whole structure

This commit is contained in:
Michael Zanetti 2013-12-30 20:04:50 +01:00
parent 62601d3a72
commit 9d60f7f2ba
12 changed files with 192 additions and 161 deletions

View File

@ -1,7 +1,6 @@
#include "deviceclass.h" #include "deviceclass.h"
DeviceClass::DeviceClass(const QUuid &id, QObject *parent): DeviceClass::DeviceClass(const QUuid &id):
QObject(parent),
m_id(id) m_id(id)
{ {
@ -16,3 +15,13 @@ QUuid DeviceClass::id() const
{ {
return m_id; return m_id;
} }
QString DeviceClass::name() const
{
return m_name;
}
void DeviceClass::setName(const QString &name)
{
m_name = name;
}

View File

@ -1,22 +1,22 @@
#ifndef DEVICECLASS_H #ifndef DEVICECLASS_H
#define DEVICECLASS_H #define DEVICECLASS_H
#include <QObject>
#include <QUuid> #include <QUuid>
class DeviceClass: public QObject class DeviceClass
{ {
Q_OBJECT
Q_PROPERTY(QUuid id READ id CONSTANT)
public: public:
DeviceClass(const QUuid &id, QObject *parent = 0); DeviceClass(const QUuid &id);
virtual ~DeviceClass(); virtual ~DeviceClass();
virtual QUuid id() const; QUuid id() const;
QString name() const;
void setName(const QString &name);
private: private:
QUuid m_id; QUuid m_id;
QString m_name;
}; };
#endif #endif

View File

@ -13,6 +13,7 @@ public:
virtual ~DevicePlugin(); virtual ~DevicePlugin();
virtual QList<DeviceClass> supportedDevices() const = 0; virtual QList<DeviceClass> supportedDevices() const = 0;
}; };
#endif #endif

35
server/devicemanager.cpp Normal file
View File

@ -0,0 +1,35 @@
#include "devicemanager.h"
#include "radio433.h"
#include "device.h"
#include "deviceclass.h"
#include "deviceplugin.h"
#include "deviceplugins/rfswitch/rfswitch.h"
DeviceManager::DeviceManager(QObject *parent) :
QObject(parent)
{
m_radio433 = new Radio433(this);
// TODO: load dynamically
RfSwitch *rfSwitch = new RfSwitch(this);
m_supportedDevices.append(rfSwitch->supportedDevices());
m_devicePlugins.append(rfSwitch);
}
QList<DeviceClass> DeviceManager::supportedDevices()
{
return m_supportedDevices;
}
QList<Device *> DeviceManager::devices() const
{
return m_devices;
}

34
server/devicemanager.h Normal file
View File

@ -0,0 +1,34 @@
#ifndef DEVICEMANAGER_H
#define DEVICEMANAGER_H
#include "deviceclass.h"
#include <QObject>
class Device;
class DevicePlugin;
class Radio433;
class DeviceManager : public QObject
{
Q_OBJECT
public:
explicit DeviceManager(QObject *parent = 0);
QList<DeviceClass> supportedDevices();
QList<Device*> devices() const;
signals:
public slots:
private:
QList<DeviceClass> m_supportedDevices;
QList<Device*> m_devices;
QList<DevicePlugin*> m_devicePlugins;
Radio433* m_radio433;
};
#endif // DEVICEMANAGER_H

View File

@ -1,8 +1,6 @@
#include "hivecore.h" #include "hivecore.h"
#include "jsonrpcserver.h" #include "jsonrpcserver.h"
#include "devicemanager.h"
#include "device.h"
#include "deviceclass.h"
#include <QDebug> #include <QDebug>
@ -16,127 +14,19 @@ HiveCore *HiveCore::instance()
return s_instance; return s_instance;
} }
QList<Device *> HiveCore::devices() const DeviceManager *HiveCore::deviceManager() const
{ {
return m_devices; return m_deviceManager;
} }
HiveCore::HiveCore(QObject *parent) : HiveCore::HiveCore(QObject *parent) :
QObject(parent) QObject(parent)
{ {
// create a fake device
m_devices.append(new Device(this));
m_deviceManager = new DeviceManager(this);
// start the server // start the server
m_jsonServer = new JsonRPCServer(this); m_jsonServer = new JsonRPCServer(this);
m_radio433 = new Radio433(this);
//==============================================
int pulseLenght = 350;
QList<int> signal;
//sync
signal.append(pulseLenght);
signal.append(pulseLenght*31);
//==============================================
// 0
signal.append(pulseLenght);
signal.append(pulseLenght*3);
// 1
signal.append(pulseLenght*3);
signal.append(pulseLenght);
// 0
signal.append(pulseLenght);
signal.append(pulseLenght*3);
// 1
signal.append(pulseLenght*3);
signal.append(pulseLenght);
// 0
signal.append(pulseLenght);
signal.append(pulseLenght*3);
// 1
signal.append(pulseLenght*3);
signal.append(pulseLenght);
// 0
signal.append(pulseLenght);
signal.append(pulseLenght*3);
// 1
signal.append(pulseLenght*3);
signal.append(pulseLenght);
// 0
signal.append(pulseLenght);
signal.append(pulseLenght*3);
// 1
signal.append(pulseLenght*3);
signal.append(pulseLenght);
//==============================================
// 0
signal.append(pulseLenght);
signal.append(pulseLenght*3);
// 1
signal.append(pulseLenght*3);
signal.append(pulseLenght);
// 0
signal.append(pulseLenght);
signal.append(pulseLenght*3);
// 1
signal.append(pulseLenght*3);
signal.append(pulseLenght);
// 0
signal.append(pulseLenght);
signal.append(pulseLenght*3);
// 1
signal.append(pulseLenght*3);
signal.append(pulseLenght);
// 0
signal.append(pulseLenght);
signal.append(pulseLenght*3);
// 0
signal.append(pulseLenght);
signal.append(pulseLenght*3);
// 0
signal.append(pulseLenght);
signal.append(pulseLenght*3);
// 1
signal.append(pulseLenght*3);
signal.append(pulseLenght);
//==============================================
// 0
signal.append(pulseLenght);
signal.append(pulseLenght*3);
// 1
signal.append(pulseLenght*3);
signal.append(pulseLenght);
// 0
signal.append(pulseLenght);
signal.append(pulseLenght*3);
// 0
signal.append(pulseLenght);
signal.append(pulseLenght*3);
//==============================================
qDebug() << "sendsignal";
m_radio433->sendData(signal);
m_radio433->sendData(signal);
m_radio433->sendData(signal);
m_radio433->sendData(signal);
} }

View File

@ -2,40 +2,24 @@
#define HIVECORE_H #define HIVECORE_H
#include <QObject> #include <QObject>
#include <radio433.h>
class JsonRPCServer; class JsonRPCServer;
class Device; class DeviceManager;
class DeviceClass;
class HiveCore : public QObject class HiveCore : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
static HiveCore* instance(); static HiveCore* instance();
QList<Device*> devices() const; DeviceManager* deviceManager() const;
private: private:
explicit HiveCore(QObject *parent = 0); explicit HiveCore(QObject *parent = 0);
static HiveCore *s_instance; static HiveCore *s_instance;
JsonRPCServer *m_jsonServer; JsonRPCServer *m_jsonServer;
Radio433 *m_radio433; DeviceManager *m_deviceManager;
// 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 #endif // HIVECORE_H

View File

@ -2,16 +2,22 @@
#include "tcpserver.h" #include "tcpserver.h"
#include "hivecore.h"
#include "devicemanager.h"
#include "deviceclass.h"
#include <QJsonDocument> #include <QJsonDocument>
#include <QStringList>
JsonRPCServer::JsonRPCServer(QObject *parent): JsonRPCServer::JsonRPCServer(QObject *parent):
QObject(parent),
m_tcpServer(new TcpServer(this)) m_tcpServer(new TcpServer(this))
{ {
connect(m_tcpServer, &TcpServer::jsonDataAvailable, this, &JsonRPCServer::processData); connect(m_tcpServer, &TcpServer::jsonDataAvailable, this, &JsonRPCServer::processData);
m_tcpServer->startServer(); m_tcpServer->startServer();
} }
void JsonRPCServer::processData(const QByteArray &jsonData) void JsonRPCServer::processData(int clientId, const QByteArray &jsonData)
{ {
QJsonParseError error; QJsonParseError error;
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &error); QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &error);
@ -21,8 +27,42 @@ void JsonRPCServer::processData(const QByteArray &jsonData)
} }
qDebug() << "-------------------------\n" << jsonDoc.toJson(); qDebug() << "-------------------------\n" << jsonDoc.toJson();
QVariantMap command = jsonDoc.toVariant().toMap(); QVariantMap message = jsonDoc.toVariant().toMap();
QVariantMap params = jsonDoc.toVariant().toMap().value("params").toMap();
bool success;
int commandId = message.value("id").toInt(&success);
if (!success) {
qWarning() << "Error parsing command. Missing \"id\":" << jsonData;
return;
}
QStringList commandList = message.value("method").toString().split('.');
if (commandList.count() != 2) {
qWarning() << "Error parsing method.\nGot:" << message.value("method").toString() << "\nExpected: \"Namespace.method\"";
return;
}
QString targetNamspace = commandList.first();
QString method = commandList.last();
QVariantMap params = message.value("params").toMap();
qDebug() << "got:" << targetNamspace << method << params;
emit commandReceived(targetNamspace, method, params);
if (targetNamspace == "Devices") {
if (method == "GetSupportedDevices") {
qDebug() << "getSupportedDevices";
QVariantMap params;
QVariantList supportedDeviceList;
foreach (const DeviceClass &deviceClass, HiveCore::instance()->deviceManager()->supportedDevices()) {
supportedDeviceList.append(packDeviceClass(deviceClass));
}
params.insert("deviceClasses", supportedDeviceList);
sendResponse(clientId, commandId, params);
}
} else {
qDebug() << "got unknown namespace" << targetNamspace;
}
//DeviceJsonPlugin plugin; //DeviceJsonPlugin plugin;
// {<device>: "name", <method>: "doBla", <id>: "int", <command> { <name>: "name", ... }} // {<device>: "name", <method>: "doBla", <id>: "int", <command> { <name>: "name", ... }}
@ -38,4 +78,22 @@ void JsonRPCServer::processData(const QByteArray &jsonData)
} }
QVariantMap JsonRPCServer::packDeviceClass(const DeviceClass &deviceClass)
{
QVariantMap variant;
variant.insert("name", deviceClass.name());
variant.insert("id", deviceClass.id());
return variant;
}
void JsonRPCServer::sendResponse(int clientId, int commandId, const QVariantMap &params)
{
QVariantMap rsp;
rsp.insert("id", commandId);
rsp.insert("params", params);
QJsonDocument jsonDoc = QJsonDocument::fromVariant(rsp);
m_tcpServer->sendResponse(clientId, jsonDoc.toJson());
}

View File

@ -1,7 +1,11 @@
#ifndef JSONRPCSERVER_H #ifndef JSONRPCSERVER_H
#define JSONRPCSERVER_H #define JSONRPCSERVER_H
#include "deviceclass.h"
#include <QObject> #include <QObject>
#include <QVariantMap>
#include <QString>
class TcpServer; class TcpServer;
@ -12,9 +16,15 @@ public:
JsonRPCServer(QObject *parent = 0); JsonRPCServer(QObject *parent = 0);
signals: signals:
void commandReceived(const QString &targetNamespace, const QString &command, const QVariantMap &params);
private slots: private slots:
void processData(const QByteArray &jsonData); void processData(int clientId, const QByteArray &jsonData);
private:
QVariantMap packDeviceClass(const DeviceClass &deviceClass);
void sendResponse(int clientId, int commandId, const QVariantMap &params);
private: private:
TcpServer *m_tcpServer; TcpServer *m_tcpServer;

View File

@ -15,10 +15,14 @@ SOURCES += main.cpp \
jsonrpcserver.cpp \ jsonrpcserver.cpp \
radio433.cpp \ radio433.cpp \
tcpserver.cpp \ tcpserver.cpp \
gpio.cpp gpio.cpp \
deviceplugins/rfswitch/rfswitch.cpp \
devicemanager.cpp \
HEADERS += hivecore.h \ HEADERS += hivecore.h \
jsonrpcserver.h \ jsonrpcserver.h \
radio433.h \ radio433.h \
tcpserver.h \ tcpserver.h \
gpio.h gpio.h \
deviceplugins/rfswitch/rfswitch.h \
devicemanager.h \

View File

@ -17,6 +17,14 @@ TcpServer::TcpServer(QObject *parent) :
} }
void TcpServer::sendResponse(int clientId, const QByteArray &data)
{
QTcpSocket *client = m_clientList.value(clientId);
if (client) {
client->write(data);
}
}
void TcpServer::newClientConnected() void TcpServer::newClientConnected()
{ {
// got a new client connected // got a new client connected
@ -25,7 +33,7 @@ void TcpServer::newClientConnected()
qDebug() << "new client connected:" << newConnection->peerAddress().toString(); qDebug() << "new client connected:" << newConnection->peerAddress().toString();
// append the new client to the client list // append the new client to the client list
m_clientList.append(newConnection); m_clientList.insert(m_clientList.count(), newConnection);
connect(newConnection, SIGNAL(readyRead()),this,SLOT(readPackage())); connect(newConnection, SIGNAL(readyRead()),this,SLOT(readPackage()));
connect(newConnection,SIGNAL(disconnected()),this,SLOT(clientDisconnected())); connect(newConnection,SIGNAL(disconnected()),this,SLOT(clientDisconnected()));
@ -42,9 +50,8 @@ void TcpServer::readPackage()
QByteArray dataLine = client->readLine(); QByteArray dataLine = client->readLine();
qDebug() << "line in:" << dataLine; qDebug() << "line in:" << dataLine;
message.append(dataLine); message.append(dataLine);
if(dataLine.endsWith("}\r")){ if(dataLine.endsWith('\n')){
qDebug() << message; emit jsonDataAvailable(m_clientList.key(client), message);
emit jsonDataAvailable(message);
message.clear(); message.clear();
} }
} }
@ -64,7 +71,7 @@ bool TcpServer::startServer()
if(server->listen(address, 1234)) { if(server->listen(address, 1234)) {
qDebug() << "server listening on" << address.toString(); qDebug() << "server listening on" << address.toString();
connect(server, SIGNAL(newConnection()), SLOT(newClientConnected())); connect(server, SIGNAL(newConnection()), SLOT(newClientConnected()));
m_serverList.append(server); m_serverList.insert(m_serverList.count(), server);
} else { } else {
qDebug() << "ERROR: can not listening to" << address.toString(); qDebug() << "ERROR: can not listening to" << address.toString();
delete server; delete server;

View File

@ -12,13 +12,14 @@ class TcpServer : public QObject
public: public:
explicit TcpServer(QObject *parent = 0); explicit TcpServer(QObject *parent = 0);
private: void sendResponse(int clientId, const QByteArray &data);
QList<QTcpServer*> m_serverList;
QList<QTcpSocket*> m_clientList;
private:
QHash<int, QTcpServer*> m_serverList;
QHash<int, QTcpSocket*> m_clientList;
signals: signals:
void jsonDataAvailable(const QByteArray &data); void jsonDataAvailable(int clientId, const QByteArray &data);
private slots: private slots:
void newClientConnected(); void newClientConnected();
@ -29,8 +30,6 @@ public slots:
bool startServer(); bool startServer();
bool stopServer(); bool stopServer();
void sendToAll(QByteArray data); void sendToAll(QByteArray data);
}; };
#endif // TCPSERVER_H #endif // TCPSERVER_H