-
Notifications
You must be signed in to change notification settings - Fork 3
/
weather.html
404 lines (385 loc) · 15.1 KB
/
weather.html
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<!DOCTYPE html>
<html>
<head>
<title>Spark Core Weather Station</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
<script type="text/javascript" src="http://codeorigin.jquery.com/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script>
$(document).ready(function() {
var statusTimer = null;
var timer1, timer2, timer3, timer4, timer5, timer6, timer7, timer8;
var baseURL = "weather.php";
//--------------------------------------------------------------
// START - EDITABLE USER DATA
//
var accessToken = "";
var coreID = "xxxxxxxxxxxxxxxxxxxxxxxx";
// App Heading
var appHeading = "Spark Core Weather Station";
// Function 1
var funcKey1 = "start";
var args1 = "";
var label1 = ""; // button label 1 (set to "" to disable)
// Function 2
var funcKey2 = "trunk";
var args2 = "";
var label2 = ""; // button label 2 (set to "" to disable)
// Function 3
var funcKey3 = "lock";
var args3 = "";
var label3 = ""; // button label 3 (set to "" to disable)
// Function 4
var funcKey4 = "unlock";
var args4 = "";
var label4 = ""; // button label 4 (set to "" to disable)
// Variable 1
var varKey1 = "light";
var var1onLabel = "Running!";
var var1offLabel = "Stopped.";
var var1onState = null; // Set to null to allow raw value to be displayed.
var var1offState = null; // Set to null to allow raw value to be displayed.
var refresh1 = 10000; // variable 1 refresh rate in milliseconds (set to 0 to disable)
// Variable 2
var varKey2 = "press";
var var2onLabel = "Open!";
var var2offLabel = "Closed.";
var var2onState = null; // Set to null to allow raw value to be displayed.
var var2offState = null; // Set to null to allow raw value to be displayed.
var refresh2 = 10000; // variable 2 refresh rate in milliseconds (set to 0 to disable)
// Variable 3
var varKey3 = "temp1";
var var3onLabel = "Locked.";
var var3offLabel = "Unlocked!";
var var3onState = null; // Set to null to allow raw value to be displayed.
var var3offState = null; // Set to null to allow raw value to be displayed.
var refresh3 = 10000; // variable 3 refresh rate in milliseconds (set to 0 to disable)
// Variable 4
var varKey4 = "temp2";
var var4onLabel = "";
var var4offLabel = "";
var var4onState = null; // Set to null to allow raw value to be displayed.
var var4offState = null; // Set to null to allow raw value to be displayed.
var refresh4 = 10000; // variable 4 refresh rate in milliseconds (set to 0 to disable)
// Variable 5
var varKey5 = "humid";
var var5onLabel = "Running!";
var var5offLabel = "Stopped.";
var var5onState = null; // Set to null to allow raw value to be displayed.
var var5offState = null; // Set to null to allow raw value to be displayed.
var refresh5 = 10000; // variable 1 refresh rate in milliseconds (set to 0 to disable)
// Variable 6
var varKey6 = "wspeed";
var var6onLabel = "Open!";
var var6offLabel = "Closed.";
var var6onState = null; // Set to null to allow raw value to be displayed.
var var6offState = null; // Set to null to allow raw value to be displayed.
var refresh6 = 10000; // variable 2 refresh rate in milliseconds (set to 0 to disable)
// Variable 7
var varKey7 = "wdir";
var var7onLabel = "Locked.";
var var7offLabel = "Unlocked!";
var var7onState = null; // Set to null to allow raw value to be displayed.
var var7offState = null; // Set to null to allow raw value to be displayed.
var refresh7 = 10000; // variable 3 refresh rate in milliseconds (set to 0 to disable)
// Variable 8
var varKey8 = "rain";
var var8onLabel = "";
var var8offLabel = "";
var var8onState = null; // Set to null to allow raw value to be displayed.
var var8offState = null; // Set to null to allow raw value to be displayed.
var refresh8 = 10000; // variable 4 refresh rate in milliseconds (set to 0 to disable)
//
// END - EDITABLE USER DATA
//--------------------------------------------------------------
// Update app heading
$("#app-heading").html(appHeading);
// Override button styles
$(".btn-lg").css({"width":"60%"});
// Update button labels to code definitions
// or hide them if not defined.
(label1) ? $("#button-1").html(label1) : $("#button-1").hide();
(label2) ? $("#button-2").html(label2) : $("#button-2").hide();
(label3) ? $("#button-3").html(label3) : $("#button-3").hide();
(label4) ? $("#button-4").html(label4) : $("#button-4").hide();
// Override text input styles
$(".form-control").css({"display":"inline","width":"35%"});
// Hide variable text fields if not in use.
if(refresh1 === 0) $("#var-val-1").hide();
if(refresh2 === 0) $("#var-val-2").hide();
if(refresh3 === 0) $("#var-val-3").hide();
if(refresh4 === 0) $("#var-val-4").hide();
if(refresh5 === 0) $("#var-val-5").hide();
if(refresh6 === 0) $("#var-val-6").hide();
if(refresh7 === 0) $("#var-val-7").hide();
if(refresh8 === 0) $("#var-val-8").hide();
// Auto-refresh
// Turn on/off the variable refresh if refresh rates are defined
if(refresh1) {
$("#get-var-1").attr("disabled", "disabled");
timer1 = setInterval(function () {
getVariable1();
}, refresh1);
}
if(refresh2) {
$("#get-var-2").attr("disabled", "disabled");
timer2 = setInterval(function () {
getVariable2();
}, refresh2);
}
if(refresh3) {
$("#get-var-3").attr("disabled", "disabled");
timer3 = setInterval(function () {
getVariable3();
}, refresh3);
}
if(refresh4) {
$("#get-var-4").attr("disabled", "disabled");
timer4 = setInterval(function () {
getVariable4();
}, refresh4);
}
if(refresh5) {
$("#get-var-5").attr("disabled", "disabled");
timer5 = setInterval(function () {
getVariable5();
}, refresh5);
}
if(refresh6) {
$("#get-var-6").attr("disabled", "disabled");
timer6 = setInterval(function () {
getVariable6();
}, refresh6);
}
if(refresh7) {
$("#get-var-7").attr("disabled", "disabled");
timer7 = setInterval(function () {
getVariable7();
}, refresh7);
}
if(refresh8) {
$("#get-var-8").attr("disabled", "disabled");
timer8 = setInterval(function () {
getVariable8();
}, refresh8);
}
////
// Alerts
////
$("#info-alert").alert();
$("#info-alert").affix();
////
// Methods
////
function onMethodSuccess() {
alert = $("#info-alert");
alert.text("Success!").removeClass("alert-danger").addClass("alert-success");
if(statusTimer != null) {
clearTimeout(statusTimer);
alert.hide();
setTimeout(function() {
alert.show();
statusTimer = setTimeout(function() {
statusTimer = null;
alert.hide();
}, 1750);
}, 250);
}
else {
alert.show();
statusTimer = setTimeout(function() {
statusTimer = null;
alert.hide();
}, 2000);
}
}
function onMethodFailure(data) {
alert = $("#info-alert");
alert.text((data)?"Error! "+data:"Error!").removeClass("alert-success").addClass("alert-danger");
if(statusTimer != null) {
clearTimeout(statusTimer);
alert.hide();
setTimeout(function() {
alert.show();
statusTimer = setTimeout(function() {
statusTimer = null;
alert.hide();
}, 1750);
}, 250);
}
else {
alert.show();
statusTimer = setTimeout(function() {
statusTimer = null;
alert.hide();
}, 2000);
}
}
// The base level run method command
function doMethod(method, data) {
var url = baseURL + coreID + "/" + method;
$.ajax({
type: "POST",
url: url,
data: {
access_token: accessToken,
args: data
},
dataType: "json"
}).success(function(obj) {
console.log(obj);
(obj.return_value && obj.return_value == 200) ? onMethodSuccess() : onMethodFailure((obj.error)?obj.error:"");
}).fail(function(obj) {
onMethodFailure();
});
}
$("#button-1").on("click", function () {
doMethod(funcKey1, args1);
});
$("#button-2").on("click", function () {
doMethod(funcKey2, args2);
});
$("#button-3").on("click", function () {
doMethod(funcKey3, args3);
});
$("#button-4").on("click", function () {
doMethod(funcKey4, args4);
});
////
// Variables
////
function getVariable(variable, callback) {
var url = baseURL + "?method=" + variable + "&device=" + coreID;
$.ajax({
url: url,
dataType: "json"
}).success(function(obj) {
console.log(obj);
(obj.coreInfo.deviceID && obj.coreInfo.deviceID == coreID) ? onMethodSuccess() : onMethodFailure((obj.error)?obj.error:"");
callback(obj.result);
}).fail(function(obj) {
onMethodFailure();
});
}
// Get variable methods
function getVariable1() {
getVariable(varKey1, function (res) {
if(res === var1onState)
$("#var-val-1").val(var1onLabel);
else if(res === var1offState)
$("#var-val-1").val(var1offLabel);
else
$("#var-val-1").val(res);
});
}
function getVariable2() {
getVariable(varKey2, function (res) {
if(res === var2onState)
$("#var-val-2").val(var2onLabel);
else if(res === var2offState)
$("#var-val-2").val(var2offLabel);
else
$("#var-val-2").val(res);
});
}
function getVariable3() {
getVariable(varKey3, function (res) {
if(res === var3onState)
$("#var-val-3").val(var3onLabel);
else if(res === var3offState)
$("#var-val-3").val(var3offLabel);
else
$("#var-val-3").val(res);
});
}
function getVariable4() {
getVariable(varKey4, function (res) {
if(res === var4onState)
$("#var-val-4").val(var4onLabel);
else if(res === var4offState)
$("#var-val-4").val(var4offLabel);
else
$("#var-val-4").val(res);
});
}
function getVariable5() {
getVariable(varKey5, function (res) {
if(res === var5onState)
$("#var-val-5").val(var5onLabel);
else if(res === var5offState)
$("#var-val-5").val(var4offLabel);
else
$("#var-val-5").val(res);
});
}
function getVariable6() {
getVariable(varKey6, function (res) {
if(res === var6onState)
$("#var-val-6").val(var6onLabel);
else if(res === var6offState)
$("#var-val-6").val(var6offLabel);
else
$("#var-val-6").val(res);
});
}
function getVariable7() {
getVariable(varKey7, function (res) {
if(res === var7onState)
$("#var-val-7").val(var7onLabel);
else if(res === var7offState)
$("#var-val-7").val(var7offLabel);
else
$("#var-val-7").val(res);
});
}
function getVariable8() {
getVariable(varKey8, function (res) {
if(res === var8onState)
$("#var-val-8").val(var8onLabel);
else if(res === var8offState)
$("#var-val-8").val(var8offLabel);
else
$("#var-val-8").val(res);
});
}
});
</script>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h4 class="panel-title" id="app-heading">
Control
</h4>
</div>
<div id="buttons" class="panel">
<div class="panel-body">
<button type="button" class="btn btn-primary btn-lg" id="button-1">BUTTON 1</button>
Light Level: <input type="text" class="form-control" value="---" readonly id="var-val-1"><br/><br/>
<button type="button" class="btn btn-primary btn-lg" id="button-2">BUTTON 2</button>
Pressure: <input type="text" class="form-control" value="---" readonly id="var-val-2"><br/><br/>
<button type="button" class="btn btn-primary btn-lg" id="button-3">BUTTON 3</button>
Temperature 1: <input type="text" class="form-control" value="---" readonly id="var-val-3"><br/><br/>
<button type="button" class="btn btn-primary btn-lg" id="button-4">BUTTON 4</button>
Temperature 2: <input type="text" class="form-control" value="---" readonly id="var-val-4"><br/><br/>
Humidity: <input type="text" class="form-control" value="---" readonly id="var-val-5"><br/><br/>
Wind Speed: <input type="text" class="form-control" value="---" readonly id="var-val-6"><br/><br/>
Wind Direction: <input type="text" class="form-control" value="---" readonly id="var-val-7"><br/><br/>
Rainfall: <input type="text" class="form-control" value="---" readonly id="var-val-8"><br/><br/>
</div>
</div>
</div>
<div class="alert fade in" id="info-alert" hidden data-spy="affix"></div>
</div>
</body>
</html>