From a6a02ba6fb4edea30614d30e0b6ce6b84faa3a5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Mon, 22 Jun 2015 11:13:41 +0200 Subject: [PATCH] make writable states more clear in plugin json --- libguh/plugin/deviceplugin.cpp | 56 ++++--- libguh/plugin/deviceplugin.h | 1 + .../boblight/devicepluginboblight.json | 2 +- .../deviceplugins/eq-3/deviceplugineq-3.json | 4 +- .../deviceplugingenericelements.json | 2 +- .../philipshue/devicepluginphilipshue.json | 32 ++-- .../deviceplugins/tune/deviceplugintune.json | 18 ++- .../deviceplugins/wemo/devicepluginwemo.json | 2 +- plugins/generateplugininfo.py | 149 +++++++++--------- 9 files changed, 139 insertions(+), 127 deletions(-) diff --git a/libguh/plugin/deviceplugin.cpp b/libguh/plugin/deviceplugin.cpp index 475d3fb0..4b265353 100644 --- a/libguh/plugin/deviceplugin.cpp +++ b/libguh/plugin/deviceplugin.cpp @@ -243,21 +243,22 @@ QList DevicePlugin::supportedDevices() const stateTypes.append(stateType); // create ActionType if this StateType is writable - if (st.value("writable").toBool()) { + if (st.contains("writable")) { ActionType actionType(st.value("id").toString()); actionType.setName("set " + st.value("name").toString()); // Note: fields already checked in StateType ParamType paramType(st.value("name").toString(), t, st.value("defaultValue").toVariant()); - if (st.contains("allowedValues")) { + if (st.value("writable").toObject().contains("allowedValues")) { QVariantList allowedValues; - foreach (const QJsonValue &allowedTypesJson, st.value("allowedValues").toArray()) { + foreach (const QJsonValue &allowedTypesJson, st.value("writable").toObject().value("allowedValues").toArray()) { allowedValues.append(allowedTypesJson.toVariant()); } paramType.setAllowedValues(allowedValues); } - // states don't have input types + paramType.setInputType(inputTypeStringToInputType(st.value("writable").toObject().value("inputType").toString())); paramType.setUnit(unitStringToUnit(st.value("unit").toString())); - paramType.setLimits(st.value("minValue").toVariant(), st.value("maxValue").toVariant()); + paramType.setLimits(st.value("writable").toObject().value("minValue").toVariant(), + st.value("writable").toObject().value("maxValue").toVariant()); actionType.setParamTypes(QList() << paramType); actionTypes.append(actionType); } @@ -401,26 +402,7 @@ QList DevicePlugin::parseParamTypes(const QJsonArray &array) const } // set the input type if there is any if (pt.contains("inputType")) { - QString inputTypeString = pt.value("inputType").toString(); - if (inputTypeString == "TextLine") { - paramType.setInputType(Types::InputTypeTextLine); - } else if (inputTypeString == "TextArea") { - paramType.setInputType(Types::InputTypeTextArea); - } else if (inputTypeString == "Password") { - paramType.setInputType(Types::InputTypePassword); - } else if (inputTypeString == "Search") { - paramType.setInputType(Types::InputTypeSearch); - } else if (inputTypeString == "Mail") { - paramType.setInputType(Types::InputTypeMail); - } else if (inputTypeString == "IPv4Address") { - paramType.setInputType(Types::InputTypeIPv4Address); - } else if (inputTypeString == "IPv6Address") { - paramType.setInputType(Types::InputTypeIPv6Address); - } else if (inputTypeString == "Url") { - paramType.setInputType(Types::InputTypeUrl); - } else if (inputTypeString == "MacAddress") { - paramType.setInputType(Types::InputTypeMacAddress); - } + paramType.setInputType(inputTypeStringToInputType(pt.value("inputType").toString())); } // set the unit if there is any @@ -736,3 +718,27 @@ Types::Unit DevicePlugin::unitStringToUnit(const QString &unitString) const } return Types::UnitNone; } + +Types::InputType DevicePlugin::inputTypeStringToInputType(const QString &inputType) const +{ + if (inputType == "TextLine") { + return Types::InputTypeTextLine; + } else if (inputType == "TextArea") { + return Types::InputTypeTextArea; + } else if (inputType == "Password") { + return Types::InputTypePassword; + } else if (inputType == "Search") { + return Types::InputTypeSearch; + } else if (inputType == "Mail") { + return Types::InputTypeMail; + } else if (inputType == "IPv4Address") { + return Types::InputTypeIPv4Address; + } else if (inputType == "IPv6Address") { + return Types::InputTypeIPv6Address; + } else if (inputType == "Url") { + return Types::InputTypeUrl; + } else if (inputType == "MacAddress") { + return Types::InputTypeMacAddress; + } + return Types::InputTypeNone; +} diff --git a/libguh/plugin/deviceplugin.h b/libguh/plugin/deviceplugin.h index 41a1ed61..14de6588 100644 --- a/libguh/plugin/deviceplugin.h +++ b/libguh/plugin/deviceplugin.h @@ -113,6 +113,7 @@ private: QStringList verifyFields(const QStringList &fields, const QJsonObject &value) const; Types::Unit unitStringToUnit(const QString &unitString) const; + Types::InputType inputTypeStringToInputType(const QString &inputType) const; DeviceManager *m_deviceManager; diff --git a/plugins/deviceplugins/boblight/devicepluginboblight.json b/plugins/deviceplugins/boblight/devicepluginboblight.json index 6b678ad9..bf022a1b 100644 --- a/plugins/deviceplugins/boblight/devicepluginboblight.json +++ b/plugins/deviceplugins/boblight/devicepluginboblight.json @@ -18,7 +18,7 @@ "name": "color", "type": "QColor", "defaultValue": "#000000", - "writable": true + "writable": {} } ] } diff --git a/plugins/deviceplugins/eq-3/deviceplugineq-3.json b/plugins/deviceplugins/eq-3/deviceplugineq-3.json index bc91029e..edc953cc 100644 --- a/plugins/deviceplugins/eq-3/deviceplugineq-3.json +++ b/plugins/deviceplugins/eq-3/deviceplugineq-3.json @@ -210,7 +210,7 @@ "type": "double", "unit": "DegreeCelsius", "defaultValue": 0, - "writable": true + "writable": {} }, { @@ -373,7 +373,7 @@ "unit": "DegreeCelsius", "type": "double", "defaultValue": 0, - "writable": true + "writable": {} }, { "id": "576da571-9a65-478f-96bf-19256c8b9ece", diff --git a/plugins/deviceplugins/genericelements/deviceplugingenericelements.json b/plugins/deviceplugins/genericelements/deviceplugingenericelements.json index e4973387..bc905863 100644 --- a/plugins/deviceplugins/genericelements/deviceplugingenericelements.json +++ b/plugins/deviceplugins/genericelements/deviceplugingenericelements.json @@ -25,7 +25,7 @@ "name": "state", "type": "bool", "defaultValue": false, - "writable": true + "writable": {} } ] }, diff --git a/plugins/deviceplugins/philipshue/devicepluginphilipshue.json b/plugins/deviceplugins/philipshue/devicepluginphilipshue.json index bdb2482b..27da92a5 100644 --- a/plugins/deviceplugins/philipshue/devicepluginphilipshue.json +++ b/plugins/deviceplugins/philipshue/devicepluginphilipshue.json @@ -109,7 +109,7 @@ "name": "power", "type": "bool", "defaultValue": false, - "writable": true + "writable": {} }, { "id": "c0f4206f-f219-4f06-93c4-4ca515a56f79", @@ -117,10 +117,11 @@ "name": "color temperature", "type": "int", "unit": "Mired", - "writable": true, "defaultValue": 170, - "minValue": 154, - "maxValue": 500 + "writable": { + "minValue": 154, + "maxValue": 500 + } }, { "id": "d25423e7-b924-4b20-80b6-77eecc65d089", @@ -128,7 +129,7 @@ "name": "color", "type": "QColor", "defaultValue": "#000000", - "writable": true + "writable": {} }, { @@ -137,22 +138,25 @@ "name": "brightness", "type": "int", "unit": "Percentage", - "writable": true, - "minValue": 0, - "maxValue": 100, - "defaultValue": 0 + "defaultValue": 0, + "writable": { + "minValue": 0, + "maxValue": 100 + } }, { "id": "0b7cdd8d-4db8-4183-abe2-f3c01d1c9afc", "idName": "hueEffect", "name": "effect", "type": "QString", - "writable": true, "defaultValue": "none", - "allowedValues": [ - "none", - "color loop" - ] + "writable": { + "allowedValues": [ + "none", + "color loop" + ] + } + } ], "actionTypes": [ diff --git a/plugins/deviceplugins/tune/deviceplugintune.json b/plugins/deviceplugins/tune/deviceplugintune.json index 01a7d47d..3fef525f 100644 --- a/plugins/deviceplugins/tune/deviceplugintune.json +++ b/plugins/deviceplugins/tune/deviceplugintune.json @@ -53,7 +53,7 @@ "name": "active", "type": "bool", "defaultValue": false, - "writable": true + "writable": {} }, { "id": "cb8a89c2-dc12-4965-b047-57896058b421", @@ -61,10 +61,11 @@ "name": "value", "type": "int", "unit": "Percentage", - "minValue": 0, - "maxValue": 100, "defaultValue": 0, - "writable": true + "writable": { + "minValue": 0, + "maxValue": 100 + } } ] }, @@ -121,7 +122,7 @@ "name": "power", "type": "bool", "defaultValue": 0, - "writable": true + "writable": {} }, { "id": "677cd9ec-c264-47ee-9d2e-d3662237792c", @@ -129,10 +130,11 @@ "name": "brightness", "type": "int", "unit": "Percentage", - "minValue": 0, - "maxValue": 100, "defaultValue": 0, - "writable": true + "writable": { + "minValue": 0, + "maxValue": 100 + } } ] } diff --git a/plugins/deviceplugins/wemo/devicepluginwemo.json b/plugins/deviceplugins/wemo/devicepluginwemo.json index 6d0c742c..121ab6b5 100644 --- a/plugins/deviceplugins/wemo/devicepluginwemo.json +++ b/plugins/deviceplugins/wemo/devicepluginwemo.json @@ -39,7 +39,7 @@ "name": "power", "type": "bool", "defaultValue": false, - "writable": true + "writable": {} }, { diff --git a/plugins/generateplugininfo.py b/plugins/generateplugininfo.py index e95ae11d..d92760ac 100755 --- a/plugins/generateplugininfo.py +++ b/plugins/generateplugininfo.py @@ -9,99 +9,98 @@ outputfile = open(sys.argv[2], "w") variableNames = [] try: - pluginMap = json.loads(inputFile.read()) - + pluginMap = json.loads(inputFile.read()) + except ValueError as e: - print " --> Error loading input file \"%s\"" % (sys.argv[1]) - print " %s" % (e) - exit -1 + print " --> Error loading input file \"%s\"" % (sys.argv[1]) + print " %s" % (e) + exit -1 def out(line): - outputfile.write("%s\n" % line) + outputfile.write("%s\n" % line) def extractVendors(pluginMap): - for vendor in pluginMap['vendors']: - try: - out("VendorId %sVendorId = VendorId(\"%s\");" % pluginMap["idName"], pluginMap["id"]) - except: - pass - extractDeviceClasses(vendor) + for vendor in pluginMap['vendors']: + try: + out("VendorId %sVendorId = VendorId(\"%s\");" % pluginMap["idName"], pluginMap["id"]) + except: + pass + extractDeviceClasses(vendor) def extractDeviceClasses(vendorMap): - for deviceClass in vendorMap["deviceClasses"]: - print("have deviceclass %s" % deviceClass["deviceClassId"]) - try: - variableName = "%sDeviceClassId" % (deviceClass["idName"]) - if not variableName in variableNames: - variableNames.append(variableName) - out("DeviceClassId %s = DeviceClassId(\"%s\");" % (variableName, deviceClass["deviceClassId"])) - else: - print("duplicated variable name \"%s\" for DeviceClassId %s -> skipping") % (variableName, deviceClass["deviceClassId"]) - except: - pass - extractActionTypes(deviceClass) - extractStateTypes(deviceClass) - extractEventTypes(deviceClass) + for deviceClass in vendorMap["deviceClasses"]: + print("have deviceclass %s" % deviceClass["deviceClassId"]) + try: + variableName = "%sDeviceClassId" % (deviceClass["idName"]) + if not variableName in variableNames: + variableNames.append(variableName) + out("DeviceClassId %s = DeviceClassId(\"%s\");" % (variableName, deviceClass["deviceClassId"])) + else: + print("duplicated variable name \"%s\" for DeviceClassId %s -> skipping") % (variableName, deviceClass["deviceClassId"]) + except: + pass + extractActionTypes(deviceClass) + extractStateTypes(deviceClass) + extractEventTypes(deviceClass) def extractStateTypes(deviceClassMap): - try: - for stateType in deviceClassMap["stateTypes"]: - try: - variableName = "%sStateTypeId" % (stateType["idName"]) - if not variableName in variableNames: - variableNames.append(variableName) - out("StateTypeId %s = StateTypeId(\"%s\");" % (variableName, stateType["id"])) - else: - print("duplicated variable name \"%s\" for StateTypeId %s -> skipping") % (variableName, stateType["id"]) - # create ActionTypeId if the state is writable - if 'writable' in stateType: - if stateType['writable'] == True: - print("create ActionTypeId for StateType %s" % stateType["id"]) - vName = "%sActionTypeId" % (stateType["idName"]) - if not vName in variableNames: - variableNames.append(vName) - out("ActionTypeId %s = ActionTypeId(\"%s\");" % (vName, stateType["id"])) - else: - print("duplicated variable name \"%s\" for ActionTypeId %s -> skipping") % (variableName, stateType["id"]) - except: - pass - except: - pass + try: + for stateType in deviceClassMap["stateTypes"]: + try: + variableName = "%sStateTypeId" % (stateType["idName"]) + if not variableName in variableNames: + variableNames.append(variableName) + out("StateTypeId %s = StateTypeId(\"%s\");" % (variableName, stateType["id"])) + else: + print("duplicated variable name \"%s\" for StateTypeId %s -> skipping") % (variableName, stateType["id"]) + # create ActionTypeId if the state is writable + if 'writable' in stateType: + print("create ActionTypeId for StateType %s" % stateType["id"]) + vName = "%sActionTypeId" % (stateType["idName"]) + if not vName in variableNames: + variableNames.append(vName) + out("ActionTypeId %s = ActionTypeId(\"%s\");" % (vName, stateType["id"])) + else: + print("duplicated variable name \"%s\" for ActionTypeId %s -> skipping") % (variableName, stateType["id"]) + except: + pass + except: + pass def extractActionTypes(deviceClassMap): - try: - for actionType in deviceClassMap["actionTypes"]: - try: - variableName = "%sActionTypeId" % (actionType["idName"]) - if not variableName in variableNames: - variableNames.append(variableName) - out("ActionTypeId %s = ActionTypeId(\"%s\");" % (variableName, actionType["id"])) - else: - print("duplicated variable name \"%s\" for ActionTypeId %s -> skipping") % (variableName, actionType["id"]) - except: - pass - except: - pass + try: + for actionType in deviceClassMap["actionTypes"]: + try: + variableName = "%sActionTypeId" % (actionType["idName"]) + if not variableName in variableNames: + variableNames.append(variableName) + out("ActionTypeId %s = ActionTypeId(\"%s\");" % (variableName, actionType["id"])) + else: + print("duplicated variable name \"%s\" for ActionTypeId %s -> skipping") % (variableName, actionType["id"]) + except: + pass + except: + pass def extractEventTypes(deviceClassMap): - try: - for eventType in deviceClassMap["eventTypes"]: - try: - variableName = "%sEventTypeId" % (eventType["idName"]) - if not variableName in variableNames: - variableNames.append(variableName) - out("EventTypeId %s = EventTypeId(\"%s\");" % (variableName, eventType["id"])) - else: - print("duplicated variable name \"%s\" for EventTypeId %s -> skipping") % (variableName, eventType["id"]) - except: - pass - except: - pass + try: + for eventType in deviceClassMap["eventTypes"]: + try: + variableName = "%sEventTypeId" % (eventType["idName"]) + if not variableName in variableNames: + variableNames.append(variableName) + out("EventTypeId %s = EventTypeId(\"%s\");" % (variableName, eventType["id"])) + else: + print("duplicated variable name \"%s\" for EventTypeId %s -> skipping") % (variableName, eventType["id"]) + except: + pass + except: + pass print " --> generate plugininfo.h"