forst gui for client to test communication

This commit is contained in:
Simon Stuerz 2013-08-31 10:44:35 +02:00
parent 94580e3925
commit a37f282f6f
22 changed files with 476 additions and 90 deletions

View File

@ -15,7 +15,13 @@ QML_IMPORT_PATH =
# The .cpp file which was generated for your project. Feel free to hack it.
SOURCES += main.cpp \
hiveclientcore.cpp
hiveclientcore.cpp \
settings.cpp
HEADERS += \
hiveclientcore.h \
settings.h
# Installation path
# target.path =
@ -24,9 +30,6 @@ SOURCES += main.cpp \
include(qtquick2applicationviewer/qtquick2applicationviewer.pri)
qtcAddDeployment()
HEADERS += \
hiveclientcore.h
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../libhive/release/ -llibhive

View File

@ -7,12 +7,53 @@ HiveClientCore::HiveClientCore(QObject *parent) :
QObject(parent)
{
m_id = 0;
m_settings = new Settings(this);
m_client = new Client(this);
m_client->connectToHost("10.10.10.40","1234");
m_viewer = new QtQuick2ApplicationViewer;
m_viewer->setMainQmlFile(QStringLiteral("qml/hive_client/main.qml"));
m_viewer->showExpanded();
// QML application window
m_engine = new QQmlApplicationEngine(this);
m_engine->load(QUrl("qml/hive_client/main.qml"));
m_engine->rootContext()->setContextProperty("client", m_client);
m_engine->rootContext()->setContextProperty("settings", m_settings);
topLevel = m_engine->rootObjects().value(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
window->show();
connect(m_client,SIGNAL(connected()),this,SLOT(onConnected()));
}
void HiveClientCore::onConnected()
{
//sendSomething("device", "getAll");
sendSomething("device", "add");
}
void HiveClientCore::sendSomething(QString deviceName, QString method)
{
QVariantMap responseMap;
responseMap.insert("device", deviceName);
responseMap.insert("method", method);
responseMap.insert("id", m_id++);
QVariantMap params;
params.insert("deviceType","actor");
params.insert("name","Schreibtisch On");
params.insert("protocol","RF433");
params.insert("linecode","remote");
params.insert("code","000000000000010101010001");
responseMap.insert("params", params);
QJsonDocument doc = QJsonDocument::fromVariant(responseMap);
qDebug() << "_______________________________________";
qDebug() << "send to hive:";
QByteArray json = doc.toJson();
qDebug() << json;
m_client->sendData(json);
}

View File

@ -2,8 +2,11 @@
#define HIVECLIENTCORE_H
#include <QObject>
#include "qtquick2applicationviewer.h"
#include <QQmlApplicationEngine>
#include <QQuickWindow>
#include "client.h"
#include "settings.h"
class HiveClientCore : public QObject
{
@ -13,9 +16,16 @@ public:
private:
Client *m_client;
Settings *m_settings;
QQmlApplicationEngine *m_engine;
QObject *topLevel;
QtQuick2ApplicationViewer *m_viewer;
int m_id;
private slots:
void onConnected();
void sendSomething(QString deviceName, QString method);
signals:

View File

@ -1,6 +1,4 @@
#include <QtGui/QGuiApplication>
#include <QQmlEngine>
#include <QQmlComponent>
#include "hiveclientcore.h"
int main(int argc, char *argv[])

View File

@ -2,51 +2,103 @@ import QtQuick 2.0
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.0
ApplicationWindow{
id: mainItem
title: "Hive Client"
width: 600
height: 360
// ColumnLayout {
// id: mainLayout
// anchors.fill: parent
// GroupBox{
// id: connectionBox
// title: "Connection"
// anchors.horizontalCenter: parent.horizontalCenter
// RowLayout{
// Button{
// id: connectionButton
// text: "Connect"
// }
// TextInput{
// id: ipText
// text: "10.10.10.40"
// }
// TextInput{
// id: portText
// text: "1234"
// }
// }
// }
// }
StatusBar{
id: mainWindow
menuBar: MenuBar{
id: mainWindowMenuBar
Menu {
title: "File"
MenuItem {
text: "Close"
shortcut: "Ctrl+Q"
onTriggered: mainWindow.close()
}
}
}
statusBar: StatusBar {
id: statusBar
width: parent.width
anchors.right: parent.right
anchors.left: parent.left
anchors.bottom: parent.bottom
anchors.horizontalCenter: parent.horizontalCenter
}
title: "Hive Client"
width: 600
height: 500
GroupBox {
id: connectionGroupBox
anchors.right: parent.right
anchors.left: parent.left
anchors.margins: 20
title: "Connection"
RowLayout{
anchors.fill: parent
Button{
id: connectButton
text: "Connect"
onClicked: {
settings.setIPaddress(ipTextInput.text)
settings.setPort(portTextInput.text)
client.connectToHost(ipTextInput.text,portTextInput.text)
}
}
Text {
id: ipLable
text: "IP:"
horizontalAlignment: Text.AlignHCenter
}
TextField {
id: ipTextInput
text: "10.10.10.40"
horizontalAlignment: TextInput.AlignHCenter
font.pointSize: 9
}
Text {
id: portLable
text: "Port:"
wrapMode: Text.NoWrap
verticalAlignment: Text.AlignTop
horizontalAlignment: Text.AlignHCenter
}
TextField {
id: portTextInput
text: "1234"
horizontalAlignment: TextInput.AlignHCenter
font.pointSize: 9
}
}
}
TabView{
id: tabView
anchors.right: parent.right
anchors.rightMargin: 20
anchors.left: parent.left
anchors.leftMargin: 20
anchors.top: connectionGroupBox.bottom
anchors.bottom: statusBar.top
Tab{
id: actorTab
title: "Actor"
}
Tab{
id: sensorTab
title: "Sensor"
}
Tab{
id: ruleTab
title: "Rules"
}
Tab{
id: settingsTab
title: "Settings"
}
}
}

View File

@ -1,8 +0,0 @@
import QtQuick 2.0
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.0
import QtQuick.Window 2.0
Rectangle{
}

View File

@ -0,0 +1,31 @@
#include "settings.h"
#include <QDebug>
Settings::Settings(QObject *parent) :
QObject(parent)
{
m_settings = new QSettings(QString(),"hiveClient",this);
}
QString Settings::ipaddress()
{
return m_settings->value("server","10.10.10.40").toString();
}
void Settings::setIPaddress(QString ipaddress)
{
m_settings->setValue("server",ipaddress);
emit ipaddressChanged();
}
QString Settings::port()
{
return m_settings->value("port","1234").toString();
}
void Settings::setPort(QString port)
{
m_settings->setValue("port",port);
emit ipaddressChanged();
}

View File

@ -0,0 +1,32 @@
#ifndef SETTINGS_H
#define SETTINGS_H
#include <QObject>
#include <QSettings>
class Settings : public QObject
{
Q_OBJECT
public:
explicit Settings(QObject *parent = 0);
private:
QSettings* m_settings;
signals:
void ipaddressChanged();
void portChanged();
public slots:
QString ipaddress();
QString port();
void setIPaddress(QString ipaddress);
void setPort(QString port);
};
#endif // SETTINGS_H

View File

@ -1,5 +1,6 @@
#include "client.h"
#include <QDebug>
#include <QJsonDocument>
Client::Client(QObject *parent) :
QObject(parent)
@ -18,16 +19,26 @@ void Client::connectionError(QAbstractSocket::SocketError error)
void Client::readData()
{
QByteArray message;
while(m_tcpSocket->canReadLine()){
QByteArray dataLine = m_tcpSocket->readLine();
message.append(dataLine);
if(dataLine == "}\n"){
emit jsonDataAvailable(message);
message.clear();
}
}
}
void Client::connectedToHost()
{
qDebug() << "connected to hive server";
emit connected();
}
void Client::connectToHost(QString ipAddress, QString port)
{
qDebug() << "connecting to" << ipAddress << ":" << port;
m_tcpSocket->connectToHost(QHostAddress(ipAddress), port.toInt());
}
@ -37,7 +48,7 @@ void Client::disconnectFromHost()
qDebug() << "connection to hive server closed";
}
void Client::sendData(QString target, QString command)
void Client::sendData(QByteArray data)
{
m_tcpSocket->write(data);
}

View File

@ -8,7 +8,7 @@
class Client : public QObject
{
Q_OBJECT
//Q_PROPERTY(QString ipAddress READ ipAddress WRITE setIpAddress NOTIFY ipAddressChanged)
public:
explicit Client(QObject *parent = 0);
@ -19,6 +19,9 @@ private:
signals:
void connected();
void jsonDataAvailable(const QByteArray &data);
private slots:
void connectionError(QAbstractSocket::SocketError error);
@ -28,7 +31,7 @@ private slots:
public slots:
void connectToHost(QString ipAddress, QString port);
void disconnectFromHost();
void sendData(QString target, QString command);
void sendData(QByteArray data);
};
#endif // CLIENT_H

View File

@ -7,9 +7,11 @@
JsonHandler::JsonHandler(QObject *parent) :
QObject(parent)
{
m_device = new DeviceJsonPlugin(this);
}
QByteArray JsonHandler::process(const QByteArray &data)
{
QJsonParseError error;
@ -18,8 +20,43 @@ QByteArray JsonHandler::process(const QByteArray &data)
if(error.error != QJsonParseError::NoError) {
qDebug() << "failed to parse data" << data << ":" << 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);
}else{
return NULL;
}
}
QByteArray JsonHandler::formatResponse(const QVariantMap &command, const QVariantMap &responseParams)
{
QVariantMap responseMap;
responseMap.insert("id", command.value("id"));
responseMap.insert("success", true);
responseMap.insert("params", responseParams);
QByteArray response = QJsonDocument::fromVariant(responseMap).toBinaryData();
response.append(QString('\n'));
qDebug() << "ERROR response: " << response;
return response;
}
QByteArray JsonHandler::formatErrorResponse(const QVariantMap &command, const QString &error)
{
QVariantMap responseMap;
responseMap.insert("id", command.value("id"));
responseMap.insert("success", false);
responseMap.insert("error", error);
QByteArray response = QJsonDocument::fromVariant(responseMap).toBinaryData();
response.append(QString('\n'));
qDebug() << "ERROR response: " << response;
return response;
}

View File

@ -2,6 +2,9 @@
#define JSONHANDLER_H
#include <QObject>
#include <QVariant>
#include <jsonplugin/jsonplugin.h>
#include <jsonplugin/devicejsonplugin.h>
class JsonHandler : public QObject
@ -10,9 +13,6 @@ class JsonHandler : public QObject
public:
explicit JsonHandler(QObject *parent = 0);
private:
signals:
void notifyAll(const QByteArray &data);
@ -20,6 +20,13 @@ signals:
public slots:
QByteArray process(const QByteArray &data);
private:
DeviceJsonPlugin *m_device;
QByteArray formatResponse(const QVariantMap &command, const QVariantMap &responseParams);
QByteArray formatErrorResponse(const QVariantMap &command, const QString &error);
};

