Make backend type dynamic and introduce GetAvailableBackends

This commit is contained in:
Simon Stürz 2020-11-18 12:21:40 +01:00
parent 2e6f68bc41
commit e2cbffeebd
5 changed files with 76 additions and 43 deletions

View File

@ -42,12 +42,8 @@ ZigbeeHandler::ZigbeeHandler(ZigbeeManager *zigbeeManager, QObject *parent) :
m_zigbeeManager(zigbeeManager) m_zigbeeManager(zigbeeManager)
{ {
qRegisterMetaType<nymeaserver::ZigbeeAdapter>(); qRegisterMetaType<nymeaserver::ZigbeeAdapter>();
qRegisterMetaType<nymeaserver::ZigbeeAdapter::ZigbeeBackendType>();
registerEnum<ZigbeeManager::ZigbeeNetworkState>(); registerEnum<ZigbeeManager::ZigbeeNetworkState>();
registerEnum<ZigbeeAdapter::ZigbeeBackendType>();
registerEnum<ZigbeeManager::ZigbeeError>(); registerEnum<ZigbeeManager::ZigbeeError>();
registerObject<ZigbeeAdapter, ZigbeeAdapters>(); registerObject<ZigbeeAdapter, ZigbeeAdapters>();
// Network object describing a network instance // Network object describing a network instance
@ -64,54 +60,56 @@ ZigbeeHandler::ZigbeeHandler(ZigbeeManager *zigbeeManager, QObject *parent) :
zigbeeNetworkDescription.insert("permitJoiningEnabled", enumValueName(Bool)); zigbeeNetworkDescription.insert("permitJoiningEnabled", enumValueName(Bool));
zigbeeNetworkDescription.insert("permitJoiningDuration", enumValueName(Uint)); zigbeeNetworkDescription.insert("permitJoiningDuration", enumValueName(Uint));
zigbeeNetworkDescription.insert("permitJoiningRemaining", enumValueName(Uint)); zigbeeNetworkDescription.insert("permitJoiningRemaining", enumValueName(Uint));
zigbeeNetworkDescription.insert("backendType", enumRef<ZigbeeAdapter::ZigbeeBackendType>()); zigbeeNetworkDescription.insert("backend", enumValueName(String));
zigbeeNetworkDescription.insert("networkState", enumRef<ZigbeeManager::ZigbeeNetworkState>()); zigbeeNetworkDescription.insert("networkState", enumRef<ZigbeeManager::ZigbeeNetworkState>());
registerObject("ZigbeeNetwork", zigbeeNetworkDescription); registerObject("ZigbeeNetwork", zigbeeNetworkDescription);
QVariantMap params, returns; QVariantMap params, returns;
QString description; QString description;
// GetAvailableBackends
params.clear(); returns.clear();
description = "Get the list of available Zigbee backends.";
returns.insert("backends", QVariantList() << enumValueName(String));
registerMethod("GetAvailableBackends", description, params, returns);
// GetAdapters // GetAdapters
params.clear(); returns.clear(); params.clear(); returns.clear();
description = "Get the list of available Zigbee adapter candidates in order to set up the zigbee network " description = "Get the list of available Zigbee adapters and serial ports in order to set up the Zigbee network "
"on the desired serial interface. The serialPort property can be used as unique identifier. " "on the desired interface. The \'serialPort\' property can be used as unique identifier for an adapter. "
"If an adapter hardware has been recognized as a supported " "If an adapter hardware has been recognized as a well known Zigbee adapter, "
"hardware, the \'hardwareRecognized\' property will be true and the baud rate and backend " "the \'hardwareRecognized\' property will be true and the \'baudRate\' and \'backend\' "
"configurations can be used as they where given, otherwise the user might set the backend " "configurations can be used as they where given, otherwise the user might set the backend "
"type and baud rate manually."; "and baud rate manually. The available backends can be fetched using the GetAvailableBackends method.";
returns.insert("adapters", objectRef<ZigbeeAdapters>()); returns.insert("adapters", objectRef<ZigbeeAdapters>());
registerMethod("GetAdapters", description, params, returns); registerMethod("GetAdapters", description, params, returns);
// AdapterAdded notification // AdapterAdded notification
params.clear(); params.clear();
description = "Emitted whenever a new Zigbee adapter candidate has been detected in the system."; description = "Emitted whenever a new Zigbee adapter or serial port has been detected in the system.";
params.insert("adapter", objectRef<ZigbeeAdapter>()); params.insert("adapter", objectRef<ZigbeeAdapter>());
registerNotification("AdapterAdded", description, params); registerNotification("AdapterAdded", description, params);
// AdapterRemoved notification // AdapterRemoved notification
params.clear(); params.clear();
description = "Emitted whenever a Zigbee adapter has been removed from the system (i.e. unplugged)."; description = "Emitted whenever a Zigbee adapter or serial port has been removed from the system (i.e. unplugged).";
params.insert("adapter", objectRef<ZigbeeAdapter>()); params.insert("adapter", objectRef<ZigbeeAdapter>());
registerNotification("AdapterRemoved", description, params); registerNotification("AdapterRemoved", description, params);
// GetNetworks // GetNetworks
params.clear(); returns.clear(); params.clear(); returns.clear();
description = "Returns the list of zigbee networks configured in the system."; description = "Returns the list of configured Zigbee networks in the system.";
returns.insert("zigbeeNetworks", QVariantList() << objectRef("ZigbeeNetwork")); returns.insert("zigbeeNetworks", QVariantList() << objectRef("ZigbeeNetwork"));
registerMethod("GetNetworks", description, params, returns); registerMethod("GetNetworks", description, params, returns);
// AddNetwork // AddNetwork
params.clear(); returns.clear(); params.clear(); returns.clear();
description = "Create a new Zigbee network for the given serialPort, baud rate and backend type. " description = "Create a new Zigbee network for the given \'serialPort\', \'baudRate\' and \'backend\'. "
"Get those information from the available Zigbee adapters." "The serial ports can be fetched from the available adapters. See \'GetAdapters\' for more information. "
"The channel mask is optional and defaults to all channels [11, 26]. " "The available backends can be fetched using the \'GetAvailableBackends\' method.";
"The quietest channel from the given channel mask will be picked during network creation. "
"The channel mask is a uint32 flag and the the bit number represents the channel which should "
"enabled for scanning. All channels would be the value 0x07fff800.";
params.insert("serialPort", enumValueName(String)); params.insert("serialPort", enumValueName(String));
params.insert("baudRate", enumValueName(Uint)); params.insert("baudRate", enumValueName(Uint));
params.insert("backendType", enumRef<ZigbeeAdapter::ZigbeeBackendType>()); params.insert("backend", enumValueName(String));
params.insert("o:channelMask", enumValueName(Uint));
returns.insert("zigbeeError", enumRef<ZigbeeManager::ZigbeeError>()); returns.insert("zigbeeError", enumRef<ZigbeeManager::ZigbeeError>());
returns.insert("o:networkUuid", enumValueName(Uuid)); returns.insert("o:networkUuid", enumValueName(Uuid));
registerMethod("AddNetwork", description, params, returns); registerMethod("AddNetwork", description, params, returns);
@ -143,22 +141,21 @@ ZigbeeHandler::ZigbeeHandler(ZigbeeManager *zigbeeManager, QObject *parent) :
// FactoryResetNetwork // FactoryResetNetwork
params.clear(); returns.clear(); params.clear(); returns.clear();
description = "Factory reset the network with the given networkUuid. The network does not have " description = "Factory reset the network with the given \'networkUuid\'. The network does not have "
"to be Online for this procedure, and all associated nodes and things will be removed permanently. " "to be online for this procedure, and all associated nodes and things will be removed permanently.";
"Make sure the user realy wants to do this because this can not be undone.";
params.insert("networkUuid", enumValueName(Uuid)); params.insert("networkUuid", enumValueName(Uuid));
returns.insert("zigbeeError", enumRef<ZigbeeManager::ZigbeeError>()); returns.insert("zigbeeError", enumRef<ZigbeeManager::ZigbeeError>());
registerMethod("FactoryResetNetwork", description, params, returns); registerMethod("FactoryResetNetwork", description, params, returns);
// SetPermitJoin // SetPermitJoin
params.clear(); returns.clear(); params.clear(); returns.clear();
description = "Allow or deny nodes to join the network with the given networkUuid for a specific duration in seconds. " description = "Allow or deny nodes to join the network with the given \'networkUuid\' for a specific \'duration\' in seconds. "
"The duration values has to be between 0 and 255 seconds. The permitJoinDuration indicates how long permit " "The duration value has to be between 0 and 255 seconds. The \'permitJoinDuration\' property of Zigbee network "
"has been enabled and the permitJoinDuration indicates the rest of the time. Those values can be used to " "object indicates how long permit has been enabled and the \'permitJoiningRemaining\' indicates the rest of the time. "
"show a countdown or progressbar. This method can be recalled for resetting the timeout. " "Those values can be used to show a countdown or progressbar. This method can be recalled for resetting the timeout. "
"If the duration is set to 0 seconds, joining will be disabled immediatly for the entire network. " "If the duration is set to 0 seconds, joining will be disabled immediatly for the entire network. "
"The shortAddress is optional and defaults to the broadcast address (0xfffc) for all routers in the network. " "The \'shortAddress\' is optional and defaults to the broadcast address 0xfffc for all routers in the network. "
"If the short address matches the address of a router node in the network, only that router will " "If the short address matches the address of a router node in the network, only that specific router will "
"be able to allow new nodes to join the network. A new node will join to the router with the best link quality index (LQI)."; "be able to allow new nodes to join the network. A new node will join to the router with the best link quality index (LQI).";
params.insert("networkUuid", enumValueName(Uuid)); params.insert("networkUuid", enumValueName(Uuid));
params.insert("duration", enumValueName(Uint)); params.insert("duration", enumValueName(Uint));
@ -203,6 +200,19 @@ QString ZigbeeHandler::name() const
return "Zigbee"; return "Zigbee";
} }
JsonReply *ZigbeeHandler::GetAvailableBackends(const QVariantMap &params)
{
Q_UNUSED(params)
QVariantMap returnMap;
QVariantList backendsList;
foreach (const QString &backendName, ZigbeeAdapter::backendNames().values())
backendsList << backendName;
returnMap.insert("backends", backendsList);
return createReply(returnMap);
}
JsonReply *ZigbeeHandler::GetAdapters(const QVariantMap &params) JsonReply *ZigbeeHandler::GetAdapters(const QVariantMap &params)
{ {
Q_UNUSED(params) Q_UNUSED(params)
@ -218,11 +228,17 @@ JsonReply *ZigbeeHandler::GetAdapters(const QVariantMap &params)
JsonReply *ZigbeeHandler::AddNetwork(const QVariantMap &params) JsonReply *ZigbeeHandler::AddNetwork(const QVariantMap &params)
{ {
QVariantMap returnMap;
QString serialPort = params.value("serialPort").toString(); QString serialPort = params.value("serialPort").toString();
uint baudRate = params.value("baudRate").toUInt(); uint baudRate = params.value("baudRate").toUInt();
ZigbeeAdapter::ZigbeeBackendType backendType = enumNameToValue<ZigbeeAdapter::ZigbeeBackendType>(params.value("backendType").toString()); QString backendString = params.value("backend").toString();
QPair<ZigbeeManager::ZigbeeError, QUuid> result = m_zigbeeManager->createZigbeeNetwork(serialPort, baudRate, backendType); if (!ZigbeeAdapter::backendNames().values().contains(backendString)) {
QVariantMap returnMap; returnMap.insert("zigbeeError", enumValueName<ZigbeeManager::ZigbeeError>(ZigbeeManager::ZigbeeErrorUnknownBackend));
return createReply(returnMap);
}
QPair<ZigbeeManager::ZigbeeError, QUuid> result = m_zigbeeManager->createZigbeeNetwork(serialPort, baudRate, ZigbeeAdapter::backendNames().key(backendString));
if (result.first == ZigbeeManager::ZigbeeErrorNoError) { if (result.first == ZigbeeManager::ZigbeeErrorNoError) {
returnMap.insert("networkUuid", result.second); returnMap.insert("networkUuid", result.second);
} }
@ -293,10 +309,10 @@ QVariantMap ZigbeeHandler::packNetwork(ZigbeeNetwork *network)
switch (network->backendType()) { switch (network->backendType()) {
case Zigbee::ZigbeeBackendTypeDeconz: case Zigbee::ZigbeeBackendTypeDeconz:
networkMap.insert("backendType", enumValueName<ZigbeeAdapter::ZigbeeBackendType>(ZigbeeAdapter::ZigbeeBackendTypeDeconz)); networkMap.insert("backend", ZigbeeAdapter::backendNames().value(ZigbeeAdapter::ZigbeeBackendTypeDeconz));
break; break;
case Zigbee::ZigbeeBackendTypeNxp: case Zigbee::ZigbeeBackendTypeNxp:
networkMap.insert("backendType", enumValueName<ZigbeeAdapter::ZigbeeBackendType>(ZigbeeAdapter::ZigbeeBackendTypeNxp)); networkMap.insert("backend", ZigbeeAdapter::backendNames().value(ZigbeeAdapter::ZigbeeBackendTypeNxp));
break; break;
} }

View File

@ -49,6 +49,7 @@ public:
QString name() const override; QString name() const override;
Q_INVOKABLE JsonReply *GetAvailableBackends(const QVariantMap &params);
Q_INVOKABLE JsonReply *GetAdapters(const QVariantMap &params); Q_INVOKABLE JsonReply *GetAdapters(const QVariantMap &params);
Q_INVOKABLE JsonReply *GetNetworks(const QVariantMap &params); Q_INVOKABLE JsonReply *GetNetworks(const QVariantMap &params);
Q_INVOKABLE JsonReply *AddNetwork(const QVariantMap &params); Q_INVOKABLE JsonReply *AddNetwork(const QVariantMap &params);

View File

@ -97,6 +97,11 @@ void ZigbeeAdapter::setBackendType(ZigbeeAdapter::ZigbeeBackendType backendType)
m_backendType = backendType; m_backendType = backendType;
} }
QString ZigbeeAdapter::backend() const
{
return backendNames().value(m_backendType);
}
qint32 ZigbeeAdapter::baudRate() const qint32 ZigbeeAdapter::baudRate() const
{ {
return m_baudRate; return m_baudRate;
@ -117,6 +122,13 @@ bool ZigbeeAdapter::operator==(const ZigbeeAdapter &other) const
&& m_baudRate == other.baudRate(); && m_baudRate == other.baudRate();
} }
QHash<ZigbeeAdapter::ZigbeeBackendType, QString> ZigbeeAdapter::backendNames()
{
QHash<ZigbeeAdapter::ZigbeeBackendType, QString> backendNameHash;
backendNameHash.insert(ZigbeeBackendTypeDeconz, "deCONZ");
backendNameHash.insert(ZigbeeBackendTypeNxp, "NXP");
return backendNameHash;
}
QDebug operator<<(QDebug debug, const ZigbeeAdapter &adapter) QDebug operator<<(QDebug debug, const ZigbeeAdapter &adapter)
{ {

View File

@ -41,13 +41,13 @@ namespace nymeaserver {
class ZigbeeAdapter class ZigbeeAdapter
{ {
Q_GADGET Q_GADGET
Q_PROPERTY(QString name READ name WRITE setName) Q_PROPERTY(QString name READ name)
Q_PROPERTY(QString description READ description WRITE setDescription) Q_PROPERTY(QString description READ description)
Q_PROPERTY(QString serialPort READ serialPort WRITE setSerialPort) Q_PROPERTY(QString serialPort READ serialPort)
Q_PROPERTY(QString serialNumber READ serialNumber WRITE setSerialNumber) Q_PROPERTY(QString serialNumber READ serialNumber)
Q_PROPERTY(bool hardwareRecognized READ hardwareRecognized WRITE setHardwareRecognized) Q_PROPERTY(bool hardwareRecognized READ hardwareRecognized)
Q_PROPERTY(nymeaserver::ZigbeeAdapter::ZigbeeBackendType backendType READ backendType WRITE setBackendType) Q_PROPERTY(QString backend READ backend)
Q_PROPERTY(qint32 baudRate READ baudRate WRITE setBaudRate) Q_PROPERTY(qint32 baudRate READ baudRate)
public: public:
enum ZigbeeBackendType { enum ZigbeeBackendType {
@ -75,12 +75,15 @@ public:
ZigbeeAdapter::ZigbeeBackendType backendType() const; ZigbeeAdapter::ZigbeeBackendType backendType() const;
void setBackendType(ZigbeeAdapter::ZigbeeBackendType backendType); void setBackendType(ZigbeeAdapter::ZigbeeBackendType backendType);
QString backend() const;
qint32 baudRate() const; qint32 baudRate() const;
void setBaudRate(qint32 baudRate); void setBaudRate(qint32 baudRate);
bool operator==(const ZigbeeAdapter &other) const; bool operator==(const ZigbeeAdapter &other) const;
static QHash<ZigbeeBackendType, QString> backendNames();
private: private:
QString m_name; QString m_name;
QString m_description; QString m_description;

View File

@ -60,7 +60,8 @@ public:
ZigbeeErrorAdapterAlreadyInUse, ZigbeeErrorAdapterAlreadyInUse,
ZigbeeErrorNetworkUuidNotFound, ZigbeeErrorNetworkUuidNotFound,
ZigbeeErrorDurationOutOfRange, ZigbeeErrorDurationOutOfRange,
ZigbeeErrorNetworkOffline ZigbeeErrorNetworkOffline,
ZigbeeErrorUnknownBackend
}; };
Q_ENUM(ZigbeeError) Q_ENUM(ZigbeeError)