diff options
author | Ryan Chu <[email protected]> | 2018-05-29 09:34:50 +0200 |
---|---|---|
committer | Ryan Chu <[email protected]> | 2018-06-26 13:22:42 +0000 |
commit | bf62d04b3d6aad292b7dab46d7c4567965450537 (patch) | |
tree | 86402c72fb81b595bc844feeb98dbfe621ddf8aa | |
parent | f338e8cb5ee89df15d41f6559c19718fd889df86 (diff) |
This example demonstrates receiving push notifications from the Kaltiot
IoT Cloud, and then displays the content on the screen. The Kaltiot
gateway daemon will notify the registered application that the new
message is available to sync. You can send push notifications to the
devices running this example by using the IoT-Devices service in the
Kaltiot Console or Kaltiot HTTP Rest API with curl command. This example
works on both Linux64 and Raspberry Pi devices.
Task-number: QTPM-182
Change-Id: Ifbcf3efbbcdef28e44ce46d019806510dc34194b
Reviewed-by: Ryan Chu <[email protected]>
Reviewed-by: Ari Salmi <[email protected]>
Reviewed-by: Timur Pocheptsov <[email protected]>
-rw-r--r-- | examples/examples.pro | 3 | ||||
-rw-r--r-- | examples/kaltiot/kaltiot.pro | 2 | ||||
-rw-r--r-- | examples/kaltiot/smartiot/doc/images/smartiot-example.png | bin | 0 -> 18812 bytes | |||
-rw-r--r-- | examples/kaltiot/smartiot/doc/src/qtcloudmessaging-kaltiot-smartiot.qdoc | 63 | ||||
-rw-r--r-- | examples/kaltiot/smartiot/main.cpp | 78 | ||||
-rw-r--r-- | examples/kaltiot/smartiot/main.qrc | 5 | ||||
-rw-r--r-- | examples/kaltiot/smartiot/qml/main.qml | 87 | ||||
-rw-r--r-- | examples/kaltiot/smartiot/smartiot.cpp | 100 | ||||
-rw-r--r-- | examples/kaltiot/smartiot/smartiot.h | 78 | ||||
-rw-r--r-- | examples/kaltiot/smartiot/smartiot.pro | 20 |
10 files changed, 436 insertions, 0 deletions
diff --git a/examples/examples.pro b/examples/examples.pro index c39edda..c1748a9 100644 --- a/examples/examples.pro +++ b/examples/examples.pro @@ -2,3 +2,6 @@ TEMPLATE = subdirs # Example of Firebase (Cloud Messaging) Push Notification firebase: SUBDIRS += firebase + +# Example of Kaltiot (SmartIoT) Push Notification +embedded-kaltiot: SUBDIRS += kaltiot diff --git a/examples/kaltiot/kaltiot.pro b/examples/kaltiot/kaltiot.pro new file mode 100644 index 0000000..524eb93 --- /dev/null +++ b/examples/kaltiot/kaltiot.pro @@ -0,0 +1,2 @@ +TEMPLATE = subdirs +SUBDIRS += smartiot diff --git a/examples/kaltiot/smartiot/doc/images/smartiot-example.png b/examples/kaltiot/smartiot/doc/images/smartiot-example.png Binary files differnew file mode 100644 index 0000000..1f569ce --- /dev/null +++ b/examples/kaltiot/smartiot/doc/images/smartiot-example.png diff --git a/examples/kaltiot/smartiot/doc/src/qtcloudmessaging-kaltiot-smartiot.qdoc b/examples/kaltiot/smartiot/doc/src/qtcloudmessaging-kaltiot-smartiot.qdoc new file mode 100644 index 0000000..080f6dd --- /dev/null +++ b/examples/kaltiot/smartiot/doc/src/qtcloudmessaging-kaltiot-smartiot.qdoc @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: https://www.gnu.org/licenses/fdl-1.3.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example kaltiot/smartiot + \title Kaltiot Push Notification Example + \ingroup examples-qtcloudmessaging + \brief Demonstrates Kaltiot Smart IoT support with Qt. + + \image smartiot-example.png Screenshot of the example + + \e {Kaltiot Smart IoT} is a bi-directional messaging solution. It provides + battery optimized communication for resource-constrained devices in lossy + wireless networks. Using \e {Kaltiot Smart IoT}, you can send notification + messages to notify a client app that a new message is available to sync. To + learn more, visit \l {https://kaltiot.com/iot}. + + This example works on both \e Linux64 and \e {Raspberry Pi} devices. It + demonstrates receiving push notifications from the \e {Kaltiot IoT Cloud}, + and then displays the content on the screen. You can send push notifications + to the devices running this example by using the \e IoT-Devices service in + the \e {Kaltiot Console} or \e {Kaltiot HTTP Rest API} with \e curl or \e + wget. Filter children by the \c providerId (\e KaltiotSmartIoT) and look for + one with the same \e RID (resource ID) as the \c onClientTokenReceived + callback reports. + + To use this example, the user must install \e {Kaltiot Smart IoT SDK} in + their environment. Register and download via the \e {Kaltiot Console} \l + {https://console.torqhub.io?iot} + \note Set environment variable \e KALTIOT_SDK to the root directory of \e + {Kaltiot Smart IoT SDK}. + \note Copy the \e {IoT-Credentials API Key} from the \e {Kaltiot Console} + and replace the value of \e SERVER_API_KEY in \c smartiot.cpp. + \note Start the gateway daemon of \e {Kaltiot Smart IoT} and run it in the + background before this application starts (\c {$KALTIOT_SDK/ks_gw + --ipc-socket-path=0.0.0.0:50000 &}). + + \include examples-run.qdocinc +*/ diff --git a/examples/kaltiot/smartiot/main.cpp b/examples/kaltiot/smartiot/main.cpp new file mode 100644 index 0000000..de27500 --- /dev/null +++ b/examples/kaltiot/smartiot/main.cpp @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCloudMessaging module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include <QtQuick> + +#include "smartiot.h" + +int main(int argc, char **argv) +{ + QGuiApplication app(argc, argv); + QQuickView view; + + PushNotification notifications(QStringLiteral("KaltiotSmartIoT"), QStringLiteral("IoTClient"), + &view); + + QQmlContext *ctxt = view.rootContext(); + ctxt->setContextProperty(QStringLiteral("notifications"), notifications.service()); + + view.setResizeMode(QQuickView::SizeRootObjectToView); + view.setSource(QUrl(QStringLiteral("qrc:/qml/main.qml"))); + view.show(); + + if (!notifications.startService()) { + if (QObject *message = view.rootObject()->findChild<QObject*>("kaltiot_message")) + message->setProperty("text", "Starting service of PushNotification fails!\n" + "Ensure Kaltiot daemon ($KALTIOT_SDK/ks_gw) is running."); + } + + return app.exec(); +} diff --git a/examples/kaltiot/smartiot/main.qrc b/examples/kaltiot/smartiot/main.qrc new file mode 100644 index 0000000..69145a8 --- /dev/null +++ b/examples/kaltiot/smartiot/main.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/"> + <file>qml/main.qml</file> + </qresource> +</RCC> diff --git a/examples/kaltiot/smartiot/qml/main.qml b/examples/kaltiot/smartiot/qml/main.qml new file mode 100644 index 0000000..848e425 --- /dev/null +++ b/examples/kaltiot/smartiot/qml/main.qml @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCloudMessaging module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 + +Rectangle { + width: 500 + height: 500 + color: "white" + + Column { + anchors.fill: parent + Text { + id: title + color: "black" + font.pixelSize: parent.width / 20 + text: "Qt Push Notification" + width: parent.width + horizontalAlignment: Text.AlignHCenter + } + Text { + id: kaltiot_message + objectName: "kaltiot_message" + color: "black" + text: "Waiting for the message from Kaltiot SmartIoT" + wrapMode: Text.WrapAtWordBoundaryOrAnywhere + width: parent.width + } + } + Connections { + target: notifications + onMessageReceived: { + console.log(message) + kaltiot_message.text = message + } + onClientTokenReceived: { + console.log("RID: " + token) + } + } +} diff --git a/examples/kaltiot/smartiot/smartiot.cpp b/examples/kaltiot/smartiot/smartiot.cpp new file mode 100644 index 0000000..32b562a --- /dev/null +++ b/examples/kaltiot/smartiot/smartiot.cpp @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCloudMessaging module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "smartiot.h" + +PushNotification::PushNotification(const QString &provider, const QString &client, QObject *parent) + : QObject(parent), providerId(provider), clientId(client) {} + +PushNotification::~PushNotification() +{ + // Disconnect the clients from the registered provider. + for (const auto &clientId : cloudMessage.localClients(providerId)) + cloudMessage.disconnectClient(providerId, clientId); + + // Deregister the service provider and remove the provider id. + // It will return immediately if the providerId is unregistered. + cloudMessage.deregisterProvider(providerId); +} + +bool PushNotification::startService() +{ + // A SERVER_API_KEY that authorizes your app for access to Kaltiot services + // when sending messages via the Kaltiot Smart IoT SDK. + // You can get it in the IoT-Credentials tab of the Kaltiot console. + QVariantMap params; + params[QStringLiteral("SERVER_API_KEY")] = "Get your API key from the Kaltiot console"; + + QVariantMap clientParams; + clientParams[QStringLiteral("version")] = QStringLiteral("1.0"); + + // Unique identifier of the device registering to the Kaltiot daemon. + clientParams[QStringLiteral("address")] = clientId; + + // Unique identifier of the application registering to Kaltiot gateway. + clientParams[QStringLiteral("customer_id")] = providerId; + + // The "--ipc-socket-path ip:port" option tells the daemon + // ($KALTIOT_SDK/ks_gw) to listen for connections by device applications. + // For example, here's how to have the Kaltiot daemon bind on port 50000. + // $KALTIOT_SDK/ks_gw --ipc-socket-path=0.0.0.0:50000 + clientParams[QStringLiteral("kaltiot_daemon_path")] = QStringLiteral("127.0.0.1:50000"); + + // Creating default channels to listen + QVariantList channels; + channels.append(QStringLiteral("QtCloudMessaging")); + clientParams[QStringLiteral("channels")] = channels; + + // Registering the Kaltiot SmartIoT service component. + if (cloudMessage.registerProvider(providerId, &kaltiotProvider, params)) + return !cloudMessage.connectClient(providerId, clientId, clientParams).isEmpty(); + + return false; +} diff --git a/examples/kaltiot/smartiot/smartiot.h b/examples/kaltiot/smartiot/smartiot.h new file mode 100644 index 0000000..ee4672d --- /dev/null +++ b/examples/kaltiot/smartiot/smartiot.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCloudMessaging module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SMARTIOT_H +#define SMARTIOT_H + +#include <QObject> +#include <QCloudMessaging> +#include <QtCloudMessagingEmbeddedKaltiot> + +class PushNotification: public QObject +{ + Q_OBJECT +public: + PushNotification(const QString &provider, const QString &client, QObject *parent = nullptr); + ~PushNotification(); + + bool startService(); + QCloudMessaging *service() {return &cloudMessage;} + +private: + Q_DISABLE_COPY(PushNotification) + + const QString providerId; + const QString clientId; + + QCloudMessaging cloudMessage; + QCloudMessagingEmbeddedKaltiotProvider kaltiotProvider; +}; + +#endif // SMARTIOT_H diff --git a/examples/kaltiot/smartiot/smartiot.pro b/examples/kaltiot/smartiot/smartiot.pro new file mode 100644 index 0000000..a302086 --- /dev/null +++ b/examples/kaltiot/smartiot/smartiot.pro @@ -0,0 +1,20 @@ +QT += quick cloudmessagingembeddedkaltiot + +SOURCES += \ + main.cpp \ + smartiot.cpp + +OTHER_FILES += \ + qml/main.qml + +RESOURCES += \ + main.qrc + +HEADERS += \ + smartiot.h + +KALTIOT_SDK = $$(KALTIOT_SDK) +!isEmpty(KALTIOT_SDK): INCLUDEPATH += $${KALTIOT_SDK}/src + +target.path = $$[QT_INSTALL_EXAMPLES]/kaltiot/smartiot +INSTALLS += target |