This repository has been archived by the owner on Oct 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (79 loc) · 2.23 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
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
const app = require("express")();
const server = require("http").Server(app);
const io = require("socket.io")(server);
const Board = require("./utils/cyton-board");
const socketC = require("./constants/socket-action-types");
const headsetC = require("./constants/headset-action-types");
/**
* Config Variables
*/
require("dotenv").load();
const { PORT } = process.env;
/**
* Socket Connection
*/
io.on("connection", socket => {
// Connect Websocket
console.log("Websocket connected");
socket.emit(socketC.CONNECT_SUCCESS);
var board;
// Connect Cyton Board
socket.on(headsetC.CONNECT_HEADSET, async config => {
board = new Board({
debug: config.debug || true,
verbose: config.debug || true,
simulate: config.debug || true,
boardType: "daisy",
hardSet: true
});
try {
await board.connect(config);
socket.emit(headsetC.CONNECT_HEADSET_SUCCESS);
} catch (error) {
socket.emit(headsetC.CONNECT_HEADSET_FAILURE, { error });
throw error;
}
});
// Start Sample
socket.on(headsetC.START_SAMPLE, async () => {
try {
await board.streamStart();
socket.emit(headsetC.START_SAMPLE_SUCCESS);
board.on("sample", sample => {
const data = new Buffer(JSON.stringify(sample));
socket.emit(headsetC.DATA_SAMPLE, { data });
// console.log(sample);
});
} catch (error) {
socket.emit(headsetC.START_SAMPLE_FAILURE, { error });
}
});
// Stop Sample
socket.on(headsetC.STOP_SAMPLE, async () => {
try {
await board.streamStop();
socket.emit(headsetC.STOP_SAMPLE_SUCCESS);
} catch (error) {
socket.emit(headsetC.STOP_SAMPLE_FAILURE, { error });
}
});
// Disconnect Board
socket.on(headsetC.DISCONNECT_HEADSET, async () => {
if (board.isStreaming()) {
try {
await board.streamStop();
} catch (error) {
socket.emit(headsetC.DISCONNECT_HEADSET_FAILURE, { error });
}
}
try {
await board.disconnect();
socket.emit(headsetC.DISCONNECT_HEADSET_SUCCESS);
} catch (error) {
socket.emit(headsetC.DISCONNECT_HEADSET_FAILURE, { error });
}
});
});
exports.server = server.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});