add support for overriding discovery params
This commit is contained in:
parent
430d60d9df
commit
e6929df34f
@ -17,11 +17,11 @@ QVariant DeviceDiscovery::data(const QModelIndex &index, int role) const
|
|||||||
{
|
{
|
||||||
switch (role) {
|
switch (role) {
|
||||||
case RoleId:
|
case RoleId:
|
||||||
return m_foundDevices.at(index.row()).m_id;
|
return m_foundDevices.at(index.row())->id();
|
||||||
case RoleName:
|
case RoleName:
|
||||||
return m_foundDevices.at(index.row()).m_name;
|
return m_foundDevices.at(index.row())->name();
|
||||||
case RoleDescription:
|
case RoleDescription:
|
||||||
return m_foundDevices.at(index.row()).m_description;
|
return m_foundDevices.at(index.row())->description();
|
||||||
}
|
}
|
||||||
|
|
||||||
return QVariant();
|
return QVariant();
|
||||||
@ -38,17 +38,21 @@ QHash<int, QByteArray> DeviceDiscovery::roleNames() const
|
|||||||
|
|
||||||
void DeviceDiscovery::discoverDevices(const QUuid &deviceClassId, const QVariantList &discoveryParams)
|
void DeviceDiscovery::discoverDevices(const QUuid &deviceClassId, const QVariantList &discoveryParams)
|
||||||
{
|
{
|
||||||
|
if (m_busy) {
|
||||||
|
qWarning() << "Busy... not restarting discovery";
|
||||||
|
return;
|
||||||
|
}
|
||||||
beginResetModel();
|
beginResetModel();
|
||||||
m_foundDevices.clear();
|
m_foundDevices.clear();
|
||||||
endResetModel();
|
endResetModel();
|
||||||
emit countChanged();
|
emit countChanged();
|
||||||
|
|
||||||
if (!m_jsonRpcClient) {
|
if (!m_engine) {
|
||||||
qWarning() << "Cannot discover devices. No JsonRpcClient set";
|
qWarning() << "Cannot discover devices. No Engine set";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!m_jsonRpcClient->connected()) {
|
if (!m_engine->jsonRpcClient()->connected()) {
|
||||||
qWarning() << "Cannot discover devices. JsonRpcClient not connected.";
|
qWarning() << "Cannot discover devices. Not connected.";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,21 +61,29 @@ void DeviceDiscovery::discoverDevices(const QUuid &deviceClassId, const QVariant
|
|||||||
if (!discoveryParams.isEmpty()) {
|
if (!discoveryParams.isEmpty()) {
|
||||||
params.insert("discoveryParams", discoveryParams);
|
params.insert("discoveryParams", discoveryParams);
|
||||||
}
|
}
|
||||||
m_jsonRpcClient->sendCommand("Devices.GetDiscoveredDevices", params, this, "discoverDevicesResponse");
|
m_engine->jsonRpcClient()->sendCommand("Devices.GetDiscoveredDevices", params, this, "discoverDevicesResponse");
|
||||||
m_busy = true;
|
m_busy = true;
|
||||||
emit busyChanged();
|
emit busyChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonRpcClient *DeviceDiscovery::jsonRpcClient() const
|
DeviceDescriptor *DeviceDiscovery::get(int index) const
|
||||||
{
|
{
|
||||||
return m_jsonRpcClient;
|
if (index < 0 || index >= m_foundDevices.count()) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return m_foundDevices.at(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeviceDiscovery::setJsonRpcClient(JsonRpcClient *jsonRpcClient)
|
Engine *DeviceDiscovery::engine() const
|
||||||
{
|
{
|
||||||
if (m_jsonRpcClient != jsonRpcClient) {
|
return m_engine;
|
||||||
m_jsonRpcClient = jsonRpcClient;
|
}
|
||||||
emit jsonRpcClientChanged();
|
|
||||||
|
void DeviceDiscovery::setEngine(Engine *engine)
|
||||||
|
{
|
||||||
|
if (m_engine != engine) {
|
||||||
|
m_engine = engine;
|
||||||
|
emit engineChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,13 +99,19 @@ void DeviceDiscovery::discoverDevicesResponse(const QVariantMap ¶ms)
|
|||||||
|
|
||||||
// qDebug() << "response received" << params;
|
// qDebug() << "response received" << params;
|
||||||
QVariantList descriptors = params.value("params").toMap().value("deviceDescriptors").toList();
|
QVariantList descriptors = params.value("params").toMap().value("deviceDescriptors").toList();
|
||||||
foreach (const QVariant &descriptor, descriptors) {
|
foreach (const QVariant &descriptorVariant, descriptors) {
|
||||||
qDebug() << "descriptor" << descriptor;
|
qDebug() << "Found device. Descriptor:" << descriptorVariant;
|
||||||
if (!contains(descriptor.toMap().value("id").toUuid())) {
|
if (!contains(descriptorVariant.toMap().value("id").toUuid())) {
|
||||||
beginInsertRows(QModelIndex(), m_foundDevices.count(), m_foundDevices.count());
|
beginInsertRows(QModelIndex(), m_foundDevices.count(), m_foundDevices.count());
|
||||||
m_foundDevices.append(DeviceDescriptor(descriptor.toMap().value("id").toUuid(),
|
DeviceDescriptor *descriptor = new DeviceDescriptor(descriptorVariant.toMap().value("id").toUuid(),
|
||||||
descriptor.toMap().value("title").toString(),
|
descriptorVariant.toMap().value("title").toString(),
|
||||||
descriptor.toMap().value("description").toString()));
|
descriptorVariant.toMap().value("description").toString());
|
||||||
|
foreach (const QVariant ¶mVariant, descriptorVariant.toMap().value("deviceParams").toList()) {
|
||||||
|
qDebug() << "Adding param:" << paramVariant.toMap().value("paramTypeId").toString() << paramVariant.toMap().value("value");
|
||||||
|
Param* p = new Param(paramVariant.toMap().value("paramTypeId").toString(), paramVariant.toMap().value("value"));
|
||||||
|
descriptor->params()->addParam(p);
|
||||||
|
}
|
||||||
|
m_foundDevices.append(descriptor);
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
emit countChanged();
|
emit countChanged();
|
||||||
}
|
}
|
||||||
@ -102,10 +120,40 @@ void DeviceDiscovery::discoverDevicesResponse(const QVariantMap ¶ms)
|
|||||||
|
|
||||||
bool DeviceDiscovery::contains(const QUuid &deviceDescriptorId) const
|
bool DeviceDiscovery::contains(const QUuid &deviceDescriptorId) const
|
||||||
{
|
{
|
||||||
foreach (const DeviceDescriptor &descriptor, m_foundDevices) {
|
foreach (DeviceDescriptor *descriptor, m_foundDevices) {
|
||||||
if (descriptor.m_id == deviceDescriptorId) {
|
if (descriptor->id() == deviceDescriptorId) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DeviceDescriptor::DeviceDescriptor(const QUuid &id, const QString &name, const QString &description, QObject *parent):
|
||||||
|
QObject(parent),
|
||||||
|
m_id(id),
|
||||||
|
m_name(name),
|
||||||
|
m_description(description),
|
||||||
|
m_params(new Params(this))
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
QUuid DeviceDescriptor::id() const
|
||||||
|
{
|
||||||
|
return m_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString DeviceDescriptor::name() const
|
||||||
|
{
|
||||||
|
return m_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString DeviceDescriptor::description() const
|
||||||
|
{
|
||||||
|
return m_description;
|
||||||
|
}
|
||||||
|
|
||||||
|
Params* DeviceDescriptor::params() const
|
||||||
|
{
|
||||||
|
return m_params;
|
||||||
|
}
|
||||||
|
|||||||
@ -4,12 +4,33 @@
|
|||||||
#include <QAbstractListModel>
|
#include <QAbstractListModel>
|
||||||
#include <QUuid>
|
#include <QUuid>
|
||||||
|
|
||||||
#include "jsonrpc/jsonrpcclient.h"
|
#include "engine.h"
|
||||||
|
|
||||||
|
class DeviceDescriptor: public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
Q_PROPERTY(QUuid id READ id CONSTANT)
|
||||||
|
Q_PROPERTY(QString name READ name CONSTANT)
|
||||||
|
Q_PROPERTY(QString description READ description CONSTANT)
|
||||||
|
Q_PROPERTY(Params* params READ params CONSTANT)
|
||||||
|
public:
|
||||||
|
DeviceDescriptor(const QUuid &id, const QString &name, const QString &description, QObject *parent = nullptr);
|
||||||
|
|
||||||
|
QUuid id() const;
|
||||||
|
QString name() const;
|
||||||
|
QString description() const;
|
||||||
|
Params* params() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
QUuid m_id;
|
||||||
|
QString m_name;
|
||||||
|
QString m_description;
|
||||||
|
Params *m_params = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
class DeviceDiscovery : public QAbstractListModel
|
class DeviceDiscovery : public QAbstractListModel
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_PROPERTY(JsonRpcClient* jsonRpcClient READ jsonRpcClient WRITE setJsonRpcClient)
|
Q_PROPERTY(Engine* engine READ engine WRITE setEngine)
|
||||||
Q_PROPERTY(bool busy READ busy NOTIFY busyChanged)
|
Q_PROPERTY(bool busy READ busy NOTIFY busyChanged)
|
||||||
Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
|
Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
|
||||||
public:
|
public:
|
||||||
@ -28,8 +49,10 @@ public:
|
|||||||
|
|
||||||
Q_INVOKABLE void discoverDevices(const QUuid &deviceClassId, const QVariantList &discoveryParams = {});
|
Q_INVOKABLE void discoverDevices(const QUuid &deviceClassId, const QVariantList &discoveryParams = {});
|
||||||
|
|
||||||
JsonRpcClient* jsonRpcClient() const;
|
Q_INVOKABLE DeviceDescriptor* get(int index) const;
|
||||||
void setJsonRpcClient(JsonRpcClient *jsonRpcClient);
|
|
||||||
|
Engine* engine() const;
|
||||||
|
void setEngine(Engine *jsonRpcClient);
|
||||||
|
|
||||||
bool busy() const;
|
bool busy() const;
|
||||||
|
|
||||||
@ -39,22 +62,14 @@ private slots:
|
|||||||
signals:
|
signals:
|
||||||
void busyChanged();
|
void busyChanged();
|
||||||
void countChanged();
|
void countChanged();
|
||||||
void jsonRpcClientChanged();
|
void engineChanged();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class DeviceDescriptor {
|
Engine *m_engine = nullptr;
|
||||||
public:
|
|
||||||
DeviceDescriptor(const QUuid &id, const QString &name, const QString &description): m_id(id), m_name(name), m_description(description) {}
|
|
||||||
QUuid m_id;
|
|
||||||
QString m_name;
|
|
||||||
QString m_description;
|
|
||||||
};
|
|
||||||
|
|
||||||
JsonRpcClient *m_jsonRpcClient = nullptr;
|
|
||||||
bool m_busy = false;
|
bool m_busy = false;
|
||||||
|
|
||||||
bool contains(const QUuid &deviceDescriptorId) const;
|
bool contains(const QUuid &deviceDescriptorId) const;
|
||||||
QList<DeviceDescriptor> m_foundDevices;
|
QList<DeviceDescriptor*> m_foundDevices;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // DEVICEDISCOVERY_H
|
#endif // DEVICEDISCOVERY_H
|
||||||
|
|||||||
@ -241,6 +241,7 @@ void DeviceManager::getConfiguredDevicesResponse(const QVariantMap ¶ms)
|
|||||||
device->setStateValue(stateTypeId, value);
|
device->setStateValue(stateTypeId, value);
|
||||||
// qDebug() << "Set device state value:" << device->stateValue(stateTypeId) << value;
|
// qDebug() << "Set device state value:" << device->stateValue(stateTypeId) << value;
|
||||||
}
|
}
|
||||||
|
qDebug() << "Confgured Device JSON:" << qUtf8Printable(QJsonDocument::fromVariant(deviceVariant).toJson(QJsonDocument::Indented));
|
||||||
devices()->addDevice(device);
|
devices()->addDevice(device);
|
||||||
qDebug() << "*** Added device:" << endl << device;
|
qDebug() << "*** Added device:" << endl << device;
|
||||||
}
|
}
|
||||||
@ -318,13 +319,14 @@ void DeviceManager::savePluginConfig(const QUuid &pluginId)
|
|||||||
m_jsonClient->sendCommand("Devices.SetPluginConfiguration", params, this, "setPluginConfigResponse");
|
m_jsonClient->sendCommand("Devices.SetPluginConfiguration", params, this, "setPluginConfigResponse");
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeviceManager::addDiscoveredDevice(const QUuid &deviceClassId, const QUuid &deviceDescriptorId, const QString &name)
|
void DeviceManager::addDiscoveredDevice(const QUuid &deviceClassId, const QUuid &deviceDescriptorId, const QString &name, const QVariantList &deviceParams)
|
||||||
{
|
{
|
||||||
qDebug() << "JsonRpc: add discovered device " << deviceClassId.toString();
|
qDebug() << "JsonRpc: add discovered device " << deviceClassId.toString();
|
||||||
QVariantMap params;
|
QVariantMap params;
|
||||||
params.insert("deviceClassId", deviceClassId.toString());
|
params.insert("deviceClassId", deviceClassId.toString());
|
||||||
params.insert("name", name);
|
params.insert("name", name);
|
||||||
params.insert("deviceDescriptorId", deviceDescriptorId.toString());
|
params.insert("deviceDescriptorId", deviceDescriptorId.toString());
|
||||||
|
params.insert("deviceParams", deviceParams);
|
||||||
m_jsonClient->sendCommand("Devices.AddConfiguredDevice", params, this, "addDeviceResponse");
|
m_jsonClient->sendCommand("Devices.AddConfiguredDevice", params, this, "addDeviceResponse");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -64,7 +64,7 @@ public:
|
|||||||
bool fetchingData() const;
|
bool fetchingData() const;
|
||||||
|
|
||||||
Q_INVOKABLE void addDevice(const QUuid &deviceClassId, const QString &name, const QVariantList &deviceParams);
|
Q_INVOKABLE void addDevice(const QUuid &deviceClassId, const QString &name, const QVariantList &deviceParams);
|
||||||
Q_INVOKABLE void addDiscoveredDevice(const QUuid &deviceClassId, const QUuid &deviceDescriptorId, const QString &name);
|
Q_INVOKABLE void addDiscoveredDevice(const QUuid &deviceClassId, const QUuid &deviceDescriptorId, const QString &name, const QVariantList &deviceParams);
|
||||||
Q_INVOKABLE void pairDevice(const QUuid &deviceClassId, const QUuid &deviceDescriptorId, const QString &name);
|
Q_INVOKABLE void pairDevice(const QUuid &deviceClassId, const QUuid &deviceDescriptorId, const QString &name);
|
||||||
Q_INVOKABLE void confirmPairing(const QUuid &pairingTransactionId, const QString &secret = QString());
|
Q_INVOKABLE void confirmPairing(const QUuid &pairingTransactionId, const QString &secret = QString());
|
||||||
Q_INVOKABLE void removeDevice(const QUuid &deviceId, RemovePolicy policy = RemovePolicyNone);
|
Q_INVOKABLE void removeDevice(const QUuid &deviceId, RemovePolicy policy = RemovePolicyNone);
|
||||||
|
|||||||
@ -114,6 +114,7 @@ void registerQmlTypes() {
|
|||||||
qmlRegisterUncreatableType<DeviceClasses>(uri, 1, 0, "DeviceClasses", "Can't create this in QML. Get it from the DeviceManager.");
|
qmlRegisterUncreatableType<DeviceClasses>(uri, 1, 0, "DeviceClasses", "Can't create this in QML. Get it from the DeviceManager.");
|
||||||
qmlRegisterType<DeviceClassesProxy>(uri, 1, 0, "DeviceClassesProxy");
|
qmlRegisterType<DeviceClassesProxy>(uri, 1, 0, "DeviceClassesProxy");
|
||||||
qmlRegisterType<DeviceDiscovery>(uri, 1, 0, "DeviceDiscovery");
|
qmlRegisterType<DeviceDiscovery>(uri, 1, 0, "DeviceDiscovery");
|
||||||
|
qmlRegisterUncreatableType<DeviceDescriptor>(uri, 1, 0, "DeviceDescriptor", "Get it from DeviceDiscovery");
|
||||||
|
|
||||||
qmlRegisterType<DeviceModel>(uri, 1, 0, "DeviceModel");
|
qmlRegisterType<DeviceModel>(uri, 1, 0, "DeviceModel");
|
||||||
|
|
||||||
|
|||||||
@ -24,10 +24,10 @@ Page {
|
|||||||
QtObject {
|
QtObject {
|
||||||
id: d
|
id: d
|
||||||
property var vendorId: null
|
property var vendorId: null
|
||||||
property var deviceClass: null
|
property DeviceClass deviceClass: null
|
||||||
property var deviceDescriptorId: null
|
property DeviceDescriptor deviceDescriptor: null
|
||||||
property var discoveryParams: []
|
property var discoveryParams: []
|
||||||
property var deviceName: null
|
property string deviceName: null
|
||||||
property int pairRequestId: 0
|
property int pairRequestId: 0
|
||||||
property var pairingTransactionId: null
|
property var pairingTransactionId: null
|
||||||
property int addRequestId: 0
|
property int addRequestId: 0
|
||||||
@ -64,7 +64,7 @@ Page {
|
|||||||
|
|
||||||
DeviceDiscovery {
|
DeviceDiscovery {
|
||||||
id: discovery
|
id: discovery
|
||||||
jsonRpcClient: engine.jsonRpcClient
|
engine: _engine
|
||||||
}
|
}
|
||||||
|
|
||||||
StackView {
|
StackView {
|
||||||
@ -229,7 +229,7 @@ Page {
|
|||||||
subText: model.description
|
subText: model.description
|
||||||
iconName: app.interfacesToIcon(discoveryView.deviceClass.interfaces)
|
iconName: app.interfacesToIcon(discoveryView.deviceClass.interfaces)
|
||||||
onClicked: {
|
onClicked: {
|
||||||
d.deviceDescriptorId = model.id;
|
d.deviceDescriptor = discovery.get(index);
|
||||||
d.deviceName = model.name;
|
d.deviceName = model.name;
|
||||||
internalPageStack.push(paramsPage)
|
internalPageStack.push(paramsPage)
|
||||||
}
|
}
|
||||||
@ -320,11 +320,14 @@ Page {
|
|||||||
|
|
||||||
Repeater {
|
Repeater {
|
||||||
id: paramRepeater
|
id: paramRepeater
|
||||||
model: d.deviceDescriptorId == null ? d.deviceClass.paramTypes : null
|
model: engine.jsonRpcClient.ensureServerVersion("1.12") || d.deviceDescriptor == null ? d.deviceClass.paramTypes : null
|
||||||
delegate: ParamDelegate {
|
delegate: ParamDelegate {
|
||||||
// Layout.preferredHeight: 60
|
// Layout.preferredHeight: 60
|
||||||
Layout.fillWidth: true
|
Layout.fillWidth: true
|
||||||
paramType: d.deviceClass.paramTypes.get(index)
|
paramType: d.deviceClass.paramTypes.get(index)
|
||||||
|
value: d.deviceDescriptor && d.deviceDescriptor.params.getParam(paramType.id) ?
|
||||||
|
d.deviceDescriptor.params.getParam(paramType.id).value :
|
||||||
|
d.deviceClass.paramTypes.get(index).defaultValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -336,19 +339,21 @@ Page {
|
|||||||
text: "OK"
|
text: "OK"
|
||||||
onClicked: {
|
onClicked: {
|
||||||
print("setupMethod", d.deviceClass.setupMethod)
|
print("setupMethod", d.deviceClass.setupMethod)
|
||||||
|
|
||||||
|
var params = []
|
||||||
|
for (var i = 0; i < paramRepeater.count; i++) {
|
||||||
|
var param = {}
|
||||||
|
param.paramTypeId = paramRepeater.itemAt(i).paramType.id
|
||||||
|
param.value = paramRepeater.itemAt(i).value
|
||||||
|
print("adding param", param.paramTypeId, param.value)
|
||||||
|
params.push(param)
|
||||||
|
}
|
||||||
|
|
||||||
switch (d.deviceClass.setupMethod) {
|
switch (d.deviceClass.setupMethod) {
|
||||||
case 0:
|
case 0:
|
||||||
if (d.deviceDescriptorId) {
|
if (d.deviceDescriptor) {
|
||||||
engine.deviceManager.addDiscoveredDevice(d.deviceClass.id, d.deviceDescriptorId, nameTextField.text);
|
engine.deviceManager.addDiscoveredDevice(d.deviceClass.id, d.deviceDescriptor.id, nameTextField.text, params);
|
||||||
} else {
|
} else {
|
||||||
var params = []
|
|
||||||
for (var i = 0; i < paramRepeater.count; i++) {
|
|
||||||
var param = {}
|
|
||||||
param.paramTypeId = paramRepeater.itemAt(i).paramType.id
|
|
||||||
param.value = paramRepeater.itemAt(i).value
|
|
||||||
print("adding param", param.paramTypeId, param.value)
|
|
||||||
params.push(param)
|
|
||||||
}
|
|
||||||
|
|
||||||
engine.deviceManager.addDevice(d.deviceClass.id, nameTextField.text, params);
|
engine.deviceManager.addDevice(d.deviceClass.id, nameTextField.text, params);
|
||||||
}
|
}
|
||||||
@ -357,7 +362,7 @@ Page {
|
|||||||
case 1:
|
case 1:
|
||||||
case 2:
|
case 2:
|
||||||
case 3:
|
case 3:
|
||||||
engine.deviceManager.pairDevice(d.deviceClass.id, d.deviceDescriptorId, nameTextField.text);
|
engine.deviceManager.pairDevice(d.deviceClass.id, d.deviceDescriptor.id, nameTextField.text);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user