StudioClock.svelte 6.9 KB
Newer Older
David Trattnig's avatar
David Trattnig committed
1
2
<script>
	import { onMount } from 'svelte';
3
4
5
6
	export let apiUrl = "http://localhost:3333/api/v1";


	let time = new Date(); 
7
	let queryCurrent = "/clock";
8
9
10
11

	let data;
	let currentTrack = null;
	let timeLeft;
David Trattnig's avatar
David Trattnig committed
12
13
14
15
16
17
18
19


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

20
21
22
	data = fetchApi(queryCurrent);


David Trattnig's avatar
David Trattnig committed
23
24
25
	onMount(() => {
		const interval = setInterval(() => {
			time = new Date();
26
27
			timeLeft -= 1;

28
			if (timeLeft <= 0 || data == null) {
29
30
31
32
				currentTrack = null;
				data = null;
				data = fetchApi(queryCurrent);
			}
33

David Trattnig's avatar
David Trattnig committed
34
35
36
37
38
39
		}, 1000);

		return () => {
			clearInterval(interval);
		};
	});
40
41


42
43
44
45
46
47
48
49
50
51
	async function fetchApi(query) {
		let response = await fetch(apiUrl+query);
		let data;

		try {
			data = await response.json();
		} catch(e) {
			console.log("Error while converting response to JSON!", e);
			throw new Error(response.statusText);
		}
52
53
54
55

		if (response.ok) {
			return data;
		} else {
56
57
58
59
60
			console.log("Error:", data);
			throw new Error(data.message);
		}
	}

61
62
63
	function setCurrentTrack(info) {
		if (currentTrack == null && info != null && info.track != null) {
			currentTrack = info;
64
			
65
			let t = time - Date.parse(info.track_start);
66
67
			t = parseInt(t/1000);

68
			timeLeft = info.track.duration - t - 4;
69
			console.log("Current Data", info);
70
71
72
73
		}
		return "";
	}

74
75
	function displayTitle(track) {
		if (track != null) {
76
			let artist = "";
77
78
79
			if (track.artist != "")
				artist = track.artist + " - ";
			return artist + track.title;
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
		}
		return "";
	}

	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 "";
	}

99
100
101
102
103
104
	function displayShowName(show) {
		let name = ""
		if (show == null || show.name == null) {
			name = '<span class="error">No show scheduled!</span>';
		} else {
			name = show.name;
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
		return name;
	}

	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;
137
138
	}

139
140
141
142
143
144
145
	function isActive(entry, currentTrack) {
		if (currentTrack != null && entry.id == currentTrack.id) {
			return true;
		}
		return false;
	}

146

147
	
David Trattnig's avatar
David Trattnig committed
148
149
150
</script>

<style>
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
	#studio-clock {
		width: calc(100% - 200px);
		margin:100px;
		display: -webkit-flex;
		display: -ms-flexbox;
		display: flex;
		border: 2px solid #333;
		flex-direction: row;
	}

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

	#right-column {
		width: 70%;
		padding: 25px;
	}

171
172
173
174
175
	#current-schedule,
	#next-schedule {
		margin: 0 0 40px 20px;
	}

176
	#playlist {
177
178
179
180
181
		border: 2px solid #333;
		margin: 20px 20px 40px 20px;
		padding: 10px;
	}

182
183
184
185
186
	.playlist-entry {
		font-size: 1.4em;
		padding-left: 53px;
	}

187
188
189
190
191
	.play-icon,
	.track-time-left {
		margin: 12px;
	}

192
193
194
195
196
197
198
199
	.is-active {
		color: green;
		padding-left: 0;
	}

	.is-active::before {
		content: "\00a0\00a0▶\00a0\00a0";
		font-size: larger; 
200
201
202
		color: green;
	}

David Trattnig's avatar
David Trattnig committed
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
	svg {
		width: 100%;
		height: 100%;
	}

	.clock-face {
		stroke: #333;
		fill: white;
	}

	.minor {
		stroke: #999;
		stroke-width: 0.5;
	}

	.major {
		stroke: #333;
		stroke-width: 1;
	}

	.hour {
		stroke: #333;
	}

	.minute {
		stroke: #666;
	}

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

	.second-counterweight {
		stroke-width: 3;
	}
David Trattnig's avatar
David Trattnig committed
238
239
240
241
242
243
244

	footer {
		width: 100%;
		text-align: center;
		font-size: 0.8em;
		color: gray;
	}
David Trattnig's avatar
David Trattnig committed
245
246
</style>

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
<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
278

279
			<!-- minute hand -->
David Trattnig's avatar
David Trattnig committed
280
			<line
281
282
283
284
				class='minute'
				y1='4'
				y2='-30'
				transform='rotate({6 * minutes + seconds / 10})'
David Trattnig's avatar
David Trattnig committed
285
			/>
286
287
288
289
290
291
292
293
294
295
296

			<!-- 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">

297
		{#await data}
298
299
300
301
			<div class="spinner-border mt-5" role="status">
				<span class="sr-only">Loading...</span>
			</div>
		{:then value}
302
303
			{setCurrentTrack(value)}
			<!-- <h2>&#9654; Playing</h2> -->
304
305
306
307
308
309
310
311
			
			{#if value.current.show}

			<div id="current-schedule">
				<h1 class="schedule-title">{@html displayShowName(value.current.show)} {displayShowSchedule(value.current)}</h1>
				<div class="schedule-details">
					<b>Type:</b> {value.current.type}, <b>Host:</b> {value.current.host}</div>
				</div>
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
			<div id="playlist">

				{#if value.current.playlist}
					<ol>
					{#each value.current.playlist.entries as entry, index}
						{#if isActive(entry, value.track)}
						
						<li class="playlist-entry is-active">
							<!-- <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>
							<span class="track-duration">({formatTime(entry.duration)})</span>
						</li>	

						{/if}

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

					<div id="current-track">
						<h2>
							<span class="track-title is-active">{displayTitle(value.track)}</span>
							<span class="track-time-left">({formatTime(timeLeft)})</span>
						</h2>
					</div>
				{/if}
345
346
			</div>
			<div id="next-schedule">
347
348
349
350
				<h2 class="schedule-title">Next: {@html displayShowName(value.next.show)} {displayShowSchedule(value)}</h2>
				<div class="schedule-details">
					<b>Type:</b> {value.next.type}, <b>Host:</b> {value.next.host}
				</div>
351
			</div>
352
353

			{/if}
354
		{:catch error}
355
			<p style="color:red">{error}</p>
356
		{/await}
357

358
359
	</div>

David Trattnig's avatar
David Trattnig committed
360
361
362
</div>

<footer>Studio Clock is powered by <a href="https://gitlab.servus.at/autoradio">Aura Engine</a></footer>