-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugFunctions.js
219 lines (187 loc) · 6.4 KB
/
plugFunctions.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
Plug Functions
*/
function fetchPlugData(showAll) {
$.ajax({
url: "http://192.168.178.31/Scripts/SwitchPlug.php?function-name=GetAll",
success: function (result) {
console.log("success!");
console.log(result);
var arr = JSON.parse(result);
console.log(arr);
//var arr =
//createSwitches(arr);
//$('#S1_descption').text(result);
drawHome(arr, showAll);
},
error:function(result){
console.log("error: ");
console.log(result);
//var tArr = '{ "plugs" : [ { "plug" : "2", "description" : "Test", "display" : 1, "state" : 1} ]}';
var tArr = '[ { "plug" : "2", "description" : "Test", "display" : 1, "state" : 1}, { "plug" : "3", "description" : "Test 1", "display" : 0, "state" : 0}]';
var arr = JSON.parse(tArr);
console.log(arr);
drawHome(arr,showAll);
}
//async: false
});
/*
$.get("/Scripts/SwitchPlug.php?function-name=GetAll",
function(result){
console.log("result: ")
console.log(result);
return JSON.parse(result);
});*/
}
function togglePlug(plugId,id) {
console.log("ID: ");
console.log(id);
var checked = $('#switch-'+plugId).is(':checked');
console.log("plug id : "+plugId);
console.log(checked);
if (!checked) {
console.log('switch is on - turning it off')
switch_plug(plugId,false);
} else {
console.log('switch is off - turning it on')
switch_plug(plugId,true);
}
}
function togglePlugDisplay(plugId,id) {
console.log("ID: ");
console.log(id);
var checked = $('#switch-'+plugId).is(':checked');
console.log("plug id : "+plugId);
console.log(checked);
if (checked) {
console.log('switch is on - turning it off')
changeDisplay(plugId,false);
} else {
console.log('switch is off - turning it on')
changeDisplay(plugId,true);
}
}
function drawHome(arr, showAll)
{
console.log("here 1");
//var arr = fetchAllData();
//console.log("here 2");
console.log(arr);
console.log("Length: ");
console.log(arr.length);
var html = '';
for(var i = 0; i < arr.length; i++)
{
var p = arr[i];
console.log("here 3, Plug: "+i+"display"+p.display);
if(showAll){
html += createSwitchSettings(p.description, p.plug, p.state, p.display);
}
else if(p.display == "1"){
html += createSwitch(p.description, p.plug, p.state, p.display);
}
}
$("#switch-wrapper").html(html);
//return html;
//$("#switch-wrapper").html(html);
}
function createSwitch(description, plugId, state, display){
var checked = '';
if(state == 1){
checked = 'checked';
bnState = 'btn-default';
}
else{
bnState = 'btn-success';
}
var id = 'switch-' + plugId;
html =' <div class="switch-row">'+
'<div class="btn-group switch-btn-group" role="group" aria-label="...">'+
'<button type="button" class="btn btn-default btn-title btn-lg" aria-label="description" onclick=""> <span class="glyphicon"></span>'+ description +' </button>'+
'<button type="button" class="btn btn-default btn-switch btn-lg" aria-label="switchOn" onclick="switch_plug('+plugId+',true);"> <span class="glyphicon glyphicon-off" aria-hidden="true"></span> ON</button>'+
'<button type="button" class="btn btn-success btn-switch btn-lg" aria-label="switchOff" onclick="switch_plug('+plugId+',false);"> <span class="glyphicon glyphicon-off" aria-hidden="true"></span> OFF</button>'+
'</div>'+
'</div>';
return html;
}
function createSwitchSettings(description, plugId, state, display){
var checked = '';
if(state == 1){
checked = 'checked';
bnState = 'btn-default';
}
else{
bnState = 'btn-success';
}
var id = 'switch-' + plugId;
if(display){
togDisplay = '<button type="button" class="btn btn-default btn-switch btn-lg" aria-label="display" onclick="changeDisplay('+plugId+',true);"> <span class="glyphicon" aria-hidden="true"></span> SHOW</button>';
}
else{
togDisplay = '<button type="button" class="btn btn-success btn-switch btn-lg" aria-label="display" onclick="changeDisplay('+plugId+',true);"> <span class="glyphicon" aria-hidden="true"></span> SHOW</button>';
}
html =' <div class="switch-row">'+
'<div class="btn-group switch-settings-btn-group" role="group" aria-label="...">'+
'<button type="button" class="btn btn-default btn-title btn-lg" aria-label="description" onclick=""> <span class="glyphicon"></span>'+ description +' </button>'+
'<button type="button" class="btn btn-default btn-switch btn-lg" aria-label="switchOn" onclick="switch_plug('+plugId+',true);"> <span class="glyphicon glyphicon-off" aria-hidden="true"></span> ON</button>'+
'<button type="button" class="btn btn-success btn-switch btn-lg" aria-label="switchOff" onclick="switch_plug('+plugId+',false);"> <span class="glyphicon glyphicon-off" aria-hidden="true"></span> OFF</button>'+
togDisplay+
'</div>'+
'</div>';
return html;
}
function switch_plug(plugId, state) { //TODO
console.log("switching plug: plugid = "+plugId +", state = " +state);
$.ajax({
url: "/Scripts/SwitchPlug.php?function-name=switchPlug&plug="+plugId+"&state="+state,
success: function (result) {
console.log("success!");
console.log(result);
},
error:function(result){
console.log("error: ");
console.log(result);
}
});
}
function changeDisplay(plugId, state)
{
console.log("Changeing plug display plug: plugid = "+plugId +", state = " +state);
$.ajax({
url: "/Scripts/SwitchPlug.php?function-name=Change_Visibility&plug="+plugId+"&state="+state,
success: function (result) {
console.log("success!");
console.log(result);
},
error:function(result){
console.log("error: ");
console.log(result);
}
});
}
function addPlug(plugId, description, oncode, offcode) {
$.ajax({
url: "/Scripts/SwitchPlug.php?function-name=addPlug&plug="+plugId+"&description="+description+"&oncode="+oncode+"&offcode="+offcode,
success: function (result) {
console.log("success!");
console.log(result);
},
error:function(result){
console.log("error: ");
console.log(result);
}
});
}
function ChangePlugDescription(plugId, description) {
$.ajax({
url: "/Scripts/SwitchPlug.php?function-name=changeDescription&plug="+plugId+"&description="+description,
success: function (result) {
console.log("success!");
console.log(result);
},
error:function(result){
console.log("error: ");
console.log(result);
}
});
}