forked from Cambridge105/studio-screen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
228 lines (200 loc) · 7.93 KB
/
script.js
File metadata and controls
228 lines (200 loc) · 7.93 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
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var clockTimer = setInterval(function () { updateClock() }, 250);
var mainTimer = setInterval(function () { updateTimer() }, 1000);
var thisProg = "";
var nextProg = "";
var thisProgIsLive = false;
var thisProgEnds = 0;
var nextProgType = "";
var scheduledMessages = [];
var clock = null;
loadScheduledMessages();
loadSchedule();
$(function() {
createClock();
});
function loadScheduledMessages() {
d = new Date();
scheduledMessages = [];
messageFile = "messages/"+days[d.getDay()].toLowerCase() + ".js?nocache=" + d.getTime();
$.getJSON(messageFile, function (schedMsgs) {
scheduledMessages = schedMsgs;
});
}
function unloadTimeout() {
clearInterval(mainTimer);
clearInterval(clockTimer);
}
function updateClock() {
var dateParts = [0, 0, 0, 'Monday', 1, 'January', 1970];
dateParts = getDateParts();
updateTextClock(dateParts);
refreshClock();
}
function updateTimer() {
var dateParts = [0, 0, 0, 'Monday', 1, 'January', 1970];
dateParts = getDateParts();
micLiveStatus = getMicLiveStatus();
checkForScheduledNotices(dateParts);
// Only load the schedule at xx:00:00, xx:30:00
if (((dateParts[1] == 0 || dateParts[1] == 30) && dateParts[2] == 0)) { loadSchedule(); }
// Only update the engineering notice at xx:00:15, xx:10:15, xx:20:15 etc.
if ((dateParts[1] == 0 || dateParts[1] == 10 || dateParts[1] == 20 || dateParts[1] == 30 || dateParts[1] == 40 || dateParts[1] == 50) && dateParts[2] == 15) { getEngineeringMessage(); }
return true;
}
function getDateParts() {
d = new Date();
return [d.getHours(), d.getMinutes(), d.getSeconds(), days[d.getDay()], d.getDate(), months[d.getMonth()], d.getFullYear()];
}
function updateTextClock(dateParts) {
$('#time').html(padZeros(dateParts[0]) + ":" + padZeros(dateParts[1]) + ":" + padZeros(dateParts[2]));
$('#date').html(dateParts[3] + ", " + dateParts[4] + " " + dateParts[5] + " " + dateParts[6]);
}
function getMicLiveStatus() {
$.getJSON("http://localhost:8081/miclive", function (data) {
if (data['micLiveState'] == '1') { updateMicLiveLight(true); } else { updateMicLiveLight(false); }
});
}
function updateMicLiveLight(micLiveStatus) {
if (micLiveStatus == true) {
$('#micLive').css("color","black");
$('#micLive').css("background-color","red");
}
else {
$('#micLive').css("color", "#808080");
$('#micLive').css("background-color", "black");
}
}
function updateStudioLiveLight() {
if (thisProgIsLive == true) {
$('#studioLive').css("color", "black");
$('#studioLive').css("background-color", "orange");
}
else {
$('#studioLive').css("color", "#808080");
$('#studioLive').css("background-color", "black");
}
}
function checkForScheduledNotices(dateParts) {
messageSet = false;
if (dateParts[1] >= 55) { displayTOTHNotice(dateParts[1], dateParts[2]); messageSet = true;}
else if (dateParts[2] == 1) {
// Update only once a minute so we don't degrade performance
// NB: This is a bit of a hack but it's done at xx:xx:01 to ensure we reset after schedule loads at xx:00:00 and xx:30:00
// Is it time for travel?
//console.log("Scheduled messages; " + scheduledMessages);
$.each(scheduledMessages, function (key, schedMsg) {
//console.log(schedMsg);
offtime = schedMsg["m"] + schedMsg["d"];
if (dateParts[0] == schedMsg["h"] && dateParts[1] >= schedMsg["m"] && dateParts[1] < offtime)
{displayNotice(schedMsg["msg"],schedMsg["c"]); messageSet = true;}
});
if (messageSet == false)
{
if (dateParts[1] >= 45 && endOfProgInNext15Mins()) { displayNextProgramme(); }
else { displayProgrammeName(); }
}
}
}
function displayTOTHNotice(mins,secs) {
$('#footer').css('color', 'yellow');
secsToTOTH = ((59 - mins) * 60) + (60 - secs);
secsToTOTH = secsToTOTH - 10; // News intro
if (secsToTOTH < 0) {
$('#footer').html('"This is community radio in your city, Cambridge 105"');
}
else {
minsToTOTH = Math.floor(secsToTOTH / 60);
secsToTOTH = secsToTOTH - (minsToTOTH * 60);
countToNews = padZeros(minsToTOTH) + ":" + padZeros(secsToTOTH);
$('#footer').html('NEWS INTRO in: <span class="countdown">' + countToNews + '</span>');
}
}
function displayNotice(message,color) {
if (color === undefined || color.length<1) {color = "yellow";}
$('#footer').css('color', color);
$('#footer').html(message);
}
function displayNextProgramme() {
$('#footer').css('color', 'white');
$('#footer').html('<strong>NEXT:</strong> ' + nextProg + " (" + nextProgType + ")");
if (nextProg.length < 1) { $('#footer').html('<strong>NEXT:</strong> Failed to load schedule'); }
}
function displayProgrammeName() {
$('#footer').css('color', 'white');
$('#footer').html(thisProg);
if (thisProg.length < 1) { $('#footer').html("Failed to load schedule"); }
}
function loadSchedule() {
thisProg = "";
nextProg = "";
thisProgEnds = 0;
thisProgIsLive = false;
nextProgType = "";
timeNow = new Date().getTime() + 5000; // Pretend we're 5 secs in the future to avoid race condition if we load exactly when a prog ends
$.getJSON("schedule.js?nocache=" + (new Date()).getTime(), function (sched) {
$.each(sched, function (key, progInfo) {
//$.each(progInfo, function (progInfoKey, progInfoValue) {
//console.log("Loading: " + progInfo['title']);
if ((progInfo['start'] * 1000) <= timeNow && (progInfo['end'] * 1000) > timeNow) {
thisProg = progInfo['title'];
console.log("Current programme " + thisProg);
if (progInfo['type'] == "LIVE") { thisProgIsLive = true; } else { thisProgIsLive = false; }
thisProgEnds = progInfo['end'] * 1000;
updateStudioLiveLight();
displayProgrammeName();
}
else if ((progInfo['start'] * 1000) == thisProgEnds) {
nextProg = progInfo['title'];
nextProgType = progInfo['type'];
}
//});
});
});
}
function endOfProgInNext15Mins() {
timeNow = new Date().getTime();
timeRemaining = thisProgEnds - timeNow;
if (timeRemaining <= (15 * 60 * 1000)) { return true; }
return false;
}
function padZeros(num) {
if (num < 10) { num = "0" + num; }
return num;
}
function getEngineeringMessage() {
var req = $.ajax({
url: "http://cambridge105.github.io/studio-screen/studioMessage.js",
dataType: "jsonp",
timeout: 10000,
jsonpCallback: "displayMessage"
});
req.success(function () {
//console.log('JSONP OK');
});
req.error(function () {
//console.log('JSONP failed');
});
}
function displayMessage(response) {
$('#message').html('<span class=\"engNotice\">Engineering notice:</span><br />' + response.message);
if (response.message.length < 1) { $('#message').hide(); } else { $('#message').show(); }
};
function createClock() {
clock = new CoolClock({
canvasId: 'clockid',
skinId: 'chunkySwissOnBlack',
displayRadius: 200
});
clock.stop(); // stop the internal timer so we can refresh manually
}
function refreshClock() {
if(clock) {
clock.refreshDisplay();
}
}
// -------- JSONP magic
//var tag = document.createElement("script");
//tag.src = 'http://www.domsmith.co.uk/c105/studioMessage.js?callback=displayMessage&nocache=' + (new Date()).getTime();
//document.getElementsByTagName("head")[0].appendChild(tag);