forked from mint-dewit/folderplayout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
158 lines (137 loc) · 3.75 KB
/
controller.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
var chokidar = require('chokidar');
var {CasparCG} = require('casparcg-connection')
var mediaHelper = require('./lib/mediaHelper.js');
var libqueue = require('./lib/queue.js');
var fs = require('fs');
var connection = new CasparCG({onConnected: connected});
var clips = {Playlist: []};
var queue;
var timetable;
var watched = [];
var mediaFolder;
queue = libqueue(connection);
function connected () {
console.log('connected');
connection.clear(1);
connection.playDecklink
(1, 10, 1);
}
function fileAdded (path) {
var folder = mediaHelper.parseFolder(path);
var parsedPath = mediaHelper.parsePath(path);
var parsedFile = mediaHelper.parseFileName(path);
connection.cinf(parsedFile).then(casparObject => {
var parsedDuratiion = mediaHelper.parseDuration(casparObject.response.data.duration, casparObject.response.data.fps);
clips[folder].push({path: parsedPath, name: parsedFile, duration: parsedDuratiion});
clips[folder].sort(mediaHelper.compareClipOrder);
console.log(clips);
})
}
function fileChanged (path) {
var folder = mediaHelper.parseFolder(path);
var parsedPath = mediaHelper.parsePath(path);
var parsedFile = mediaHelper.parseFileName(path);
for (let clip of clips[folder]) {
if (clip.path === parsedPath) {
connection.cinf(parsedFile).then(casparObject => {
clip.path = parsedPath;
clip.name = parsedFile;
clip.duration = mediaHelper.parseDuration(casparObject.response.data.duration, casparObject.response.data.fps);
clip.created = casparObject.response.data.created;
clips[folder].sort(mediaHelper.compareClipOrder);
});
}
}
}
function fileRemoved (path) {
var folder = mediaHelper.parseFolder(path);
var parsedPath = mediaHelper.parsePath(path);
for (var i in clips[folder]) {
if (clips[folder][i].path === parsedPath) {
clips[folder].splice(i, 1);
clips[folder].sort(mediaHelper.compareClipOrder);
}
}
}
var playbackDirectories = chokidar.watch('../Server/media/Playlist'); // hardcoded path is questionable
playbackDirectories
.on('add', fileAdded)
.on('change', fileChanged)
.on('unlink', fileRemoved);
function timesChanged () {
for (let dir in timetable) {
let found = false;
for (let watcher of watched) {
if (watcher === dir) found = true;
}
if (!found) {
watched.push(dir);
console.log('../Server/media/'+dir)
playbackDirectories.add('../Server/media/'+dir);
clips[dir] = [];
}
}
for (var i in watched) {
let found = false
for (let dir in timetable) {
if (dir === watched[i]) {
found = true;
}
}
if (!found) {
watched.splice(i, 1);
playbackDirectories.unwatch('../Server/media/'+dir);
clips[dir] = undefined
}
}
}
var configFile = chokidar.watch('./timetable.json');
configFile.on('change', () => {
var config = fs.readFileSync('./timetable.json');
try {
timetable = JSON.parse(config);
configChanged();
}
catch (err) {
console.log('error parsing config!');
}
})
function checkTime () {
var date = new Date ();
var mute = false;
if (date.getMinutes() === 59 && date.getSeconds() === 58) {
console.log('load');
for (let clip of clips.Playlist) {
queue.add(clip);
mute = true;
}
for (var dir in timetable) {
for (let time of timetable[dir]) {
if (time-1 === date.getHours()) {
console.log('append folder:', dir);
for (let clip of clips[dir]) {
mute = true;
queue.add(clip);
}
}
}
}
if (mute) connection.mixerVolume(1, 10, 0, 50);
setTimeout(checkTime, 1000);
}
else if (date.getMinutes() === 0 && date.getSeconds() === 0) {
queue.play();
setTimeout(checkTime, 1000);
} else {
setTimeout(checkTime, 50);
}
}
try {
let times = fs.readFileSync('./timetable.json');
timetable = JSON.parse(times);
timesChanged();
}
catch (err) {
console.log('error parsing config!', err);
}
checkTime();