-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbak.html
More file actions
234 lines (191 loc) · 6.26 KB
/
bak.html
File metadata and controls
234 lines (191 loc) · 6.26 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
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
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Kritsnam : Real-time data delivery Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="mqttws31.js" type="text/javascript"></script>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="config.js" type="text/javascript"></script>
<link type="text/css" rel="stylesheet" href="rickshaw.min.css">
<script src="d3.min.js"></script>
<script src="d3.layout.min.js"></script>
<script src="rickshaw.min.js"></script>
<div id="chart"></div>
<div id="y_axis"></div>
<div id="chart"></div>
<div id="legend"></div>
<form id="offset_form" class="toggler">
<input type="radio" name="offset" id="lines" value="lines" checked>
<label class="lines" for="lines">lines</label><br>
<input type="radio" name="offset" id="stack" value="zero">
<label class="stack" for="stack">stack</label>
</form>
<script type="text/javascript">
var mqtt;
var reconnectTimeout = 3000;
function MQTTconnect() {
if (typeof path == "undefined") {
path = '/mqtt';
}
mqtt = new Paho.MQTT.Client(
host,
port,
path,
"web_" + parseInt(Math.random() * 100, 10)
);
var options = {
timeout: 3,
useSSL: useTLS,
cleanSession: cleansession,
onSuccess: onConnect,
onFailure: function (message) {
$('#status').val("Connection failed: " + message.errorMessage + "Retrying");
setTimeout(MQTTconnect, reconnectTimeout);
}
};
mqtt.onConnectionLost = onConnectionLost;
mqtt.onMessageArrived = onMessageArrived;
if (username != null) {
options.userName = username;
options.password = password;
}
console.log("Host="+ host + ", port=" + port + ", path=" + path + " TLS = " + useTLS + " username=" + username + " password=" + password);
mqtt.connect(options);
}
function onConnect() {
$('#status').val('Connected to ' + host + ':' + port + path);
// Connection succeeded; subscribe to our topic
mqtt.subscribe(topic, {qos: 0});
$('#topic').val(topic);
}
function onConnectionLost(response) {
setTimeout(MQTTconnect, reconnectTimeout);
$('#status').val("connection lost: " + responseObject.errorMessage + ". Reconnecting");
};
var arr = [];
var dict = {};
var temp = [];
var palette = new Rickshaw.Color.Palette();
var graph = new Rickshaw.Graph( {
element: document.querySelector("#chart"),
width: 540,
height: 240,
renderer: 'line',
series: [
{
name: "Northeast",
data: temp,
color: palette.color()
},
]
} );
var x_axis = new Rickshaw.Graph.Axis.Time( { graph: graph } );
var y_axis = new Rickshaw.Graph.Axis.Y( {
graph: graph,
orientation: 'left',
tickFormat: Rickshaw.Fixtures.Number.formatKMBT,
element: document.getElementById('y_axis'),
} );
var legend = new Rickshaw.Graph.Legend( {
element: document.querySelector('#legend'),
graph: graph
} );
var offsetForm = document.getElementById('offset_form');
offsetForm.addEventListener('change', function(e) {
var offsetMode = e.target.value;
if (offsetMode == 'lines') {
graph.setRenderer('line');
graph.offset = 'zero';
} else {
graph.setRenderer('stack');
graph.offset = offsetMode;
}
graph.render();
}, false);
function onMessageArrived(message) {
var topic = message.destinationName;
var payload = message.payloadString;
var table = document.getElementById("demo");
var row = table.insertRow(1);
var col0 = row.insertCell(0);
var col1 = row.insertCell(1);
var col2 = row.insertCell(2);
var col3 = row.insertCell(3);
var col4 = row.insertCell(4);
var node_id = topic.split("/")[1]
var msg = payload.split(",")
col0.innerHTML = node_id; // node_id
col1.innerHTML = msg[0]; // data
col2.innerHTML = msg[1]; // battery
col3.innerHTML = msg[2]; // rssi
var fTime = new Date(msg[3]*1000);
var hours = fTime.getHours();
var minutes = "0" + fTime.getMinutes();
var seconds = "0" + fTime.getSeconds();
var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
col4.innerHTML = formattedTime; // timestamp
var dict = {};
dict["x"] = parseInt(msg[3]);
dict["y"] = parseInt(msg[0]);
arr.push(dict);
var step;
temp = [];
if (arr.length > 10) {
for (step = arr.length - 10; step <= arr.length ; step++) {
var dict = {};
dict["x"] = arr[step].x;
alert(dict["x"]);
dict["y"] = arr[step].y;
temp.push(dict);
}
graph.render();
}
};
$(document).ready(function() {
MQTTconnect();
});
</script>
</head>
<body>
<h1>Real-Time data retrieval for Testing</h1>
<div>
<div>Subscribed to <input type='text' id='topic' disabled />
Status: <input type='text' id='status' size="64" disabled /></div>
<div id="demo_table">
<table id="demo" cellspacing="32">
<tr>
<th>Node ID</th>
<th>Data</th>
<th>Battery</th>
<th>RSSI</th>
<th>Timestamp</th>
</tr>
</table>
</div>
</div>
<style>
#chart_container {
display: inline-block;
font-family: Arial, Helvetica, sans-serif;
}
#chart {
float: left;
}
#legend {
float: left;
margin-left: 15px;
}
#offset_form {
float: left;
margin: 2em 0 0 15px;
font-size: 13px;
}
#y_axis {
float: left;
width: 40px;
}
</style>
</div>
</body>
</html>