This repository was archived by the owner on Sep 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
285 lines (242 loc) · 8.58 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
const fetchParameters = {
method: 'GET',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
},
redirect: 'follow',
referrerPolicy: 'no-referrer',
};
let currentPlayList = []
let hyperDeckClipList = [];
let currentlyDisplayedTable = 'tableShowClips';
let currentTimeCodeInteger = 0;
let currentClipIndex ='';
let previousClipIndex ='';
const convertTimeCodeToInteger = (timeString, framerate) =>
{
// converts to an arbitrary integer to be used for timecode comparisons
const timeArray = timeString.split(":");
return parseInt(timeArray[0] + timeArray[1] + timeArray[2] + timeArray[3]);
}
const getHyperDeckStatus = async () =>
{
const response = await fetch('/status', fetchParameters);
const data = await response.json();
currentTimeCodeInteger = convertTimeCodeToInteger(data['display timecode']);
currentClipIndex = convertTimeCodeToInteger(data['clip id']);
if (currentClipIndex !== previousClipIndex && currentlyDisplayedTable === 'tableShowClips')
{
previousClipIndex = currentClipIndex;
getHyperDeckClips().then((clipList => listClipsAsTable(clipList, 'H')))
}
return data;
}
const displayHyperDeckStatus = async () =>
{
const data = await getHyperDeckStatus();
/*
<td id="tdStatus"></td>
<td id="tdSpeed"></td>
<td id="tdSlotId"></td>
<td id="tdClipId"></td>
<td id="tdSingleClipMode"></td>
<td id="tdCurrentTimeCode"></td>
<td id="tdVideoFormat"></td>
<td id="tdLoop"></td>
*/
if (data)
{
document.getElementById('tdStatus').innerText = `Status: ${data.status}`;
document.getElementById('tdSpeed').innerText = `Speed: ${data.speed}%`;
document.getElementById('tdSlotId').innerText = `Card Slot #: ${data['slot id']}`;
document.getElementById('tdClipId').innerText = `Current Clip: ${data['clip id']}`;
document.getElementById('tdSingleClipMode').innerText = `Play single clip?: ${data['single clip]'] ? "Yes" : "No"}`;
document.getElementById('tdCurrentTimeCode').innerText = `Current TimeCode: ${data['display timecode']} (${currentTimeCodeInteger})`;
document.getElementById('tdVideoFormat').innerText = `Video format: ${data['video format']}`;
document.getElementById('tdLoop').innerText = `Status: ${data.loop ? "Yes" : "No"}`;
}
}
const getHyperDeckClips = async () =>
{
const response = await fetch('/getclips', fetchParameters);
return await response.json();
}
const play = async (loop) =>
{
const response = await fetch(`/play${loop ? '/loop' : ''}`, fetchParameters);
return await response.json();
}
const playNow = async (clipIndex, loop) =>
{
const response = await fetch(`/playclip/${clipIndex}${loop ? '/loop' : ''}`, fetchParameters);
return await response.json();
}
const stop = async () =>
{
const response = await fetch('/stop', fetchParameters);
return await response.json();
}
const switchClipTables = async (button) =>
{
if (currentlyDisplayedTable === 'tableShowClips')
{
listClipsAsTable(currentPlayList, 'P');
currentlyDisplayedTable = 'tablePlayList';
button.innerText = "Show Clips in HyperDeck";
}
else
{
listClipsAsTable(hyperDeckClipList, 'H');
currentlyDisplayedTable = 'tableShowClips';
button.innerText = "Show Play List";
}
}
const getPlayLists = async () =>
{
const response = await fetch(`/playlist`, fetchParameters);
return await response.json();
}
const populateSelectControlWithPlayLists = async () =>
{
const playLists = await getPlayLists();
const selectPlaylists = document.getElementById('selectPlayLists');
for (const playList of playLists)
{
const option = document.createElement('option');
option.value = playList['playListId'];
option.text = playList['playListName'];
selectPlaylists.appendChild(option);
}
}
const getClipsInPlayList = async (selectControl) =>
{
const playListId = selectControl.value;
const response = await fetch(`/playlist/${playListId}`, fetchParameters);
const data = await response.json();
return data;
}
const refreshClipList = () =>
{
getHyperDeckClips()
.then((clipList =>
{
hyperDeckClipList = clipList;
listClipsAsTable(clipList, 'H')
}));
}
const addClipToPlayList = (clip) =>
{
if (!currentPlayList.find(playListClip => playListClip.index === clip.index ))
{
currentPlayList.push(clip);
}
listClipsAsTable(hyperDeckClipList, 'H');
}
const addClipToPlayListOnServer = async (clip) =>
{
const response = await fetch('/status', fetchParameters);
const data = await response.json();
}
const removeClipFromPlayList = (clip) =>
{
if (currentPlayList.find(playListClip => playListClip.index === clip.index ))
{
let updatedPlayList = [];
for (let thisClip of currentPlayList)
{
if (thisClip.index !== clip.index)
updatedPlayList.push(thisClip);
}
currentPlayList = updatedPlayList;
}
listClipsAsTable( currentPlayList, 'P');
}
const listClipsAsTable = (clipList, clipListType) =>
{
// clipListType can be 'H' for HyperDeck clips, or 'P' for playlist
let table = document.getElementById('tableShowClips');
table.innerHTML = ""; //empty the table
let row = document.createElement('tr');
//Set headers
let thClipIndex = document.createElement('th');
let thClipName = document.createElement('th');
let thClipTimeCode = document.createElement('th');
let thClipDuration = document.createElement('th');
let thActions = document.createElement('th');
thClipIndex.innerText = "No.";
thClipName.innerText = "Name";
thClipTimeCode.innerText = "TimeCode";
thClipDuration.innerText = "Duration";
thActions.innerText = "Actions";
row.appendChild(thClipIndex);
row.appendChild(thClipName);
row.appendChild(thClipTimeCode);
row.appendChild(thClipDuration);
row.appendChild(thActions);
table.appendChild(row);
//Find which clip is the current clip that the playhead is on:
//Display data
for (let clip of clipList)
{
let row = document.createElement('tr');
let tdClipIndex = document.createElement('td');
tdClipIndex.style.textAlign = "right";
let tdClipName = document.createElement('td');
let tdClipTimeCode = document.createElement('td');
let tdClipDuration = document.createElement('td');
let tdActions = document.createElement('td');
tdClipIndex.innerText = clip.hyperDeckClipIndex;
tdClipName.innerText = clip.name;
tdClipTimeCode.innerText = clip.timecode;
tdClipDuration.innerText = clip.duration;
const buttonAddRemoveClip = document.createElement('button');
buttonAddRemoveClip.onclick = () => addClipToPlayList(clip);
if (clipListType === "H")
{
const clipInPlaylistCount = currentPlayList.filter(playListClip => playListClip.hyperDeckClipIndex === clip.hyperDeckClipIndex ).length;
buttonAddRemoveClip.innerText = `Add ${clipInPlaylistCount > 0 ? " again" : ""}`;
buttonAddRemoveClip.onclick = () => addClipToPlayList(clip);
}
else
{
buttonAddRemoveClip.onclick = () => removeClipFromPlayList(clip);
buttonAddRemoveClip.innerText = "Remove";
}
tdActions.appendChild(buttonAddRemoveClip);
const buttonPlayNow = document.createElement('button');
buttonPlayNow.onclick = () => playNow(clip.hyperDeckClipIndex, false);
buttonPlayNow.innerText = "Play Now";
tdActions.appendChild(buttonPlayNow);
row.appendChild(tdClipIndex);
row.appendChild(tdClipName);
row.appendChild(tdClipTimeCode);
row.appendChild(tdClipDuration);
row.appendChild(tdActions);
if (clip.index === currentClipIndex)
{
row.style.background = '#ddaaaa';
}
table.appendChild(row);
}
}
const getCurrentPlayList = async () =>
{
//Get current playlist
currentPlayList = await getClipsInPlayList(document.getElementById('selectPlayLists'));
}
window.addEventListener('load', () =>
{
displayHyperDeckStatus()
.then(() => getHyperDeckClips())
.then((clipList =>
{
hyperDeckClipList = clipList;
listClipsAsTable(clipList, 'H')
}))
.then(() => populateSelectControlWithPlayLists())
.then(() => getCurrentPlayList());
})
setInterval(displayHyperDeckStatus, 500);