Skip to content

Commit

Permalink
Merge branch 'main' into deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
davwheat committed Nov 3, 2023
2 parents ba9552f + 7c4468f commit 80bfdca
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions src/announcement-data/systems/stations/KeTechPhil.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2846,6 +2846,7 @@ export default class KeTechPhil extends StationAnnouncementSystem {
'RFD',
'RFY',
'RGL',
'RGP',
'RGT',
'RGW',
'RHD',
Expand Down Expand Up @@ -3431,6 +3432,7 @@ export default class KeTechPhil extends StationAnnouncementSystem {
'WOM',
'WON',
'WOO',
'WOP',
'WOR',
'WOS',
'WPE',
Expand Down Expand Up @@ -3816,6 +3818,8 @@ const useLiveTrainsStyles = makeStyles({
},
})

const MIN_TIME_TO_ANNOUNCE = 4

function LiveTrainAnnouncements({ nextTrainHandler, system }: LiveTrainAnnouncementsProps) {
const classes = useLiveTrainsStyles()

Expand Down Expand Up @@ -3873,10 +3877,7 @@ function LiveTrainAnnouncements({ nextTrainHandler, system }: LiveTrainAnnouncem
if (!hasRealEta) return 0

const sTime = std.split(':')
console.log(sTime)

const eTime = etd.split(':')
console.log(eTime)

const [h, m] = sTime.map(x => parseInt(x))
const [eH, eM] = eTime.map(x => parseInt(x))
Expand All @@ -3893,6 +3894,35 @@ function LiveTrainAnnouncements({ nextTrainHandler, system }: LiveTrainAnnouncem
return delayMins
}

function calculateArrivalInMins(etd: string): number {
// HH:mm in UK
const std = new Date().toLocaleTimeString('en-GB', { hour12: false, timeZone: 'Europe/London' }).slice(0, 5)

const isDelayed = etd !== 'On time' && etd !== std
if (!isDelayed) return 0

const hasRealEta = (etd as string).includes(':')

if (!hasRealEta) return 0

const sTime = std.split(':')
const eTime = etd.split(':')

const [h, m] = sTime.map(x => parseInt(x))
const [eH, eM] = eTime.map(x => parseInt(x))

console.log(`[ETA mins] ${h}:${m} (${std}) -> ${eH}:${eM} (${etd}) = ${eH * 60 + eM - (h * 60 + m)}`)

let delayMins = Math.abs(eH * 60 + eM - (h * 60 + m))

if (delayMins < 0) {
// crosses over midnight
return calculateDelayMins(std, '23:59') + calculateDelayMins('00:00', etd)
}

return delayMins
}

useEffect(() => {
if (!hasEnabledFeature) return

Expand All @@ -3905,7 +3935,7 @@ function LiveTrainAnnouncements({ nextTrainHandler, system }: LiveTrainAnnouncem
console.log('[Live Trains] Checking for new services')

const resp = await fetch(
`https://national-rail-api.davwheat.dev/departures/${selectedCrs}?expand=true&numServices=3&timeOffset=0&timeWindow=10`,
`https://national-rail-api.davwheat.dev/departures/${selectedCrs}?expand=true&numServices=3&timeOffset=0&timeWindow=${MIN_TIME_TO_ANNOUNCE}`,
)

if (!resp.ok) {
Expand Down Expand Up @@ -3947,6 +3977,12 @@ function LiveTrainAnnouncements({ nextTrainHandler, system }: LiveTrainAnnouncem
console.log(`[Live Trains] Skipping ${s.serviceIdGuid} (${s.std} to ${s.destination[0].locationName}) as it has no confirmed platform`)
return false
}
if (calculateArrivalInMins(s.etd) > MIN_TIME_TO_ANNOUNCE) {
console.log(
`[Live Trains] Skipping ${s.serviceIdGuid} (${s.std} to ${s.destination[0].locationName}) as it is more than ${MIN_TIME_TO_ANNOUNCE} mins away`,
)
return false
}

return true
})
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 80bfdca

Please sign in to comment.