Skip to content

Commit

Permalink
Merge pull request #15 from minaorangina/android
Browse files Browse the repository at this point in the history
Android
  • Loading branch information
minaorangina authored Oct 31, 2017
2 parents 6d0d9f6 + 85cbaaf commit be18d60
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Instead, this is for someone who knows *exactly* where they're going, but **does

Simple as that :smile:

### Uses Node v6.9.1
### Uses Node v8.7.0

#### I'm keeping track of any problems or bugs encountered [here](https://github.com/minaorangina/run/blob/master/problems-solutions.md)

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Run or walk for your transport?",
"main": "start.js",
"engines": {
"node": "6.9.1"
"node": "8.7.0"
},
"scripts": {
"start": "node start.js",
Expand Down Expand Up @@ -40,6 +40,7 @@
"dotenv": "^4.0.0",
"express": "^4.14.0",
"immutability-helper": "^2.0.0",
"lodash.isequal": "^4.5.0",
"moment": "^2.15.1",
"morgan": "^1.7.0",
"node-sass": "^3.10.1",
Expand Down
98 changes: 56 additions & 42 deletions server/lib/getTrainArrivals.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,78 @@
"use strict";
const soap = require('soap');
const _isEqual = require('lodash.isequal');
const sendBackData = require('./sendBackData');

const url = 'https://lite.realtime.nationalrail.co.uk/OpenLDBWS/wsdl.aspx?ver=2015-05-14';
const accessToken = '<AccessToken><TokenValue>' + process.env.TOKEN + '</TokenValue></AccessToken>';

let INTERVAL_ID = '';
let dataCache = {};
const mode = 'train';

function getTrainArrivals (io, mode, direction) {
async function getTrainArrivals (io, direction, useCache) {

if (INTERVAL_ID) {
clearInterval(INTERVAL_ID);
}
pollAPI(io, mode, direction);
if (Object.keys(io.nsp.connected).length > 0) {
await pollAPI(io, direction);
if (useCache && Object.keys(dataCache).length > 0) {
sendBackData(io, dataCache);
}
}
}

function pollAPI (io, mode, direction) {

soap.createClient(url, function (err, client) {
function pollAPI (io, direction) {
INTERVAL_ID = setInterval(() => {
getTrainArrivals(io, direction);
}, 10000);
return new Promise((resolve, reject) => {
soap.createClient(url, function (err, client) {

if (err) {
console.error('Error creating client...');
io.emit(`${mode}:error`, new Error(`Error creating soap client ${err.message}`));
}
if (err) {
console.error('Error creating client...');
io.emit(`${mode}:error`, new Error(`Error creating soap client ${err.message}`));
return reject();
}

const away = {
numRows: 9,
crs: process.env.AWAYWARDS_ORIGIN_TRAIN,
filterCrs: process.env.AWAYWARDS_DESTINATION_TRAIN,
filterType: 'to',
timeOffset: 0,
timeWindow: 120
};
const away = {
numRows: 9,
crs: process.env.AWAYWARDS_ORIGIN_TRAIN,
filterCrs: process.env.AWAYWARDS_DESTINATION_TRAIN,
filterType: 'to',
timeOffset: 0,
timeWindow: 120
};

const home = {
numRows: 9,
crs: process.env.HOMEWARDS_ORIGIN_TRAIN,
filterCrs: process.env.AWAYWARDS_ORIGIN_TRAIN,
filterType: 'to',
timeOffset: 0,
timeWindow: 120
};
const home = {
numRows: 9,
crs: process.env.HOMEWARDS_ORIGIN_TRAIN,
filterCrs: process.env.AWAYWARDS_ORIGIN_TRAIN,
filterType: 'to',
timeOffset: 0,
timeWindow: 120
};

const args = direction === 'home' ? home : away;
client.addSoapHeader(accessToken);
client.GetDepBoardWithDetails(args, function (err, result) {
if (err) {
console.error('Error getting departures...');
console.error(`${mode}:error`, err.response.toJSON());
io.emit(`${mode}:error`, err.response.toJSON());
return;
}
const stationBoard = result.GetStationBoardResult;
const data = stationBoard.trainServices ? stationBoard.trainServices.service.slice(0, 6) : [];
const origin = direction === 'away' ? process.env.AWAYWARDS_ORIGIN_TRAIN_NAME : process.env.AWAYWARDS_DESTINATION_TRAIN_NAME;
io.emit(`${mode}:arrivals`, { data, direction, origin, destination: stationBoard.filterLocationName, last_updated: new Date().toISOString() });
const args = direction === 'home' ? home : away;
client.addSoapHeader(accessToken);
client.GetDepBoardWithDetails(args, function (err, result) {
if (err) {
console.error('Error getting departures...');
console.error(`${mode}:error`, err);
io.emit(`${mode}:error`, err);
return reject();
}
const stationBoard = result.GetStationBoardResult;
const data = stationBoard.trainServices ? stationBoard.trainServices.service.slice(0, 6) : [];
const currentData = { data, mode: 'train', direction, destination: stationBoard && stationBoard.filterLocationName };
if (!_isEqual(currentData, dataCache)) {
dataCache = { ...currentData };
sendBackData(io, dataCache);
}
resolve();
});
});

INTERVAL_ID = setInterval(() => {
getTrainArrivals(io, mode, direction);
}, 10000);
});
}

Expand Down
40 changes: 40 additions & 0 deletions server/lib/sendBackData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

function sendBackData (io, { data = [], mode, direction, destination }) {
const origin = direction === 'away' ? process.env.AWAYWARDS_ORIGIN_TRAIN_NAME : process.env.AWAYWARDS_DESTINATION_TRAIN_NAME;
let androidData;
parseTrainData(data)
.then(parsed => {
if (parsed) {
androidData = parsed;
}
io.emit(`${mode}:arrivals`, { androidData, direction, origin, destination, last_updated: new Date().toISOString() });
})
.catch(console.error);
}

function parseTrainData (trainData, mode = 'train') {
return new Promise((resolve) => {
if (trainData.length === 0) {
return resolve();
}
const mapped = trainData.map(arrival => {
const finalDestination = arrival.destination.location[0].locationName;
return {
std: arrival.std,
etd: arrival.etd,
identifier: `${arrival.std} to ${normaliseStationName(finalDestination, mode)}`
};
});
resolve(mapped);
});
}
function normaliseStationName (stationName, mode) {
if (mode === 'dlr' && stationName.length > 0) {
return stationName.replace(' DLR Station', '');
}
if (mode === 'train' && stationName !== 'London Bridge') {
return stationName.replace('London ', '');
}
}

module.exports = sendBackData;
7 changes: 6 additions & 1 deletion server/socketRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ const getTrainArrivals = require('./lib/getTrainArrivals');

function socketRouter (io) {

io.on("foo", msg => {
console.log(msg);
io.emit('bar', { worked: 'hell yeah!!' });
});

io.on('dlr', (direction) => {
getTfLArrivals(io, 'dlr', direction);
});
Expand All @@ -12,7 +17,7 @@ function socketRouter (io) {
});

io.on('train', (direction) => {
getTrainArrivals(io, 'train', direction);
getTrainArrivals(io, direction, true);
});
}

Expand Down

0 comments on commit be18d60

Please sign in to comment.