add json api to add a device
This commit is contained in:
parent
2f0520189d
commit
514323ce5d
@ -25,12 +25,27 @@ QList<DeviceClass> DeviceManager::supportedDevices()
|
||||
return m_supportedDevices;
|
||||
}
|
||||
|
||||
void DeviceManager::createDevice(const DeviceClass &deviceClass, const QVariantMap ¶ms)
|
||||
DeviceManager::DeviceError DeviceManager::addConfiguredDevice(const QUuid &deviceClassId, const QVariantMap ¶ms)
|
||||
{
|
||||
Device *device = new Device(deviceClass.id(), this);
|
||||
DeviceClass deviceClass = findDeviceClass(deviceClassId);
|
||||
if (deviceClass.id().isNull()) {
|
||||
qWarning() << "cannot find a device class with id" << deviceClassId;
|
||||
return DeviceErrorDeviceClassNotFound;
|
||||
}
|
||||
foreach (const QVariant ¶m, deviceClass.params()) {
|
||||
if (!params.contains(param.toMap().value("name").toString())) {
|
||||
qWarning() << "Missing parameter when creating device:" << param.toMap().value("name").toString();
|
||||
return DeviceErrorMissingParameter;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: check if params match with template from DeviceClass
|
||||
|
||||
Device *device = new Device(deviceClassId, this);
|
||||
device->setName(deviceClass.name());
|
||||
device->setParams(params);
|
||||
m_configuredDevices.append(device);
|
||||
return DeviceErrorNoError;
|
||||
}
|
||||
|
||||
QList<Device *> DeviceManager::configuredDevices() const
|
||||
@ -49,6 +64,16 @@ QList<Device *> DeviceManager::findConfiguredDevices(const DeviceClass &deviceCl
|
||||
return ret;
|
||||
}
|
||||
|
||||
DeviceClass DeviceManager::findDeviceClass(const QUuid &deviceClassId)
|
||||
{
|
||||
foreach (const DeviceClass &deviceClass, m_supportedDevices) {
|
||||
if (deviceClass.id() == deviceClassId) {
|
||||
return deviceClass;
|
||||
}
|
||||
}
|
||||
return DeviceClass(QUuid());
|
||||
}
|
||||
|
||||
Radio433 *DeviceManager::radio433() const
|
||||
{
|
||||
return m_radio433;
|
||||
|
||||
@ -14,15 +14,22 @@ class DeviceManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum DeviceError {
|
||||
DeviceErrorNoError,
|
||||
DeviceErrorDeviceNotFound,
|
||||
DeviceErrorDeviceClassNotFound,
|
||||
DeviceErrorMissingParameter
|
||||
};
|
||||
|
||||
explicit DeviceManager(QObject *parent = 0);
|
||||
|
||||
QList<DeviceClass> supportedDevices();
|
||||
|
||||
void createDevice(const DeviceClass &deviceClass, const QVariantMap ¶ms);
|
||||
|
||||
QList<Device*> configuredDevices() const;
|
||||
DeviceError addConfiguredDevice(const QUuid &deviceClassId, const QVariantMap ¶ms);
|
||||
|
||||
QList<Device*> findConfiguredDevices(const DeviceClass &deviceClass);
|
||||
DeviceClass findDeviceClass(const QUuid &deviceClassId);
|
||||
|
||||
Radio433 *radio433() const;
|
||||
|
||||
|
||||
@ -59,6 +59,23 @@ void JsonRPCServer::processData(int clientId, const QByteArray &jsonData)
|
||||
}
|
||||
params.insert("deviceClasses", supportedDeviceList);
|
||||
sendResponse(clientId, commandId, params);
|
||||
} else if (method == "AddConfiguredDevice") {
|
||||
QUuid deviceClass = params.value("deviceClass").toUuid();
|
||||
QVariantMap deviceParams = params.value("deviceParams").toMap();
|
||||
DeviceManager::DeviceError status = HiveCore::instance()->deviceManager()->addConfiguredDevice(deviceClass, deviceParams);
|
||||
switch(status) {
|
||||
case DeviceManager::DeviceErrorNoError:
|
||||
sendResponse(clientId, commandId);
|
||||
break;
|
||||
case DeviceManager::DeviceErrorDeviceClassNotFound:
|
||||
sendErrorResponse(clientId, commandId, "Error creating device. Device class not found.");
|
||||
break;
|
||||
case DeviceManager::DeviceErrorMissingParameter:
|
||||
sendErrorResponse(clientId, commandId, "Error creating device. Missing parameter.");
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
sendErrorResponse(clientId, commandId, "No such method");
|
||||
}
|
||||
} else {
|
||||
qDebug() << "got unknown namespace" << targetNamspace;
|
||||
@ -88,10 +105,22 @@ void JsonRPCServer::sendResponse(int clientId, int commandId, const QVariantMap
|
||||
{
|
||||
QVariantMap rsp;
|
||||
rsp.insert("id", commandId);
|
||||
rsp.insert("status", "success");
|
||||
rsp.insert("params", params);
|
||||
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromVariant(rsp);
|
||||
m_tcpServer->sendResponse(clientId, jsonDoc.toJson());
|
||||
}
|
||||
|
||||
void JsonRPCServer::sendErrorResponse(int clientId, int commandId, const QString &error)
|
||||
{
|
||||
QVariantMap rsp;
|
||||
rsp.insert("id", commandId);
|
||||
rsp.insert("status", "error");
|
||||
rsp.insert("error", error);
|
||||
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromVariant(rsp);
|
||||
m_tcpServer->sendResponse(clientId, jsonDoc.toJson());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -24,7 +24,8 @@ private slots:
|
||||
private:
|
||||
QVariantMap packDeviceClass(const DeviceClass &deviceClass);
|
||||
|
||||
void sendResponse(int clientId, int commandId, const QVariantMap ¶ms);
|
||||
void sendResponse(int clientId, int commandId, const QVariantMap ¶ms = QVariantMap());
|
||||
void sendErrorResponse(int clientId, int commandId, const QString &error);
|
||||
|
||||
private:
|
||||
TcpServer *m_tcpServer;
|
||||
|
||||
Reference in New Issue
Block a user