diff --git a/simulation/devicepluginsimulation.cpp b/simulation/devicepluginsimulation.cpp index f044ac84..cbce3004 100644 --- a/simulation/devicepluginsimulation.cpp +++ b/simulation/devicepluginsimulation.cpp @@ -25,6 +25,7 @@ #include #include #include +#include DevicePluginSimulation::DevicePluginSimulation() { @@ -56,10 +57,14 @@ DeviceManager::DeviceSetupStatus DevicePluginSimulation::setupDevice(Device *dev qCDebug(dcSimulation()) << "Set up device" << device->name(); if (device->deviceClassId() == garageGateDeviceClassId || device->deviceClassId() == extendedAwningDeviceClassId || - device->deviceClassId() == rollerShutterDeviceClassId) { + device->deviceClassId() == rollerShutterDeviceClassId || + device->deviceClassId() == fingerPrintSensorDeviceClassId) { m_simulationTimers.insert(device, new QTimer(device)); connect(m_simulationTimers[device], &QTimer::timeout, this, &DevicePluginSimulation::simulationTimerTimeout); } + if (device->deviceClassId() == fingerPrintSensorDeviceClassId && device->stateValue(fingerPrintSensorUsersStateTypeId).toStringList().count() > 0) { + m_simulationTimers.value(device)->start(10000); + } return DeviceManager::DeviceSetupStatusSuccess; } @@ -353,6 +358,99 @@ DeviceManager::DeviceError DevicePluginSimulation::executeAction(Device *device, } } + if (device->deviceClassId() == fingerPrintSensorDeviceClassId) { + if (action.actionTypeId() == fingerPrintSensorAddUserActionTypeId) { + QStringList users = device->stateValue(fingerPrintSensorUsersStateTypeId).toStringList(); + QString username = action.param(fingerPrintSensorAddUserActionUserIdParamTypeId).value().toString(); + QString finger = action.param(fingerPrintSensorAddUserActionFingerParamTypeId).value().toString(); + QSettings settings; + settings.beginGroup(device->id().toString()); + QStringList usedFingers = settings.value(username).toStringList(); + if (users.contains(username) && usedFingers.contains(finger)) { + return DeviceManager::DeviceErrorDuplicateUuid; + } + QTimer::singleShot(5000, this, [this, action, device, username, finger]() { + if (username.toLower().trimmed() == "john") { + emit actionExecutionFinished(action.id(), DeviceManager::DeviceErrorHardwareFailure); + } else { + emit actionExecutionFinished(action.id(), DeviceManager::DeviceErrorNoError); + QStringList users = device->stateValue(fingerPrintSensorUsersStateTypeId).toStringList(); + if (!users.contains(username)) { + users.append(username); + device->setStateValue(fingerPrintSensorUsersStateTypeId, users); + m_simulationTimers.value(device)->start(10000); + } + + QSettings settings; + settings.beginGroup(device->id().toString()); + QStringList usedFingers = settings.value(username).toStringList(); + usedFingers.append(finger); + settings.setValue(username, usedFingers); + settings.endGroup(); + } + }); + return DeviceManager::DeviceErrorAsync; + } + if (action.actionTypeId() == fingerPrintSensorRemoveUserActionTypeId) { + QStringList users = device->stateValue(fingerPrintSensorUsersStateTypeId).toStringList(); + QString username = action.params().first().value().toString(); + if (!users.contains(username)) { + return DeviceManager::DeviceErrorInvalidParameter; + } + users.removeAll(username); + device->setStateValue(fingerPrintSensorUsersStateTypeId, users); + if (users.count() == 0) { + m_simulationTimers.value(device)->stop(); + } + return DeviceManager::DeviceErrorNoError; + } + } + + if (device->deviceClassId() == simpleBlindDeviceClassId) { + if (action.actionTypeId() == simpleBlindOpenActionTypeId) { + qCDebug(dcSimulation()) << "Opening simple blind"; + return DeviceManager::DeviceErrorNoError; + } + if (action.actionTypeId() == simpleBlindCloseActionTypeId) { + qCDebug(dcSimulation()) << "Closing simple blind"; + return DeviceManager::DeviceErrorNoError; + } + if (action.actionTypeId() == simpleBlindStopActionTypeId) { + qCDebug(dcSimulation()) << "Stopping simple blind"; + return DeviceManager::DeviceErrorNoError; + } + } + + if (device->deviceClassId() == extendedBlindDeviceClassId) { + if (action.actionTypeId() == extendedBlindOpenActionTypeId) { + qCDebug(dcSimulation()) << "Opening extended blind"; + m_simulationTimers.value(device)->setProperty("targetValue", 100); + m_simulationTimers.value(device)->start(500); + device->setStateValue(extendedBlindMovingStateTypeId, true); + return DeviceManager::DeviceErrorNoError; + } + if (action.actionTypeId() == extendedBlindCloseActionTypeId) { + qCDebug(dcSimulation()) << "Closing extended blind"; + m_simulationTimers.value(device)->setProperty("targetValue", 0); + m_simulationTimers.value(device)->start(500); + device->setStateValue(extendedBlindMovingStateTypeId, true); + return DeviceManager::DeviceErrorNoError; + } + if (action.actionTypeId() == extendedBlindStopActionTypeId) { + qCDebug(dcSimulation()) << "Stopping extended blind"; + m_simulationTimers.value(device)->stop(); + device->setStateValue(extendedBlindMovingStateTypeId, false); + return DeviceManager::DeviceErrorNoError; + } + if (action.actionTypeId() == extendedBlindPercentageActionTypeId) { + qCDebug(dcSimulation()) << "Setting extended blind to" << action.param(extendedBlindPercentageActionPercentageParamTypeId); + m_simulationTimers.value(device)->setProperty("targetValue", action.param(extendedBlindPercentageActionPercentageParamTypeId).value()); + m_simulationTimers.value(device)->start(500); + device->setStateValue(extendedBlindMovingStateTypeId, true); + return DeviceManager::DeviceErrorNoError; + } + } + qCWarning(dcSimulation()) << "Unhandled device class" << device->deviceClassId() << "for device" << device->name(); return DeviceManager::DeviceErrorDeviceClassNotFound; @@ -507,5 +605,23 @@ void DevicePluginSimulation::simulationTimerTimeout() t->stop(); device->setStateValue(rollerShutterMovingStateTypeId, false); } + } else if (device->deviceClassId() == fingerPrintSensorDeviceClassId) { + EventTypeId evt = qrand() % 2 == 0 ? fingerPrintSensorAccessGrantedEventTypeId : fingerPrintSensorAccessDeniedEventTypeId; + ParamList params; + if (evt == fingerPrintSensorAccessGrantedEventTypeId) { + QStringList users = device->stateValue(fingerPrintSensorUsersStateTypeId).toStringList(); + QString user = users.at(qrand() % users.count()); + QSettings settings; + settings.beginGroup(device->id().toString()); + QStringList fingers = settings.value(user).toStringList(); + params.append(Param(fingerPrintSensorAccessGrantedEventUserIdParamTypeId, user)); + QString finger = fingers.at(qrand() % fingers.count()); + params.append(Param(fingerPrintSensorAccessGrantedEventFingerParamTypeId, finger)); + qCDebug(dcSimulation()) << "Emitting fingerprint accepted for user" << user << "and finger" << finger; + } else { + qCDebug(dcSimulation()) << "Emitting fingerprint denied"; + } + Event event(evt, device->id(), params); + emitEvent(event); } } diff --git a/simulation/devicepluginsimulation.json b/simulation/devicepluginsimulation.json index a0d57828..be682e8b 100644 --- a/simulation/devicepluginsimulation.json +++ b/simulation/devicepluginsimulation.json @@ -312,6 +312,30 @@ } ] }, + { + "id": "a13c14f1-361e-4aad-8785-c04b094fb19a", + "name": "simpleBlind", + "displayName": "Simple blind", + "createMethods": ["user"], + "interfaces": ["blind"], + "actionTypes": [ + { + "id": "06b99eb1-c3b6-4bea-95cf-690078297206", + "name": "open", + "displayName": "Open" + }, + { + "id": "7f1bdeef-a57c-4b82-80ad-e3e31f16027f", + "name": "stop", + "displayName": "stop" + }, + { + "id": "0c55d32d-c916-472b-a03e-66fe7115e85d", + "name": "close", + "displayName": "close" + } + ] + }, { "id": "1d7aaa1d-fc50-4d7b-9657-1449322e40d8", "name": "extendedBlind", @@ -327,6 +351,8 @@ "type": "int", "unit": "Percentage", "displayNameEvent": "percentage changed", + "writable": true, + "displayNameAction": "set percentage", "defaultValue": 0 }, { @@ -549,7 +575,7 @@ "name": "fingerPrintSensor", "displayName": "Finger Print Sensor", "createMethods": ["user"], - "interfaces": ["connectable"], + "interfaces": ["fingerprintreader", "connectable"], "deviceIcon": "Network", "basicTags": [ "Device", @@ -564,13 +590,93 @@ "displayNameEvent": "Connected changed", "type": "bool", "defaultValue": true + }, + { + "id": "02482093-3b82-4918-a3ce-2a2f4831aae0", + "name": "users", + "displayName": "Users", + "displayNameEvent": "Users changed", + "type": "QStringList", + "defaultValue": [] + } + ], + "actionTypes": [ + { + "id": "c3805253-a09b-4657-b86c-97936f390672", + "name": "addUser", + "displayName": "Add user", + "paramTypes": [ + { + "id": "d9e0c68f-8b61-4f5a-9909-b27a4ac562a3", + "displayName": "User ID", + "name": "userId", + "type": "QString" + }, + { + "id": "2a97de6c-5ffb-4ca8-b4c7-41ad6790668d", + "displayName": "Finger", + "name": "finger", + "type": "QString", + "allowedValues": [ + "ThumbLeft", + "IndexFingerLeft", + "MiddleFingerLeft", + "RingFingerLeft", + "PinkyLeft", + "ThumbRight", + "IndexFingerRight", + "MiddleFingerRight", + "RingFingerRight", + "PinkyRight" + ] + } + ] + }, + { + "id": "28bf4cd1-bb1c-442b-8ba3-ed019f34abbd", + "name": "removeUser", + "displayName": "Remove user", + "paramTypes": [ + { + "id": "ca2ffce8-ee71-47ff-8247-f17fca14fd87", + "displayName": "User ID", + "name": "userId", + "type": "QString" + } + ] } ], "eventTypes": [ { "id": "1d2dde79-7121-4f8c-b7c1-904ced66a79e", "name": "accessGranted", - "displayName": "Access granted" + "displayName": "Access granted", + "paramTypes": [ + { + "id": "84addd61-15e9-4e98-aa80-6b0bf5d82a15", + "name": "userId", + "displayName": "User ID", + "type": "QString" + }, + { + "id": "3611bfc0-be3c-4ddb-8184-b64fc38c7256", + "displayName": "Finger", + "name": "finger", + "type": "QString", + "allowedValues": [ + "ThumbLeft", + "IndexFingerLeft", + "MiddleFingerLeft", + "RingFingerLeft", + "PinkyLeft", + "ThumbRight", + "IndexFingerRight", + "MiddleFingerRight", + "RingFingerRight", + "PinkyRight" + ] + } + ] }, { "id": "992b7742-af0c-447c-bd94-9ec70b872268",