From 1252fdb973f7ba815017447c1a3ed49852de0597 Mon Sep 17 00:00:00 2001 From: Tasuku Suzuki Date: Tue, 13 Dec 2016 12:22:08 +0900 Subject: add binding initial code in qml Change-Id: I0efeb67ff2659a0a01be116ab59947db77ec6574 Signed-off-by: Tasuku Suzuki --- app/HVAC.qml | 15 +++++++++---- app/api/Binding.qml | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ app/api/MessageId.js | 22 +++++++++++++++++++ app/hvac.qrc | 2 ++ app/main.cpp | 30 +++++++++++++++++++++++++ 5 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 app/api/Binding.qml create mode 100644 app/api/MessageId.js diff --git a/app/HVAC.qml b/app/HVAC.qml index 565b108..f5cd76b 100644 --- a/app/HVAC.qml +++ b/app/HVAC.qml @@ -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 index 0000000..834bdf8 --- /dev/null +++ b/app/api/Binding.qml @@ -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 index 0000000..001ea93 --- /dev/null +++ b/app/api/MessageId.js @@ -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 diff --git a/app/hvac.qrc b/app/hvac.qrc index 03fbf7d..6327049 100644 --- a/app/hvac.qrc +++ b/app/hvac.qrc @@ -3,5 +3,7 @@ HVAC.qml SeatHeatButton.qml HeatDegree.qml + api/Binding.qml + api/MessageId.js diff --git a/app/main.cpp b/app/main.cpp index b2133f7..b532b73 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -15,8 +15,11 @@ */ #include +#include +#include #include #include +#include #include #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(); -- cgit 1.2.3-korg