-
Notifications
You must be signed in to change notification settings - Fork 1
/
robot.js
77 lines (65 loc) · 1.89 KB
/
robot.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const { Board, Piezo, Led } = require('johnny-five');
const express = require('express');
const { Server } = require('http');
const socketIO = require('socket.io');
const songs = require('j5-songs');
// Import project config
const config = require('./config');
// Set up the socker server
const app = express();
const http = Server(app);
const io = socketIO.listen(http);
// Make a new johnny-five Board() instance
const board = new Board();
// Begin the server under the specified port
http.listen(config.port, () => {
console.log(`Server Running under *:${config.port}. Remember to run 'yarn start' to run the app.`);
});
/**
*
* When the board is ready, notify the terminal console.
* Then specify the buzzer's pin using the Piezo class.
*
*/
board.on('ready', function() {
console.log('board ready');
// Store the Piezo in a constant
const buzzer = new Piezo(3);
// If the board is connected and is connected to the client, give a handshake.
io.on('connect', (client) => {
client.on('join', handshake => {
/**
*
* Let the app know that they've now connected to the robot so the app's
* interface can be updated to remove the loading animation and show the
* playlist controls.
*
*/
io.emit('robot-connected', 'Robot Connected');
// Write the handshake in the terminal console
console.log(handshake);
});
/**
*
* When the app selects a song to play, stop the buzzer playing the current
* song, then play the selected song.
*
*/
client.on('play-song', (song) => {
buzzer.stop();
buzzer.play(songs.load(song), (songEnded) => {
if(songEnded) {
io.emit('song-ended');
}
});
});
/**
*
* If the app selects a song that's already playing, stop the buzzer.
*
*/
client.on('stop-song', () => {
buzzer.stop();
});
});
});