Binding.qml: hack to receive update event
[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     property int nbFanSpeed : 0
32
33         property Connections c : Connections {
34                 target: root
35                 onFanSpeedChanged: {
36                         var json = [MessageId.call, '9999', 'hvac/set', {'FanSpeed': fanSpeed}]
37                         console.debug(JSON.stringify(json))
38             nbFanSpeed++
39                         sendTextMessage(JSON.stringify(json))
40                 }
41                 onLeftTemperatureChanged: {
42                         var json = [MessageId.call, '9999', 'hvac/set', {'LeftTemperature': leftTemperature}]
43                         console.debug(JSON.stringify(json))
44                         sendTextMessage(JSON.stringify(json))
45
46                         var json1 = [MessageId.call, '9999', 'hvac/temp_left_zone_led', {'LeftLed': leftTemperature}]
47                         console.debug(JSON.stringify(json1))
48                         sendTextMessage(JSON.stringify(json1))
49                 }
50                 onRightTemperatureChanged: {
51                         var json = [MessageId.call, '9999', 'hvac/set', {'RightTemperature': rightTemperature}]
52                         console.debug(JSON.stringify(json))
53                         sendTextMessage(JSON.stringify(json))
54
55                         var json1 = [MessageId.call, '9999', 'hvac/temp_right_zone_led', {'RightLed': rightTemperature}]
56                         console.debug(JSON.stringify(json1))
57                         sendTextMessage(JSON.stringify(json1))
58                 }
59                 onLanguageChanged: {
60                         var json = [MessageId.call, '9999', 'hvac/set', {'Language': language}]
61                         console.debug(JSON.stringify(json))
62                         sendTextMessage(JSON.stringify(json))
63                 }
64         }
65
66         onTextMessageReceived: {
67                 var json = JSON.parse(message)
68                 var request = json[2].request
69                 var response = json[2].response
70                 console.log("HVAC Binding Message: ",message)
71                 switch (json[0]) {
72                 case MessageId.call:
73                         break
74                 case MessageId.retok:
75                         root.statusString = request.status
76                         break
77                 case MessageId.reterr:
78                         root.statusString = "Bad return value, binding probably not installed"
79                         break
80                 case MessageId.event:
81                         if (json[1] == "hvac/language") {
82                                 console.log("HVAC event received: ",json[2])
83                                 root.language = json[2].data
84                                 root.statusString = "Language changed to "+language
85             } else if (json[1] == "hvac/update") {
86                                 console.log("HVAC event update received: ", json[2])
87                 console.log(json[2].data.LeftTemperature)
88                 if(json[2].data.LeftTemperature) {
89                     console.log("THATS COOL:::", json[2].data)
90                     binding = leftTemperature = json[2].data.LeftTemperature
91                 } else if (json[2].data.FanSpeed) {
92                     console.log("FANSPEED :: ", json[2].data.FanSpeed)
93                     if(nbFanSpeed > 0)
94                         nbFanSpeed--
95                     if(nbFanSpeed == 0)
96                         binding.fanSpeed = json[2].data.FanSpeed
97                 } else {
98                     console.log("THATS NOT COOL!!!!!!!!!!!!!!!!")
99                 }
100             }
101                         break
102                 }
103         }
104         onStatusChanged: {
105                 switch (status) {
106                 case WebSocket.Error:
107                         root.statusString = "WebSocket error: " + root.errorString
108                         break
109                 }
110         }
111 }