From 5a6fe0f3f5ff8209c0d90be8fe31ad3fd8f553df Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Fri, 7 Jul 2023 21:59:06 +0100 Subject: [PATCH] Modulisation and additions to StaffLDB --- src/lib/islands/overlay-island.svelte | 2 +- src/lib/ldb/staff-train-detail.svelte | 142 ------------- src/lib/ldb/staff/service-row.svelte | 158 ++++++++++++++ src/lib/ldb/{ => staff}/staff-ldb.svelte | 80 ++++---- src/lib/ldb/staff/train-detail.svelte | 251 +++++++++++++++++++++++ src/lib/raw-fetchers/reason.svelte | 50 +++++ src/routes/ldb/+page.svelte | 2 +- 7 files changed, 505 insertions(+), 180 deletions(-) delete mode 100644 src/lib/ldb/staff-train-detail.svelte create mode 100644 src/lib/ldb/staff/service-row.svelte rename src/lib/ldb/{ => staff}/staff-ldb.svelte (79%) create mode 100644 src/lib/ldb/staff/train-detail.svelte create mode 100644 src/lib/raw-fetchers/reason.svelte diff --git a/src/lib/islands/overlay-island.svelte b/src/lib/islands/overlay-island.svelte index 971731c..c03b5d3 100644 --- a/src/lib/islands/overlay-island.svelte +++ b/src/lib/islands/overlay-island.svelte @@ -23,7 +23,7 @@ transform: translateY(-50%) translateX(-50%); width: 85%; height: auto; - max-height: 75vh; + max-height: 85vh; overflow-y: auto; max-width: 400px; margin: auto; diff --git a/src/lib/ldb/staff-train-detail.svelte b/src/lib/ldb/staff-train-detail.svelte deleted file mode 100644 index 4267eee..0000000 --- a/src/lib/ldb/staff-train-detail.svelte +++ /dev/null @@ -1,142 +0,0 @@ - - - -
- {#await getTrain(detail.rid)} -
{detail.headcode}
- - {:then train} -
{train.GetServiceDetailsResult.operatorCode}: {detail.headcode}
-

Locations in grey are not scheduled stops -
- Some stops may be operational stops, not passenger stops. -

- - - - - - - - - - {#each train.GetServiceDetailsResult.locations.location as location} - - - - - - {#await parseDelay(location)} - - {:then delay} - - {/await} - - {/each} -
LocationPlat.Sch ArrSch DepDelay
{location.tiploc}{location.platform || ''}ARDP-{delay.string}
- {:catch} -
Error loading data
- {/await} -
-
- - \ No newline at end of file diff --git a/src/lib/ldb/staff/service-row.svelte b/src/lib/ldb/staff/service-row.svelte new file mode 100644 index 0000000..aa1c4c8 --- /dev/null +++ b/src/lib/ldb/staff/service-row.svelte @@ -0,0 +1,158 @@ + + +{#await generateServiceData(service)} + + Loading... + + {:then serviceStats} + + {service.trainid} + {serviceStats.from} + {serviceStats.to} + {serviceStats.platform.number || '-'} + {serviceStats.schArr} + {serviceStats.isArrDelayed ? 'LATE' : serviceStats.expArr} + {serviceStats.schDep} + {serviceStats.isDepDelayed ? 'LATE' : serviceStats.expDep} + + + + {service.operator} + {#if serviceStats.length} | {serviceStats.length} carriages{/if} +
+ {#if service.cancelReason} + + {/if} + {#if service?.delayReason && !service.isCancelled} + + {/if} + + +{:catch} + + Unable to load service + +{/await} \ No newline at end of file diff --git a/src/lib/ldb/staff-ldb.svelte b/src/lib/ldb/staff/staff-ldb.svelte similarity index 79% rename from src/lib/ldb/staff-ldb.svelte rename to src/lib/ldb/staff/staff-ldb.svelte index 4578cd6..c35651b 100644 --- a/src/lib/ldb/staff-ldb.svelte +++ b/src/lib/ldb/staff/staff-ldb.svelte @@ -2,8 +2,10 @@ export let station = ''; export let title = 'Loading...'; import { onMount } from 'svelte'; - import AlertBar from './alert-bar.svelte'; - import StaffTrainDetail from '$lib/ldb/staff-train-detail.svelte'; + import AlertBar from '../alert-bar.svelte'; + import ServiceRow from './service-row.svelte'; + import StaffTrainDetail from '$lib/ldb/staff/train-detail.svelte'; + import Reason from '$lib/raw-fetchers/reason.svelte'; import Loading from '$lib/navigation/loading.svelte'; import Nav from '$lib/navigation/nav.svelte'; import { uuid } from '$lib/stores/uuid'; @@ -16,7 +18,7 @@ let dataAge = null; let isLoading = true; let alerts = []; - let detail = {show: false, rid:'',uid:'', headcode:''} + let detail = { show: false, rid: '', uid: '', headcode: '' }; $: { if (jsonData?.GetBoardResult?.generatedAt) { @@ -60,13 +62,6 @@ } } - async function getReasonCodeData(code) { - const url = `https://owlboard.info/api/v2/ref/reasonCode/${code}`; - const res = await fetch(url); - const json = await res.json(); - return json; - } - async function generateServiceData(service) { const timeDetails = await parseTimes(service); let serviceData = { @@ -85,10 +80,12 @@ isLateDep: timeDetails.delDep, isCancelledDep: false, isCancelled: Boolean(service?.isCancelled), + canArr: timeDetails.canArr, + canDep: timeDetails.canDep, isDelayed: service?.arrivalType === 'Delayed', isArrDelayed: service?.arrivalType === 'Delayed', isDepDelayed: service?.departureType === 'Delayed', - isNonPublic: service?.isPassengerService === "false" ? true : false + isNonPublic: service?.isPassengerService === 'false' ? true : false }; return serviceData; } @@ -104,7 +101,6 @@ async function parseLocation(location) { if (!Array.isArray(location.location)) { - //console.log(location.location?.tiploc) return location.location?.tiploc; } let locations = []; @@ -133,10 +129,12 @@ let expDep = new Date(service?.etd || service?.atd); let isEarlyArr = false, isDelayedArr = false, - isArr = false; + isArr = false, + canArr = false; let isEarlyDep = false, isDelayedDep = false, - isDep = false; + isDep = false, + canDep = false; const timeDifferenceThreshold = 60 * 1000; // 60 seconds in milliseconds if (expArr - schArr < -timeDifferenceThreshold) { isEarlyArr = true; @@ -160,6 +158,9 @@ } else { parsedExpArr = parseIndividualTime(expArr); } + } else if (service.isCancelled === 'true') { + parsedExpArr = 'CANC'; + canArr = true; } else { parsedExpArr = '-'; } @@ -171,6 +172,9 @@ } else { parsedExpDep = parseIndividualTime(expDep); } + } else if (service.isCancelled === 'true') { + parsedExpDep = 'CANC'; + canDep = true; } else { parsedExpDep = '-'; } @@ -182,7 +186,9 @@ earArr: isEarlyArr, delArr: isDelayedArr, earDep: isEarlyDep, - delDep: isDelayedDep + delDep: isDelayedDep, + canArr: canArr, + canDep: canDep }; } @@ -268,17 +274,29 @@ {:then serviceStats} - {service.trainid} - {serviceStats.from} - {serviceStats.to} + {service.trainid} + {serviceStats.from} + {serviceStats.to} {serviceStats.platform.number || '-'} {serviceStats.schArr} - {serviceStats.isArrDelayed ? 'LATE' : serviceStats.expArr}{serviceStats.isArrDelayed ? 'LATE' : serviceStats.expArr} {serviceStats.schDep} - {serviceStats.isDepDelayed ? 'LATE' : serviceStats.expDep}{serviceStats.isDepDelayed ? 'LATE' : serviceStats.expDep} @@ -286,21 +304,11 @@ {service.operator} {#if serviceStats.length} | {serviceStats.length} carriages{/if}
- {#if service.isCancelled} - {#await getReasonCodeData(service.cancelReason)} - This train has been cancelled - {:then reasonCode} - {reasonCode[0].cancReason} -
- {/await} + {#if service.cancelReason} + {/if} - {#if service?.delayReason} - {#await getReasonCodeData(service.delayReason)} - This train has been delayed - {:then reasonCode} - {reasonCode[0].lateReason} -
- {/await} + {#if service?.delayReason && !service.isCancelled} + {/if} diff --git a/src/lib/ldb/staff/train-detail.svelte b/src/lib/ldb/staff/train-detail.svelte new file mode 100644 index 0000000..2e7040c --- /dev/null +++ b/src/lib/ldb/staff/train-detail.svelte @@ -0,0 +1,251 @@ + + + +
+ {#await getTrain(detail.rid)} +
{detail.headcode}
+ + {:then train} +
{train.GetServiceDetailsResult.operatorCode}: {detail.headcode}
+ +

+ Locations in grey are not scheduled stops +
+ Times in yellow are estimated times +

+ + {#if train.GetServiceDetailsResult.delayReason} + + {/if} + {#if train.GetServiceDetailsResult.cancelReason} + + {/if} + + + + + + + + + + + + + {#each train.GetServiceDetailsResult.locations.location as location} + + + + {#await parseTimes(location)} + + + + + {/await} + {#await parseDelay(location)} + + {:then delay} + + {/await} + + {/each} +
+ +
+ +
+ ArrivalDeparture +
LocationPl.SchEst/ActSchEst/Act +
{location.tiploc}{location.platform || ''} + + + + {:then times} + {times.sta}{times.eata}{times.std}{times.eatd}-{delay.string}
+ {:catch} +
Error loading data
+ {/await} +
+
+ + diff --git a/src/lib/raw-fetchers/reason.svelte b/src/lib/raw-fetchers/reason.svelte new file mode 100644 index 0000000..83b7f0f --- /dev/null +++ b/src/lib/raw-fetchers/reason.svelte @@ -0,0 +1,50 @@ + + +{#if type === "cancel"} + {#await getCancel(code)} + This train has been cancelled + {:then reason} + {reason} + {:catch} + This train has been cancelled + {/await} +{:else if type === "delay"} + {#await getDelay(code)} + This train has been delayed + {:then reason} + {reason} + {:catch} + This train has been delayed + {/await} +{/if} \ No newline at end of file diff --git a/src/routes/ldb/+page.svelte b/src/routes/ldb/+page.svelte index 4c6f602..0218f90 100644 --- a/src/routes/ldb/+page.svelte +++ b/src/routes/ldb/+page.svelte @@ -2,7 +2,7 @@ import Header from '$lib/navigation/header.svelte'; import Nav from '$lib/navigation/nav-ldb.svelte'; import PublicLdb from '$lib/ldb/public-ldb.svelte'; - import StaffLdb from '$lib/ldb/staff-ldb.svelte'; + import StaffLdb from '$lib/ldb/staff/staff-ldb.svelte'; import { uuid } from '$lib/stores/uuid.js'; import { onMount } from 'svelte';