View File

@ -0,0 +1,75 @@
#include "devicejsonplugin.h"
#include <QDebug>
#include <QUuid>
#include <QSettings>
DeviceJsonPlugin::DeviceJsonPlugin(QObject *parent) :
JsonPlugin(parent)
{
m_deviceManager = new DeviceManager(this);
}
QString DeviceJsonPlugin::deviceName()
{
QString deviceName = "device";
return deviceName;
}
QByteArray DeviceJsonPlugin::process(const QVariantMap &command, const QVariantMap &parameters)
{
// check if we have a id
if(!command.contains("id")){
qDebug() << "request contains no id..";
}
// check the methods
if(command.value("method").toString() == "add"){
qDebug() << "got a ADD DEVICE command";
add(parameters);
}
return 0;
}
void DeviceJsonPlugin::add(QVariantMap parameters)
{
QUuid uuid = QUuid::createUuid();
//qDebug() << uuid;
if(parameters.value("deviceType").toString() == "actor"){
QSettings settings("hive");
settings.beginGroup(uuid.toString());
QMapIterator<QString, QVariant> i(parameters);
while(i.hasNext()){
i.next();
qDebug() << i.key() << "=" << i.value().toString();
settings.setValue(i.key(),i.value());
}
settings.endGroup();
}
}
void DeviceJsonPlugin::remove()
{
}
void DeviceJsonPlugin::editValue(QString value, QVariant key)
{
}
void DeviceJsonPlugin::getAll()
{
}
QByteArray DeviceJsonPlugin::formatResponse()
{
}
QByteArray DeviceJsonPlugin::formatErrorResponse()
{
}

