From 562e7aa89df1388da4e0acd35a86ec138887ec98 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Thu, 16 Nov 2017 12:46:48 +0100 Subject: [PATCH] update how id names are generated include the deviceClass/plugin name in all defines to avoid collisions between deviceClasses within the same file. So far this hasn't really been an issue because using idName we could set random ids. Now interfaces dictate the names, so having multiple deviceClasses in one file and both implementing the same interface would clash. This also should improve readability in the plugins code as we won't have ids like: "bridgeConnected" and "connected" available which easily causes the developer to accidentally use "connected" where instead "bridgeConnected" should be used (I actually found some bugs like this while updating plugins for this). The new style would force those states to be named like e.g. "bridgeConnected" and "lightConnected" which are not as easy to mix up. --- plugins/guh-generateplugininfo | 24 +++--- plugins/mock/devicepluginmock.cpp | 127 +++++++++++++++++++++--------- plugins/mock/httpdaemon.cpp | 2 +- server/main.cpp | 3 +- 4 files changed, 104 insertions(+), 52 deletions(-) diff --git a/plugins/guh-generateplugininfo b/plugins/guh-generateplugininfo index d414a942..87adb204 100755 --- a/plugins/guh-generateplugininfo +++ b/plugins/guh-generateplugininfo @@ -77,7 +77,7 @@ def extractPlugin(pluginMap): def extractParamTypes(paramTypes, contextName): for paramType in paramTypes: try: - variableName = '%sParamTypeId' % (paramType['name']) + variableName = '%sParamTypeId' % (contextName + paramType['name'][0].capitalize() + paramType['name'][1:]) if not variableName in variableNames: variableNames.append(variableName) printInfo('Define ParamTypeId %s = %s' % (variableName, paramType['id'])) @@ -155,7 +155,7 @@ def extractStateTypes(stateTypes, deviceClassName): for stateType in stateTypes: try: # Define StateType - variableName = '%sStateTypeId' % (stateType['name']) + variableName = '%s%sStateTypeId' % (deviceClassName, stateType['name'][0].capitalize() + stateType['name'][1:]) #addTranslationString(stateType['name'], 'The name of the stateType (%s) of DeviceClass %s' % (stateType['id'], deviceClassName)) if not variableName in variableNames: variableNames.append(variableName) @@ -167,7 +167,7 @@ def extractStateTypes(stateTypes, deviceClassName): printWarning('Duplicated variable name \"%s\" for StateTypeId %s -> skipping' % (variableName, stateType['id'])) # Create EventTypeId for this state - variableName = '%sEventTypeId' % (stateType['name']) + variableName = '%s%sEventTypeId' % (deviceClassName, stateType['name'][0].capitalize() + stateType['name'][1:]) if not variableName in variableNames: addTranslationString(stateType['displayNameEvent'], 'The name of the autocreated EventType (%s)' % stateType['id']) variableNames.append(variableName) @@ -179,7 +179,7 @@ def extractStateTypes(stateTypes, deviceClassName): printWarning('Duplicated variable name \"%s\" for autocreated EventTypeId %s -> skipping' % (variableName, stateType['id'])) #ParamType for EventType/ActionType - variableName = '%sStateParamTypeId' % (stateType['name']) + variableName = '%s%sStateParamTypeId' % (deviceClassName, stateType['name'][0].capitalize() + stateType['name'][1:]) if not variableName in variableNames: variableNames.append(variableName) printInfo('Define ParamTypeId %s for StateType %s = %s' % (variableName, variableName, stateType['id'])) @@ -192,7 +192,7 @@ def extractStateTypes(stateTypes, deviceClassName): # Create ActionTypeId if the state is writable if 'writable' in stateType and stateType['writable']: - variableName = '%sActionTypeId' % (stateType['name']) + variableName = '%s%sActionTypeId' % (deviceClassName, stateType['name'][0].capitalize() + stateType['name'][1:]) if not variableName in variableNames: variableNames.append(variableName) printInfo('Define ActionTypeId for writable StateType %s = %s' % (variableName, stateType['id'])) @@ -212,7 +212,7 @@ def extractActionTypes(actionTypes, deviceClassName): for actionType in actionTypes: try: # Define ActionTypeId - variableName = '%sActionTypeId' % (actionType['name']) + variableName = '%s%sActionTypeId' % (deviceClassName, actionType['name'][0].capitalize() + actionType['name'][1:]) if not variableName in variableNames: variableNames.append(variableName) addTranslationString(actionType['displayName'], 'The name of the ActionType %s of deviceClass %s' % (actionType['id'], deviceClassName)) @@ -235,7 +235,7 @@ def extractEventTypes(eventTypes, deviceClassName): for eventType in eventTypes: try: # Define EventTypeId - variableName = '%sEventTypeId' % (eventType['name']) + variableName = '%s%sEventTypeId' % (deviceClassName, eventType['name'][0].capitalize() + eventType['name'][1:]) if not variableName in variableNames: variableNames.append(variableName) addTranslationString(eventType['displayName'], 'The name of the EventType %s of deviceClass %s' % (eventType['id'], deviceClassName)) @@ -320,9 +320,10 @@ def writePluginInfoFile(): writeToFile('// Logging category') if 'name' in pluginMap: - writeToFile('Q_DECLARE_LOGGING_CATEGORY(dc%s)' % pluginMap['name']) - writeToFile('Q_LOGGING_CATEGORY(dc%s, \"%s\")' % (pluginMap['name'], pluginMap['name'])) - printInfo('Define logging category: \'dc%s\'' % pluginMap['name']) + debugCategoryName = pluginMap['name'][0].capitalize() + pluginMap['name'][1:] + writeToFile('Q_DECLARE_LOGGING_CATEGORY(dc%s)' % debugCategoryName) + writeToFile('Q_LOGGING_CATEGORY(dc%s, \"%s\")' % (debugCategoryName, debugCategoryName)) + printInfo('Define logging category: \'dc%s\'' % debugCategoryName) writeToFile('') @@ -359,7 +360,8 @@ def writeExternPluginInfoFile(): writeToFile('// Logging category definition') if 'name' in pluginMap: - writeToFile('Q_DECLARE_LOGGING_CATEGORY(dc%s)' % pluginMap['name']) + debugCategoryName = pluginMap['name'][0].capitalize() + pluginMap['name'][1:] + writeToFile('Q_DECLARE_LOGGING_CATEGORY(dc%s)' % debugCategoryName) writeToFile('') writeToFile('#endif // EXTERNPLUGININFO_H') diff --git a/plugins/mock/devicepluginmock.cpp b/plugins/mock/devicepluginmock.cpp index ba47fb64..200a47f8 100644 --- a/plugins/mock/devicepluginmock.cpp +++ b/plugins/mock/devicepluginmock.cpp @@ -63,19 +63,19 @@ DevicePluginMock::~DevicePluginMock() DeviceManager::DeviceError DevicePluginMock::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) { - if (deviceClassId == mockDeviceClassId || deviceClassId == mockDeviceAutoDeviceClassId) { + if (deviceClassId == mockDeviceClassId) { qCDebug(dcMockDevice) << "starting mock discovery:" << params; - m_discoveredDeviceCount = params.paramValue(resultCountParamTypeId).toInt(); + m_discoveredDeviceCount = params.paramValue(mockResultCountParamTypeId).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(resultCountParamTypeId).toInt(); + m_discoveredDeviceCount = params.paramValue(mockPushButtonResultCountParamTypeId).toInt(); QTimer::singleShot(1000, this, SLOT(emitPushButtonDevicesDiscovered())); return DeviceManager::DeviceErrorAsync; } else if (deviceClassId == mockDisplayPinDeviceClassId) { qCDebug(dcMockDevice) << "starting mock display pin discovery:" << params; - m_discoveredDeviceCount = params.paramValue(resultCountParamTypeId).toInt(); + m_discoveredDeviceCount = params.paramValue(mockDisplayPinResultCountParamTypeId).toInt(); QTimer::singleShot(1000, this, SLOT(emitDisplayPinDevicesDiscovered())); return DeviceManager::DeviceErrorAsync; } @@ -85,12 +85,17 @@ DeviceManager::DeviceError DevicePluginMock::discoverDevices(const DeviceClassId DeviceManager::DeviceSetupStatus DevicePluginMock::setupDevice(Device *device) { if (device->deviceClassId() == mockDeviceClassId || device->deviceClassId() == mockDeviceAutoDeviceClassId) { - qCDebug(dcMockDevice) << "Mockdevice created returning true" - << device->paramValue(httpportParamTypeId).toInt() - << device->paramValue(asyncParamTypeId).toBool() - << device->paramValue(brokenParamTypeId).toBool(); + bool async = false; + bool broken = false; + if (device->deviceClassId() == mockDeviceClassId) { + async = device->paramValue(mockAsyncParamTypeId).toBool(); + broken = device->paramValue(mockBrokenParamTypeId).toBool(); + } else { + async = device->paramValue(mockDeviceAutoAsyncParamTypeId).toBool(); + broken = device->paramValue(mockDeviceAutoBrokenParamTypeId).toBool(); + } - if (device->paramValue(brokenParamTypeId).toBool()) { + if (broken) { qCWarning(dcMockDevice) << "This device is intentionally broken."; return DeviceManager::DeviceSetupStatusFailure; } @@ -108,7 +113,7 @@ DeviceManager::DeviceSetupStatus DevicePluginMock::setupDevice(Device *device) // Keep this queued or it might happen that the HttpDaemon is deleted before it is able to reply to the caller connect(daemon, &HttpDaemon::disappear, this, &DevicePluginMock::onDisappear, Qt::QueuedConnection); - if (device->paramValue(asyncParamTypeId).toBool()) { + if (async) { m_asyncSetupDevices.append(device); QTimer::singleShot(1000, this, SLOT(emitDeviceSetupFinished())); return DeviceManager::DeviceSetupStatusAsync; @@ -125,7 +130,7 @@ DeviceManager::DeviceSetupStatus DevicePluginMock::setupDevice(Device *device) return DeviceManager::DeviceSetupStatusSuccess; } else if (device->deviceClassId() == mockChildDeviceClassId) { qCDebug(dcMockDevice) << "Setup Child mock device" << device->params(); - device->setParentId(DeviceId(device->params().paramValue(parentUuidParamTypeId).toString())); + device->setParentId(DeviceId(device->params().paramValue(mockChildParentUuidParamTypeId).toString())); return DeviceManager::DeviceSetupStatusSuccess; } else if (device->deviceClassId() == mockInputTypeDeviceClassId) { qCDebug(dcMockDevice) << "Setup InputType mock device" << device->params(); @@ -166,7 +171,7 @@ void DevicePluginMock::startMonitoringAutoDevices() ParamList params; qsrand(QDateTime::currentMSecsSinceEpoch()); int port = 4242 + (qrand() % 1000); - Param param(httpportParamTypeId, port); + Param param(mockDeviceAutoHttpportParamTypeId, port); params.append(param); mockDescriptor.setParams(params); @@ -218,50 +223,94 @@ DeviceManager::DeviceError DevicePluginMock::displayPin(const PairingTransaction DeviceManager::DeviceError DevicePluginMock::executeAction(Device *device, const Action &action) { - if (device->deviceClassId() == mockDeviceClassId || device->deviceClassId() == mockDeviceAutoDeviceClassId) { - if (!myDevices().contains(device)) - return DeviceManager::DeviceErrorDeviceNotFound; + if (!myDevices().contains(device)) + return DeviceManager::DeviceErrorDeviceNotFound; - if (action.actionTypeId() == mockAsyncActionTypeId || action.actionTypeId() == mockAsyncFailingActionTypeId) { + if (device->deviceClassId() == mockDeviceClassId) { + if (action.actionTypeId() == mockMockAsyncActionTypeId || action.actionTypeId() == mockMockAsyncFailingActionTypeId) { m_asyncActions.append(qMakePair(action, device)); QTimer::singleShot(1000, this, SLOT(emitActionExecuted())); return DeviceManager::DeviceErrorAsync; } - if (action.actionTypeId() == mockFailingActionTypeId) + if (action.actionTypeId() == mockMockFailingActionTypeId) return DeviceManager::DeviceErrorSetupFailed; m_daemons.value(device)->actionExecuted(action.actionTypeId()); return DeviceManager::DeviceErrorNoError; - } else if (device->deviceClassId() == mockPushButtonDeviceClassId || device->deviceClassId() == mockDisplayPinDeviceClassId) { - if (action.actionTypeId() == colorActionTypeId) { - QString colorString = action.param(colorStateParamTypeId).value().toString(); + } else if (device->deviceClassId() == mockDeviceAutoDeviceClassId) { + if (action.actionTypeId() == mockDeviceAutoMockActionAsyncActionTypeId || action.actionTypeId() == mockDeviceAutoMockActionAsyncBrokenActionTypeId) { + m_asyncActions.append(qMakePair(action, device)); + QTimer::singleShot(1000, this, SLOT(emitActionExecuted())); + return DeviceManager::DeviceErrorAsync; + } + + if (action.actionTypeId() == mockDeviceAutoMockActionBrokenActionTypeId) + return DeviceManager::DeviceErrorSetupFailed; + + m_daemons.value(device)->actionExecuted(action.actionTypeId()); + return DeviceManager::DeviceErrorNoError; + } else if (device->deviceClassId() == mockPushButtonDeviceClassId) { + if (action.actionTypeId() == mockPushButtonColorActionTypeId) { + QString colorString = action.param(mockPushButtonColorStateParamTypeId).value().toString(); QColor color(colorString); if (!color.isValid()) { qCWarning(dcMockDevice) << "Invalid color parameter"; return DeviceManager::DeviceErrorInvalidParameter; } - device->setStateValue(colorStateTypeId, colorString); + device->setStateValue(mockPushButtonColorStateTypeId, colorString); return DeviceManager::DeviceErrorNoError; - } else if (action.actionTypeId() == percentageActionTypeId) { - device->setStateValue(percentageStateTypeId, action.param(percentageStateParamTypeId).value().toInt()); + } else if (action.actionTypeId() == mockPushButtonPercentageActionTypeId) { + device->setStateValue(mockPushButtonPercentageStateTypeId, action.param(mockPushButtonPercentageStateParamTypeId).value().toInt()); return DeviceManager::DeviceErrorNoError; - } else if (action.actionTypeId() == allowedValuesActionTypeId) { - device->setStateValue(allowedValuesStateTypeId, action.param(allowedValuesStateParamTypeId).value().toString()); + } else if (action.actionTypeId() == mockPushButtonAllowedValuesActionTypeId) { + device->setStateValue(mockPushButtonAllowedValuesStateTypeId, action.param(mockPushButtonAllowedValuesStateParamTypeId).value().toString()); return DeviceManager::DeviceErrorNoError; - } else if (action.actionTypeId() == doubleActionTypeId) { - device->setStateValue(doubleStateTypeId, action.param(doubleStateParamTypeId).value().toDouble()); + } else if (action.actionTypeId() == mockPushButtonDoubleActionTypeId) { + device->setStateValue(mockPushButtonDoubleStateTypeId, action.param(mockPushButtonDoubleStateParamTypeId).value().toDouble()); return DeviceManager::DeviceErrorNoError; - } else if (action.actionTypeId() == boolActionTypeId) { - device->setStateValue(boolValueStateTypeId, action.param(boolValueStateParamTypeId).value().toBool()); + } else if (action.actionTypeId() == mockPushButtonBoolActionTypeId) { + device->setStateValue(mockPushButtonBoolStateTypeId, action.param(mockPushButtonBoolStateParamTypeId).value().toBool()); return DeviceManager::DeviceErrorNoError; - } else if (action.actionTypeId() == timeoutActionTypeId) { + } else if (action.actionTypeId() == mockPushButtonTimeoutActionTypeId) { return DeviceManager::DeviceErrorAsync; } return DeviceManager::DeviceErrorActionTypeNotFound; - } else if (device->deviceClassId() == mockParentDeviceClassId || device->deviceClassId() == mockChildDeviceClassId) { - if (action.actionTypeId() == boolValueParentActionTypeId) { - device->setStateValue(boolValueParentStateTypeId, action.param(boolValueParentStateParamTypeId).value().toBool()); + } else if (device->deviceClassId() == mockDisplayPinDeviceClassId) { + if (action.actionTypeId() == mockDisplayPinColorActionTypeId) { + QString colorString = action.param(mockDisplayPinColorStateParamTypeId).value().toString(); + QColor color(colorString); + if (!color.isValid()) { + qCWarning(dcMockDevice) << "Invalid color parameter"; + return DeviceManager::DeviceErrorInvalidParameter; + } + device->setStateValue(mockDisplayPinColorStateTypeId, colorString); + return DeviceManager::DeviceErrorNoError; + } else if (action.actionTypeId() == mockDisplayPinPercentageActionTypeId) { + device->setStateValue(mockDisplayPinPercentageStateTypeId, action.param(mockDisplayPinPercentageStateParamTypeId).value().toInt()); + return DeviceManager::DeviceErrorNoError; + } else if (action.actionTypeId() == mockDisplayPinAllowedValuesActionTypeId) { + device->setStateValue(mockDisplayPinAllowedValuesStateTypeId, action.param(mockDisplayPinAllowedValuesStateParamTypeId).value().toString()); + return DeviceManager::DeviceErrorNoError; + } else if (action.actionTypeId() == mockDisplayPinDoubleActionTypeId) { + device->setStateValue(mockDisplayPinDoubleStateTypeId, action.param(mockDisplayPinDoubleStateParamTypeId).value().toDouble()); + return DeviceManager::DeviceErrorNoError; + } else if (action.actionTypeId() == mockDisplayPinBoolActionTypeId) { + device->setStateValue(mockDisplayPinBoolStateTypeId, action.param(mockDisplayPinBoolStateParamTypeId).value().toBool()); + return DeviceManager::DeviceErrorNoError; + } else if (action.actionTypeId() == mockDisplayPinTimeoutActionTypeId) { + return DeviceManager::DeviceErrorAsync; + } + return DeviceManager::DeviceErrorActionTypeNotFound; + } else if (device->deviceClassId() == mockParentDeviceClassId) { + if (action.actionTypeId() == mockParentBoolValueParentActionTypeId) { + device->setStateValue(mockParentBoolValueParentStateTypeId, action.param(mockParentBoolValueParentStateParamTypeId).value().toBool()); + return DeviceManager::DeviceErrorNoError; + } + return DeviceManager::DeviceErrorActionTypeNotFound; + } else if (device->deviceClassId() == mockChildDeviceClassId) { + if (action.actionTypeId() == mockChildBoolValueParentActionTypeId) { + device->setStateValue(mockChildBoolValueParentStateTypeId, action.param(mockChildBoolValueParentStateParamTypeId).value().toBool()); return DeviceManager::DeviceErrorNoError; } return DeviceManager::DeviceErrorActionTypeNotFound; @@ -311,7 +360,7 @@ void DevicePluginMock::emitDevicesDiscovered() if (m_discoveredDeviceCount > 0) { DeviceDescriptor d1(mockDeviceClassId, "Mock Device 1 (Discovered)", "55555"); ParamList params; - Param httpParam(httpportParamTypeId, "55555"); + Param httpParam(mockHttpportParamTypeId, "55555"); params.append(httpParam); d1.setParams(params); deviceDescriptors.append(d1); @@ -320,7 +369,7 @@ void DevicePluginMock::emitDevicesDiscovered() if (m_discoveredDeviceCount > 1) { DeviceDescriptor d2(mockDeviceClassId, "Mock Device 2 (Discovered)", "55556"); ParamList params; - Param httpParam(httpportParamTypeId, "55556"); + Param httpParam(mockHttpportParamTypeId, "55556"); params.append(httpParam); d2.setParams(params); deviceDescriptors.append(d2); @@ -376,7 +425,7 @@ void DevicePluginMock::emitDeviceSetupFinished() { qCDebug(dcMockDevice) << "Emitting setup finised"; Device *device = m_asyncSetupDevices.takeFirst(); - if (device->paramValue(brokenParamTypeId).toBool()) { + if (device->paramValue(mockBrokenParamTypeId).toBool()) { emit deviceSetupFinished(device, DeviceManager::DeviceSetupStatusFailure); } else { emit deviceSetupFinished(device, DeviceManager::DeviceSetupStatusSuccess); @@ -386,10 +435,10 @@ void DevicePluginMock::emitDeviceSetupFinished() void DevicePluginMock::emitActionExecuted() { QPair action = m_asyncActions.takeFirst(); - if (action.first.actionTypeId() == mockAsyncActionTypeId) { + if (action.first.actionTypeId() == mockMockAsyncActionTypeId) { m_daemons.value(action.second)->actionExecuted(action.first.actionTypeId()); emit actionExecutionFinished(action.first.id(), DeviceManager::DeviceErrorNoError); - } else if (action.first.actionTypeId() == mockAsyncFailingActionTypeId) { + } else if (action.first.actionTypeId() == mockMockAsyncFailingActionTypeId) { emit actionExecutionFinished(action.first.id(), DeviceManager::DeviceErrorSetupFailed); } } @@ -411,7 +460,7 @@ void DevicePluginMock::onChildDeviceDiscovered(const DeviceId &parentId) qCDebug(dcMockDevice) << "Child device discovered for parent" << parentId.toString(); DeviceDescriptor mockDescriptor(mockChildDeviceClassId, "Child Mock Device (Auto created)"); ParamList params; - params.append(Param(parentUuidParamTypeId, parentId)); + params.append(Param(mockChildParentUuidParamTypeId, parentId)); mockDescriptor.setParams(params); emit autoDevicesAppeared(mockChildDeviceClassId, QList() << mockDescriptor); diff --git a/plugins/mock/httpdaemon.cpp b/plugins/mock/httpdaemon.cpp index a9ccfc7e..d42e7829 100644 --- a/plugins/mock/httpdaemon.cpp +++ b/plugins/mock/httpdaemon.cpp @@ -39,7 +39,7 @@ HttpDaemon::HttpDaemon(Device *device, DevicePlugin *parent): QTcpServer(parent), disabled(false), m_plugin(parent), m_device(device) { - listen(QHostAddress::Any, device->paramValue(httpportParamTypeId).toInt()); + listen(QHostAddress::Any, device->paramValue(mockHttpportParamTypeId).toInt()); } HttpDaemon::~HttpDaemon() diff --git a/server/main.cpp b/server/main.cpp index 30730c66..f24e4909 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -141,7 +141,8 @@ int main(int argc, char *argv[]) QHash loggingFiltersPlugins; foreach (const QJsonObject &pluginMetadata, DeviceManager::pluginsMetadata()) { - loggingFiltersPlugins.insert(pluginMetadata.value("name").toString(), false); + QString pluginName = pluginMetadata.value("name").toString(); + loggingFiltersPlugins.insert(pluginName.left(1).toUpper() + pluginName.mid(1), false); } // Translator for the server application