-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
53 lines (47 loc) · 1.37 KB
/
index.js
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
var app = angular.module('Index', ["chart.js"]);
app.controller('IndexCtrl', function ($scope, $http, $interval) {
$scope.tilt = null;
$scope.options = {
animation: {
duration: 0
},
scales: {
yAxes: [{
ticks: {
max: 180,
min: -180,
stepSize: 30
}
}]
},
maintainAspectRatio: false
};
$scope.labels = [];
$scope.values = [];
$scope.numEntries = 10;
for(var i=0; i<$scope.numEntries; i++) {
$scope.labels.push(' ');
$scope.values.push(null);
}
$scope.data = [ $scope.values ];
$scope.series = ["Tilt"];
var host = location.origin.replace(/^http/, 'ws');
var ws = new WebSocket(host);
ws.onmessage = function (event) {
$scope.$apply(function(){
var data = JSON.parse(event.data);
$scope.tilt = data.tilt;
var d = new Date();
$scope.labels.push(d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds());
$scope.values.push($scope.tilt);
if($scope.values.length > $scope.numEntries) {
$scope.labels.shift();
$scope.values.shift();
}
});
};
$interval(ping, 1000);
function ping() {
ws.send("ping");
}
});