View File

@ -0,0 +1,32 @@
#ifndef DEVICEJSONPLUGIN_H
#define DEVICEJSONPLUGIN_H
#include <QObject>
#include <QVariant>
#include <jsonplugin/jsonplugin.h>
#include <devicemanager.h>
class DeviceJsonPlugin : public JsonPlugin
{
public:
explicit DeviceJsonPlugin(QObject *parent = 0);
QString deviceName();
QByteArray process(const QVariantMap &command, const QVariantMap &parameters);
private:
DeviceManager *m_deviceManager;
void add(QVariantMap parameters);
void remove();
void editValue(QString value, QVariant key);
void getAll();
QByteArray formatResponse();
QByteArray formatErrorResponse();
signals:
};
#endif // DEVICEJSONPLUGIN_H

View File

@ -0,0 +1,7 @@
#include "jsonplugin.h"
JsonPlugin::JsonPlugin(QObject *parent) :
QObject(parent)
{
}

View File

@ -0,0 +1,22 @@
#ifndef JSONPLUGIN_H
#define JSONPLUGIN_H
#include <QObject>
class JsonPlugin : public QObject
{
Q_OBJECT
public:
explicit JsonPlugin(QObject *parent = 0);
//virtual ~JsonPlugin();
virtual QString deviceName() = 0;
virtual QByteArray process(const QVariantMap & command, const QVariantMap & parameters) = 0;
signals:
public slots:
};
#endif // JSONPLUGIN_H

