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 Feb 9, 2024
2 parents 8fa31c4 + 5876a4a commit 2d1753f
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 237 deletions.
73 changes: 57 additions & 16 deletions cf-workers/src/get-services.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import type { Env } from '.'

export interface AssociatedServiceDetail {
cancelReason: CancelLatenessReason | null
delayReason: CancelLatenessReason | null
isCharter: boolean
isPassengerService: boolean
category: string
sta: string
staSpecified: boolean
ata: string
ataSpecified: boolean
eta: string
etaSpecified: boolean
std: string
stdSpecified: boolean
atd: string
atdSpecified: boolean
etd: string
etdSpecified: boolean
rid: string
uid: string
locations: AssociatedServiceLocation[]
}

export interface StaffServicesResponse {
trainServices: TrainService[] | null
busServices: null
Expand All @@ -18,14 +41,14 @@ export interface StaffServicesResponse {
servicesAreUnavailable: boolean
}

interface TrainService {
export interface TrainService {
previousLocations: any
subsequentLocations: SubsequentLocation[]
subsequentLocations: TimingLocation[]
cancelReason: CancelLatenessReason | null
delayReason: CancelLatenessReason | null
category: string
activities: string
length: number
length: number | null
isReverseFormation: boolean
detachFront: boolean
origin: EndPointLocation[]
Expand Down Expand Up @@ -73,7 +96,7 @@ interface TrainService {
adhocAlerts: any
}

interface SubsequentLocation {
export interface TimingLocation {
locationName: string
tiploc: string
crs?: string
Expand Down Expand Up @@ -106,16 +129,29 @@ interface SubsequentLocation {
lateness: any
associations: Association[] | null
adhocAlerts: any
activities?: string
}

interface Association {
export enum AssociationCategory {
Join = 0,
Divide = 1,
LinkedFrom = 2,
LinkedTo = 3,
}

interface AssociatedServiceLocation extends TimingLocation {
length: number | null
falseDest: null | EndPointLocation[]
}

export interface Association<Category extends AssociationCategory = AssociationCategory> {
/**
* 0: Join
* 1: Divide
* 2: Linked-From (last service)
* 3: Linked-To (next service)
*/
category: number
category: AssociationCategory
rid: string
uid: string
trainid: string
Expand All @@ -132,7 +168,7 @@ interface Association {
/**
* Added by this proxy
*/
service: TrainService
service: Category extends AssociationCategory.Divide ? AssociatedServiceDetail : undefined
}

interface CancelLatenessReason {
Expand All @@ -142,7 +178,7 @@ interface CancelLatenessReason {
stationName?: string | null
}

interface EndPointLocation {
export interface EndPointLocation {
isOperationalEndPoint: boolean
locationName: string
crs: string
Expand All @@ -160,9 +196,9 @@ interface NrccMessage {

import TiplocToStation from './tiploc_to_station.json'

async function getServiceByRid(rid: string): Promise<TrainService> {
async function getServiceByRid(rid: string): Promise<AssociatedServiceDetail> {
const response = await fetch(`https://national-rail-api.davwheat.dev/service/${rid}`)
const json: TrainService = await response.json()
const json: AssociatedServiceDetail = await response.json()

return json
}
Expand Down Expand Up @@ -196,33 +232,38 @@ export async function getServicesHandler(request: Request, env: Env, ctx: Execut
const json: StaffServicesResponse = await response.json()

for (const s in json.trainServices) {
const service: TrainService = json.trainServices[s]
const service: TrainService = json.trainServices[s as any]
const serviceData = await getServiceByRid(service.rid)

if (service.cancelReason?.near) {
service.cancelReason.stationName = TiplocToStation[service.cancelReason.tiploc] || null
service.cancelReason.stationName = TiplocToStation[service.cancelReason.tiploc as keyof typeof TiplocToStation].crs || null
}

if (service.delayReason?.near) {
service.delayReason.stationName = TiplocToStation[service.delayReason.tiploc] || null
service.delayReason.stationName = TiplocToStation[service.delayReason.tiploc as keyof typeof TiplocToStation].crs || null
}

for (const l in service.subsequentLocations) {
const location: SubsequentLocation = service.subsequentLocations[l]
const location: TimingLocation = service.subsequentLocations[l]

for (const a in location.associations) {
const association: Association = location.associations[a]
const association: Association = location.associations[a as any]

// Joins/Divides only
if ([0, 1].includes(association.category)) {
;(association as any).service = await getServiceByRid(association.rid)
}
}

location.activities = serviceData.locations.find(l => {
return l.tiploc === location.tiploc && (l.sta === location.sta || l.std === location.std)
})?.activities
}
}

return Response.json(json)
} catch (ex) {
if (ex && ex.message) {
if (ex && ex instanceof Error) {
return Response.json({ error: true, message: ex.message })
} else {
return Response.json({ error: true, message: 'Unknown error' })
Expand Down
14 changes: 11 additions & 3 deletions src/announcement-data/systems/stations/AmeyCelia.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ export default class AmeyCelia extends AmeyPhil {

readonly DelayCodeMapping = DelayCodeMapping

protected readonly genericOptions = {
platform: 's.platform-2',
platformZeroM: 'm.0',
platformZeroE: 'e.0',
}

protected readonly callingPointsOptions = {
beforeCallingAtDelay: this.BEFORE_SECTION_DELAY,
afterCallingAtDelay: 0,
Expand All @@ -32,9 +38,11 @@ export default class AmeyCelia extends AmeyPhil {
}

get PLATFORMS() {
return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
.flatMap(x => [`${x}`, `${x}a`, `${x}b`, `${x}c`, `${x}d`])
.concat(['13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24'])
return ['0'].concat(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
.flatMap(x => [`${x}`, `${x}a`, `${x}b`, `${x}c`, `${x}d`])
.concat(['13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24']),
)
}

protected get AVAILABLE_TOCS() {
Expand Down
61 changes: 45 additions & 16 deletions src/announcement-data/systems/stations/AmeyPhil.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ export default class AmeyPhil extends StationAnnouncementSystem {

readonly DelayCodeMapping: Record<string, { e: string; m: string }> = DelayCodeMapping

protected readonly genericOptions = {
platform: 's.platform-2',
platformZeroM: 'm.0',
// No end inflection
platformZeroE: 'm.0',
}

protected readonly callingPointsOptions = {
beforeCallingAtDelay: this.BEFORE_SECTION_DELAY,
afterCallingAtDelay: 0,
Expand Down Expand Up @@ -844,9 +851,11 @@ export default class AmeyPhil extends StationAnnouncementSystem {
}

get PLATFORMS() {
return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
.flatMap(x => [`${x}`, `${x}a`, `${x}b`, `${x}c`, `${x}d`])
.concat(['13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', 'a', 'b', 'c', 'd'])
return ['0'].concat(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
.flatMap(x => [`${x}`, `${x}a`, `${x}b`, `${x}c`, `${x}d`])
.concat(['13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', 'a', 'b', 'c', 'd']),
)
}

get STATIONS() {
Expand Down Expand Up @@ -4303,21 +4312,27 @@ export default class AmeyPhil extends StationAnnouncementSystem {

const plat = parseInt(options.platform)

function getPlatFiles(startDelay: number = 0) {
const getPlatFiles = (startDelay: number = 0) => {
const platFiles: AudioItem[] = []

if (plat <= 12 || ['a', 'b'].includes(options.platform.toLowerCase())) {
if (options.platform === '0') {
platFiles.push(
{ id: this.genericOptions.platform, opts: { delayStart: 250 } },
`m.0`,
options.isDelayed ? `m.for the delayed` : `m.for the`,
)
} else if (plat <= 12 || ['a', 'b'].includes(options.platform.toLowerCase())) {
platFiles.push({ id: `s.platform ${options.platform} for the`, opts: { delayStart: 250 } })
if (options.isDelayed) platFiles.push('m.delayed')
} else if (plat >= 21) {
files.push(
{ id: `s.platform`, opts: { delayStart: 250 } },
platFiles.push(
{ id: this.genericOptions.platform, opts: { delayStart: 250 } },
`mins.m.${options.platform}`,
options.isDelayed ? `m.for the delayed` : `m.for the`,
)
} else {
platFiles.push(
{ id: `s.platform`, opts: { delayStart: 250 } },
{ id: this.genericOptions.platform, opts: { delayStart: 250 } },
`platform.s.${options.platform}`,
options.isDelayed ? `m.for the delayed` : `m.for the`,
)
Expand Down Expand Up @@ -4414,8 +4429,10 @@ export default class AmeyPhil extends StationAnnouncementSystem {
function getPlatFiles() {
const platFiles: AudioItem[] = []

if (plat >= 21) {
files.push(`mins.m.${options.platform}`, options.isDelayed ? `m.is the delayed` : `m.is the`)
if (options.platform === '0') {
platFiles.push(`m.0`, options.isDelayed ? `m.is the delayed` : `m.is the`)
} else if (plat >= 21) {
platFiles.push(`mins.m.${options.platform}`, options.isDelayed ? `m.is the delayed` : `m.is the`)
} else {
platFiles.push(`platform.s.${options.platform}`, options.isDelayed ? `m.is the delayed` : `m.is the`)
}
Expand Down Expand Up @@ -4579,7 +4596,17 @@ export default class AmeyPhil extends StationAnnouncementSystem {

let plat = parseInt(options.platform)

files.push('s.stand well away from the edge of platform', plat >= 21 ? `mins.e.${options.platform}` : `platform.e.${options.platform}`, {
const platformAudio = (() => {
if (options.platform === '0') {
return `e.0`
} else if (isNaN(plat) || plat <= 20) {
return `platform.e.${options.platform}`
} else {
return `mins.e.${options.platform}`
}
})()

files.push('s.stand well away from the edge of platform', platformAudio, {
id: 'w.the approaching train is not scheduled to stop at this station',
opts: { delayStart: this.BEFORE_SECTION_DELAY },
})
Expand All @@ -4602,7 +4629,9 @@ export default class AmeyPhil extends StationAnnouncementSystem {

const plat = parseInt(options.platform)

if (plat <= 20 && options.platform.match(/^\d+$/)) {
if (options.platform === '0') {
files.push(`s.the train now approaching platform`, 'm.0')
} else if (plat <= 20 && options.platform.match(/^\d+$/)) {
files.push(`s.the train now approaching platform ${plat}`)
} else if (plat >= 21) {
files.push(`s.the train now approaching platform`, `mins.m.${options.platform}`)
Expand Down Expand Up @@ -4895,7 +4924,7 @@ export default class AmeyPhil extends StationAnnouncementSystem {
},
platform: {
name: 'Platform',
default: this.PLATFORMS[0],
default: this.PLATFORMS[1],
options: this.PLATFORMS.map(p => ({ title: `Platform ${p.toUpperCase()}`, value: p })),
type: 'select',
},
Expand Down Expand Up @@ -5022,7 +5051,7 @@ export default class AmeyPhil extends StationAnnouncementSystem {
},
platform: {
name: 'Platform',
default: this.PLATFORMS[0],
default: this.PLATFORMS[1],
options: this.PLATFORMS.map(p => ({ title: `Platform ${p.toUpperCase()}`, value: p })),
type: 'select',
},
Expand Down Expand Up @@ -5124,7 +5153,7 @@ export default class AmeyPhil extends StationAnnouncementSystem {
},
platform: {
name: 'Platform',
default: this.PLATFORMS[0],
default: this.PLATFORMS[1],
options: this.PLATFORMS.map(p => ({ title: `Platform ${p.toUpperCase()}`, value: p })),
type: 'select',
},
Expand Down Expand Up @@ -5359,7 +5388,7 @@ export default class AmeyPhil extends StationAnnouncementSystem {
},
platform: {
name: 'Platform',
default: this.PLATFORMS[0],
default: this.PLATFORMS[1],
options: this.PLATFORMS.map(p => ({ title: `Platform ${p.toUpperCase()}`, value: p })),
type: 'select',
},
Expand Down
Loading

0 comments on commit 2d1753f

Please sign in to comment.