aboutsummaryrefslogtreecommitdiffstats
path: root/README.md
blob: 7e8b2573f29f77af6a27e89737b557f7744c2996 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
QT CLOUD MESSAGING API INSTALLATION:

1. To install with available backend add following to qmake script:
    qmake "CONFIG += embedded-kaltiot"
    or
    qmake "CONFIG += firebase"

    e.g.:
    qmake "CONFIG += embedded-kaltiot firebase"
    make qmake_all
    make
    make install

Google Firebase requirements:
2. Download and unzip google firebase c++ SDK:
    https://firebase.google.com/docs/cpp/setup

3. To use firebase as backend, define the following ENVIRONMENT variable
    GOOGLE_FIREBASE_SDK =
    and make it to point to your firebase sdk root

Embedded-Kaltiot requirements:
4. To use in embedded Kaltiot backend:
    3.1. Register your app in
        https://console.torqhub.io/signin

    3.2. Download platform SDK from the console download page

    3.3. Add environment variable to pointing to downloaded and unzipped SDK root:
        KALTIOT_SDK = ../../../<yourappname>_RasperryPi_SDK_1.0.17


5. Install first the QtCloudMessaging from command line with:

    qmake "CONFIG += embedded-kaltiot firebase"
    make
    make install

6. Now you can use the QtCloudMessaging in your app with

    Just API wrapper (e.g. creating new service providers)
    QT += cloudmessaging

    With Firebase backend:
    QT += cloudmessagingfirebase

    With Embedded devices & Kaltiot
    QT += cloudmessagingembeddedkaltiot

    See more from the example apps.
    NOTE: Examples are in Github:
    https://github.com/snowgrains/qtcloudmessaging-examples

