-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (100 loc) · 4.26 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const express = require('express')
const ws = require('ws');
const HyperDeck = require("hyperdeck-js-lib");
const app = express()
const http = require('http')
const server = http.createServer(app);
const wss = new ws.Server({ server });
wss.on('connection', (ws) => {
let HyperDeckClient
ws.on('message', async (message) => {
let data = JSON.parse(message)
console.log(`Command received: ${data.command}`);
if (data.command == 'connect') {
HyperDeckClient = new HyperDeck.Hyperdeck(data.ipAddress);
HyperDeckClient.onConnected().then(function() {
HyperDeckClient.makeRequest("device info").then(function(response) {
data = {
type: 'connection',
status: response.code,
message: `Connected to Hyperdeck: ${response.params["unique id"]}`
}
ws.send(JSON.stringify(data))
})
HyperDeckClient.makeRequest("notify: display timecode: true").then(function(response) {
console.log(response);
})
HyperDeckClient.getNotifier().on("asynchronousEvent", function(response) {
if (response.code == 513) {
let send = {
type: 'displayTimecode',
timecode: response.params['display timecode']
}
ws.send(JSON.stringify(send))
return;
} else if (response.code == 502) {
let send = {
type: 'updateClips'
}
ws.send(JSON.stringify(send))
return;
}
console.log("Got an asynchronous event", response);
});
HyperDeckClient.getNotifier().on("connectionLost", function() {
console.error("Connection lost.");
});
}).catch(function() {
console.error("Failed to connect to hyperdeck.");
});
} else if (data.command == 'getClips') {
HyperDeckClient.clipsGet().then((res) => {
let clips = []
for (let i = 1; i <= res.params['clip count']; i++) {
let params = res.params[`${i}`]
params = params.split(' ')
let clipOutTime = params[params.length - 1]
let clipInTime = params[params.length - 2]
let clipName = res.params[`${i}`]
clipName = clipName.substring(0, clipName.length - 24)
clips.push({
id: i,
name: clipName.toUpperCase(),
outTime: clipOutTime,
inTime: clipInTime
})
}
clips.sort((a,b) => a.name.localeCompare(b.name))
let send = {
type: 'clipList',
clips: clips
}
ws.send(JSON.stringify(send))
})
} else if (data.command == 'play') {
HyperDeckClient.makeRequest(`play: single clip: true`).then((res) => {
ws.send(JSON.stringify(res))
})
} else if (data.command == 'stop') {
HyperDeckClient.stop().then((res) => {
let send = {
type: 'stop'
}
ws.send(JSON.stringify(send))
})
} else if (data.command == 'loadClip') {
console.log(data.clipId);
HyperDeckClient.makeRequest(`goto: clip id: ${data.clipId}`).then((res) => {
console.log(res);
})
} else if (data.command == 'goToStart') {
HyperDeckClient.makeRequest(`goto: clip: start`).then((res) => {
console.log(res);
})
}
});
});
server.listen(process.env.PORT || 8999, () => {
console.log(`Server started on port ${server.address().port}`);
});
app.use(express.static('public'))