-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.html
174 lines (161 loc) · 5.76 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>Super Simple Spark Core Controller</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 baseURL = "https://api.spark.io/v1/devices/";
//--------------------------------------------------------------
// START - EDITABLE USER DATA
//
var accessToken = "0123456789012345678901234567890123456789";
var coreID = "0123456789012345678901234";
// App Heading
var appHeading = "My App";
// Function 1
var funcKey1 = "test";
var args1 = "";
var label1 = "Test 1"; // button label 1
// Function 2
var funcKey2 = "test";
var args2 = "";
var label2 = "Test 2"; // button label 2
// Function 3
var funcKey3 = "";
var args3 = "";
var label3 = ""; // button label 3
// Function 4
var funcKey4 = "test";
var args4 = "";
var label4 = "Test 4"; // button label 4
//
// END - EDITABLE USER DATA
//--------------------------------------------------------------
// Update app heading
$("#app-heading").html(appHeading);
// 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();
////
// Alerts
////
$("#info-alert").alert();
$("#info-alert").affix();
////
// Methods
////
// On method success
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);
});
});
</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><br/><br/>
<button type="button" class="btn btn-primary btn-lg" id="button-2">BUTTON 2</button><br/><br/>
<button type="button" class="btn btn-primary btn-lg" id="button-3">BUTTON 3</button><br/><br/>
<button type="button" class="btn btn-primary btn-lg" id="button-4">BUTTON 4</button>
</div>
</div>
</div>
<div class="alert fade in" id="info-alert" hidden data-spy="affix"></div>
</div>
</body>
</html>