-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelayControlButton.qml
More file actions
148 lines (127 loc) · 4.13 KB
/
Copy pathRelayControlButton.qml
File metadata and controls
148 lines (127 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import QtQuick 2.0
HomeMenuButton{
id:relayButton
property string relayAddress: "localhost"
property int relayNumber : -1
property bool isOn : false
property string statusEndpoint : "/relaystatus"
property string onEndpoint : "/relayon"
property string offEndpoint : "/relayoff"
property string relayParam : "?relaynum=" + relayNumber
buttonLabel: "LightRelay"
commandName: "lightRelay"
color:"grey"
state:"preInit"
onButtonPressed: {
if(isOn){
console.log("Turning relay off");
turnRelayOff();
}
else{
console.log("Turning relay on");
turnRelayOn();
}
}
onIsOnChanged: {
console.log("Changing state")
if(isOn) state = 'relayOn';
else state = 'relayOff';
}
states:[
State{
name:"relayOn"
PropertyChanges{target:relayButton; color:"green"}
},
State{
name:"relayOff"
PropertyChanges{target:relayButton; color:"darkRed"}
},
State{
name:"preInit"
PropertyChanges{target:relayButton; color:"grey"}
},
State{
name:"changing"
PropertyChanges {target: relayButton; color:"orange"}
}
]
Component.onCompleted: {
getRelayState();
}
function parseRelayState(stateResponseText){
console.log(stateResponseText);
var responseArr = stateResponseText.split(":");
var relayIsOn = parseInt(responseArr[1]);
if (relayIsOn == 1) relayIsOn = true;
else if (relayIsOn == 0) relayIsOn = false;
if(relayIsOn){
console.log('Relay Reported on');
isOn = true;
state = 'relayOn';
}
else{
console.log('Relay Reported off');
isOn = false;
state = 'relayOff';
}
}
function getRelayState(){
var http = new XMLHttpRequest()
var url = "http://" + relayAddress + statusEndpoint + relayParam
console.log(url);
http.open("GET", url, true);
// Send the proper header information along with the request
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() { // Call a function when the state changes.
if (http.readyState == 4) {
if (http.status == 200) {
console.log("ok");
parseRelayState(http.responseText);
} else {
console.log("error: " + http.status)
}
}
}
http.send();
}
function turnRelayOn(){
var http = new XMLHttpRequest()
var url = "http://" + relayAddress + onEndpoint + relayParam
console.log(url);
http.open("GET", url, true);
// Send the proper header information along with the request
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() { // Call a function when the state changes.
if (http.readyState == 4) {
if (http.status == 200) {
console.log("ok");
parseRelayState(http.responseText);
} else {
console.log("error: " + http.status)
}
}
}
http.send();
}
function turnRelayOff(){
var http = new XMLHttpRequest()
var url = "http://" + relayAddress + offEndpoint + relayParam
console.log(url);
http.open("GET", url, true);
// Send the proper header information along with the request
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() { // Call a function when the state changes.
if (http.readyState == 4) {
if (http.status == 200) {
console.log("ok");
parseRelayState(http.responseText);
} else {
console.log("error: " + http.status)
}
}
}
http.send();
}
function setRelay(state){
}
}