View File

@ -19,7 +19,9 @@ SOURCES += libhive.cpp \
devicemanager.cpp \
logwriter.cpp \
client.cpp \
jsonhandler.cpp
jsonhandler.cpp \
jsonplugin/jsonplugin.cpp \
jsonplugin/devicejsonplugin.cpp
HEADERS += libhive.h\
libhive_global.h \
@ -27,7 +29,9 @@ HEADERS += libhive.h\
devicemanager.h \
logwriter.h \
client.h \
jsonhandler.h
jsonhandler.h \
jsonplugin/jsonplugin.h \
jsonplugin/devicejsonplugin.h
#unix:!symbian {
# maemo5 {

View File

@ -1,5 +1,6 @@
#include "server.h"
#include <QDebug>
#include <QJsonDocument>
Server::Server(QObject *parent) :
QObject(parent)
@ -26,7 +27,7 @@ void Server::newClientConnected()
// append the new client to the client list
m_clientList.append(newConnection);
connect(newConnection, SIGNAL(readyRead()), SLOT(readPackage()));
connect(newConnection, SIGNAL(readyRead()),this,SLOT(readPackage()));
connect(newConnection,SIGNAL(disconnected()),this,SLOT(clientDisconnected()));
}
@ -35,10 +36,17 @@ void Server::newClientConnected()
void Server::readPackage()
{
QTcpSocket *client = qobject_cast<QTcpSocket*>(sender());
//qDebug() << "-----------> data comming from" << client->peerAddress().toString();
QByteArray message;
while(client->canReadLine()){
QByteArray data = client->readLine();
qDebug() << "data to parse:" << data.remove(data.length() - 1, 1);
emit dataLineAvailable(data);
QByteArray dataLine = client->readLine();
//qDebug() << "line in:" << dataLine;
message.append(dataLine);
if(dataLine == "}\n"){
//qDebug() << message;
emit jsonDataAvailable(message);
message.clear();
}
}
}

View File

@ -18,7 +18,7 @@ private:
signals:
void dataLineAvailable(const QByteArray &dataLine);
void jsonDataAvailable(const QByteArray &data);
private slots:
void newClientConnected();

View File

@ -9,20 +9,26 @@ HiveCore::HiveCore(QObject *parent) :
m_server->startServer();
m_deviceManager = new DeviceManager(this);
m_deviceManager->getDevices();
m_jsonHandler = new JsonHandler(this);
// create 433.92 MHz sender
m_sender = new RadioSender(this);
m_sender->setFrequency(RadioSender::RF433MHz);
m_sender->setLineCode(RadioSender::REMOTE);
m_sender->setPulseLength(320);
//m_sender->sendBin("000000000000010101010001");
connect(m_server,SIGNAL(jsonDataAvailable(QByteArray)),m_jsonHandler,SLOT(process(QByteArray)));
// create 433.92 MHz receiver
m_reciver = new RadioReciver(this);
m_reciver->setFrequency(RadioReciver::RF433MHz);
m_reciver->setPin(2);
m_reciver->enableReceiver();
// // create 433.92 MHz sender
// m_sender = new RadioSender(this);
// m_sender->setFrequency(RadioSender::RF433MHz);
// m_sender->setLineCode(RadioSender::REMOTE);
// 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();
}

View File

@ -4,6 +4,7 @@
#include <QObject>
#include "server.h"
#include "devicemanager.h"
#include <jsonhandler.h>
#include "radio/radioreciver.h"
#include "radio/radiosender.h"
@ -15,9 +16,11 @@ public:
private:
Server *m_server;
RadioReciver *m_reciver;
RadioSender *m_sender;
DeviceManager *m_deviceManager;
//RadioReciver *m_reciver;
//RadioSender *m_sender;
//DeviceManager *m_deviceManager;
JsonHandler *m_jsonHandler;
signals:
public slots:

View File

@ -182,8 +182,20 @@ void RadioReciver::detectProtocol(QList<int> rawData)
qDebug() <<"VALID SIGNAL (48 bit)" << " --> pulse width =" << rawData.first()/31;
qDebug() << rawData;
// int pulseWidth = rawData.first()/31;
// QList<int> rawDataKGV;
// foreach (int timing, rawData){
// int kgv = ((float)timing/pulseWidth)+0.5;
// rawDataKGV.append(kgv);
// }
// qDebug() << "-------= " << rawDataKGV;
}
//void RadioReciver::detectProtocol(int signalCount)
//{
// if(signalCount < 49){
// //qDebug() << "ERROR: got a signal with just" << signalCount << "signals";
// return;