Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
559 lines (505 loc) · 16.7 KB
/
Copy pathscript.js
File metadata and controls
559 lines (505 loc) · 16.7 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const DATEPART_HOUR = 0;
const DATEPART_MIN = 1;
const DATEPART_SEC = 2;
var clockTimer = setInterval(updateClock, 250);
var mainTimer = setInterval(updateTimer, 1000);
var minuteTimer = setInterval(function () { checkForIrn(); checkForAds(); getEngineeringMessage(); }, 60 * 1000);
var halfHourTimer = setInterval(function() { loadSchedule(); loadScheduledMessages(); }, 30 * 60 * 1000);
var clock = null;
var networkGreenroomOK = true;
var networkStudioAOK = true;
var networkExternalOK = true;
var currentStudio = null;
var loadedFromGreenroom = false;
var allowGreenroomSlideAnimation = true;
var maxSlideshowImgs = 0;
var lastSlideshowImg = -1;
var secondsSinceSlideChange = 12;
var runningInStudio = "";
var studioDelay = 0;
var runningRemote = false;
var apiBase = "http://c105r-fs1.studio.cambridge105.fm/";
var schedule = [];
// Array of scheduled event objects:
// - timestamp (result of Date.getTime())
// - label (text label to display)
// - cssClass (class to apply to text label)
// - countdownTime (duration to show countdown before event time, ms)
// - duration (how long to keep event visible)
var scheduledEvents = [];
loadScheduledMessages();
loadSchedule();
checkRunningStudio();
checkForOBDelay();
checkForIrn();
checkForAds();
if (window.location.href.indexOf("greenroom") > -1) { loadedFromGreenroom = true; }
if (runningRemote) {
apiBase = "https://clock.cambridge105.co.uk/";
}
$(function() {
createClock();
loadSlides();
if (runningRemote) {
updateLight('remote', true);
}
});
var fakeDate = null;
function getDate() {
if (fakeDate != null) {
return new Date(fakeDate);
} else {
return new Date();
}
}
// This checks the list of scheduled events for one with the same timestamp; if it
// finds one, it replaces that event with the supplied one; if not, it appends the
// new event to the array.
function insertOrUpdateScheduledEvent(e) {
let foundExisting = false;
for (let i = 0; i < scheduledEvents.length; i++) {
if (scheduledEvents[i].timestamp == e.timestamp) {
scheduledEvents[i] = e;
foundExisting = true;
break;
}
}
if (!foundExisting) {
scheduledEvents.push(e);
}
}
// Fetch any scheduled event we should be caring about at the moment:
// - Get the one with the closest start date in the future, where we are within the countdown period
// - Or the most recent start date in the past, where we are within the duration period
function getActiveScheduledEvent() {
let nextFutureEvent = null;
let mostRecentPastEvent = null;
let now = getDate().getTime();
for (let i = 0; i < scheduledEvents.length; i++) {
let e = scheduledEvents[i];
if (e.timestamp >= now) {
// Future event. Is it closer to now than our current future event? If not, we don't care.
if (nextFutureEvent == null || e.timestamp < nextFutureEvent.timestamp) {
// If it is closer, are we within its countdown period?
if ((e.timestamp - e.countdownTime) < now) {
nextFutureEvent = e;
}
}
} else {
// Past event. Is it more recent than our current past event? If not, we don't care.
if (mostRecentPastEvent == null || e.timestamp > mostRecentPastEvent.timestamp) {
// Are we still within the duration period?
if ((e.timestamp + e.duration) >= now) {
mostRecentPastEvent = e;
}
}
}
}
// If we have both a future and past event, prefer the future one - what's coming up is more important than what's happened
if (nextFutureEvent != null) {
return nextFutureEvent;
} else {
return mostRecentPastEvent;
}
}
function loadSlides() {
if (loadedFromGreenroom)
{
slideTxt = "<div id='img0' class='slideimg'><img src='slides/welcome.jpg'></div>";
slideTxt += "<div id='img1' class='slideimg'><img src='slides/acrossthecity.jpg'></div>";
slideTxt += "<div id='img2' class='slideimg'><img src='slides/liveandlocal.jpg'></div>";
slideTxt += "<div id='img3' class='slideimg'><img src='slides/localnews.jpg'></div>";
slideTxt += "<div id='img4' class='slideimg'><img src='slides/travelnews.jpg'></div>";
slideTxt += "<div id='img5' class='slideimg'><img src='slides/advertise-2026-01.jpg'></div>";
slideTxt += "<div id='img6' class='slideimg'><img src='slides/volunteer-2026-01.jpg'></div>";
maxSlideshowImgs = 6;
$('#slideshow').html(slideTxt);
}
}
function rotateSlideshow() {
$('#img' + lastSlideshowImg).css("display","none");
if (lastSlideshowImg == maxSlideshowImgs) {lastSlideshowImg = -1;}
lastSlideshowImg = lastSlideshowImg + 1;
$('#img' + lastSlideshowImg).css("display","inline");
}
function checkForSlideRotate() {
if (secondsSinceSlideChange == 12) {rotateSlideshow(); secondsSinceSlideChange = 0;}
secondsSinceSlideChange = secondsSinceSlideChange + 1;
}
function loadScheduledMessages() {
d = getDate();
messageFile = "messages/"+days[d.getDay()].toLowerCase() + ".js?nocache=" + d.getTime();
$.ajax({
url: messageFile,
dataType: 'json',
timeout: 5000
}).done(function (schedMsgs) {
for (let i = 0; i < schedMsgs.length; i++) {
let m = schedMsgs[i];
let mDate = getDate();
mDate.setHours(m.h);
mDate.setMinutes(m.m);
mDate.setSeconds(0);
insertOrUpdateScheduledEvent({
timestamp: mDate.getTime(),
label: m.msg,
countdownTime: m.c * 1000,
duration: m.d * 60 * 1000
});
}
});
}
function updateClock() {
let dateParts = getDateParts();
updateTextClock(dateParts);
refreshClock();
}
function updateTimer() {
let dateParts = getDateParts();
micLiveStatus = getMicLiveStatus();
getStudioStatus();
if (loadedFromGreenroom && dateParts[DATEPART_MIN] == 2 && dateParts[DATEPART_SEC] == 0)
{
// Reset the slides animation at xx:02:00
allowGreenroomSlideAnimation = true;
$('#slideshowOverlay').css("display", "none");
$('#specialNoticeContent').css("display","none");
}
if (loadedFromGreenroom) {checkForSlideRotate();}
//displayNetworkMessage();
if (!displayScheduledEvent()) {
allowGreenroomSlideAnimation = true;
displayProgrammeName();
}
// At 03:25:00, reload the whole page so we hopefully drop any DOM objects we've leaked
if (dateParts[DATEPART_HOUR] == 3 && dateParts[DATEPART_MIN] == 25 && dateParts[DATEPART_SEC] == 0) { location.reload(true); }
return true;
}
function getDateParts() {
d = getDate();
if (studioDelay > 0) {
d = new Date(d.getTime() + (studioDelay * 1000));
}
return [d.getHours(), d.getMinutes(), d.getSeconds(), days[d.getDay()], d.getDate(), months[d.getMonth()], d.getFullYear()];
}
function updateTextClock(dateParts) {
$('#time').html(padZeros(dateParts[DATEPART_HOUR]) + ":" + padZeros(dateParts[DATEPART_MIN]) + ":" + padZeros(dateParts[DATEPART_SEC]));
if (studioDelay > 0)
{
$('#time').html(padZeros(dateParts[DATEPART_HOUR]) + ":" + padZeros(dateParts[DATEPART_MIN]) + ":" + padZeros(dateParts[DATEPART_SEC]) + " (+" + studioDelay + "s)");
}
$('#date').html(dateParts[3] + ", " + dateParts[4] + " " + dateParts[5] + " " + dateParts[6]);
}
function getMicLiveStatus() {
if (runningInStudio == "b" || runningRemote == true) {$('#micLive').css("visibility","hidden"); return;}
$.ajax({
url: "http://studioa-pi:8081/miclive",
dataType: "json",
timeout: 2000
}).done(function (data) {
networkStudioAOK = true;
if (data['micLiveState'] == '1') { updateLight('micLive',true); } else { updateLight('micLive',false); }
}).fail(function() {
networkStudioAOK = false;
});
}
function getStudioStatus() {
if (runningRemote == true) { return;}
$.ajax({
url: "http://greenroom-pi:8081/studios",
dataType: "json",
timeout: 2000
}).done(function (data) {
networkGreenroomOK = true;
var newStudio;
if (data['a'] == '1') {updateLight('studioA',true); newStudio = 'Studio A'; } else {updateLight('studioA',false);}
if (data['b'] == '1') {updateLight('studioB',true); newStudio = 'Studio B'; } else {updateLight('studioB',false);}
if (data['remote'] == '1') {updateLight('remote',true); newStudio = 'Remote'; } else {updateLight('remote',false);}
if (currentStudio != null && newStudio !== currentStudio) {
$('#flash-container').css('display', 'block');
$('#flash-message').html('Station output changed to ' + newStudio);
currentStudio = newStudio;
setTimeout(function() {
$('#flash-container').css('display', 'none');
}, 5000);
}
}).fail(function() {
networkGreenroomOK = false;
});
}
function updateLight(divid,status) {
var div = $('#' + divid);
if (status) {
div.css("color","black");
div.css("background-color","red");
}
else {
div.css("color", "#808080");
div.css("background-color", "black");
}
}
function displayScheduledEvent() {
let e = getActiveScheduledEvent();
let now = getDate().getTime();
let labelHtml = null;
if (e != null) {
labelHtml = e.label;
if (e.timestamp >= now) {
// Event is in the future - we're in countdown mode
let secsToGo = (e.timestamp - now) / 1000;
let minsToGo = Math.floor(secsToGo / 60);
secsToGo = Math.floor(secsToGo - (minsToGo * 60));
labelHtml = labelHtml + " <span class=\"countdown\">" + padZeros(minsToGo) + ":" + padZeros(secsToGo);
}
if (loadedFromGreenroom) {
$('#onNextText').html(labelHtml);
} else {
$('#footer').html(labelHtml);
}
return true;
} else {
return false;
}
}
// TODO: reinstate this.
function displayGreenroomNews(type) {
if ($('#slideshow').html().indexOf('news.jpg') < 1)
{
dateParts = getDateParts();
$('#slideshowOverlay').html('<img src="slides/news.jpg">');
$('#slideshowOverlay').css("display", "block");
hours12 = dateParts[0];
if (hours12 > 12) {hours12 = hours12 - 12;} // 12-hour clock
if (hours12 < 1) {hours12 = 12;}
newsintro = ""From the " + type + " at " + hours12 + "..."";
$('#specialNoticeContent').html(newsintro);
$('#specialNoticeContent').css("display","inline");
allowGreenroomSlideAnimation = false;
}
}
function displayProgrammeName() {
let currentProgramme = getCurrentProgramme();
let nextProgramme = getNextProgramme();
if (loadedFromGreenroom == true)
{
if (currentProgramme != null) {
$('#onNowText').html(currentProgramme.title);
$('#onNextText').html(nextProgramme.title);
var nextProgTime = new Date(nextProgramme.start * 1000);
var nextProgTimeString = setLeadingZeros(nextProgTime.getHours()) + ":" + setLeadingZeros(nextProgTime.getMinutes());
$('#nextLabel').html(nextProgTimeString);
} else {
$('#onNowText').html("Failed to load schedule");
$('#onNextText').html("-");
}
}
else
{
let now = getDate().getTime();
$('#footer').css('color', 'white');
if (nextProgramme != null && nextProgramme.start * 1000 < now + 15 * 60 * 1000) {
// Next programme starts within 15 mins; count down to that instead
$('#footer').html('<strong>NEXT:</strong> ' + nextProgramme.title + ' (' + nextProgramme.type + ')');
} else if (currentProgramme != null) {
$('#footer').html(currentProgramme.title);
} else {
$('#footer').html("Failed to load schedule");
}
}
}
function setLeadingZeros(myInt) {
myInt = myInt + ""; // Force cast to string
if (myInt.length > 1) {return myInt;}
else {return "0" + myInt;}
}
function loadSchedule() {
timeNow = getDate().getTime();
$.ajax({
url: "schedule.js?nocache=" + (getDate()).getTime(),
dataType: "json",
timeout: 10000
}).done(function (sched) {
schedule = sched;
});
}
function getCurrentProgramme() {
if (!schedule) {
return null;
}
let now = getDate().getTime();
for (let i = 0; i < schedule.length; i++) {
let p = schedule[i];
if (p.start * 1000 <= now && p.end * 1000 > now) {
return p;
}
}
return null;
}
function getNextProgramme() {
if (!schedule) {
return null;
}
let now = getDate().getTime();
let nextProgramme = null;
for (let i = 0; i < schedule.length; i++) {
let p = schedule[i];
if (p.start * 1000 > now && (nextProgramme == null || p.start < nextProgramme.start)) {
nextProgramme = p;
}
}
return nextProgramme;
}
function padZeros(num) {
if (num < 10) { num = "0" + num; }
return num;
}
function getEngineeringMessage() {
if (!loadedFromGreenroom && !runningRemote)
{
var req = $.ajax({
url: "https://clock.cambridge105.co.uk/studioMessage.js?nocache=" + (getDate()).getTime(),
dataType: "json",
timeout: 5000
});
req.success(displayMessage);
req.fail(function () {
networkExternalOK = false;
});
}
}
function checkForIrn() {
// Don't check for IRN in the first 45 minutes of the hour: it won't be valid yet
if (getDate().getMinutes() < 45)
return;
var req = $.ajax({
type: 'GET',
crossDomain: true,
dataType: 'text',
url: apiBase + "trackdata/irnnext?nocache=" + (getDate()).getTime(),
headers: {
"Access-Control-Request-Method": "Get",
"Access-Control-Request-Headers": "Content-Type"
},
timeout: 3000
});
req.done(scheduleIrnCountdown);
req.fail(function () {
console.log("IRN False");
hasIrnNextHour = false;
});
}
function scheduleIrnCountdown() {
// Work out the time IRN is going to start - top of the *next* hour
let irnStartTime = getDate();
irnStartTime.setHours(irnStartTime.getHours() + 1);
irnStartTime.setMinutes(0);
irnStartTime.setSeconds(0);
insertOrUpdateScheduledEvent({
timestamp: irnStartTime.getTime(),
label: "SKY NEWS",
countdownTime: 60 * 1000,
duration: 2 * 60 * 1000,
});
}
function checkForAds() {
// Don't check for TOTH ads in the first 45 minutes of the hour: it won't be valid yet
if (getDate().getMinutes() < 45)
return;
var req = $.ajax({
type: 'GET',
crossDomain: true,
dataType: 'text',
url: apiBase + "trackdata/tothbreak?nocache=" + (getDate()).getTime(),
headers: {
"Access-Control-Request-Method": "Get",
"Access-Control-Request-Headers": "Content-Type"
},
timeout: 3000
});
req.done(scheduleAdsCountdown);
}
function scheduleAdsCountdown() {
// Work out the time the TOTH ads are going to start
let tothStartTime = getDate();
tothStartTime.setMinutes(58);
tothStartTime.setSeconds(30);
insertOrUpdateScheduledEvent({
timestamp: tothStartTime.getTime(),
label: "TOTH SEQUENCE",
countdownTime: 5 * 60 * 1000,
duration: 90 * 1000,
});
}
function displayMessage(response) {
networkExternalOK = true;
if (response.content && response.content.length > 3) {
displayMessageText('<span class=\"' + response.class + '\">' + response.content + '</span>');
} else {
displayMessageText('');
}
}
function displayMessageText(message) {
if (!loadedFromGreenroom)
{
$('#message').html(message);
if (message.length < 1) { $('#message').hide(); } else { $('#message').show(); }
}
}
function createClock() {
if (loadedFromGreenroom) {radius = 160;} else {radius = 200;}
clock = new CoolClock({
canvasId: 'clockid',
skinId: 'chunkySwissOnBlack',
displayRadius: radius
});
clock.stop(); // stop the internal timer so we can refresh manually
}
function refreshClock() {
if(clock) {
clock.refreshDisplay();
}
}
function displayNetworkMessage() {
if (networkExternalOK == true)
{
if (networkGreenroomOK == false && networkStudioAOK == false) {displayMessageText('ERROR: Green Room and Studio A Pis appear offline. All info may be inaccurate.');}
else if (networkGreenroomOK == false) {displayMessageText('ERROR: Green Room Pi offline. Studio switching status may be inaccurate.');}
else if (networkStudioAOK == false) {displayMessageText('ERROR: Studio A Pi offline. Mic live status may be inaccurate.');}
else { };
}
else
{
if (networkStudioAOK == false || networkGreenroomOK == false) {displayMessageText('ERROR: No network connection. All info may be inaccurate.');}
else (displayMessageText('ERROR: No WAN connection. Schedule may be inaccurate.'));
}
}
function getParameterByName(name, url) {
//From: https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function checkRunningStudio() {
let studio = getParameterByName("studio");
if (!studio || studio == "remote") {
// Studio not set, so assume remote
runningInStudio = "remote";
runningRemote = true;
return;
} else {
runningInStudio = studio.toLowerCase();
runningRemote = false;
}
}
function checkForOBDelay() {
let delay = getParameterByName("delay");
if (delay != null) {
studioDelay = delay;
}
}