StudioClock.svelte 10.7 KB
Newer Older
1
<svelte:options tag="aura-clock"/>
2
<main bind:this={rootElement}></main>
David Trattnig's avatar
David Trattnig committed
3
4
<script>
	import { onMount } from 'svelte';
5
	export let css = "";
6
	export let api = "http://localhost:8008/api/v1/";
7
8
9
	export let name = "Studio Clock";
	export let logo = "https://gitlab.servus.at/aura/meta/-/raw/master/images/aura-logo.png";
	export let logosize  = "100px";
10
11
12
	export let noschedulemessage = "Nothing scheduled next!";
	export let unknowntitle = "Unknown Title";
	export let playoffset = 3;
13
14

	let time = new Date(); 
15
	let queryCurrent = "clock";
16
	let rootElement;
17
18
19
	let data;
	let currentTrack = null;
	let timeLeft;
David Trattnig's avatar
David Trattnig committed
20
21
22
23
24
25
26
27


	// these automatically update when `time`
	// changes, because of the `$:` prefix
	$: hours = time.getHours();
	$: minutes = time.getMinutes();
	$: seconds = time.getSeconds();

28
29
	data = fetchApi(queryCurrent);

30
	/* When component is mounted to the DOM */
David Trattnig's avatar
David Trattnig committed
31
32
33
	onMount(() => {
		const interval = setInterval(() => {
			time = new Date();
34
35
			timeLeft -= 1;

36
			if (timeLeft <= 0 || data == null) {
37
38
39
40
				currentTrack = null;
				data = null;
				data = fetchApi(queryCurrent);
			}
David Trattnig's avatar
David Trattnig committed
41
42
43
44
45
46
		}, 1000);

		return () => {
			clearInterval(interval);
		};
	});
47

48
	/* Load Clock data from the API */
49
	async function fetchApi(query) {
50
		let response;
51
52
		let data;

53
		try {
54
			response = await fetch(api+query);
55
		} catch {
56
			throw new Error(`Cannot connect to Engine API at "${api}"!`);
57
58
		}

59
60
61
62
63
64
		try {
			data = await response.json();
		} catch(e) {
			console.log("Error while converting response to JSON!", e);
			throw new Error(response.statusText);
		}
65
66
67
68

		if (response.ok) {
			return data;
		} else {
69
70
71
72
73
			console.log("Error:", data);
			throw new Error(data.message);
		}
	}

74
	/* Initialize the component */
75
76
77
78
79
80
81
	function initComponent(info) {

		/* Load external CSS */
		if (css != null)
			loadExternalCss(rootElement, css);

		/* Set currently loaded data */
David Trattnig's avatar
David Trattnig committed
82
		if (currentTrack == null && info != null && info.current_track != null) {
83
			currentTrack = info;
84
			
David Trattnig's avatar
David Trattnig committed
85
			let t = time - Date.parse(info.current_track.track_start);
86
87
			t = parseInt(t/1000);

88
			timeLeft = info.current_track.track_duration - t - playoffset;
89
			console.log("Current Data", info);
90
91
92
93
		}
		return "";
	}

94
	/* Display the title of a track */
95
96
	function displayTitle(track) {
		if (track != null) {
97
			let artist = "";
98
99
			let title = unknowntitle;
			if (track.track_artist != null && track.track_artist != "")
David Trattnig's avatar
David Trattnig committed
100
				artist = track.track_artist + " - ";
101
102
103
			if (track.track_title != null && track.track_title != "")
				title = track.track_title;
			return artist + title;
104
105
106
107
		}
		return "";
	}

108
	/* Format the time */
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
	function formatTime(seconds) {
		if (seconds != null && Number.isInteger(seconds)) {
			let d = new Date(null);
			d.setSeconds(seconds);

			let s;
			if (seconds > 3600)
				s = d.toISOString().substr(11, 8);
			else
				s = d.toISOString().substr(14, 5);
			return s;
		}
		return "";
	}