7. Usage basics:

    main.cpp:

    // Using QCloudMessaging:
    #include <QtCloudMessaging>
    #include <kaltiotdemo.h>

    // Depending on your backend provider, add:

    // For Embedded systems
    #include <QtCloudMessagingEmbeddedKaltiot>

    // for Google firebase
    #include <QCloudMessagingFirebaseProvider>

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>

    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);

        QQmlApplicationEngine engine;

        //*** QTCLOUDMSG DEFINITIONS
        QCloudMessaging *pushServices = new QCloudMessaging();

        //***** For Kaltiot Embedded systems
        QCloudMessagingEmbeddedKaltiotProvider *kaltiotPushService = new QCloudMessagingEmbeddedKaltiotProvider()

        // Provider based init parameters are given with QVariantMap
        QVariantMap provider_params;
        provider_params["SERVER_API_KEY"] = "Your API key from the Kaltiot console for server communication";

        // Creating name for provider which can be used cross your app.
        pushServices->registerProvider("KaltiotService", kaltiotPushService, provider_params);

        QVariantMap client_params;
        client_params["address"] = "IOTSensor1";
        client_params["version"] = "1.0";
        client_params["customer_id"] = "Kaltiot";
        clientParams[QStringLiteral("kaltiot_daemon_path")] = QStringLiteral("127.0.0.1:50000");

        QVariantList channels;
        channels.append("Temperatures");
        client_params["channels"] = channels;

        /*! Connected client for the device.
          \param Service name "KaltiotService"
          \param Client identifier name to be used inside the application
          \param Parameters for the client specific for the provider API
        */
        pushServices->connectClient("KaltiotService", "IOTSensor1", client_params);

        //***** OR For Firebase mobile systems:
        QCloudMessagingFirebaseProvider *m_firebaseService = new QCloudMessagingFirebaseProvider();

        QVariantMap provider_params;

        // Server API key is not recommended to store inside to the application code due security reasons.
        // But if you do, make sure it is inside compiled C file or if you are doing a server side implementation with C++ & Qt.
        // SERVER_API_KEY Is needed to be able to send topic messages from the client without Firebase application server.

        provider_params["SERVER_API_KEY"] = "Get your SERVER API KEY from the google firebase console";

        // Registering the Google firebase service component.
        pushServices->registerProvider("GoogleFireBase", m_firebaseService, provider_params);

        /*! Connected client is needed for mobile device.
          \param Service name "GoogleFireBase"
          \param Client identifier name to be used inside the demo application
          \param Parameters for the client. No params for firebase client.
        */
        pushServices->connectClient("GoogleFireBase", "MobileClient", QVariantMap());

        //! Automatically subscribe to listen one example topic
        pushServices->subscribeToChannel("ChatRoom", "GoogleFireBase", "MobileClient");

        //*** END OF QTCLOUD MSG DEFINITIONS

        // these are needed for keeping the received RID in memory after restart (in Android)
        QCoreApplication::setOrganizationName("MyOrganisation");
        QCoreApplication::setOrganizationDomain("MyOrganisation.com");
        QCoreApplication::setApplicationName("QtCloudMessagingDemo");


        // To Give QML the push service context:
        engine.rootContext()->setContextProperty("pushServices", pushServices);

        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

        return app.exec();
    }

    main.qml:

    import QtQuick 2.6
    import QtQuick.Window 2.2


    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Qt Cloud Messaging Demo")


        // NOTIFICATIONS FROM THE ALL PROVIDERS:
        Connections {
           target:pushServices
           onMessageReceived:{
                console.log("Message to " + providerId + " service to " + clientId + " client.")
                console.log("Message: " + message)

                var msg_in_json = JSON.parse(decodeURI(message));

                // Example to respond to embedded system request:
                if (msg_in_json.command === "REQUESTING_TEMPERATURE")
                    embeddedPublishTemperatureToServer(msg_in_json.serverID, mydevicecommand.getTemperature());

                // Or firebase the message itself is a container of the info.
                updateGameNotification(message);
           }

           // Own Uuid to be used or broadcasted to server.
           onClientTokenReceived: {

                console.log("MY Uuid:"+rid)

                // Id this is server code:
                serverUuid = rid;

                // Id this is client code:
                clientUuid = rid;

           }
        }
        Component.onCompleted {
            // In case android app and firebase sdk gets started fast and token is needed, it can be requested with:
            clientUuid = pushServices.clientToken("GoogleFireBase", "MobileClient");
        }

        //***** EMBEDDED DEVICES

        // Function to send msg as server with Kaltiot provider to all clients as broadcast
        property string serverUuid: ""

        function sendKaltiotEmbeddedServerMessage(command_msg, msg) {

            var payload = {command: command_msg, server_id : serverUuid}; // e.g. "REQUESTING_TEMPERATURE"
            var payload_array = [{"payload_type": "STRING","payload": encodeURI(JSON.stringify(payload))}]
            var p = "payload=" + JSON.stringify( payload_array );

            pushServices.sendMessage(p, "KaltiotService", "", "", "Temperatures");
        }

        // Function to send temperature status message from the embedded client to Kaltiot server:
        property string clientUuid :""

        function embeddedPublishTemperatureToServer(serverUuid, temperature) {
            var payload = {command: "TEMPERATURE_INFO", clientId: clientUuid}
            var payload_array = [{"payload_type": "STRING","payload": encodeURI(JSON.stringify(payload))}]
            var p = "payload=" + JSON.stringify( payload_array );

            pushServices.sendMessage(p, "KaltiotService", "IOTSensor1", serverUuid,"");
        }
        //***** FIREBASE
        // Function to post msg as a server and broadcast it to all clients in the same room
        function sendFirebaseServerMessage(msg){
            var data = {"data": {"message": {"text": msg } },
                        "notification": {"body": msg, "title": "Qt Cloud Messaging Chat"}}

            /*!
             * \brief SendMessage
             * \param msg               Message as text which needs to be modified to the provider specific JSON.
             * \param providerId        Provider name specified at config
             * \param localclientId     clientId spefied at startup
             * \param send_to_device    Device Uuid (rid) to send for one known device
             * \param send_to_channel   Channel name to broadcast message to channel
             * \return
             */

            pushServices.sendMessage(JSON.stringify(data), "GoogleFireBase", "MobileClient", "", "ChatRoom");
        }
    }