mirror of https://github.com/nymea/nymea.git
some more work on the whole structure
parent
62601d3a72
commit
9d60f7f2ba
|
|
@ -1,7 +1,6 @@
|
|||
#include "deviceclass.h"
|
||||
|
||||
DeviceClass::DeviceClass(const QUuid &id, QObject *parent):
|
||||
QObject(parent),
|
||||
DeviceClass::DeviceClass(const QUuid &id):
|
||||
m_id(id)
|
||||
{
|
||||
|
||||
|
|
@ -16,3 +15,13 @@ QUuid DeviceClass::id() const
|
|||
{
|
||||
return m_id;
|
||||
}
|
||||
|
||||
QString DeviceClass::name() const
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
void DeviceClass::setName(const QString &name)
|
||||
{
|
||||
m_name = name;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,22 +1,22 @@
|
|||
#ifndef DEVICECLASS_H
|
||||
#define DEVICECLASS_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QUuid>
|
||||
|
||||
class DeviceClass: public QObject
|
||||
class DeviceClass
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QUuid id READ id CONSTANT)
|
||||
|
||||
public:
|
||||
DeviceClass(const QUuid &id, QObject *parent = 0);
|
||||
DeviceClass(const QUuid &id);
|
||||
virtual ~DeviceClass();
|
||||
|
||||
virtual QUuid id() const;
|
||||
QUuid id() const;
|
||||
|
||||
QString name() const;
|
||||
void setName(const QString &name);
|
||||
|
||||
private:
|
||||
QUuid m_id;
|
||||
QString m_name;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ public:
|
|||
virtual ~DevicePlugin();
|
||||
|
||||
virtual QList<DeviceClass> supportedDevices() const = 0;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
@ -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
|
||||
|
|
@ -1,8 +1,6 @@
|
|||
#include "hivecore.h"
|
||||
#include "jsonrpcserver.h"
|
||||
|
||||
#include "device.h"
|
||||
#include "deviceclass.h"
|
||||
#include "devicemanager.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
|
|
@ -16,127 +14,19 @@ HiveCore *HiveCore::instance()
|
|||
return s_instance;
|
||||
}
|
||||
|
||||
QList<Device *> HiveCore::devices() const
|
||||
DeviceManager *HiveCore::deviceManager() const
|
||||
{
|
||||
return m_devices;
|
||||
return m_deviceManager;
|
||||
}
|
||||
|
||||
HiveCore::HiveCore(QObject *parent) :
|
||||
QObject(parent)
|
||||
{
|
||||
// create a fake device
|
||||
m_devices.append(new Device(this));
|
||||
|
||||
|
||||
m_deviceManager = new DeviceManager(this);
|
||||
|
||||
// start the server
|
||||
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);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,40 +2,24 @@
|
|||
#define HIVECORE_H
|
||||
|
||||
#include <QObject>
|
||||
#include <radio433.h>
|
||||
|
||||
class JsonRPCServer;
|
||||
class Device;
|
||||
class DeviceClass;
|
||||
class DeviceManager;
|
||||
|
||||
class HiveCore : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
static HiveCore* instance();
|
||||
|
||||
QList<Device*> devices() const;
|
||||
|
||||
DeviceManager* deviceManager() const;
|
||||
|
||||
private:
|
||||
explicit HiveCore(QObject *parent = 0);
|
||||
static HiveCore *s_instance;
|
||||
|
||||
JsonRPCServer *m_jsonServer;
|
||||
Radio433 *m_radio433;
|
||||
|
||||
// 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;
|
||||
DeviceManager *m_deviceManager;
|
||||
};
|
||||
|
||||
#endif // HIVECORE_H
|
||||
|
|
|
|||
|
|
@ -2,16 +2,22 @@
|
|||
|
||||
#include "tcpserver.h"
|
||||
|
||||
#include "hivecore.h"
|
||||
#include "devicemanager.h"
|
||||
#include "deviceclass.h"
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QStringList>
|
||||
|
||||
JsonRPCServer::JsonRPCServer(QObject *parent):
|
||||
QObject(parent),
|
||||
m_tcpServer(new TcpServer(this))
|
||||
{
|
||||
connect(m_tcpServer, &TcpServer::jsonDataAvailable, this, &JsonRPCServer::processData);
|
||||
m_tcpServer->startServer();
|
||||
}
|
||||
|
||||
void JsonRPCServer::processData(const QByteArray &jsonData)
|
||||
void JsonRPCServer::processData(int clientId, const QByteArray &jsonData)
|
||||
{
|
||||
QJsonParseError error;
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &error);
|
||||
|
|
@ -21,8 +27,42 @@ void JsonRPCServer::processData(const QByteArray &jsonData)
|
|||
}
|
||||
qDebug() << "-------------------------\n" << jsonDoc.toJson();
|
||||
|
||||
QVariantMap command = jsonDoc.toVariant().toMap();
|
||||
QVariantMap params = jsonDoc.toVariant().toMap().value("params").toMap();
|
||||
QVariantMap message = jsonDoc.toVariant().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;
|
||||
// {<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 ¶ms)
|
||||
{
|
||||
QVariantMap rsp;
|
||||
rsp.insert("id", commandId);
|
||||
rsp.insert("params", params);
|
||||
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromVariant(rsp);
|
||||
m_tcpServer->sendResponse(clientId, jsonDoc.toJson());
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
#ifndef JSONRPCSERVER_H
|
||||
#define JSONRPCSERVER_H
|
||||
|
||||
#include "deviceclass.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QVariantMap>
|
||||
#include <QString>
|
||||
|
||||
class TcpServer;
|
||||
|
||||
|
|
@ -12,9 +16,15 @@ public:
|
|||
JsonRPCServer(QObject *parent = 0);
|
||||
|
||||
signals:
|
||||
void commandReceived(const QString &targetNamespace, const QString &command, const QVariantMap ¶ms);
|
||||
|
||||
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 ¶ms);
|
||||
|
||||
private:
|
||||
TcpServer *m_tcpServer;
|
||||
|
|
|
|||
|
|
@ -15,10 +15,14 @@ SOURCES += main.cpp \
|
|||
jsonrpcserver.cpp \
|
||||
radio433.cpp \
|
||||
tcpserver.cpp \
|
||||
gpio.cpp
|
||||
gpio.cpp \
|
||||
deviceplugins/rfswitch/rfswitch.cpp \
|
||||
devicemanager.cpp \
|
||||
|
||||
HEADERS += hivecore.h \
|
||||
jsonrpcserver.h \
|
||||
radio433.h \
|
||||
tcpserver.h \
|
||||
gpio.h
|
||||
gpio.h \
|
||||
deviceplugins/rfswitch/rfswitch.h \
|
||||
devicemanager.h \
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
{
|
||||
// got a new client connected
|
||||
|
|
@ -25,7 +33,7 @@ void TcpServer::newClientConnected()
|
|||
qDebug() << "new client connected:" << newConnection->peerAddress().toString();
|
||||
|
||||
// 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(disconnected()),this,SLOT(clientDisconnected()));
|
||||
|
|
@ -42,9 +50,8 @@ void TcpServer::readPackage()
|
|||
QByteArray dataLine = client->readLine();
|
||||
qDebug() << "line in:" << dataLine;
|
||||
message.append(dataLine);
|
||||
if(dataLine.endsWith("}\r")){
|
||||
qDebug() << message;
|
||||
emit jsonDataAvailable(message);
|
||||
if(dataLine.endsWith('\n')){
|
||||
emit jsonDataAvailable(m_clientList.key(client), message);
|
||||
message.clear();
|
||||
}
|
||||
}
|
||||
|
|
@ -64,7 +71,7 @@ bool TcpServer::startServer()
|
|||
if(server->listen(address, 1234)) {
|
||||
qDebug() << "server listening on" << address.toString();
|
||||
connect(server, SIGNAL(newConnection()), SLOT(newClientConnected()));
|
||||
m_serverList.append(server);
|
||||
m_serverList.insert(m_serverList.count(), server);
|
||||
} else {
|
||||
qDebug() << "ERROR: can not listening to" << address.toString();
|
||||
delete server;
|
||||
|
|
|
|||
|
|
@ -12,13 +12,14 @@ class TcpServer : public QObject
|
|||
public:
|
||||
explicit TcpServer(QObject *parent = 0);
|
||||
|
||||
private:
|
||||
QList<QTcpServer*> m_serverList;
|
||||
QList<QTcpSocket*> m_clientList;
|
||||
void sendResponse(int clientId, const QByteArray &data);
|
||||
|
||||
private:
|
||||
QHash<int, QTcpServer*> m_serverList;
|
||||
QHash<int, QTcpSocket*> m_clientList;
|
||||
|
||||
signals:
|
||||
void jsonDataAvailable(const QByteArray &data);
|
||||
void jsonDataAvailable(int clientId, const QByteArray &data);
|
||||
|
||||
private slots:
|
||||
void newClientConnected();
|
||||
|
|
@ -29,8 +30,6 @@ public slots:
|
|||
bool startServer();
|
||||
bool stopServer();
|
||||
void sendToAll(QByteArray data);
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // TCPSERVER_H
|
||||
|
|
|
|||
Loading…
Reference in New Issue