124
	/* Display the name of a show */
David Trattnig's avatar
David Trattnig committed
125
	function displayShowName(schedule) {
126
		let name = ""
David Trattnig's avatar
David Trattnig committed
127
		if (schedule == null || schedule.show_name == null) {
128
			name = '<span class="error">'+noschedulemessage+'</span>';
129
		} else {
David Trattnig's avatar
David Trattnig committed
130
			name = schedule.show_name;
131
		}
132
133
134
		return name;
	}

135
	/* Display the schedule of a show */
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
	function displayShowSchedule(schedule) {
		let str = "";

		if (schedule != null && schedule.schedule_start != null) {
			let scheduleStart = ""
			let scheduleEnd = "";

			if (schedule.schedule_start != null) {
				let scheduleStart = new Date(Date.parse(schedule.schedule_start));
				scheduleStart = scheduleStart.toLocaleTimeString(navigator.language, {
					hour: '2-digit',
					minute:'2-digit'
				});
				str = "(" + scheduleStart;
			}
			if (schedule.schedule_end != null) {
				scheduleEnd = new Date(Date.parse(schedule.schedule_end));
				scheduleEnd = scheduleEnd.toLocaleTimeString(navigator.language, {
					hour: '2-digit',
					minute:'2-digit'
				});
				str = str + " - " + scheduleEnd + ")";
			} else {
				str += ")";
			}

		}
		return str;
164
165
	}

166
	/* Check if the given track is currently playing */
167
	function isActive(entry, currentTrack) {
David Trattnig's avatar
David Trattnig committed
168
		if (currentTrack != null && entry.track_num == currentTrack.track_num) {
169
			// Scroll to current playlist entry
170
171
172
173
174
175
			// location.hash = "#current-playlist-entry";

			var element = document.querySelector("#current-playlist-entry");
			if (element != null)
				element.scrollIntoView();

176
177
178
179
180
			return true;
		}
		return false;
	}

181
	/* Hack to load external CSS into the Web Component */
182
183
184
185
186
187
188
	function loadExternalCss(root, file) {
		let element = document.createElement("link");
		element.setAttribute("rel", "stylesheet");
		element.setAttribute("type", "text/css");
		element.setAttribute("href", file);
		root.appendChild(element);
	}
189
	
190
191
192
193
194
195
196
197
198
	/* Called when the clock finished rendering */
	function finalize_rendering() {

		/* Sroll to currently playing track */
		var element = document.querySelector("#current-playlist-entry");
		if (element != null)
			element.scrollIntoView();
		return ""
	}
David Trattnig's avatar
David Trattnig committed
199
200
201
</script>

<style>
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222

	#station-header {
		width: 100%;
		height: 50px;
		padding: 40px 100px;
	}

	#station-name {
		margin: 0;
    	font-size: 3em;
		line-height: 80px;
	}

	#station-logo {
		align-content: left;
		text-align: right;
		margin: 0 40px 0 10px;
		opacity: 0.5;
		filter: invert(100%);
	}

223
224
	#studio-clock {
		width: calc(100% - 200px);
225
226
		height: calc(100% - 500px);
		margin: 100px;
227
228
229
230
231
232
233
234
235
236
237
238
239
		display: -webkit-flex;
		display: -ms-flexbox;
		display: flex;
		flex-direction: row;
	}

	#left-column {
		width: 30%;
		padding: 25px;
	}

	#right-column {
		width: 70%;
240
		padding: 25px 25px 25px 50px;
241
242
	}

243
244
245
246
247
	#current-schedule,
	#next-schedule {
		margin: 0 0 40px 20px;
	}

248
249
250
251
252
253
254
255
	#next-schedule {
		background-color:rgb(24, 24, 24);
		margin-right: 20px;
		padding: 12px;
	}

	#current-schedule .schedule-title {
		color: #ccc;
256
257
		font-size: 3.3em;
		text-align: center;
258
259
260
261
262
263
	}
	#next-schedule .schedule-title {
		color: gray !important;
		font-size: 2em;
	}

