blob: 0fe83b4cd0ce9c05b0d7ce70654c80c846f41bfa [file] [log] [blame]
Comran Morshedc4ce9512015-03-08 11:51:09 +00001<!DOCTYPE html>
2<html>
3
4<head>
5<title>Hello, world</title>
6<script type="text/javascript">
7var escapable =
8 /[\x00-\x1f\ud800-\udfff\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufff0-\uffff]/g;
9var ws;
10var intervalTime = 50;
11var safetyTimeout;
12var safetyIntervalTime = 10;
13var selected = 0;
14
15// Filter out junky JSON packets that will otherwise cause nasty decoding errors.
16function filterUnicode(quoted) {
17 escapable.lastIndex = 0;
18 if (!escapable.test(quoted)) return quoted;
19
20 return quoted.replace(escapable, function(a) {
21 return '';
22 });
23}
24
25// Change the current data index to plot on the graph.
26function changeSelected(select) {
27 selected = select;
28 document.getElementById("selected").innerHTML = "Selected: " + selected;
29}
30
31// Get a new JSON packet from the websocket on the robot.
32function refresh() {
33 ws.send(lastSampleID);
34 safetyTimeout = setTimeout(safetyRefresh, safetyIntervalTime);
35}
36
37function safetyRefresh(){
38 console.log("Safety timeout exceeded. Performing additional refresh...");
39 refresh();
40}
41
42window.onload = function() {
43 var dps = [];
44 var numDatas = 0;
45 var chart = new CanvasJS.Chart("chartContainer", {
46 title: {
47 text: "Live Data"
48 },
49 axisX: {
50 title: "Time (sec)"
51 },
52 axisY: {
53 title: "Units"
54 },
55 zoomEnabled: true,
56 panEnabled: true,
57 data: [{
58 color: 'rgba(119, 152, 191, 1.0)',
59 type: "scatter",
60 dataPoints: dps
61 }],
62 });
63
64 chart.render();
65
66 $(function() {
67 ws = new WebSocket('ws://' + document.location.host + '/ws');
68 run = true;
69 xVal = 1;
70 lastSampleID = -1;
71
72 // Socket created & first opened.
73 ws.onopen = function() {
74 run = true;
75 refresh();
76 };
77
78 // Socket disconnected.
79 ws.onclose = function() {
80 console.log('onclose');
81 run = false;
82 $('#message').text('Lost connection.');
83 };
84
85 // Received message over the socket, so parse and chart it.
86 ws.onmessage = function(message) {
87 console.log(message);
88 clearTimeout(safetyTimeout);
89 message = message.data;
90 //$('#data').html(message);
91 if(message.charAt(0) == "*"){
92 message = message.substring(1);
93 var names = message.split(",");
94 for (var i = numDatas; i < names.length; i++) {
95 $('#dataTable').append('<tr onClick="changeSelected(' + i +
96 ')"><td>' + names[i] + '</td><td></td></tr>');
97 numDatas++;
98 }
99 lastSampleID = 0;
100 }else{
101 var samples = message.split("$");
102 for(var x = 0;x < samples.length;x++){
103 var info = samples[x].split("%");
104 lastSampleID = info[0];
105
106 if(!(typeof info[2] === "undefined")){
107 var values = info[2].split(",");
108 for(var y = 0;y < values.length;y++){
109 if(!(typeof info[1] === "undefined"
110 || typeof values[y] === "undefined")){
111 $('#dataTable').find('tr').eq(y).find('td').eq(1)
112 .text(values[y]);
113 if(y == selected){
114 dps.push({
115 x: parseFloat(info[1]),
116 y: parseFloat(values[y])
117 });
118 if(dps.length > 10000){
119 dps.shift();
120 }
121 }
122 }
123 }
124 }
125 }
126
127 chart.render();
128 }
129
130 if(run){
131 setTimeout(refresh, intervalTime);
132 }
133 };
134
135 // Socket error, most likely due to a server-side error.
136 ws.onerror = function(error) {
137 console.log('onerror ' + error);
138 run = false;
139 };
140 });
141}
142</script>
143<script type="text/javascript" src='/lib/jquery-1.4.4.js'></script>
144<script type="text/javascript" src='/lib/canvasjs.min.js'></script>
145<script type="text/javascript" src='/lib/reconnecting-websocket.min.js'></script>
146</head>
147<body>
148<div style="width: 1200px;margin-left: auto;margin-right:auto">
149 <table id="dataTable" style="width: 200px;cell-spacing:0;cell-padding:0;
150 text-align:left">
151 </table>
152 <div id="chartContainer" style="height:600px; width:100%;"></div>
153 <div style="width: 1000px;float: right">
154 <p id="message" style="color: #FF0000"></p>
155 <p id="selected">Selected: 0</p>
156 <p id="data"></p>
157 </div>
158</div>
159</body>
160</html>