2023-02-09 20:29:07 +00:00
|
|
|
/* Fetch Functions */
|
|
|
|
async function publicLdb(stn) {
|
2023-05-06 21:53:43 +01:00
|
|
|
var url = `${window.location.origin}/api/v1/ldb/${stn}`
|
|
|
|
console.time('Time: Fetch LDB Data')
|
|
|
|
var resp = await fetch(url)
|
|
|
|
console.timeEnd('Time: Fetch LDB Data')
|
|
|
|
return await resp.json()
|
2023-02-09 20:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set page heading */
|
|
|
|
async function setHeaders(title,time) {
|
2023-05-06 21:53:43 +01:00
|
|
|
var prefix = 'OwlBoard - '
|
|
|
|
document.title = `${prefix}${title}`
|
|
|
|
document.getElementById('stn_name').textContent = title
|
|
|
|
document.getElementById('fetch_time').textContent = time.toLocaleTimeString()
|
|
|
|
sessionStorage.setItem('board_location', title)
|
2023-02-09 20:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Display No Trains Message */
|
|
|
|
async function displayNoTrains() {
|
2023-05-06 21:53:43 +01:00
|
|
|
log('No Trains', 'WARN')
|
|
|
|
document.getElementById('no_services').style = 'display: block;'
|
|
|
|
hideLoading()
|
2023-02-09 20:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the value of `platform` to account for unknown platforms */
|
|
|
|
async function parsePlatform(svc){
|
2023-05-06 21:53:43 +01:00
|
|
|
if (svc.platform != undefined) {
|
|
|
|
var platform = svc.platform
|
|
|
|
} else {
|
|
|
|
var platform = '-'
|
|
|
|
}
|
|
|
|
if (svc.platformChanged) { // Not present in public API, ready for staff version.
|
|
|
|
var changed = 'changed'
|
|
|
|
} else {
|
|
|
|
var changed = ''
|
|
|
|
}
|
|
|
|
return {num: platform, change: changed}
|
2023-02-09 20:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Change value of time strings to fit well on small screens */
|
|
|
|
async function parseTime(string){
|
2023-05-06 21:53:43 +01:00
|
|
|
switch (string) {
|
|
|
|
case 'Delayed':
|
|
|
|
var output = 'LATE'
|
|
|
|
var change = 'changed'
|
|
|
|
break
|
|
|
|
case 'Cancelled':
|
|
|
|
var output = 'CANC'
|
|
|
|
var change = 'cancelled'
|
|
|
|
break
|
|
|
|
case 'On time':
|
|
|
|
var output = 'RT'
|
|
|
|
var change = ''
|
|
|
|
break
|
|
|
|
case '':
|
|
|
|
var output = '-'
|
|
|
|
var change = ''
|
|
|
|
break
|
|
|
|
case undefined:
|
|
|
|
var output = '-'
|
|
|
|
var change = ''
|
|
|
|
break
|
|
|
|
case 'No report':
|
|
|
|
var output = '-'
|
|
|
|
var change = ''
|
|
|
|
break
|
|
|
|
case 'undefined':
|
|
|
|
var output = false
|
|
|
|
var change = ''
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
var output = string
|
|
|
|
var change = 'changed'
|
|
|
|
}
|
|
|
|
return {data: output, changed: change}
|
2023-02-09 20:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert multiple Origin/Destinations to single string */
|
|
|
|
async function parseName(location) {
|
2023-05-06 21:53:43 +01:00
|
|
|
if (Array.isArray(location)) {
|
|
|
|
var name = `${location[0]['locationName']} & ${location[1]['locationName']}`
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return location.locationName
|
|
|
|
}
|
2023-02-09 20:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Display Alert Messages
|
|
|
|
async function displayAlerts(array) {
|
2023-05-06 21:53:43 +01:00
|
|
|
var counter = 0
|
|
|
|
var messages = ''
|
|
|
|
for(var i = 0; i < array.length; i++) {
|
|
|
|
// Increment counter
|
|
|
|
counter += 1
|
|
|
|
// Reset Vars
|
|
|
|
messages += `<p>${array[i]}</p>`
|
|
|
|
}
|
|
|
|
if (counter > 0) {
|
|
|
|
document.getElementById('alerts_msg').insertAdjacentHTML('beforeend', messages)
|
|
|
|
document.getElementById('alerts').style = 'display:block'
|
|
|
|
document.getElementById('alerts_bar').style = 'display:block'
|
|
|
|
if (counter == 1) {
|
|
|
|
document.getElementById('alert_bar_note').textContent = `There is ${counter} active alert`
|
|
|
|
} else if (counter > 1) {
|
|
|
|
document.getElementById('alert_bar_note').textContent = `There are ${counter} active alerts`
|
2023-02-09 20:29:07 +00:00
|
|
|
}
|
2023-05-06 21:53:43 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2023-02-09 20:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Show/Hide alerts box */
|
|
|
|
async function inflateAlerts() {
|
2023-05-06 21:53:43 +01:00
|
|
|
document.getElementById('alerts_msg').style = 'display:block;'
|
|
|
|
document.getElementById('alert_expand_arrow').style = 'transform: rotate(180deg);'
|
|
|
|
document.getElementById('alerts_bar').setAttribute('onclick', 'deflateAlerts()')
|
2023-02-09 20:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function deflateAlerts() {
|
2023-05-06 21:53:43 +01:00
|
|
|
document.getElementById('alerts_msg').style = 'display.none;'
|
|
|
|
document.getElementById('alert_expand_arrow').style = 'transform: rotate(0deg);'
|
|
|
|
document.getElementById('alerts_bar').setAttribute('onclick', 'inflateAlerts()')
|
2023-02-09 20:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*//// SERVICE DETAIL LISTS ////*/
|
|
|
|
// Build calling list: -- This outputs calling point data to sessionStorage in the format: key{pre: [{PREVIOUS_Stops}], post: [{POST_STOPS}]}
|
|
|
|
async function buildCallLists(svc) {
|
2023-05-06 21:53:43 +01:00
|
|
|
var sSvcId = svc.serviceID
|
|
|
|
var oSvcData = {
|
|
|
|
plat: svc.platform,
|
|
|
|
sta: svc.sta,
|
|
|
|
eta: svc.eta,
|
|
|
|
std: svc.std,
|
|
|
|
etd: svc.etd
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (typeof svc.previousCallingPoints.callingPointList.callingPoint != 'undefined') {
|
|
|
|
let array = await makeArray(svc.previousCallingPoints.callingPointList.callingPoint)
|
|
|
|
oSvcData.pre = array
|
|
|
|
}
|
|
|
|
} catch (err) { /* Do nothing if ERR */ }
|
|
|
|
try {
|
|
|
|
if (typeof svc.subsequentCallingPoints.callingPointList.callingPoint != 'undefined') {
|
|
|
|
let array = await makeArray(svc.subsequentCallingPoints.callingPointList.callingPoint)
|
|
|
|
oSvcData.post = array
|
|
|
|
}
|
|
|
|
} catch (err) { /* Do nothing if ERR */ }
|
|
|
|
sessionStorage.setItem(sSvcId, JSON.stringify(oSvcData))
|
2023-02-09 20:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Display calling list: - Read data from sessionStorage and write to DOM. */
|
|
|
|
async function showCalls(id) {
|
2023-05-06 21:53:43 +01:00
|
|
|
log(`Showing details for service ${id}`, 'INFO')
|
|
|
|
var svcDetail = await JSON.parse(sessionStorage.getItem(id))
|
|
|
|
var pre = ''
|
|
|
|
var post = ''
|
|
|
|
if (typeof svcDetail.pre != 'undefined') {
|
|
|
|
for(var preCall = 0; preCall < svcDetail.pre.length; preCall++) {
|
|
|
|
pre += await singleCall(svcDetail.pre[preCall])
|
2023-02-09 20:29:07 +00:00
|
|
|
}
|
2023-05-06 21:53:43 +01:00
|
|
|
}
|
|
|
|
if (typeof svcDetail.post != 'undefined') {
|
|
|
|
for(var postCall = 0; postCall < svcDetail.post.length; postCall++) {
|
|
|
|
post += await singleCall(svcDetail.post[postCall])
|
2023-02-09 20:29:07 +00:00
|
|
|
}
|
2023-05-06 21:53:43 +01:00
|
|
|
}
|
|
|
|
/* Run retreived data through parsers */
|
|
|
|
var thisStd = await parseTime(svcDetail.std)
|
|
|
|
var thisEtd = await parseTime(svcDetail.etd)
|
|
|
|
var thisSta = await parseTime(svcDetail.sta)
|
|
|
|
var thisEta = await parseTime(svcDetail.eta)
|
|
|
|
/* Prepare data for this station */
|
|
|
|
if (thisStd.data != '-') {
|
|
|
|
var sTime = `${thisStd.data}`
|
|
|
|
var eTime = `${thisEtd.data}`
|
|
|
|
var change = thisEtd.changed
|
|
|
|
} else {
|
|
|
|
var sTime = `${thisSta.data}`
|
|
|
|
var eTime = `${thisEta.data}`
|
|
|
|
var change = thisEta.changed
|
|
|
|
};
|
|
|
|
|
|
|
|
let here = `<tr>
|
|
|
|
<td class="detail-name detail-name-here detail-table-content">${sessionStorage.getItem('board_location')}</td>
|
2023-02-09 20:29:07 +00:00
|
|
|
<td class="detail-table-content">${sTime}</td>
|
|
|
|
<td class="detail-table-content ${change}">${eTime}</td>
|
|
|
|
</tr> `
|
2023-05-06 21:53:43 +01:00
|
|
|
/* Prepare then insert DOM Data */
|
|
|
|
let dom = ` <div id="${id}" class="call-data">
|
2023-02-09 20:29:07 +00:00
|
|
|
<p class="close-data" onclick="hideCalls('${id}')">X</p>
|
|
|
|
<table class="call-table">
|
|
|
|
<tr>
|
|
|
|
<th class="detail-name-head">Location</th>
|
|
|
|
<th class="time">Schedule</th>
|
|
|
|
<th class="time">Act/Est</th>
|
|
|
|
</tr>
|
|
|
|
${pre}
|
|
|
|
${here}
|
|
|
|
${post}
|
|
|
|
</table>
|
|
|
|
</div>`
|
|
|
|
|
2023-05-06 21:53:43 +01:00
|
|
|
document.body.insertAdjacentHTML('beforeend', dom)
|
|
|
|
document.getElementById(id).style = 'display: block;'
|
|
|
|
return
|
2023-02-09 20:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function hideCalls(id) {
|
2023-05-06 21:53:43 +01:00
|
|
|
let element = document.getElementById(id)
|
|
|
|
element.style = 'display: none;'
|
|
|
|
element.remove()
|
|
|
|
return
|
2023-02-09 20:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Builds the train data information in to a table row */
|
|
|
|
async function singleCall(data) {
|
2023-05-06 21:53:43 +01:00
|
|
|
if (typeof data.et != 'undefined') {
|
|
|
|
var time = await parseTime(data.et)
|
|
|
|
} else if (typeof data.at != 'undefined') {
|
|
|
|
var time = await parseTime(data.at)
|
|
|
|
}
|
|
|
|
return `<tr>
|
2023-02-09 20:29:07 +00:00
|
|
|
<td class="detail-name detail-table-content">${data.locationName}</td>
|
|
|
|
<td class="detail-table-content">${data.st}</td>
|
|
|
|
<td class="detail-table-content ${time.changed}">${time.data}</td>
|
|
|
|
</tr>`
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Error Handler */
|
|
|
|
async function errorHandler() {
|
2023-05-06 21:53:43 +01:00
|
|
|
if (sessionStorage.getItem('failcount')) {
|
|
|
|
var errCount = parseInt(sessionStorage.getItem('failcount'))
|
|
|
|
} else {
|
|
|
|
var errCount = 0
|
|
|
|
}
|
|
|
|
errCount += 1
|
|
|
|
sessionStorage.setItem('failcount', errCount.toString())
|
|
|
|
if (errCount < 10){
|
|
|
|
await delay(3000)
|
|
|
|
vibe('err')
|
|
|
|
location.reload()
|
|
|
|
} else {
|
|
|
|
sessionStorage.removeItem('failcount')
|
|
|
|
window.location.assign('conn-err.html')
|
|
|
|
}
|
2023-02-09 20:29:07 +00:00
|
|
|
}
|