93da6f4621161cf9f4707a58002cf2a1ddc7731f
[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
31     property Connections c : Connections {
32         target: root
33         onFanSpeedChanged: {
34             var json = [MessageId.call, '9999', 'hvac/set', {'FanSpeed': fanSpeed}]
35             console.debug(JSON.stringify(json))
36             sendTextMessage(JSON.stringify(json))
37         }
38         onLeftTemperatureChanged: {
39             var json = [MessageId.call, '9999', 'hvac/set', {'LeftTemperature': leftTemperature}]
40             console.debug(JSON.stringify(json))
41             sendTextMessage(JSON.stringify(json))
42         }
43         onRightTemperatureChanged: {
44             var json = [MessageId.call, '9999', 'hvac/set', {'RightTemperature': rightTemperature}]
45             console.debug(JSON.stringify(json))
46             sendTextMessage(JSON.stringify(json))
47         }
48     }
49
50     onTextMessageReceived: {
51         var json = JSON.parse(message)
52         var request = json[2].request
53         var response = json[2].response
54         console.log("HVAC Binding Message: ",message)
55         switch (json[0]) {
56         case MessageId.call:
57             break
58         case MessageId.retok:
59             root.statusString = request.status
60             break
61         case MessageId.reterr:
62             root.statusString = "Bad return value, binding probably not installed"
63             break
64         case MessageId.event:
65             break
66         }
67     }
68     onStatusChanged: {
69         switch (status) {
70         case WebSocket.Error:
71             root.statusString = "WebSocket error: " + root.errorString
72             break
73         }
74     }
75 }