add binding initial code in qml 57/7557/4
authorTasuku Suzuki <tasuku.suzuki@qt.io>
Tue, 13 Dec 2016 03:22:08 +0000 (12:22 +0900)
committerTasuku Suzuki <tasuku.suzuki@qt.io>
Tue, 13 Dec 2016 14:42:32 +0000 (23:42 +0900)
Change-Id: I0efeb67ff2659a0a01be116ab59947db77ec6574
Signed-off-by: Tasuku Suzuki <tasuku.suzuki@qt.io>
app/HVAC.qml
app/api/Binding.qml [new file with mode: 0644]
app/api/MessageId.js [new file with mode: 0644]
app/hvac.qrc
app/main.cpp

index 565b108..f5cd76b 100644 (file)
@@ -18,10 +18,17 @@ import QtQuick 2.6
 import QtQuick.Layouts 1.1
 import QtQuick.Controls 2.0
 import AGL.Demo.Controls 1.0
+import 'api' as API
 
 ApplicationWindow {
     id: root
 
+    API.Binding {
+        id: binding
+        url: bindingAddress
+        onFanSpeedChanged: fanSpeedSlider.value = fanSpeed
+    }
+
     ColumnLayout {
         anchors.fill: parent
         anchors.topMargin: width / 10
@@ -35,17 +42,17 @@ ApplicationWindow {
             Item {
                 width: root.width * 0.8
                 Slider {
-                    id: fanSpeed
+                    id: fanSpeedSlider
                     anchors.left: parent.left
                     anchors.right: parent.right
                     anchors.verticalCenter: parent.verticalCenter
                     onValueChanged: {
-                        console.debug('Fan', value)
+                        binding.fanSpeed = value
                     }
                 }
                 Label {
-                    anchors.left: fanSpeed.left
-                    anchors.top: fanSpeed.bottom
+                    anchors.left: fanSpeedSlider.left
+                    anchors.top: fanSpeedSlider.bottom
                     font.pixelSize: 32
                     text: 'FAN SPEED'
                 }
diff --git a/app/api/Binding.qml b/app/api/Binding.qml
new file mode 100644 (file)
index 0000000..834bdf8
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2016 The Qt Company Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import QtQuick 2.6
+import QtWebSockets 1.0
+import 'MessageId.js' as MessageId
+
+WebSocket {
+    id: root
+    active: true
+
+    property string statusString: "waiting..."
+
+    property real fanSpeed: 0.0
+
+    property Connections c : Connections {
+        target: root
+        onFanSpeedChanged: {
+            var json = [MessageId.call, '9999', 'hvac/set', {'FanSpeed': fanSpeed}]
+            console.debug(JSON.stringify(json))
+            sendTextMessage(JSON.stringify(json))
+        }
+    }
+
+    onTextMessageReceived: {
+        var json = JSON.parse(message)
+        var request = json[2].request
+        var response = json[2].response
+        switch (json[0]) {
+        case MessageId.call:
+            break
+        case MessageId.retok:
+            root.statusString = request.info
+            break
+        case MessageId.reterr:
+            root.statusString = "Bad return value, binding probably not installed"
+            break
+        case MessageId.event:
+            break
+        }
+    }
+    onStatusChanged: {
+        switch (status) {
+        case WebSocket.Error:
+            root.statusString = "WebSocket error: " + root.errorString
+            break
+        }
+    }
+}
diff --git a/app/api/MessageId.js b/app/api/MessageId.js
new file mode 100644 (file)
index 0000000..001ea93
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2016 The Qt Company Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+.pragma library
+
+var call = 2
+var retok = 3
+var reterr = 4
+var event = 5
index 03fbf7d..6327049 100644 (file)
@@ -3,5 +3,7 @@
         <file>HVAC.qml</file>
         <file>SeatHeatButton.qml</file>
         <file>HeatDegree.qml</file>
+        <file>api/Binding.qml</file>
+        <file>api/MessageId.js</file>
     </qresource>
 </RCC>
index b2133f7..b532b73 100644 (file)
  */
 
 #include <QtCore/QDebug>
+#include <QtCore/QCommandLineParser>
+#include <QtCore/QUrlQuery>
 #include <QtGui/QGuiApplication>
 #include <QtQml/QQmlApplicationEngine>
+#include <QtQml/QQmlContext>
 #include <QtQuickControls2/QQuickStyle>
 
 #ifdef HAVE_LIBHOMESCREEN
@@ -35,10 +38,37 @@ int main(int argc, char *argv[])
 #endif
 
     QGuiApplication app(argc, argv);
+    app.setApplicationName(QStringLiteral("HVAC"));
+    app.setApplicationVersion(QStringLiteral("0.1.0"));
+    app.setOrganizationDomain(QStringLiteral("automotivelinux.org"));
+    app.setOrganizationName(QStringLiteral("AutomotiveGradeLinux"));
 
     QQuickStyle::setStyle("AGL");
 
+    QCommandLineParser parser;
+    parser.addPositionalArgument("port", app.translate("main", "port for binding"));
+    parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
+    parser.addHelpOption();
+    parser.addVersionOption();
+    parser.process(app);
+    QStringList positionalArguments = parser.positionalArguments();
+
+
     QQmlApplicationEngine engine;
+    if (positionalArguments.length() == 2) {
+        int port = positionalArguments.takeFirst().toInt();
+        QString secret = positionalArguments.takeFirst();
+        QUrl bindingAddress;
+        bindingAddress.setScheme(QStringLiteral("ws"));
+        bindingAddress.setHost(QStringLiteral("localhost"));
+        bindingAddress.setPort(port);
+        bindingAddress.setPath(QStringLiteral("/api"));
+        QUrlQuery query;
+        query.addQueryItem(QStringLiteral("token"), secret);
+        bindingAddress.setQuery(query);
+        QQmlContext *context = engine.rootContext();
+        context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
+    }
     engine.load(QUrl(QStringLiteral("qrc:/HVAC.qml")));
 
     return app.exec();