264
	#playlist {
265
266
267
		border: 2px solid #333;
		margin: 20px 20px 40px 20px;
		padding: 10px;
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
		height: calc(80% - 100px);
		overflow-y: auto;
		scroll-behavior: smooth; 
		background-color: #111;
		display: flex;
   		align-items: center;
	}

	#playlist::-webkit-scrollbar-track
	{
		border-radius: 10px;
		-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
		background-color: rgb(77, 73, 73);
	}

	#playlist::-webkit-scrollbar
	{
		width: 12px;
		background-color: rgb(0, 0, 0);
	}

	#playlist::-webkit-scrollbar-thumb
	{
		border-radius: 10px;
		-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);
		background-color: rgb(34, 32, 32);
294
295
	}

296
297
298
299
	#playlist ol {
		margin-left: 33px;
	}

300
	.playlist-entry {
David Trattnig's avatar
David Trattnig committed
301
		font-size: 1.9em;
302
		padding-left: 53px;
303
		padding-bottom: 13px;
304
305
	}

306
	#current-track * {
David Trattnig's avatar
David Trattnig committed
307
		font-size: 1.5em;
308
309
	}

310
311
	.play-icon,
	.track-time-left {
312
		margin: 25px 50px;
313
314
	}

315
	.is-active {
316
317
		color: rgb(43, 241, 36);
		font-weight: bold;
318
319
320
		padding-left: 0;
	}

321
322
	.is-active .track-title::before {
		content: "\00a0\00a0▶\00a0\00a0\00a0";
323
		font-size: larger; 
324
		color: rgb(43, 241, 36);
325
326
	}

327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
	.is-active .track-time-left {
		color: rgb(43, 241, 36);
		background-color: #222;
		padding: 5px 15px;
	}

	.error {
		font-size: 1.3em;
		color:red;
		height:100%;
		display : flex;
		align-items : center;
		justify-content: center;
	}

David Trattnig's avatar
David Trattnig committed
342
343
344
345
346
347
	svg {
		width: 100%;
		height: 100%;
	}

	.clock-face {
348
349
		stroke: rgb(66, 66, 66);
		fill: black;
David Trattnig's avatar
David Trattnig committed
350
351
352
	}

	.minor {
353
		stroke: rgb(132, 132, 132);
David Trattnig's avatar
David Trattnig committed
354
355
356
357
		stroke-width: 0.5;
	}

	.major {
358
		stroke: rgb(162, 162, 162);
David Trattnig's avatar
David Trattnig committed
359
360
361
362
		stroke-width: 1;
	}

	.hour {
363
		stroke: rgba(255, 255, 255, 0.705);
David Trattnig's avatar
David Trattnig committed
364
365
366
	}

	.minute {
367
		stroke: rgba(255, 255, 255, 0.705);
David Trattnig's avatar
David Trattnig committed
368
369
370
371
372
373
	}

	.second, .second-counterweight {
		stroke: rgb(180,0,0);
	}

David Trattnig's avatar
David Trattnig committed
374
375
376
377
378
	footer {
		width: 100%;
		text-align: center;
		font-size: 0.8em;
		color: gray;
379
		opacity: 0.5;
David Trattnig's avatar
David Trattnig committed
380
	}
381
382
383
384
385
386
387
388
389
390
391
392

	footer a {
		color: gray;
		text-decoration: underline;
	}

	footer #aura-logo {
		filter: invert(100%);
		width: 75px;
		margin: 0 0 20px 0;
	}

David Trattnig's avatar
David Trattnig committed
393
394
</style>

395
396
397


<div id="station-header">
398
399
	<img id="station-logo" src="{logo}" style="width:{logosize}" alt="Radio Station" align="left" />
	<h1 id="station-name">{name}</h1>
