From ae26b505b1edecf84ba59bb016fe4d36a112b1b2 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Sat, 12 Dec 2020 13:49:48 +0100 Subject: [PATCH] Auto-fill in parameters for push notifications thing (Android) --- libnymea-app/libnymea-app-core.h | 3 ++ .../ui/thingconfiguration/SetupWizard.qml | 48 +++++++++++++++---- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/libnymea-app/libnymea-app-core.h b/libnymea-app/libnymea-app-core.h index c5e72b51..e9a4bbbd 100644 --- a/libnymea-app/libnymea-app-core.h +++ b/libnymea-app/libnymea-app-core.h @@ -200,8 +200,11 @@ void registerQmlTypes() { qmlRegisterUncreatableType(uri, 1, 0, "DeviceClasses", "Can't create this in QML. Get it from the DeviceManager."); qmlRegisterType(uri, 1, 0, "DeviceClassesProxy"); qmlRegisterType(uri, 1, 0, "DeviceDiscovery"); + qmlRegisterType(uri, 1, 0, "ThingDiscovery"); qmlRegisterType(uri, 1, 0, "DeviceDiscoveryProxy"); + qmlRegisterType(uri, 1, 0, "ThingDiscoveryProxy"); qmlRegisterUncreatableType(uri, 1, 0, "DeviceDescriptor", "Get it from DeviceDiscovery"); + qmlRegisterUncreatableType(uri, 1, 0, "ThingDescriptor", "Get it from ThingDiscovery"); qmlRegisterType(uri, 1, 0, "DeviceModel"); diff --git a/nymea-app/ui/thingconfiguration/SetupWizard.qml b/nymea-app/ui/thingconfiguration/SetupWizard.qml index 160d44d2..3145de8c 100644 --- a/nymea-app/ui/thingconfiguration/SetupWizard.qml +++ b/nymea-app/ui/thingconfiguration/SetupWizard.qml @@ -40,10 +40,12 @@ import "../delegates" Page { id: root - property DeviceClass deviceClass: device ? device.deviceClass : null + property ThingClass thingClass: thing ? thing.thingClass : null + property alias deviceClass: root.thingClass // Optional: If set, it will be reconfigred, otherwise a new one will be created - property Device device: null + property Thing thing: null + property alias device: root.thing // Transitional, use thing instead signal done(); @@ -66,7 +68,8 @@ Page { QtObject { id: d property var vendorId: null - property DeviceDescriptor deviceDescriptor: null + property ThingDescriptor thingDescriptor: null + property alias deviceDescriptor: d.thingDescriptor property var discoveryParams: [] property string deviceName: "" property int pairRequestId: 0 @@ -89,8 +92,8 @@ Page { } else if (root.deviceClass.createMethods.indexOf("CreateMethodUser") !== -1) { print("CreateMethodUser") // Setting up a new device - if (!root.device) { - print("New device. Opening params page") + if (!root.thing) { + print("New thing setup") internalPageStack.push(paramsPage) // Reconfigure @@ -367,7 +370,8 @@ Page { } TextField { id: nameTextField - text: d.deviceName ? d.deviceName : root.deviceClass.displayName + text: (d.deviceName ? d.deviceName : root.deviceClass.displayName) + + (root.thingClass.id.toString().match(/\{?f0dd4c03-0aca-42cc-8f34-9902457b05de\}?/) ? " (" + PlatformHelper.machineHostname + ")" : "") Layout.fillWidth: true Layout.leftMargin: app.margins Layout.rightMargin: app.margins @@ -386,9 +390,35 @@ Page { Layout.fillWidth: true enabled: !model.readOnly paramType: root.deviceClass.paramTypes.get(index) - value: d.deviceDescriptor && d.deviceDescriptor.params.getParam(paramType.id) ? - d.deviceDescriptor.params.getParam(paramType.id).value : - root.deviceClass.paramTypes.get(index).defaultValue + visible: root.thingClass.id.toString().match(/\{?f0dd4c03-0aca-42cc-8f34-9902457b05de\}?/) === null + value: { + // Discovery, use params from discovered descriptor + if (d.thingDescriptor && d.thingDescriptor.params.getParam(paramType.id)) { + return d.thingDescriptor.params.getParam(paramType.id).value + } + + // Special hook for push notifications as we need to provide the token implicitly + print("Setting up params for thing class:", root.thingClass.id, root.thingClass.name) + if (root.thingClass.id.toString().match(/\{?f0dd4c03-0aca-42cc-8f34-9902457b05de\}?/)) { + print("It's push notifications. Token is:", PushNotifications.token, "Platform is:", Qt.platform.os); + if (paramType.id.toString().match(/\{?3cb8e30e-2ec5-4b4b-8c8c-03eaf7876839\}?/)) { + if (Qt.platform.os == "android") { + return "GCM"; + } else { + print("Unsupported platform for push notifications!") + } + } + if (paramType.id.toString().match(/\{?12ec06b2-44e7-486a-9169-31c684b91c8f\}?/)) { + return PushNotifications.token; + } + if (paramType.id.toString().match(/\{?d76da367-64e3-4b7d-aa84-c96b3acfb65e\}?/)) { + return PlatformHelper.deviceSerial + "+io.guh.nymeaapp" + (appBranding.length > 0 ? "-" + appBranding : ""); + } + } + + // Manual setup, use default value from thing class + return root.thingClass.paramTypes.get(index).defaultValue + } } }