-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.htm
122 lines (101 loc) · 3.19 KB
/
index.htm
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
<!DOCTYPE HTML>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<style>
#myProgress {
position: relative;
width: 25%;
height: 20px;
background-color: #ddd;
}
#myBar {
position: absolute;
width: 1%;
height: 100%;
background-color: #4CAF50;
}
#label {
text-align: center; /* If you want to center it */
line-height: 20px; /* Set the line-height to the same as the height of the progress bar container, to center it vertically */
color: white;
}
</style>
<body>
<P>Set PSU Parameters:</br><br>
<form action="JavaScript:webset()">
Voltage: <input type="text" name="volt" id=txtVolt><br>
Current: <input type="text" name="curr" id=txtCurr><br>
<input type="checkbox" name="enable" value="en" id=enable>Enable Output<br>
<input type="submit" value="Submit">
</br>
</br>
</form>
<table border="2" id="dataTable" width="500px">
<tr>
<td>Voltage (mV)</td>
<td>Current (mA)</td>
<td>Timestamp</td>
</tr>
</table>
<br>
<br>
Battery Status:
<div id="myProgress">
<div id="myBar">
<div id="label">0%
</div>
</div>
</div>
</div>
<br>
<br>
<span id="uptime"></span><br>
<br><br>
<button onclick="start()">Connect</button>
<script type="text/javascript">
var setFunc = "webset";
var deviceID = "26003f000847343337373738";
var accessToken = "0688ae3c5c0a0177ce3b44b89647ad7a2c677035";
function start() {
document.getElementById("uptime").innerHTML = "Waiting for data...";
var eventSource = new EventSource("https://api.spark.io/v1/devices/" + deviceID + "/events/?access_token=" + accessToken);
var dt = document.getElementById("dataTable");
var rows = dt.rows.length;
eventSource.addEventListener('open', function(e) {
console.log("Opened!"); },false);
eventSource.addEventListener('error', function(e) {
console.log("Errored!"); },false);
eventSource.addEventListener('Stats', function(e) {
var parsedData = JSON.parse(e.data);
var jsonData = JSON.parse(parsedData.data);
if (dt.rows.length < 2)
{
var newRow = dt.insertRow(rows);
var cell2 = newRow.insertCell(0);
var cell3 = newRow.insertCell(1);
var cell4 = newRow.insertCell(2);
}
else
{
cell2 = dt.rows[1].cells[0];
cell3 = dt.rows[1].cells[1];
cell4 = dt.rows[1].cells[2];
}
// the letters below should match the ones from your JSON
cell2.innerHTML = jsonData.v; // <- change this
cell3.innerHTML = jsonData.c; // <- change this too
cell4.innerHTML = parsedData.published_at;
var batteryBar = document.getElementById("myBar");
batteryBar.style.width = jsonData.b + '%';
document.getElementById("label").innerHTML = jsonData.b + '%';
}, false);
}
function webset() {
var requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + setFunc + "/";
var newValue = (document.getElementById('txtVolt').value +"-" + document.getElementById('txtCurr').value +"-" +document.getElementById('enable').checked);
console.log(newValue);
$.post( requestURL, { params: newValue, access_token: accessToken });
}
</script>
</body>
</html>