400
401
</div>

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
<div id="studio-clock">
	<div id="left-column" class="column">
		<svg viewBox='-50 -50 100 100'>
			<circle class='clock-face' r='48'/>

			<!-- markers -->
			{#each [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55] as minute}
				<line
					class='major'
					y1='35'
					y2='45'
					transform='rotate({30 * minute})'
				/>

				{#each [1, 2, 3, 4] as offset}
					<line
						class='minor'
						y1='42'
						y2='45'
						transform='rotate({6 * (minute + offset)})'
					/>
				{/each}
			{/each}

			<!-- hour hand -->
			<line
				class='hour'
				y1='2'
				y2='-20'
				transform='rotate({30 * hours + minutes / 2})'
			/>
David Trattnig's avatar
David Trattnig committed
433

434
			<!-- minute hand -->
David Trattnig's avatar
David Trattnig committed
435
			<line
436
437
438
439
				class='minute'
				y1='4'
				y2='-30'
				transform='rotate({6 * minutes + seconds / 10})'
David Trattnig's avatar
David Trattnig committed
440
			/>
441
442
443
444
445
446
447
448
449
450
451

			<!-- second hand -->
			<g transform='rotate({6 * seconds})'>
				<line class='second' y1='10' y2='-38'/>
				<line class='second-counterweight' y1='10' y2='2'/>
			</g>
		</svg>
	</div>

	<div id="right-column" class="column">

452
		{#await data}
453
454
455
456
			<div class="spinner-border mt-5" role="status">
				<span class="sr-only">Loading...</span>
			</div>
		{:then value}
457
			{initComponent(value)}
458
			
David Trattnig's avatar
David Trattnig committed
459
			{#if value.current_schedule}
460
461

			<div id="current-schedule">
David Trattnig's avatar
David Trattnig committed
462
				<h1 class="schedule-title">{@html displayShowName(value.current_schedule)} {displayShowSchedule(value.current_schedule)}</h1>
463
				<!-- <div class="schedule-details">
David Trattnig's avatar
David Trattnig committed
464
					<b>Type:</b> {value.current_schedule}, <b>Host:</b> {value.current_schedule}</div>-->
465
			</div> 
466
467
			<div id="playlist">

David Trattnig's avatar
David Trattnig committed
468
				{#if value.current_playlist}
469
					<ol>
David Trattnig's avatar
David Trattnig committed
470
471
					{#each value.current_playlist.entries as entry, index}
						{#if isActive(entry, value.current_track)}
472
						
473
						<li id="current-playlist-entry" class="playlist-entry is-active">
474
475
476
477
478
479
480
481
482
							<!-- <span class="play-icon">&#9654;</span> -->
							<span class="track-title">{displayTitle(entry)}</span>
							<span class="track-time-left">({formatTime(timeLeft)})</span>
						</li>

						{:else}

						<li class="playlist-entry">
							<span class="track-title">{displayTitle(entry)}</span>
483
							<span class="track-duration">({formatTime(entry.track_duration)})</span>
484
485
486
487
488
489
490
491
						</li>	

						{/if}

					{/each}
					</ol>
				{:else}

492
					<div id="current-track" class="is-active">
493
						<h2>
494
495
							<span class="track-title">{displayTitle(value.track)}</span>
							<span class="track-time-left">{formatTime(timeLeft)}</span>
496
497
498
						</h2>
					</div>
				{/if}
499
500
			</div>
			<div id="next-schedule">
501
				<h3 class="schedule-title">{@html displayShowName(value.next_schedule)} {displayShowSchedule(value)}</h3>
502
			</div>
503
504

			{/if}
505
		{:catch error}
506
			<div class="error"><p>{error}</p></div>
507
		{/await}
508

509
510
511
512
513

		<footer>
			<!-- {finalize_rendering()} -->
			<a href="https://gitlab.servus.at/aura/engine-clock">Engine Clock</a> is fuelled by <a href="https://gitlab.servus.at/aura/engine">AURA Engine</a>
		</footer>
514
515
	</div>

David Trattnig's avatar
David Trattnig committed
516
517
</div>