diff --git a/libnymea-core/hardware/zigbee/zigbeehardwareresourceimplementation.cpp b/libnymea-core/hardware/zigbee/zigbeehardwareresourceimplementation.cpp index 3a3974c5..802977ed 100644 --- a/libnymea-core/hardware/zigbee/zigbeehardwareresourceimplementation.cpp +++ b/libnymea-core/hardware/zigbee/zigbeehardwareresourceimplementation.cpp @@ -28,7 +28,7 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ -#include "zigbeemanagerimplementation.h" +#include "zigbeehardwareresourceimplementation.h" #include "loggingcategories.h" #include "nymeasettings.h" diff --git a/libnymea-core/jsonrpc/zigbeehandler.cpp b/libnymea-core/jsonrpc/zigbeehandler.cpp index 46387a8f..c9481ac1 100644 --- a/libnymea-core/jsonrpc/zigbeehandler.cpp +++ b/libnymea-core/jsonrpc/zigbeehandler.cpp @@ -29,40 +29,70 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include "zigbeehandler.h" +#include "zigbee/zigbeemanager.h" namespace nymeaserver { -ZigbeeHandler::ZigbeeHandler(QObject *parent) : - JsonHandler(parent) +ZigbeeHandler::ZigbeeHandler(ZigbeeManager *zigbeeManager, QObject *parent) : + JsonHandler(parent), + m_zigbeeManager(zigbeeManager) { registerEnum(); registerEnum(); + registerObject(); + registerObject(); + QVariantMap params, returns; QString description; + /* 1. GetNetworkStatus + * 2. Setup network if the is no network configured + * - GetUartInterfaces + * - Setup network with given UART interface and backend type + * - + */ + params.clear(); returns.clear(); description = "Get the status of the current network."; returns.insert("available", enumValueName(Bool)); returns.insert("enabled", enumValueName(Bool)); returns.insert("configured", enumValueName(Bool)); - returns.insert("state", enumRef()); + returns.insert("serialPort", enumValueName(String)); + returns.insert("baudRate", enumValueName(Uint)); + returns.insert("backend", enumRef()); + returns.insert("firmwareVersion", enumValueName(String)); + returns.insert("networkIeeeeAddress", enumValueName(String)); + returns.insert("networkPanId", enumValueName(Uint)); + returns.insert("channel", enumValueName(Uint)); + returns.insert("permitJoin", enumValueName(Bool)); + returns.insert("networkState", enumRef()); registerMethod("GetNetworkStatus", description, params, returns); - // GetUartInterfaces params.clear(); returns.clear(); description = "Get the available UART interfaces in order to set up the zigbee network on the approriate serial interface."; - returns.insert("available", enumValueName(Bool)); - returns.insert("enabled", enumValueName(Bool)); - returns.insert("configured", enumValueName(Bool)); - returns.insert("state", enumRef()); + returns.insert("uartInterfaces", objectRef()); registerMethod("GetUartInterfaces", description, params, returns); + // GetNetworkStatus + // NetworkStatusChanged + // SetupNetwork(network, uart, baudrate, backendtype) + // PermitJoin(time, o:nwk address) + // FactoryResetNetwork - // Setup network, uart, baudrate, backendtype - + // GetNodes [Node(mac, nwkAddress, name, manufacturer, type, descriptors, associacted device, state, reachable)] + // NodeAdded + // NodeChanged (state, node base information, associated deviceId) + // RemoveNode + // NodeRemoved + // GetGroups + // GroupChanged + // AddGroup + // GroupAdded + // RemoveGroup + // GroupRemoved } @@ -71,4 +101,36 @@ QString ZigbeeHandler::name() const return "Zigbee"; } +JsonReply *ZigbeeHandler::GetNetworkStatus(const QVariantMap ¶ms) +{ + Q_UNUSED(params) + + QVariantMap ret; +// if (m_zigbeeManager->zigbeeNetwork()) { +// ret.insert("available", m_zigbeeManager->available()); +// ret.insert("enabled", m_zigbeeManager->enabled()); +// ret.insert("configured", true); +// ret.insert("networkState", m_zigbeeManager->zigbeeNetwork()->state()); +// } else { +// ret.insert("available", m_zigbeeManager->available()); +// ret.insert("enabled", m_zigbeeManager->enabled()); +// ret.insert("configured", false); +// ret.insert("state", ZigbeeNetwork::StateUninitialized); +// } + + return createReply(ret); +} + +JsonReply *ZigbeeHandler::GetUartInterfaces(const QVariantMap ¶ms) +{ + Q_UNUSED(params) + QVariantMap ret; + QVariantList portList; + foreach (const ZigbeeSerialPort &serialPort, m_zigbeeManager->availablePorts()) { + portList << pack(serialPort); + } + ret.insert("uartInterfaces", portList); + return createReply(ret); +} + } diff --git a/libnymea-core/jsonrpc/zigbeehandler.h b/libnymea-core/jsonrpc/zigbeehandler.h index d0ce3786..7b4b502e 100644 --- a/libnymea-core/jsonrpc/zigbeehandler.h +++ b/libnymea-core/jsonrpc/zigbeehandler.h @@ -39,14 +39,23 @@ namespace nymeaserver { +class ZigbeeManager; + class ZigbeeHandler : public JsonHandler { Q_OBJECT public: - explicit ZigbeeHandler(QObject *parent = nullptr); + explicit ZigbeeHandler(ZigbeeManager *zigbeeManager, QObject *parent = nullptr); QString name() const override; + Q_INVOKABLE JsonReply *GetNetworkStatus(const QVariantMap ¶ms); + Q_INVOKABLE JsonReply *GetUartInterfaces(const QVariantMap ¶ms); + + +private: + ZigbeeManager *m_zigbeeManager = nullptr; + signals: }; diff --git a/libnymea-core/libnymea-core.pro b/libnymea-core/libnymea-core.pro index f27cf711..74b8f417 100644 --- a/libnymea-core/libnymea-core.pro +++ b/libnymea-core/libnymea-core.pro @@ -3,7 +3,7 @@ TARGET = nymea-core include(../nymea.pri) -QT += sql qml +QT += sql qml serialport INCLUDEPATH += $$top_srcdir/libnymea $$top_builddir LIBS += -L$$top_builddir/libnymea/ -lnymea -lssl -lcrypto @@ -129,7 +129,8 @@ HEADERS += nymeacore.h \ cloud/cloudtransport.h \ debugreportgenerator.h \ platform/platform.h \ \ - zigbee/zigbeemanager.h + zigbee/zigbeemanager.h \ + zigbee/zigbeeserialport.h SOURCES += nymeacore.cpp \ @@ -214,7 +215,8 @@ SOURCES += nymeacore.cpp \ cloud/cloudtransport.cpp \ debugreportgenerator.cpp \ platform/platform.cpp \ \ - zigbee/zigbeemanager.cpp + zigbee/zigbeemanager.cpp \ + zigbee/zigbeeserialport.cpp versionAtLeast(QT_VERSION, 5.12.0) { diff --git a/libnymea-core/zigbee/zigbeemanager.cpp b/libnymea-core/zigbee/zigbeemanager.cpp index 562a8bc7..701b8a7e 100644 --- a/libnymea-core/zigbee/zigbeemanager.cpp +++ b/libnymea-core/zigbee/zigbeemanager.cpp @@ -29,6 +29,10 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include "zigbeemanager.h" +#include "nymeasettings.h" +#include "loggingcategories.h" + +#include namespace nymeaserver { @@ -42,9 +46,58 @@ bool ZigbeeManager::available() const return m_zigbeeNetwork != nullptr; } -void ZigbeeManager::setupNetwork(const QString serialPort, qint32 baudrate, ZigbeeNetworkManager::BackendType) +bool ZigbeeManager::enabled() const { + return m_zigbeeNetwork && m_zigbeeNetwork->state() != ZigbeeNetwork::StateUninitialized; +} +ZigbeeNetwork *ZigbeeManager::zigbeeNetwork() const +{ + return m_zigbeeNetwork; +} + +ZigbeeSerialPortList ZigbeeManager::availablePorts() +{ + ZigbeeSerialPortList ports; + foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()) { + qCDebug(dcZigbee()) << "Found serial port" << serialPortInfo.portName(); + qCDebug(dcZigbee()) << " Description:" << serialPortInfo.description(); + qCDebug(dcZigbee()) << " System location:" << serialPortInfo.systemLocation(); + qCDebug(dcZigbee()) << " Manufacturer:" << serialPortInfo.manufacturer(); + qCDebug(dcZigbee()) << " Serialnumber:" << serialPortInfo.serialNumber(); + + if (serialPortInfo.hasProductIdentifier()) { + qCDebug(dcZigbee()) << " Product identifier:" << serialPortInfo.productIdentifier(); + } + if (serialPortInfo.hasVendorIdentifier()) { + qCDebug(dcZigbee()) << " Vendor identifier:" << serialPortInfo.vendorIdentifier(); + } + + ZigbeeSerialPort port; + port.setName(serialPortInfo.portName()); + port.setDescription(serialPortInfo.description()); + port.setSystemLocation(serialPortInfo.systemLocation()); + ports.append(port); + } + + return ports; +} + +void ZigbeeManager::createZigbeeNetwork(const QString &serialPort, qint32 baudrate, ZigbeeNetworkManager::BackendType backend) +{ + if (m_zigbeeNetwork) { + delete m_zigbeeNetwork; + m_zigbeeNetwork = nullptr; + } + + m_zigbeeNetwork = ZigbeeNetworkManager::createZigbeeNetwork(backend, this); + m_zigbeeNetwork->setSerialPortName(serialPort); + m_zigbeeNetwork->setSerialBaudrate(baudrate); + m_zigbeeNetwork->setSettingsFileName(NymeaSettings(NymeaSettings::SettingsRoleGlobal).fileName()); + + m_zigbeeNetwork->startNetwork(); + + emit zigbeeNetworkChanged(m_zigbeeNetwork); } } diff --git a/libnymea-core/zigbee/zigbeemanager.h b/libnymea-core/zigbee/zigbeemanager.h index ed871fbc..02ab7ad4 100644 --- a/libnymea-core/zigbee/zigbeemanager.h +++ b/libnymea-core/zigbee/zigbeemanager.h @@ -35,6 +35,8 @@ #include +#include "zigbeeserialport.h" + namespace nymeaserver { class ZigbeeManager : public QObject @@ -44,8 +46,13 @@ public: explicit ZigbeeManager(QObject *parent = nullptr); bool available() const; + bool enabled() const; - void setupNetwork(const QString serialPort, qint32 baudrate, ZigbeeNetworkManager::BackendType); + ZigbeeNetwork *zigbeeNetwork() const; + + ZigbeeSerialPortList availablePorts(); + + void createZigbeeNetwork(const QString &serialPort, qint32 baudrate, ZigbeeNetworkManager::BackendType backend); private: ZigbeeNetwork *m_zigbeeNetwork = nullptr; diff --git a/libnymea-core/zigbee/zigbeeserialport.cpp b/libnymea-core/zigbee/zigbeeserialport.cpp new file mode 100644 index 00000000..6b2b12d7 --- /dev/null +++ b/libnymea-core/zigbee/zigbeeserialport.cpp @@ -0,0 +1,80 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +* +* Copyright 2013 - 2020, nymea GmbH +* Contact: contact@nymea.io +* +* This file is part of nymea. +* This project including source code and documentation is protected by +* copyright law, and remains the property of nymea GmbH. All rights, including +* reproduction, publication, editing and translation, are reserved. The use of +* this project is subject to the terms of a license agreement to be concluded +* with nymea GmbH in accordance with the terms of use of nymea GmbH, available +* under https://nymea.io/license +* +* GNU General Public License Usage +* Alternatively, this project may be redistributed and/or modified under the +* terms of the GNU General Public License as published by the Free Software +* Foundation, GNU version 3. This project is distributed in the hope that it +* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty +* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General +* Public License for more details. +* +* You should have received a copy of the GNU General Public License along with +* this project. If not, see . +* +* For any further details and any questions please contact us under +* contact@nymea.io or see our FAQ/Licensing Information on +* https://nymea.io/license/faq +* +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "zigbeeserialport.h" + +namespace nymeaserver { + +ZigbeeSerialPort::ZigbeeSerialPort() +{ + +} + +QString ZigbeeSerialPort::name() const +{ + return m_name; +} + +void ZigbeeSerialPort::setName(const QString &name) +{ + m_name = name; +} + +QString ZigbeeSerialPort::description() const +{ + return m_description; +} + +void ZigbeeSerialPort::setDescription(const QString &description) +{ + m_description = description; +} + +QString ZigbeeSerialPort::systemLocation() const +{ + return m_systemLocation; +} + +void ZigbeeSerialPort::setSystemLocation(const QString &systemLocation) +{ + m_systemLocation = systemLocation; +} + +QVariant ZigbeeSerialPortList::get(int index) const +{ + return QVariant::fromValue(at(index)); +} + +void ZigbeeSerialPortList::put(const QVariant &variant) +{ + append(variant.value()); +} + +} diff --git a/libnymea-core/zigbee/zigbeeserialport.h b/libnymea-core/zigbee/zigbeeserialport.h new file mode 100644 index 00000000..34397bec --- /dev/null +++ b/libnymea-core/zigbee/zigbeeserialport.h @@ -0,0 +1,77 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +* +* Copyright 2013 - 2020, nymea GmbH +* Contact: contact@nymea.io +* +* This file is part of nymea. +* This project including source code and documentation is protected by +* copyright law, and remains the property of nymea GmbH. All rights, including +* reproduction, publication, editing and translation, are reserved. The use of +* this project is subject to the terms of a license agreement to be concluded +* with nymea GmbH in accordance with the terms of use of nymea GmbH, available +* under https://nymea.io/license +* +* GNU General Public License Usage +* Alternatively, this project may be redistributed and/or modified under the +* terms of the GNU General Public License as published by the Free Software +* Foundation, GNU version 3. This project is distributed in the hope that it +* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty +* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General +* Public License for more details. +* +* You should have received a copy of the GNU General Public License along with +* this project. If not, see . +* +* For any further details and any questions please contact us under +* contact@nymea.io or see our FAQ/Licensing Information on +* https://nymea.io/license/faq +* +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef ZIGBEESERIALPORT_H +#define ZIGBEESERIALPORT_H + +#include +#include +namespace nymeaserver { + +class ZigbeeSerialPort +{ + Q_GADGET + Q_PROPERTY(QString name READ name) + Q_PROPERTY(QString description READ description) + Q_PROPERTY(QString systemLocation READ systemLocation) + +public: + ZigbeeSerialPort(); + + QString name() const; + void setName(const QString &name); + + QString description() const; + void setDescription(const QString &description); + + QString systemLocation() const; + void setSystemLocation(const QString &systemLocation); + +private: + QString m_name; + QString m_description; + QString m_systemLocation; +}; + +class ZigbeeSerialPortList: public QList +{ + Q_GADGET + Q_PROPERTY(int count READ count) +public: + Q_INVOKABLE QVariant get(int index) const; + Q_INVOKABLE void put(const QVariant &variant); +}; + +} + +Q_DECLARE_METATYPE(nymeaserver::ZigbeeSerialPort) +Q_DECLARE_METATYPE(nymeaserver::ZigbeeSerialPortList) + +#endif // ZIGBEESERIALPORT_H