Add gitreview file for flounder branch
[apps/hvac.git] / app / api / Binding.qml
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 import QtQuick 2.6
18 import QtWebSockets 1.0
19 import 'MessageId.js' as MessageId
20
21 WebSocket {
22         id: root
23         active: true
24
25         property string statusString: "waiting..."
26
27         property real fanSpeed: 0.0
28         property real leftTemperature: 21.0
29         property real rightTemperature: 21.0
30         property string language: "en_US"
31
32         property Connections c : Connections {
33                 target: root
34                 onFanSpeedChanged: {
35                         var json = [MessageId.call, '9999', 'hvac/set', {'FanSpeed': fanSpeed}]
36                         console.debug(JSON.stringify(json))
37                         sendTextMessage(JSON.stringify(json))
38                 }
39                 onLeftTemperatureChanged: {
40                         var json = [MessageId.call, '9999', 'hvac/set', {'LeftTemperature': leftTemperature}]
41                         console.debug(JSON.stringify(json))
42                         sendTextMessage(JSON.stringify(json))
43
44                         var json1 = [MessageId.call, '9999', 'hvac/temp_left_zone_led', {'LeftLed': leftTemperature}]
45                         console.debug(JSON.stringify(json1))
46                         sendTextMessage(JSON.stringify(json1))
47                 }
48                 onRightTemperatureChanged: {
49                         var json = [MessageId.call, '9999', 'hvac/set', {'RightTemperature': rightTemperature}]
50                         console.debug(JSON.stringify(json))
51                         sendTextMessage(JSON.stringify(json))
52
53                         var json1 = [MessageId.call, '9999', 'hvac/temp_right_zone_led', {'RightLed': rightTemperature}]
54                         console.debug(JSON.stringify(json1))
55                         sendTextMessage(JSON.stringify(json1))
56                 }
57                 onLanguageChanged: {
58                         var json = [MessageId.call, '9999', 'hvac/set', {'Language': language}]
59                         console.debug(JSON.stringify(json))
60                         sendTextMessage(JSON.stringify(json))
61                 }
62         }
63
64         onTextMessageReceived: {
65                 var json = JSON.parse(message)
66                 var request = json[2].request
67                 var response = json[2].response
68                 console.log("HVAC Binding Message: ",message)
69                 switch (json[0]) {
70                 case MessageId.call:
71                         break
72                 case MessageId.retok:
73                         root.statusString = request.status
74                         break
75                 case MessageId.reterr:
76                         root.statusString = "Bad return value, binding probably not installed"
77                         break
78                 case MessageId.event:
79                         if (json[1] == "hvac/language")
80                                 console.log("HVAC event received: ",json[2])
81                                 root.language = json[2].data
82                                 root.statusString = "Language changed to "+language
83                         break
84                 }
85         }
86         onStatusChanged: {
87                 switch (status) {
88                 case WebSocket.Error:
89                         root.statusString = "WebSocket error: " + root.errorString
90                         break
91                 }
92         }
93 }