add pushbutton mock device
This commit is contained in:
parent
0e3ddf724b
commit
c515dce56f
@ -45,43 +45,56 @@ DeviceManager::HardwareResources DevicePluginMock::requiredHardware() const
|
||||
|
||||
DeviceManager::DeviceError DevicePluginMock::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms)
|
||||
{
|
||||
Q_UNUSED(deviceClassId)
|
||||
qCDebug(dcMockDevice) << "starting mock discovery:" << params;
|
||||
m_discoveredDeviceCount = params.paramValue("resultCount").toInt();
|
||||
QTimer::singleShot(1000, this, SLOT(emitDevicesDiscovered()));
|
||||
return DeviceManager::DeviceErrorAsync;
|
||||
if (deviceClassId == mockDeviceClassId || deviceClassId == mockDeviceAutoDeviceClassId) {
|
||||
qCDebug(dcMockDevice) << "starting mock discovery:" << params;
|
||||
m_discoveredDeviceCount = params.paramValue("resultCount").toInt();
|
||||
QTimer::singleShot(1000, this, SLOT(emitDevicesDiscovered()));
|
||||
return DeviceManager::DeviceErrorAsync;
|
||||
} else if (deviceClassId == mockPushButtonDeviceClassId) {
|
||||
qCDebug(dcMockDevice) << "starting mock push button discovery:" << params;
|
||||
m_discoveredDeviceCount = params.paramValue("resultCount").toInt();
|
||||
QTimer::singleShot(1000, this, SLOT(emitPushButtonDevicesDiscovered()));
|
||||
return DeviceManager::DeviceErrorAsync;
|
||||
}
|
||||
return DeviceManager::DeviceErrorDeviceClassNotFound;
|
||||
}
|
||||
|
||||
DeviceManager::DeviceSetupStatus DevicePluginMock::setupDevice(Device *device)
|
||||
{
|
||||
qCDebug(dcMockDevice) << "Mockdevice created returning true"
|
||||
<< device->paramValue("name").toString()
|
||||
<< device->paramValue("httpport").toInt()
|
||||
<< device->paramValue("async").toBool()
|
||||
<< device->paramValue("broken").toBool();
|
||||
if (device->deviceClassId() == mockDeviceClassId || device->deviceClassId() == mockDeviceAutoDeviceClassId) {
|
||||
qCDebug(dcMockDevice) << "Mockdevice created returning true"
|
||||
<< device->paramValue("name").toString()
|
||||
<< device->paramValue("httpport").toInt()
|
||||
<< device->paramValue("async").toBool()
|
||||
<< device->paramValue("broken").toBool();
|
||||
|
||||
if (device->paramValue("broken").toBool()) {
|
||||
qCWarning(dcMockDevice) << "This device is intentionally broken.";
|
||||
return DeviceManager::DeviceSetupStatusFailure;
|
||||
if (device->paramValue("broken").toBool()) {
|
||||
qCWarning(dcMockDevice) << "This device is intentionally broken.";
|
||||
return DeviceManager::DeviceSetupStatusFailure;
|
||||
}
|
||||
|
||||
HttpDaemon *daemon = new HttpDaemon(device, this);
|
||||
m_daemons.insert(device, daemon);
|
||||
|
||||
if (!daemon->isListening()) {
|
||||
qCWarning(dcMockDevice) << "HTTP port opening failed.";
|
||||
return DeviceManager::DeviceSetupStatusFailure;
|
||||
}
|
||||
|
||||
connect(daemon, &HttpDaemon::triggerEvent, this, &DevicePluginMock::triggerEvent);
|
||||
connect(daemon, &HttpDaemon::setState, this, &DevicePluginMock::setState);
|
||||
|
||||
if (device->paramValue("async").toBool()) {
|
||||
m_asyncSetupDevices.append(device);
|
||||
QTimer::singleShot(1000, this, SLOT(emitDeviceSetupFinished()));
|
||||
return DeviceManager::DeviceSetupStatusAsync;
|
||||
}
|
||||
return DeviceManager::DeviceSetupStatusSuccess;
|
||||
} else if (device->deviceClassId() == mockPushButtonDeviceClassId) {
|
||||
qCDebug(dcMockDevice) << "Setup PushButton Mock device" << device->params();
|
||||
return DeviceManager::DeviceSetupStatusSuccess;
|
||||
}
|
||||
|
||||
HttpDaemon *daemon = new HttpDaemon(device, this);
|
||||
m_daemons.insert(device, daemon);
|
||||
|
||||
if (!daemon->isListening()) {
|
||||
qCWarning(dcMockDevice) << "HTTP port opening failed.";
|
||||
return DeviceManager::DeviceSetupStatusFailure;
|
||||
}
|
||||
|
||||
connect(daemon, &HttpDaemon::triggerEvent, this, &DevicePluginMock::triggerEvent);
|
||||
connect(daemon, &HttpDaemon::setState, this, &DevicePluginMock::setState);
|
||||
|
||||
if (device->paramValue("async").toBool()) {
|
||||
m_asyncSetupDevices.append(device);
|
||||
QTimer::singleShot(1000, this, SLOT(emitDeviceSetupFinished()));
|
||||
return DeviceManager::DeviceSetupStatusAsync;
|
||||
}
|
||||
return DeviceManager::DeviceSetupStatusSuccess;
|
||||
return DeviceManager::DeviceSetupStatusFailure;
|
||||
}
|
||||
|
||||
void DevicePluginMock::deviceRemoved(Device *device)
|
||||
@ -112,6 +125,27 @@ void DevicePluginMock::startMonitoringAutoDevices()
|
||||
emit autoDevicesAppeared(mockDeviceAutoDeviceClassId, deviceDescriptorList);
|
||||
}
|
||||
|
||||
DeviceManager::DeviceSetupStatus DevicePluginMock::confirmPairing(const PairingTransactionId &pairingTransactionId, const DeviceClassId &deviceClassId, const ParamList ¶ms, const QString &secret)
|
||||
{
|
||||
Q_UNUSED(params)
|
||||
Q_UNUSED(secret)
|
||||
|
||||
qCDebug(dcMockDevice) << "Confirm pairing";
|
||||
|
||||
if (deviceClassId != mockPushButtonDeviceClassId) {
|
||||
qCWarning(dcMockDevice) << "Invalid deviceclassId -> no pairing possible with this device";
|
||||
return DeviceManager::DeviceSetupStatusFailure;
|
||||
}
|
||||
if (!m_pushbuttonPressed) {
|
||||
qCDebug(dcMockDevice) << "Push button not pressed yet!";
|
||||
return DeviceManager::DeviceSetupStatusFailure;
|
||||
}
|
||||
|
||||
m_pairingId = pairingTransactionId;
|
||||
QTimer::singleShot(1000, this, SLOT(onPushButtonPairingFinished()));
|
||||
return DeviceManager::DeviceSetupStatusAsync;
|
||||
}
|
||||
|
||||
QList<ParamType> DevicePluginMock::configurationDescription() const
|
||||
{
|
||||
QList<ParamType> params;
|
||||
@ -127,9 +161,8 @@ QList<ParamType> DevicePluginMock::configurationDescription() const
|
||||
|
||||
DeviceManager::DeviceError DevicePluginMock::executeAction(Device *device, const Action &action)
|
||||
{
|
||||
if (!myDevices().contains(device)) {
|
||||
if (!myDevices().contains(device))
|
||||
return DeviceManager::DeviceErrorDeviceNotFound;
|
||||
}
|
||||
|
||||
if (action.actionTypeId() == mockAsyncActionTypeId || action.actionTypeId() == mockAsyncFailingActionTypeId) {
|
||||
m_asyncActions.append(qMakePair<Action, Device*>(action, device));
|
||||
@ -137,9 +170,9 @@ DeviceManager::DeviceError DevicePluginMock::executeAction(Device *device, const
|
||||
return DeviceManager::DeviceErrorAsync;
|
||||
}
|
||||
|
||||
if (action.actionTypeId() == mockFailingActionTypeId) {
|
||||
if (action.actionTypeId() == mockFailingActionTypeId)
|
||||
return DeviceManager::DeviceErrorSetupFailed;
|
||||
}
|
||||
|
||||
|
||||
m_daemons.value(device)->actionExecuted(action.actionTypeId());
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
@ -148,9 +181,8 @@ DeviceManager::DeviceError DevicePluginMock::executeAction(Device *device, const
|
||||
void DevicePluginMock::setState(const StateTypeId &stateTypeId, const QVariant &value)
|
||||
{
|
||||
HttpDaemon *daemon = qobject_cast<HttpDaemon*>(sender());
|
||||
if (!daemon) {
|
||||
if (!daemon)
|
||||
return;
|
||||
}
|
||||
|
||||
Device *device = m_daemons.key(daemon);
|
||||
device->setStateValue(stateTypeId, value);
|
||||
@ -200,6 +232,41 @@ void DevicePluginMock::emitDevicesDiscovered()
|
||||
emit devicesDiscovered(mockDeviceClassId, deviceDescriptors);
|
||||
}
|
||||
|
||||
void DevicePluginMock::emitPushButtonDevicesDiscovered()
|
||||
{
|
||||
QList<DeviceDescriptor> deviceDescriptors;
|
||||
|
||||
if (m_discoveredDeviceCount > 0) {
|
||||
DeviceDescriptor d1(mockDeviceClassId, "Mock Device (Push Button)", "1");
|
||||
ParamList params;
|
||||
Param name("name", "PushButton Mock Device 1");
|
||||
params.append(name);
|
||||
d1.setParams(params);
|
||||
deviceDescriptors.append(d1);
|
||||
}
|
||||
|
||||
if (m_discoveredDeviceCount > 1) {
|
||||
DeviceDescriptor d2(mockDeviceClassId, "Mock Device (Push Button)", "2");
|
||||
ParamList params;
|
||||
Param name("name", "PushButton Mock Device 2");
|
||||
params.append(name);
|
||||
d2.setParams(params);
|
||||
deviceDescriptors.append(d2);
|
||||
}
|
||||
|
||||
m_pushbuttonPressed = false;
|
||||
QTimer::singleShot(3000, this, SLOT(onPushButtonPressed()));
|
||||
qDebug() << "Start PushButton timer (will be pressed in 3 second)";
|
||||
|
||||
emit devicesDiscovered(mockPushButtonDeviceClassId, deviceDescriptors);
|
||||
}
|
||||
|
||||
void DevicePluginMock::onPushButtonPressed()
|
||||
{
|
||||
qCDebug(dcMockDevice) << "PushButton pressed";
|
||||
m_pushbuttonPressed = true;
|
||||
}
|
||||
|
||||
void DevicePluginMock::emitDeviceSetupFinished()
|
||||
{
|
||||
qCDebug(dcMockDevice) << "emitting setup finised";
|
||||
@ -221,3 +288,9 @@ void DevicePluginMock::emitActionExecuted()
|
||||
emit actionExecutionFinished(action.first.id(), DeviceManager::DeviceErrorSetupFailed);
|
||||
}
|
||||
}
|
||||
|
||||
void DevicePluginMock::onPushButtonPairingFinished()
|
||||
{
|
||||
qCDebug(dcMockDevice) << "Pairing finished";
|
||||
emit pairingFinished(m_pairingId, DeviceManager::DeviceSetupStatusSuccess);
|
||||
}
|
||||
|
||||
@ -47,6 +47,8 @@ public:
|
||||
|
||||
void startMonitoringAutoDevices() override;
|
||||
|
||||
DeviceManager::DeviceSetupStatus confirmPairing(const PairingTransactionId &pairingTransactionId, const DeviceClassId &deviceClassId, const ParamList ¶ms, const QString &secret) override;
|
||||
|
||||
QList<ParamType> configurationDescription() const override;
|
||||
|
||||
public slots:
|
||||
@ -56,15 +58,22 @@ private slots:
|
||||
void setState(const StateTypeId &stateTypeId, const QVariant &value);
|
||||
void triggerEvent(const EventTypeId &id);
|
||||
void emitDevicesDiscovered();
|
||||
void emitPushButtonDevicesDiscovered();
|
||||
void emitDeviceSetupFinished();
|
||||
void emitActionExecuted();
|
||||
|
||||
void onPushButtonPressed();
|
||||
void onPushButtonPairingFinished();
|
||||
|
||||
private:
|
||||
QHash<Device*, HttpDaemon*> m_daemons;
|
||||
QList<Device*> m_asyncSetupDevices;
|
||||
QList<QPair<Action, Device*> > m_asyncActions;
|
||||
|
||||
PairingTransactionId m_pairingId;
|
||||
|
||||
int m_discoveredDeviceCount;
|
||||
bool m_pushbuttonPressed;
|
||||
};
|
||||
|
||||
#endif // DEVICEPLUGINMOCK_H
|
||||
|
||||
@ -202,6 +202,53 @@
|
||||
"name": "Mock Action 5 (async, broken)"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"deviceClassId": "9e03144c-e436-4eea-82d9-ccb33ef778db",
|
||||
"idName": "mockPushButton",
|
||||
"name": "Mock Device (Push Button)",
|
||||
"createMethods": ["discovery"],
|
||||
"setupMethod": "pushButton",
|
||||
"pairingInfo": "Wait 3 second before you continue, the push button will be pressed automatically.",
|
||||
"discoveryParamTypes": [
|
||||
{
|
||||
"name": "resultCount",
|
||||
"type": "int",
|
||||
"defaultValue": 2,
|
||||
"allowedValues": [1, 2]
|
||||
}
|
||||
],
|
||||
"paramTypes": [
|
||||
{
|
||||
"name": "name",
|
||||
"type": "QString",
|
||||
"inputType": "TextLine",
|
||||
"defaultValue": "Mock device",
|
||||
"readOnly": true
|
||||
}
|
||||
],
|
||||
"stateTypes": [
|
||||
{
|
||||
"id": "20dc7c22-c50e-42db-837c-2bbced939f8e",
|
||||
"idName": "color",
|
||||
"name": "color",
|
||||
"type": "QColor",
|
||||
"defaultValue": "#000000",
|
||||
"writable": {}
|
||||
},
|
||||
{
|
||||
"id": "72981c04-267a-4ba0-a59e-9921d2f3af9c",
|
||||
"idName": "percentage",
|
||||
"name": "percentage",
|
||||
"type": "int",
|
||||
"unit": "Percentage",
|
||||
"defaultValue": 0,
|
||||
"writable": {
|
||||
"minValue": 0,
|
||||
"maxValue": 100
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user