From ad8be41f63198aea13155189e8215d4bf8c4c204 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Wed, 30 Nov 2016 16:46:37 +0000 Subject: Renamed to hvac-demo Change-Id: I8254485a830db6e0dee06e7df62c63c7ce9fb5d5 Signed-off-by: Romain Forlot --- CMakeLists.txt | 6 +- config.xml.in | 2 +- hvac-demo-bindings.c | 352 +++++++++++++++++++++++++++++++++++++++++++ hvac-hybrid-qml-binding.c | 360 -------------------------------------------- icon_hvac-demo.png | Bin 0 -> 4236 bytes icon_hvac_hybrid_qml.png | Bin 4236 -> 0 bytes qml/hvac-demo-app.qml | 87 +++++++++++ qml/hvac-hybrid-qml-app.qml | 87 ----------- 8 files changed, 443 insertions(+), 451 deletions(-) create mode 100644 hvac-demo-bindings.c delete mode 100644 hvac-hybrid-qml-binding.c create mode 100644 icon_hvac-demo.png delete mode 100644 icon_hvac_hybrid_qml.png create mode 100644 qml/hvac-demo-app.qml delete mode 100644 qml/hvac-hybrid-qml-app.qml diff --git a/CMakeLists.txt b/CMakeLists.txt index f0b4776..c449422 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,14 +17,14 @@ # limitations under the License. ########################################################################### -project(hvac-hybrid-qml) +project(hvac-demo) cmake_minimum_required(VERSION 3.3) include(GNUInstallDirs) set(PROJECT_VERSION "0.1") -set(PROJECT_ICON "icon_hvac_hybrid_qml.png") +set(PROJECT_ICON "icon_hvac-demo.png") set(PROJECT_LIBDIR "lib") set(CMAKE_BUILD_TYPE Debug) @@ -61,7 +61,7 @@ link_libraries(${EXTRAS_LIBRARIES}) message(STATUS "Creation of ${PROJECT_NAME} for AFB-DAEMON") ############################################################### -add_library(${PROJECT_NAME} MODULE ${PROJECT_NAME}-binding.c +add_library(${PROJECT_NAME} MODULE ${PROJECT_NAME}-bindings.c ./qml) # ./qml/${PROJECT_NAME}-app.qml) diff --git a/config.xml.in b/config.xml.in index 1d9c86d..912b3c3 100644 --- a/config.xml.in +++ b/config.xml.in @@ -3,7 +3,7 @@ @PROJECT_NAME@ - This application is used to control and dialog with HVAC system + This is a demo application used to control and dialog with HVAC system Romain Forlot <romain.forlot@iot.bzh> APL 2.0 diff --git a/hvac-demo-bindings.c b/hvac-demo-bindings.c new file mode 100644 index 0000000..b43cfa1 --- /dev/null +++ b/hvac-demo-bindings.c @@ -0,0 +1,352 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Romain Forlot" + * Author "Jose Bolo" + * + * 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. + */ +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +#define CAN_DEV "can0" + +#define SIMULATE_HVAC + +static const struct afb_binding_interface *interface; + +// Initialize CAN hvac array that will be sent trough the socket +static char *can_hvac_components[8] = { + "LeftTemperature", + "RightTemperature", + "Temperature", + NULL, + "FanSpeed", + NULL, + NULL, + NULL +}; + +// Initialize CAN hvac array that will be sent trough the socket +static uint8_t can_hvac_values[8] = { + 21, // LeftTemperature + 21, // RightTemperature + 21, // AverageTemperature + 240, // Don't know why 240 but it was 0xF0 in the original amb hvacplugin + 0, // FanSpeed + 1, // Don't know why 1 but it was 0x01 in the original amb hvacplugin + 0, // Don't know why 0 but it was 0x00 in the original amb hvacplugin + 0 // Don't know why 0 but it was 0x00 in the original amb hvacplugin +}; + +struct can_handler { + int socket; + struct sockaddr_can txAddress; +}; + +static struct can_handler can_handler = { .socket = -1 }; + +/*****************************************************************************************/ +/*****************************************************************************************/ +/** **/ +/** **/ +/** SECTION: HANDLE CAN DEVICE **/ +/** **/ +/** **/ +/*****************************************************************************************/ +/*****************************************************************************************/ + +static int open_can_dev() +{ +#if defined(SIMULATE_HVAC) + can_handler.socket = 0; + return 0; +#else + struct ifreq ifr; + + can_handler.socket = socket(PF_CAN, SOCK_RAW, CAN_RAW); + if (can_handler.socket < 0) + { + ERROR(interface, "socket could not be created"); + } + else + { + // Attemts to open a socket to CAN bus + strcpy(ifr.ifr_name, CAN_DEV); + if(ioctl(can_handler.socket, SIOCGIFINDEX, &ifr) < 0) + { + ERROR(interface, "ioctl failed"); + } + else + { + can_handler.txAddress.can_family = AF_CAN; + can_handler.txAddress.can_ifindex = ifr.ifr_ifindex; + + // And bind it to txAddress + if (bind(can_handler.socket, (struct sockaddr *)&can_handler.txAddress, sizeof(can_handler.txAddress)) < 0) + { + ERROR(interface, "bind failed"); + } + else { + return 0; + } + } + close(can_handler.socket); + can_handler.socket = -1; + } + return -1; +#endif +} + +// Get original get temperature function from cpp hvacplugin code +static uint8_t get_temperature(uint8_t value) +{ + int result = ((0xF0 - 0x10) / 15) * value - 16; + if (result < 0x10) + result = 0x10; + if (result > 0xF0) + result = 0xF0; + + return (uint8_t)result; +} + +static int write_can() +{ + struct can_frame txCanFrame; + int rc = 0; + + rc = can_handler.socket; + if (rc >= 0) + { + // Hardcoded can_id and dlc (data lenght code) + txCanFrame.can_id = 0x30; + txCanFrame.can_dlc = 8; + txCanFrame.data[0] = get_temperature(can_hvac_values[0]); + txCanFrame.data[1] = get_temperature(can_hvac_values[1]); + txCanFrame.data[2] = get_temperature((can_hvac_values[0] + can_hvac_values[1]) / 2); + txCanFrame.data[3] = can_hvac_values[3]; + txCanFrame.data[4] = can_hvac_values[4]; + txCanFrame.data[5] = can_hvac_values[5]; + txCanFrame.data[6] = can_hvac_values[6]; + txCanFrame.data[7] = can_hvac_values[7]; + +#if defined(SIMULATE_HVAC) + DEBUG(interface, "WRITING CAN: %d %d [%02x %02x %02x %02x %02x %02x %02x %02x]\n", + txCanFrame.can_id, txCanFrame.can_dlc, + txCanFrame.data[0], txCanFrame.data[1], txCanFrame.data[2], txCanFrame.data[3], + txCanFrame.data[4], txCanFrame.data[5], txCanFrame.data[6], txCanFrame.data[7]); +#else + rc = sendto(can_handler.socket, &txCanFrame, sizeof(struct can_frame), 0, + (struct sockaddr*)&can_handler.txAddress, sizeof(can_handler.txAddress)); + if (rc < 0) + { + ERROR(interface, "Sending can frame failed"); + } +#endif + } + else + { + ERROR(interface, "socket not initialized"); + } + return rc; +} + +static uint8_t read_temp_left_zone() +{ + return can_hvac_values[0]; +} + +static uint8_t read_temp_right_zone() +{ + return can_hvac_values[1]; +} + +static uint8_t read_fanspeed() +{ + return can_hvac_values[4]; +} + +/*****************************************************************************************/ +/*****************************************************************************************/ +/** **/ +/** **/ +/** SECTION: BINDING VERBS IMPLEMENTATION **/ +/** **/ +/** **/ +/*****************************************************************************************/ +/*****************************************************************************************/ + +/* + * @brief Get fan speed HVAC system + * + * @param struct afb_req : an afb request structure + * + */ +static void get_fanspeed(struct afb_req request) +{ + json_object *ret_json; + uint8_t fanspeed = read_fanspeed(); + + ret_json = json_object_new_object(); + json_object_object_add(ret_json, "FanSpeed", json_object_new_int(fanspeed)); + + afb_req_success(request, ret_json, NULL); +} + +/* + * @brief Read Consign right zone temperature for HVAC system + * + * @param struct afb_req : an afb request structure + * + */ +static void get_temp_right_zone(struct afb_req request) +{ + json_object *ret_json; + uint8_t temp = read_temp_right_zone(); + + ret_json = json_object_new_object(); + json_object_object_add(ret_json, "RightTemperature", json_object_new_int(temp)); + + afb_req_success(request, ret_json, NULL); +} + +/* + * @brief Read Consign left zone temperature for HVAC system + * + * @param struct afb_req : an afb request structure + * + */ +static void get_temp_left_zone(struct afb_req request) +{ + json_object *ret_json; + uint8_t temp = read_temp_left_zone(); + + ret_json = json_object_new_object(); + json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(temp)); + + afb_req_success(request, ret_json, NULL); +} + +/* + * @brief Read all values + * + * @param struct afb_req : an afb request structure + * + */ +static void get(struct afb_req request) +{ + json_object *ret_json; + + ret_json = json_object_new_object(); + json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(read_temp_left_zone())); + json_object_object_add(ret_json, "RightTemperature", json_object_new_int(read_temp_right_zone())); + json_object_object_add(ret_json, "FanSpeed", json_object_new_int(read_fanspeed())); + + afb_req_success(request, ret_json, NULL); +} + +/* + * @brief Set a component value using a json object retrieved from request + * + * @param struct afb_req : an afb request structure + * + */ +static void set(struct afb_req request) +{ + int i, rc, x; + struct json_object *query, *val; + uint8_t values[sizeof can_hvac_components / sizeof *can_hvac_components]; + uint8_t saves[sizeof can_hvac_components / sizeof *can_hvac_components]; + + /* records initial values */ + memcpy(values, can_hvac_values, sizeof values); + memcpy(saves, can_hvac_values, sizeof saves); + + /* Loop getting arguments */ + query = afb_req_json(request); + i = (int)(sizeof can_hvac_components / sizeof *can_hvac_components); + while (i) + { + if (json_object_object_get_ex(query, can_hvac_components[--i], &val)) + { + if (!json_object_is_type(json_type_int)) + { + afb_req_fail_f(request, "bad-request", + "argument '%s' isn't integer", can_hvac_components[i]); + return; + } + x = json_object_get_int(val); + if (x < 0 || x > 255) + { + afb_req_fail_f(request, "bad-request", + "argument '%s' is out of bounds", can_hvac_components[i]); + return; + } + values[i] = (uint8_t)x; + } + } + + /* attemps to set new values */ + memcpy(can_hvac_values, values, sizeof values); + rc = nchg ? write_can() : 0; + if (rc >= 0) + afb_req_success(request, NULL, nerr ? "error detected" : NULL); + else { + /* restore initial values */ + memcpy(can_hvac_values, saves, sizeof saves); + afb_req_fail(request, "error", "CAN error"); + } +} + +// TODO: Have to change session management flag to AFB_SESSION_CHECK to use token auth +static const struct afb_verb_desc_v1 verbs[]= { + {"get_temp_left_zone" , AFB_SESSION_NONE, get_temp_left_zone , "Get the left zone temperature"}, + {"get_temp_right_zone" , AFB_SESSION_NONE, get_temp_right_zone , "Get the right zone temperature"}, + {"get_fanspeed" , AFB_SESSION_NONE, get_fanspeed , "Read fan speed"}, + {"get" , AFB_SESSION_NONE, get , "Read all values"}, + {"set" , AFB_SESSION_NONE, set , "Set a HVAC component value"}, + {NULL} +}; + +static const struct afb_binding binding_desc = { + .type = AFB_BINDING_VERSION_1, + .v1 = { + .info = "hvac service", + .prefix = "hvac", + .verbs = verbs + } +}; + +const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf) +{ + interface = itf; + + return &binding_desc; +} + +int afbBindingV1ServiceInit(struct afb_service service) +{ + return open_can_dev(); +} + diff --git a/hvac-hybrid-qml-binding.c b/hvac-hybrid-qml-binding.c deleted file mode 100644 index 4b5434c..0000000 --- a/hvac-hybrid-qml-binding.c +++ /dev/null @@ -1,360 +0,0 @@ -/* - * Copyright (C) 2015, 2016 "IoT.bzh" - * Author "Romain Forlot" - * Author "Jose Bolo" - * - * 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. - */ -#define _GNU_SOURCE - -#include -#include -#include -#include -#include -#include -#include - -#include - -#include -#include - -#define CAN_DEV "can0" - -static const struct afb_binding_interface *interface; - -// Initialize CAN hvac array that will be sent trough the socket -static char *can_hvac_components[8] = { - "LeftTemperature", - "RightTemperature", - "Temperature", - NULL, - "FanSpeed", - NULL, - NULL, - NULL -}; - -// Initialize CAN hvac array that will be sent trough the socket -static uint8_t can_hvac_values[8] = { - 21, // LeftTemperature - 21, // RightTemperature - 21, // AverageTemperature - 240, // Don't know why 240 but it was 0xF0 in the original amb hvacplugin - 0, // FanSpeed - 1, // Don't know why 1 but it was 0x01 in the original amb hvacplugin - 0, // Don't know why 0 but it was 0x00 in the original amb hvacplugin - 0 // Don't know why 0 but it was 0x00 in the original amb hvacplugin -}; - -struct can_handler { - int socket; - struct sockaddr_can txAddress; -}; - -static struct can_handler can_handler = { .socket = -1 }; - -/*****************************************************************************************/ -/*****************************************************************************************/ -/** **/ -/** **/ -/** SECTION: HANDLE CAN DEVICE **/ -/** **/ -/** **/ -/*****************************************************************************************/ -/*****************************************************************************************/ - -static int open_can_dev() -{ - struct ifreq ifr; - - can_handler.socket = socket(PF_CAN, SOCK_RAW, CAN_RAW); - if (can_handler.socket < 0) - { - ERROR(interface, "socket could not be created"); - } - else - { - // Attemts to open a socket to CAN bus - strcpy(ifr.ifr_name, CAN_DEV); - if(ioctl(can_handler.socket, SIOCGIFINDEX, &ifr) < 0) - { - ERROR(interface, "ioctl failed"); - } - else - { - can_handler.txAddress.can_family = AF_CAN; - can_handler.txAddress.can_ifindex = ifr.ifr_ifindex; - - // And bind it to txAddress - if (bind(can_handler.socket, (struct sockaddr *)&can_handler.txAddress, sizeof(can_handler.txAddress)) < 0) - { - ERROR(interface, "bind failed"); - } - else { - return 0; - } - } - close(can_handler.socket); - can_handler.socket = -1; - } - return -1; -} - -// Get original get temperature function from cpp hvacplugin code -static uint8_t get_temperature(uint8_t value) -{ - int result = ((0xF0 - 0x10) / 15) * value - 16; - if (result < 0x10) - result = 0x10; - if (result > 0xF0) - result = 0xF0; - - return (uint8_t)result; -} - -static int write_can() -{ - struct can_frame txCanFrame; - int rc = 0; - - rc = can_handler.socket; - if (rc >= 0) - { - // Hardcoded can_id and dlc (data lenght code) - txCanFrame.can_id = 0x30; - txCanFrame.can_dlc = 8; - txCanFrame.data[0] = get_temperature(can_hvac_values[0]); - txCanFrame.data[1] = get_temperature(can_hvac_values[1]); - txCanFrame.data[2] = get_temperature((can_hvac_values[0] + can_hvac_values[1]) / 2); - txCanFrame.data[3] = can_hvac_values[3]; - txCanFrame.data[4] = can_hvac_values[4]; - txCanFrame.data[5] = can_hvac_values[5]; - txCanFrame.data[6] = can_hvac_values[6]; - txCanFrame.data[7] = can_hvac_values[7]; - - rc = sendto(can_handler.socket, &txCanFrame, sizeof(struct can_frame), 0, - (struct sockaddr*)&can_handler.txAddress, sizeof(can_handler.txAddress)); - if (rc < 0) - { - ERROR(interface, "Sending can frame failed"); - } - } - else - { - ERROR(interface, "socket not initialized"); - } - return rc; -} - -static uint8_t read_temp_left_zone() -{ - return can_hvac_values[0]; -} - -static uint8_t read_temp_right_zone() -{ - return can_hvac_values[1]; -} - -static uint8_t read_fanspeed() -{ - return can_hvac_values[4]; -} - -static int make_uint8(const char *str, uint8_t *value) -{ - long int x; - char **end; - - if (str == NULL) - return -1; - - x = strtol(str, &end, 10); - if (end == str || *end) - return -1; - - if (x < 0 || x > 255) - return -1; - - *value = (uint8_t)x; - return 0; -} - -/*****************************************************************************************/ -/*****************************************************************************************/ -/** **/ -/** **/ -/** SECTION: BINDING VERBS IMPLEMENTATION **/ -/** **/ -/** **/ -/*****************************************************************************************/ -/*****************************************************************************************/ - -/* - * @brief Get fan speed HVAC system - * - * @param struct afb_req : an afb request structure - * - */ -static void get_fanspeed(struct afb_req request) -{ - json_object *ret_json; - uint8_t fanspeed = read_fanspeed(); - - ret_json = json_object_new_object(); - json_object_object_add(ret_json, "FanSpeed", json_object_new_int(fanspeed)); - - afb_req_success(request, ret_json, NULL); -} - -/* - * @brief Read Consign right zone temperature for HVAC system - * - * @param struct afb_req : an afb request structure - * - */ -static void get_temp_right_zone(struct afb_req request) -{ - json_object *ret_json; - uint8_t temp = read_temp_right_zone(); - - ret_json = json_object_new_object(); - json_object_object_add(ret_json, "RightTemperature", json_object_new_int(temp)); - - afb_req_success(request, ret_json, NULL); -} - -/* - * @brief Read Consign left zone temperature for HVAC system - * - * @param struct afb_req : an afb request structure - * - */ -static void get_temp_left_zone(struct afb_req request) -{ - json_object *ret_json; - uint8_t temp = read_temp_left_zone(); - - ret_json = json_object_new_object(); - json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(temp)); - - afb_req_success(request, ret_json, NULL); -} - -/* - * @brief Read all values - * - * @param struct afb_req : an afb request structure - * - */ -static void get(struct afb_req request) -{ - json_object *ret_json; - - ret_json = json_object_new_object(); - json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(read_temp_left_zone())); - json_object_object_add(ret_json, "RightTemperature", json_object_new_int(read_temp_right_zone())); - json_object_object_add(ret_json, "FanSpeed", json_object_new_int(read_fanspeed())); - - afb_req_success(request, ret_json, NULL); -} - -/* - * @brief Set a component value using a json object retrieved from request - * - * @param struct afb_req : an afb request structure - * - */ -static void set(struct afb_req request) -{ - int i, rc, nerr, nchg; - const char *val, *key; - struct json_object *query; - struct json_object_iterator iter; - struct json_object_iterator iter_end; - - query = afb_req_json(request); - iter = json_object_iter_begin(query); - iter_end = json_object_iter_end(query); - - /* - * Loop over the json object that will set every component - * which it will find in it. - */ - nchg = nerr = 0; - while(!json_object_iter_equal(&iter, &iter_end)) - { - key = json_object_iter_peek_name(&iter); - i = 0; - while (i < 8 && can_hvac_components[i] != NULL && strcmp(can_hvac_components[i], key) != 0) - { - i++; - } - if (i < 8) - { - val = afb_req_value(request, key); - rc = make_uint8(val, &can_hvac_values[i]); - if (rc < 0) - nerr++; - else - nchg++; - } - else - { - // not found! ignore the error silently - nerr++; - } - - json_object_iter_next(&iter); - } - - rc = nchg ? write_can() : 0; - if (rc < 0) - afb_req_fail(request, "error", "CAN error"); - else - afb_req_success(request, NULL, nerr ? "error detected" : NULL); -} - -// TODO: Have to change session management flag to AFB_SESSION_CHECK to use token auth -static const struct afb_verb_desc_v1 verbs[]= { - {"get_temp_left_zone" , AFB_SESSION_NONE, get_temp_left_zone , "Get the left zone temperature"}, - {"get_temp_right_zone" , AFB_SESSION_NONE, get_temp_right_zone , "Get the right zone temperature"}, - {"get_fanspeed" , AFB_SESSION_NONE, get_fanspeed , "Read fan speed"}, - {"get" , AFB_SESSION_NONE, get , "Read all values"}, - {"set" , AFB_SESSION_NONE, set , "Set a HVAC component value"}, - {NULL} -}; - -static const struct afb_binding binding_desc = { - .type = AFB_BINDING_VERSION_1, - .v1 = { - .info = "hvac service", - .prefix = "hvac", - .verbs = verbs - } -}; - -const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf) -{ - interface = itf; - - return &binding_desc; -} - -int afbBindingV1ServiceInit(struct afb_service service) -{ - return open_can_dev(); -} - diff --git a/icon_hvac-demo.png b/icon_hvac-demo.png new file mode 100644 index 0000000..27df1a0 Binary files /dev/null and b/icon_hvac-demo.png differ diff --git a/icon_hvac_hybrid_qml.png b/icon_hvac_hybrid_qml.png deleted file mode 100644 index 27df1a0..0000000 Binary files a/icon_hvac_hybrid_qml.png and /dev/null differ diff --git a/qml/hvac-demo-app.qml b/qml/hvac-demo-app.qml new file mode 100644 index 0000000..4472803 --- /dev/null +++ b/qml/hvac-demo-app.qml @@ -0,0 +1,87 @@ +import QtQuick 2.0 +import QtQuick.Window 2.0 +import QtQuick.Controls 1.4 +import QtWebSockets 1.0 + +Window { + // VARIABLES + + property string port_str: Qt.application.arguments[1] + property string token_str: Qt.application.arguments[2] + property string address_str: "ws://localhost:"+port_str+"/api?token="+token_str + property string request_str: "" + property string status_str: "waiting..." + property var msgid_enu: { "call":2, "retok":3, "reterr":4, "event":5 } + + // WINDOW PROPERTIES + + visible: true + width: 340 + height: 160 + + // WEBSOCKET WIDGET (MAIN LOGIC) + + WebSocket { + id: websocket + url: address_str + onTextMessageReceived: { + // VERB RESPONSE VALIDATION + var message_json = JSON.parse (message) + var request_json = message_json[2].request + if (message_json[0] != msgid_enu.retok) { + console.log ("Return value is not ok !") + status_str = "Bad return value, binding probably not installed" + return + } + // VERB RESPONSE PARSING AND LOGIC + status_str = request_json.info + } + onStatusChanged: { + if (websocket.status == WebSocket.Error) + status_str = "WebSocket error: " + websocket.errorString + } + active: true + } + + // OTHER WIDGETS + + Rectangle { + anchors.left: parent.left + anchors.top: parent.top + anchors.horizontalCenter: parent.horizontalCenter + anchors.margins: 20 + + // TITLE SECTION + Label { + text: "QML Websocket Sample Application" + font.pixelSize: 18 + font.bold: true + anchors.centerIn: parent + y: 0 + } + Text { + id: url_notifier + text: "URL: " + websocket.url + y: 20 + } + + // PING BUTTON + Button { + text: "Ping!" + onClicked: { + request_str = '[' + msgid_enu.call + ',"99999","hvac/ping", null ]'; + websocket.sendTextMessage (request_str) + } + anchors.horizontalCenter: parent.horizontalCenter + y: 60 + } + + // STATUS SECTION + Text { + id: status_notifier + text: "Status: " + status_str + y: 100 + } + } + +} diff --git a/qml/hvac-hybrid-qml-app.qml b/qml/hvac-hybrid-qml-app.qml deleted file mode 100644 index 4472803..0000000 --- a/qml/hvac-hybrid-qml-app.qml +++ /dev/null @@ -1,87 +0,0 @@ -import QtQuick 2.0 -import QtQuick.Window 2.0 -import QtQuick.Controls 1.4 -import QtWebSockets 1.0 - -Window { - // VARIABLES - - property string port_str: Qt.application.arguments[1] - property string token_str: Qt.application.arguments[2] - property string address_str: "ws://localhost:"+port_str+"/api?token="+token_str - property string request_str: "" - property string status_str: "waiting..." - property var msgid_enu: { "call":2, "retok":3, "reterr":4, "event":5 } - - // WINDOW PROPERTIES - - visible: true - width: 340 - height: 160 - - // WEBSOCKET WIDGET (MAIN LOGIC) - - WebSocket { - id: websocket - url: address_str - onTextMessageReceived: { - // VERB RESPONSE VALIDATION - var message_json = JSON.parse (message) - var request_json = message_json[2].request - if (message_json[0] != msgid_enu.retok) { - console.log ("Return value is not ok !") - status_str = "Bad return value, binding probably not installed" - return - } - // VERB RESPONSE PARSING AND LOGIC - status_str = request_json.info - } - onStatusChanged: { - if (websocket.status == WebSocket.Error) - status_str = "WebSocket error: " + websocket.errorString - } - active: true - } - - // OTHER WIDGETS - - Rectangle { - anchors.left: parent.left - anchors.top: parent.top - anchors.horizontalCenter: parent.horizontalCenter - anchors.margins: 20 - - // TITLE SECTION - Label { - text: "QML Websocket Sample Application" - font.pixelSize: 18 - font.bold: true - anchors.centerIn: parent - y: 0 - } - Text { - id: url_notifier - text: "URL: " + websocket.url - y: 20 - } - - // PING BUTTON - Button { - text: "Ping!" - onClicked: { - request_str = '[' + msgid_enu.call + ',"99999","hvac/ping", null ]'; - websocket.sendTextMessage (request_str) - } - anchors.horizontalCenter: parent.horizontalCenter - y: 60 - } - - // STATUS SECTION - Text { - id: status_notifier - text: "Status: " + status_str - y: 100 - } - } - -} -- cgit 1.2.3-korg