-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlive.js
More file actions
91 lines (75 loc) · 2.68 KB
/
Copy pathlive.js
File metadata and controls
91 lines (75 loc) · 2.68 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
var $events = document.getElementById('events');
var $ratwheel_speed = document.getElementById('ratwheel_speed');
var $armwheel_speed = document.getElementById('armwheel_speed');
var $ratwheel_ping = document.getElementById('last_ping_ratwheel');
var $armwheel_ping = document.getElementById('last_ping_armwheel');
var $ratwheel_likes = document.getElementById('ratwheel_likes');
var $armwheel_likes = document.getElementById('armwheel_likes');
// function onchange() {
// var rpm = $input.value;
// var duration = 60 / rpm;
// $div.style.animationDuration = duration + 's';
// }
// var socket_url = ':' + SOCKET_PORT;
console.log('connecting to socket');
var socket = io();
// init rpm
var rpm = parseInt($ratwheel_speed.innerHTML);
if(rpm>0) {
var duration = 60 / rpm;
document.getElementById('ratwheel').style.animationDuration = duration + 's';
}
rpm = parseInt($armwheel_speed.innerHTML);
if(rpm>0) {
var duration = 60 / rpm;
document.getElementById('armwheel').style.animationDuration = duration + 's';
}
socket.on('update', function(data) {
console.log('update received:');
console.log(data);
if(data.status=='active') {
var duration = 60 / data.rpm;
document.getElementById(data.deviceId).style.animationDuration = duration + 's';
if(data.deviceId=='ratwheel') {
$ratwheel_speed.textContent = data.rpm;
$ratwheel_likes.textContent = data.likes;
}
if(data.deviceId=='armwheel') {
$armwheel_speed.textContent = data.rpm;
$armwheel_likes.textContent = data.likes;
}
} else {
document.getElementById(data.deviceId).style.animationDuration = 0 + 's';
if(data.deviceId=='ratwheel') {
$ratwheel_speed.textContent = '0';
$ratwheel_likes.textContent = '0';
}
if(data.deviceId=='armwheel') {
$armwheel_speed.textContent = '0';
$armwheel_likes.textContent = '0';
}
}
var item = document.createElement('li');
item.textContent = data.deviceId + ': ' + data.rpm + ' rpm (session: ' + data.sessionId + '), status: ' + data.status;
$events.appendChild(item);
});
socket.on('ping', function(data) {
console.log('ping received:');
console.log(data);
if(data.ratwheel && data.ratwheel.last_ping) {
$ratwheel_ping.textContent = data.ratwheel.last_ping;
}
if(data.armwheel && data.armwheel.last_ping) {
$armwheel_ping.textContent = data.armwheel.last_ping;
}
});
socket.on('like', function(data) {
console.log('like received:');
console.log(data);
if(data.ratwheel && data.ratwheel.session && data.ratwheel.session.status==='active') {
$ratwheel_likes.textContent = data.ratwheel.session.likes;
}
if(data.armwheel && data.armwheel.session && data.armwheel.session.status==='active') {
$armwheel_likes.textContent = data.armwheel.session.likes;
}
});