owlboard-svelte/src/lib/ldb/public-ldb.svelte

446 lines
13 KiB
Svelte
Raw Normal View History

2023-06-16 22:55:18 +01:00
<script>
2023-06-17 12:04:59 +01:00
export let station = "";
2023-06-19 14:21:17 +01:00
export let title = "Loading...";
2023-06-17 12:04:59 +01:00
import { onMount } from 'svelte'
2023-06-17 21:48:00 +01:00
import Loading from '$lib/navigation/loading.svelte';
2023-06-26 21:56:19 +01:00
import OverlayIsland from '$lib/islands/overlay-island.svelte';
2023-06-28 21:23:29 +01:00
import AlertBar from './alert-bar.svelte';
2023-06-17 12:04:59 +01:00
let requestedStation;
$: requestedStation = station;
let jsonData = null;
let services = [];
2023-06-19 14:21:17 +01:00
let busServices = [];
let ferryServices = [];
2023-06-17 15:12:30 +01:00
let dataAge = null;
let isLoading = true;
2023-06-19 19:42:41 +01:00
let dataExists = false;
2023-06-28 21:23:29 +01:00
let alerts = [];
2023-06-26 21:56:19 +01:00
let serviceDetail;
2023-06-17 12:04:59 +01:00
$: {
2023-06-19 14:21:17 +01:00
if (jsonData === null && requestedStation) {
fetchData();
}
if (jsonData?.GetStationBoardResult?.generatedAt) {
dataAge = new Date(jsonData.GetStationBoardResult.generatedAt);
}
if (jsonData?.GetStationBoardResult?.trainServices?.service) {
services = jsonData.GetStationBoardResult.trainServices.service;
} else {
services = [];
}
2023-06-17 12:04:59 +01:00
2023-06-19 14:21:17 +01:00
if (jsonData?.GetStationBoardResult?.busServices?.service) {
busServices = jsonData.GetStationBoardResult.busServices.service;
}
if (jsonData?.GetStationBoardResult?.ferryServices?.service) {
ferryServices = jsonData.GetStationBoardResult.ferryServices.service;
}
2023-06-17 12:04:59 +01:00
2023-06-19 14:21:17 +01:00
if (jsonData?.GetStationBoardResult?.locationName) {
title = jsonData.GetStationBoardResult.locationName
} else {
2023-06-19 19:42:41 +01:00
title = requestedStation.toUpperCase()
2023-06-19 14:21:17 +01:00
}
2023-06-17 01:53:08 +01:00
}
2023-06-16 22:55:18 +01:00
2023-06-17 12:04:59 +01:00
async function fetchData() {
2023-06-19 19:42:41 +01:00
dataExists = true;
2023-06-17 15:12:30 +01:00
isLoading = true; // Set loading state
try {
console.log(`Requested Station: ${requestedStation}`);
const data = await fetch(`https://owlboard.info/api/v1/ldb/${requestedStation}`);
jsonData = await data.json();
} catch (error) {
console.error("Error fetching data:", error);
2023-06-19 19:42:41 +01:00
dataExists = false;
title = "Not Found";
2023-06-17 15:12:30 +01:00
} finally {
isLoading = false; // Clear loading state
}
2023-06-28 21:23:29 +01:00
prepareNrcc()
2023-06-17 15:12:30 +01:00
}
function parseTime(string){
let output
let change
switch (string) {
case 'Delayed':
output = 'LATE'
change = "changed"
break
case 'Cancelled':
output = 'CANC'
change = "cancelled"
break
2023-06-26 21:56:19 +01:00
case 'On Time':
2023-06-17 15:12:30 +01:00
case 'On time':
output = 'RT'
change = ""
break
case '':
output = '-'
change = ""
break
case undefined:
output = '-'
change = ""
break
case 'No report':
output = '-'
change = ""
break
case 'undefined':
output = false
change = ""
break
default:
output = string
change = "changed"
}
return {data: output, changed: change}
2023-06-17 12:04:59 +01:00
}
2023-06-16 22:55:18 +01:00
2023-06-26 21:56:19 +01:00
async function loadService(sid) {
for (const service of services) {
if (service.serviceID == sid) {
serviceDetail = service
}
}
}
async function loadBusService(sid) {
for (const service of busServices) {
if (service.serviceID == sid) {
serviceDetail = service
}
}
}
2023-06-26 21:56:19 +01:00
async function closeService() {
serviceDetail = null;
}
2023-06-28 21:23:29 +01:00
async function prepareNrcc() {
if (jsonData?.GetStationBoardResult?.nrccMessages?.message) {
const nrcc = jsonData.GetStationBoardResult.nrccMessages.message;
if (Array.isArray(nrcc)) {
alerts = nrcc
return;
}
alerts.push(nrcc);
return;
}
}
2023-06-17 12:04:59 +01:00
onMount(() => {
if (requestedStation && jsonData === null) {
fetchData();
}
});
</script>
2023-06-28 21:23:29 +01:00
{#if alerts.length}
<AlertBar {alerts}/>
{/if}
2023-06-17 15:12:30 +01:00
{#if isLoading}
2023-06-17 21:48:00 +01:00
<Loading />
2023-06-17 15:12:30 +01:00
{:else}
2023-06-19 19:42:41 +01:00
{#if dataAge}
<p id="timestamp">Updated: {dataAge.toLocaleTimeString()}</p>
{#if services.length}
<table>
2023-06-19 14:21:17 +01:00
<tr>
2023-06-19 19:42:41 +01:00
<th class="from">From</th>
<th class="to">To</th>
<th class="plat">Plat.</th>
<th class="time">Sch Arr.</th>
<th class="time">Exp Arr.</th>
<th class="time">Sch Dep.</th>
<th class="time">Exp Dep.</th>
2023-06-19 14:21:17 +01:00
</tr>
2023-06-19 19:42:41 +01:00
{#each services as service}
<tr>
<td class="origdest from" on:click={loadService(service.serviceID)} on:keypress={loadService(service.serviceID)}>
{#if Array.isArray(service.origin?.location)}
{service.origin.location[0]['locationName'] +
" & " +
service.origin.location[1]['locationName']}
{:else}
{service.origin?.location?.locationName || ''}
{/if}
</td>
<td class="origdest to" on:click={loadService(service.serviceID)} on:keypress={loadService(service.serviceID)}>
{#if Array.isArray(service.destination?.location)}
{service.destination.location[0]['locationName'] +
" & " +
service.destination.location[0]['locationName']}
{:else}
{service.destination?.location?.locationName || ''}
{/if}
</td>
2023-06-19 19:42:41 +01:00
<td class="plat">{service.platform || '-'}</td>
<td class="time">{parseTime(service.sta).data}</td>
<td class="time {parseTime(service.eta).changed}">{parseTime(service.eta).data}</td>
<td class="time">{parseTime(service.std).data}</td>
<td class="time {parseTime(service.etd).changed}">{parseTime(service.etd).data}</td>
</tr>
2023-06-19 14:21:17 +01:00
2023-06-19 19:42:41 +01:00
<tr><td colspan="7">
<p class="service-detail">
A {service.operator || 'Unknown'} service
{#if service['length']}
with {service['length'] || 'some'} coaches
{/if}
</p>
{#if service.delayReason}
<p class="service-detail">{service.delayReason}</p>
2023-06-19 14:21:17 +01:00
{/if}
2023-06-19 19:42:41 +01:00
{#if service.cancelReason}
<p class="service-detail">{service.cancelReason}</p>
{/if}
</td></tr>
{/each}
</table>
{:else}
<p class="table-head-text">No Scheduled Train Services</p>
{/if}
{#if busServices.length}
<br>
<img class="transport-mode" src="/images/transport-modes/bus.svg" alt="Bus services"><br>
<span class="table-head-text">Bus Services</span>
<table>
2023-06-19 14:21:17 +01:00
<tr>
2023-06-19 19:42:41 +01:00
<th class="from">From</th>
<th class="to">To</th>
<th class="time">Sch Arr.</th>
<th class="time">Exp Arr.</th>
<th class="time">Sch Dep.</th>
<th class="time">Exp Dep.</th>
2023-06-19 14:21:17 +01:00
</tr>
2023-06-19 19:42:41 +01:00
{#each busServices as service}
<tr>
<td class="origdest from" on:click={loadBusService(service.serviceID)} on:keypress={loadBusService(service.serviceID)}>{service.origin?.location?.locationName || ''}</td>
<td class="origdest to" on:click={loadBusService(service.serviceID)} on:keypress={loadBusService(service.serviceID)}>{service.destination?.location?.locationName || ''}</td>
2023-06-19 19:42:41 +01:00
<td class="time">{parseTime(service.sta).data}</td>
<td class="time {parseTime(service.eta).changed}">{parseTime(service.eta).data}</td>
<td class="time">{parseTime(service.std).data}</td>
<td class="time {parseTime(service.etd).changed}">{parseTime(service.etd).data}</td>
</tr>
2023-06-16 22:55:18 +01:00
2023-06-19 19:42:41 +01:00
<tr><td colspan="7">
<p class="service-detail">
A {service.operator || 'Unknown'} service
</p>
{#if service.delayReason}
<p class="service-detail">{service.delayReason}</p>
{/if}
{#if service.cancelReason}
<p class="service-detail">{service.cancelReason}</p>
{/if}
</td></tr>
{/each}
</table>
{/if}
{#if ferryServices.length}
<br>
<img class="transport-mode" src="/images/transport-modes/ferry.svg" alt="Bus services"><br>
<span class="table-head-text">Ferry Services</span>
<table>
2023-06-19 14:21:17 +01:00
<tr>
2023-06-19 19:42:41 +01:00
<th class="from">From</th>
<th class="to">To</th>
<th class="time">Sch Arr.</th>
<th class="time">Exp Arr.</th>
<th class="time">Sch Dep.</th>
<th class="time">Exp Dep.</th>
2023-06-19 14:21:17 +01:00
</tr>
2023-06-19 19:42:41 +01:00
{#each ferryServices as service}
<tr>
<td class="origdest from">{service.origin?.location?.locationName || ''}</td>
<td class="origdest to">{service.destination?.location?.locationName || ''}</td>
<td class="time">{parseTime(service.sta).data}</td>
<td class="time {parseTime(service.eta).changed}">{parseTime(service.eta).data}</td>
<td class="time">{parseTime(service.std).data}</td>
<td class="time {parseTime(service.etd).changed}">{parseTime(service.etd).data}</td>
</tr>
2023-06-19 14:21:17 +01:00
2023-06-19 19:42:41 +01:00
<tr><td colspan="7">
{#if service.delayReason}
<p class="service-detail">{service.delayReason}</p>
{/if}
{#if service.cancelReason}
<p class="service-detail">{service.cancelReason}</p>
{/if}
</td></tr>
{/each}
</table>
{/if}
{:else}
<p>Unable to find this station</p>
2023-06-19 14:21:17 +01:00
{/if}
2023-06-17 15:12:30 +01:00
{/if}
2023-06-26 21:56:19 +01:00
{#if serviceDetail}
<OverlayIsland>
<h6>Service Detail</h6>
<button type="button" id="closeService" on:click={closeService}>X</button>
<table id="detailTable">
<tr>
<th>Location</th>
<th>Sch</th>
<th>Exp</th>
</tr>
{#if serviceDetail?.previousCallingPoints?.callingPointList?.callingPoint}
{#if Array.isArray(serviceDetail?.previousCallingPoints?.callingPointList?.callingPoint)}
{#each serviceDetail.previousCallingPoints.callingPointList.callingPoint as prevPoint}
<tr>
<td>{prevPoint.locationName}</td>
<td>{prevPoint.st}</td>
<td class="time {parseTime(prevPoint.at || prevPoint.et).changed}">{parseTime(prevPoint.at || prevPoint.et).data}</td>
</tr>
{/each}
{:else}
2023-06-26 21:56:19 +01:00
<tr>
<td>{serviceDetail.previousCallingPoints.callingPointList.callingPoint.locationName}</td>
<td>{serviceDetail.previousCallingPoints.callingPointList.callingPoint.st}</td>
<td class="time {parseTime(serviceDetail.previousCallingPoints.callingPointList.callingPoint.at || serviceDetail.previousCallingPoints.callingPointList.callingPoint.et).changed}">{parseTime(serviceDetail.previousCallingPoints.callingPointList.callingPoint.at || serviceDetail.previousCallingPoints.callingPointList.callingPoint.et).data}</td>
2023-06-26 21:56:19 +01:00
</tr>
{/if}
2023-06-26 21:56:19 +01:00
{/if}
<tr class="thisStop">
<td>{title}</td>
<td>{serviceDetail.std || serviceDetail.sta}</td>
<td class="time {parseTime(serviceDetail.etd || serviceDetail.eta).changed}">{parseTime(serviceDetail.etd || serviceDetail.eta).data}</td>
</tr>
{#if serviceDetail?.subsequentCallingPoints?.callingPointList?.callingPoint}
{#if Array.isArray(serviceDetail?.subsequentCallingPoints?.callingPointList?.callingPoint)}
{#each serviceDetail.subsequentCallingPoints.callingPointList.callingPoint as nextPoint}
<tr>
<td>{nextPoint.locationName}</td>
<td>{nextPoint.st}</td>
<td class="time {parseTime(nextPoint.et).changed}">{parseTime(nextPoint.et).data}</td>
</tr>
{/each}
{:else}
2023-06-26 21:56:19 +01:00
<tr>
<td>{serviceDetail.subsequentCallingPoints.callingPointList.callingPoint.locationName}</td>
<td>{serviceDetail.subsequentCallingPoints.callingPointList.callingPoint.st}</td>
<td class="time {parseTime(serviceDetail.subsequentCallingPoints.callingPointList.callingPoint.et).changed}">{parseTime(serviceDetail.subsequentCallingPoints.callingPointList.callingPoint.et).data}</td>
2023-06-26 21:56:19 +01:00
</tr>
{/if}
2023-06-26 21:56:19 +01:00
{/if}
</table>
</OverlayIsland>
{/if}
2023-06-16 22:55:18 +01:00
<style>
2023-06-17 15:12:30 +01:00
#timestamp {
margin: auto;
text-align: left;
2023-06-19 18:26:18 +01:00
font-size: 14px;
2023-06-17 15:12:30 +01:00
}
2023-06-17 12:04:59 +01:00
table {
width: 100%;
margin: auto;
color: white;
2023-06-17 15:12:30 +01:00
font-size: 13px;
}
.service-detail {
color: cyan;
text-align: left;
font-size: 12px;
padding: 0;
margin: 0;
2023-06-17 12:04:59 +01:00
}
2023-06-19 18:26:18 +01:00
.transport-mode {
width: 30px;
}
.table-head-text {
color: white;
font-size: 14px;
}
2023-06-17 15:12:30 +01:00
@media (min-width: 800px) {
table {font-size: 15px;}
.service-detail {font-size: 14px;}
2023-06-19 18:26:18 +01:00
.transport-mode {width: 50px;}
#timestamp {font-size: 16px;}
2023-06-17 15:12:30 +01:00
}
@media (min-width: 1000px) {
table {font-size: 17px;}
.service-detail{font-size: 16px;}
2023-06-19 18:26:18 +01:00
.table-head-text {font-size: 16px;}
2023-06-17 15:12:30 +01:00
}
@media (min-width: 1600px) {
table {font-size: 19px;}
.service-detail {font-size: 18px;}
}
2023-06-17 12:04:59 +01:00
.origdest {
color: yellow;
}
.from {
width: 25%;
text-align: left;
2023-06-17 12:04:59 +01:00
}
.to {
width: 25%;
text-align: left;
2023-06-17 12:04:59 +01:00
}
.plat {
width: 10%
}
.time {
width: 10%;
}
2023-06-17 15:12:30 +01:00
.changed{
animation: pulse-change 1.5s linear infinite;
}
.cancelled {
animation: pulse-cancel 1.5s linear infinite;
}
@keyframes pulse-change {
50% {
color: var(--main-warning-color);
}
}
@keyframes pulse-cancel {
50% {
color: var(--main-alert-color);
}
}
2023-06-26 21:56:19 +01:00
h6 {
position: absolute;
top: -25px;
left: 20px;
font-size: 18px;
}
#closeService {
position: absolute;
top: 10px;
right: 10px;
border: none;
border-radius: 60px;
width: 35px;
height: 35px;
background-color: var(--main-bg-color);
color: white;
font-weight: 700;
font-size: 16px;
}
#detailTable {
margin-top: 40px;
}
.thisStop {
color: yellow;
}
2023-06-17 12:04:59 +01:00
</style>