add support for closables in simulation plugin

This commit is contained in:
Michael Zanetti 2018-06-01 22:02:02 +02:00
parent 61b3bef02b
commit 430919f452
3 changed files with 104 additions and 14 deletions

View File

@ -53,9 +53,20 @@ void DevicePluginSimulation::init()
DeviceManager::DeviceSetupStatus DevicePluginSimulation::setupDevice(Device *device)
{
qCDebug(dcSimulation()) << "Set up device" << device->name();
if (device->deviceClassId() == garageGateDeviceClassId) {
m_simulationTimers.insert(device, new QTimer(device));
connect(m_simulationTimers[device], &QTimer::timeout, this, &DevicePluginSimulation::simulationTimerTimeout);
}
return DeviceManager::DeviceSetupStatusSuccess;
}
void DevicePluginSimulation::deviceRemoved(Device *device)
{
QTimer *t = m_simulationTimers.take(device);
t->stop();
t->deleteLater();
}
DeviceManager::DeviceError DevicePluginSimulation::executeAction(Device *device, const Action &action)
{
// Check the DeviceClassId for "Simple Button"
@ -229,6 +240,65 @@ DeviceManager::DeviceError DevicePluginSimulation::executeAction(Device *device,
return DeviceManager::DeviceErrorActionTypeNotFound;
}
if (device->deviceClassId() == garageGateDeviceClassId) {
if (action.actionTypeId() == garageGateOpenActionTypeId) {
if (device->stateValue(garageGateStateStateTypeId).toString() == "opening") {
qCDebug(dcSimulation()) << "Garage gate already opening.";
return DeviceManager::DeviceErrorNoError;
}
if (device->stateValue(garageGateStateStateTypeId).toString() == "open" &&
!device->stateValue(garageGateIntermediatePositionStateTypeId).toBool()) {
qCDebug(dcSimulation()) << "Garage gate already open.";
return DeviceManager::DeviceErrorNoError;
}
device->setStateValue(garageGateStateStateTypeId, "opening");
device->setStateValue(garageGateIntermediatePositionStateTypeId, true);
m_simulationTimers.value(device)->start(5000);
return DeviceManager::DeviceErrorNoError;
}
if (action.actionTypeId() == garageGateCloseActionTypeId) {
if (device->stateValue(garageGateStateStateTypeId).toString() == "closing") {
qCDebug(dcSimulation()) << "Garage gate already closing.";
return DeviceManager::DeviceErrorNoError;
}
if (device->stateValue(garageGateStateStateTypeId).toString() == "closed" &&
!device->stateValue(garageGateIntermediatePositionStateTypeId).toBool()) {
qCDebug(dcSimulation()) << "Garage gate already closed.";
return DeviceManager::DeviceErrorNoError;
}
device->setStateValue(garageGateStateStateTypeId, "closing");
device->setStateValue(garageGateIntermediatePositionStateTypeId, true);
m_simulationTimers.value(device)->start(5000);
return DeviceManager::DeviceErrorNoError;
}
if (action.actionTypeId() == garageGateStopActionTypeId) {
if (device->stateValue(garageGateStateStateTypeId).toString() == "opening" ||
device->stateValue(garageGateStateStateTypeId).toString() == "closing") {
device->setStateValue(garageGateStateStateTypeId, "open");
return DeviceManager::DeviceErrorNoError;
}
qCDebug(dcSimulation()) << "Garage gate not moving";
return DeviceManager::DeviceErrorNoError;
}
}
if (device->deviceClassId() == rollerShutterDeviceClassId) {
if (action.actionTypeId() == rollerShutterOpenActionTypeId) {
qCDebug(dcSimulation()) << "Opening roller shutter";
return DeviceManager::DeviceErrorNoError;
}
if (action.actionTypeId() == rollerShutterCloseActionTypeId) {
qCDebug(dcSimulation()) << "Closing roller shutter";
return DeviceManager::DeviceErrorNoError;
}
if (action.actionTypeId() == rollerShutterStopActionTypeId) {
qCDebug(dcSimulation()) << "Stopping roller shutter";
return DeviceManager::DeviceErrorNoError;
}
}
qCWarning(dcSimulation()) << "Unhandled device class" << device->deviceClassId() << "for device" << device->name();
return DeviceManager::DeviceErrorDeviceClassNotFound;
}
@ -304,3 +374,18 @@ void DevicePluginSimulation::onPluginTimer5Minutes()
}
}
void DevicePluginSimulation::simulationTimerTimeout()
{
QTimer *t = static_cast<QTimer*>(sender());
Device *device = m_simulationTimers.key(t);
if (device->deviceClassId() == garageGateDeviceClassId) {
if (device->stateValue(garageGateStateStateTypeId).toString() == "opening") {
device->setStateValue(garageGateIntermediatePositionStateTypeId, false);
device->setStateValue(garageGateStateStateTypeId, "open");
}
if (device->stateValue(garageGateStateStateTypeId).toString() == "closing") {
device->setStateValue(garageGateIntermediatePositionStateTypeId, false);
device->setStateValue(garageGateStateStateTypeId, "closed");
}
}
}

View File

@ -40,6 +40,7 @@ public:
void init() override;
DeviceManager::DeviceSetupStatus setupDevice(Device *device) override;
void deviceRemoved(Device *device) override;
DeviceManager::DeviceError executeAction(Device *device, const Action &action) override;
private:
@ -50,9 +51,11 @@ private:
double generateRandomDoubleValue(double min, double max);
bool generateRandomBoolValue();
QHash<Device*, QTimer*> m_simulationTimers;
private slots:
void onPluginTimer20Seconds();
void onPluginTimer5Minutes();
void simulationTimerTimeout();
};

View File

@ -259,21 +259,27 @@
},
{
"id": "cfb44bcf-b4b9-4bef-89f7-3a55baf35668",
"name": "garageDoor",
"displayName": "Garage Door",
"name": "garageGate",
"displayName": "Garage gate",
"createMethods": ["user"],
"deviceIcon": "Garage",
"basicTags": [
"Device",
"Actuator"
],
"interfaces": [ "garagegate" ],
"paramTypes": [ ],
"stateTypes": [
{
"id": "f786029d-f3a6-4b47-978a-ac1a581aac0f",
"name": "door",
"displayName": "Garage door",
"displayNameEvent": "Garage door status changed",
"name": "state",
"displayName": "Garage gate state",
"displayNameEvent": "Garage gate state changed",
"type": "QString",
"possibleValues": ["open", "closed", "opening", "closing"],
"defaultValue": "open"
},
{
"id": "324150cc-0357-4797-a746-37b554d82c44",
"name": "intermediatePosition",
"displayName": "Intermediate position",
"displayNameEvent": "Intermediate position changed",
"type": "bool",
"defaultValue": false
}
@ -302,11 +308,7 @@
"displayName": "Roller Shutter",
"createMethods": ["user"],
"deviceIcon": "RollerShutter",
"basicTags": [
"Device",
"Actuator",
"Shading"
],
"interfaces": ["shutter"],
"paramTypes": [ ],
"stateTypes": [
{