Add routing, state mgmt and train types
This commit is contained in:
@ -8,6 +8,70 @@ admin.initializeApp();
|
||||
const axios = require('axios');
|
||||
const parseString = require('xml2js').parseString;
|
||||
|
||||
// function to fetch station data from the Firestore database
|
||||
exports.getStationData = functions.https.onRequest((request, response) => {
|
||||
response.set('Access-Control-Allow-Origin', '*');
|
||||
response.set('Access-Control-Allow-Credentials', 'true');
|
||||
let jsonData = [];
|
||||
|
||||
cors(request, response, () => {
|
||||
// fetch the "stations" collection
|
||||
admin.firestore().collection('stations').get().then((snapshot) => {
|
||||
if (snapshot.empty) {
|
||||
response.send({data: "Error fetching station data from the database"})
|
||||
return;
|
||||
}
|
||||
// iterate through each of the collection's documents
|
||||
snapshot.forEach(doc => {
|
||||
jsonData.push(doc.data());
|
||||
});
|
||||
response.json({data: jsonData});
|
||||
})
|
||||
});
|
||||
})
|
||||
|
||||
// function to populate the Firestore database with station data from Irish Rail
|
||||
exports.postStationData = functions.https.onRequest((request, response) => {
|
||||
response.set('Access-Control-Allow-Origin', '*');
|
||||
response.set('Access-Control-Allow-Credentials', 'true');
|
||||
|
||||
cors(request, response, () => {
|
||||
axios.get('http://api.irishrail.ie/realtime/realtime.asmx/getAllStationsXML')
|
||||
.then((res) => {
|
||||
// XML to JSON
|
||||
parseString(res.data, function(err, result) {
|
||||
let jsonStr = JSON.stringify(result);
|
||||
let jsonObj = JSON.parse(jsonStr);
|
||||
let jsonData = jsonObj.ArrayOfObjStation.objStation;
|
||||
|
||||
// batch delete all of the "stations" collection's documents
|
||||
var db = admin.firestore();
|
||||
admin.firestore().collection('stations').get().then((snapshot) => {
|
||||
var batchDelete = db.batch();
|
||||
snapshot.forEach(doc => {
|
||||
batchDelete.delete(doc.ref);
|
||||
});
|
||||
|
||||
batchDelete.commit().then(function() {
|
||||
// batch write all station JSON objects to the "stations" collection
|
||||
var batchWrite = db.batch();
|
||||
|
||||
jsonData.forEach((doc) => {
|
||||
// set the station's ID as the document ID
|
||||
var docID = db.collection('stations').doc(doc["StationCode"][0]);
|
||||
batchWrite.set(docID, doc);
|
||||
});
|
||||
|
||||
batchWrite.commit().then(function () {
|
||||
response.send({data: "Successfully fetched and uploaded station data from Irish Rail"});
|
||||
});
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// function to fetch live train data from the Firestore database
|
||||
exports.getLiveTrainData = functions.https.onRequest((request, response) => {
|
||||
response.set('Access-Control-Allow-Origin', '*');
|
||||
@ -30,85 +94,74 @@ exports.getLiveTrainData = functions.https.onRequest((request, response) => {
|
||||
});
|
||||
})
|
||||
|
||||
// function to fetch station data from the Firestore database
|
||||
exports.getStationData = functions.https.onRequest((request, response) => {
|
||||
response.set('Access-Control-Allow-Origin', '*');
|
||||
response.set('Access-Control-Allow-Credentials', 'true');
|
||||
let jsonData = [];
|
||||
|
||||
cors(request, response, () => {
|
||||
// fetch the "stations" collection
|
||||
admin.firestore().collection('stations').get().then((snapshot) => {
|
||||
if (snapshot.empty) {
|
||||
response.status(404).send({data: "Error fetching station data from the database"})
|
||||
return;
|
||||
}
|
||||
// iterate through each of the collection's documents
|
||||
snapshot.forEach(doc => {
|
||||
jsonData.push(doc.data());
|
||||
});
|
||||
response.json({data: jsonData});
|
||||
})
|
||||
});
|
||||
})
|
||||
|
||||
// helper function to fetch data from Irish Rail given a train type (mainland, suburban, dart)
|
||||
function callIrishRail(request, response, db, trainTypeCode) {
|
||||
cors(request, response, () => {
|
||||
let url = 'https://api.irishrail.ie/realtime/realtime.asmx/getCurrentTrainsXML_WithTrainType?TrainType=' + trainTypeCode
|
||||
axios.get(url)
|
||||
.then((res) => {
|
||||
var batchWrite = db.batch();
|
||||
// XML to JSON
|
||||
parseString(res.data, function(err, result) {
|
||||
let jsonStr = JSON.stringify(result);
|
||||
let jsonObj = JSON.parse(jsonStr);
|
||||
let jsonData = jsonObj.ArrayOfObjTrainPositions.objTrainPositions;
|
||||
|
||||
jsonData.forEach((doc) => {
|
||||
// ignore trains with longitudes or latitudes equal zero
|
||||
if (!(doc["TrainLongitude"] == 0 || doc["TrainLatitude"] == 0)) {
|
||||
doc["TrainType"] = [trainTypeCode]
|
||||
// set the train's code as the document ID
|
||||
var docID = db.collection('liveTrainData').doc(doc["TrainCode"][0]);
|
||||
batchWrite.set(docID, doc);
|
||||
}
|
||||
});
|
||||
|
||||
batchWrite.commit()
|
||||
.catch((error) => {
|
||||
return false;
|
||||
})
|
||||
})
|
||||
})
|
||||
.catch((error) => {
|
||||
return false;
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// function to populate the Firestore database with live train data from Irish Rail
|
||||
exports.postLiveTrainData = functions.https.onRequest((request, response) => {
|
||||
// helper function to parse train objects
|
||||
function parseJSON(result) {
|
||||
let jsonStr = JSON.stringify(result);
|
||||
let jsonObj = JSON.parse(jsonStr);
|
||||
let jsonData = jsonObj.ArrayOfObjTrainPositions.objTrainPositions;
|
||||
return jsonData;
|
||||
}
|
||||
|
||||
// helper function to write to the database
|
||||
function batchWriteDB(request, response, db, jsonData, trainTypeCode) {
|
||||
response.set('Access-Control-Allow-Origin', '*');
|
||||
response.set('Access-Control-Allow-Credentials', 'true');
|
||||
|
||||
cors(request, response, () => {
|
||||
var batchWrite = db.batch();
|
||||
jsonData.forEach((doc) => {
|
||||
// ignore trains with longitudes or latitudes equal zero
|
||||
if (!(doc["TrainLongitude"] == 0 || doc["TrainLatitude"] == 0)) {
|
||||
doc["TrainType"] = [trainTypeCode]
|
||||
var docID = db.collection('liveTrainData').doc(doc["TrainCode"][0]);
|
||||
batchWrite.set(docID, doc);
|
||||
}
|
||||
});
|
||||
batchWrite.commit()
|
||||
})
|
||||
}
|
||||
|
||||
response.set('Access-Control-Allow-Origin', '*');
|
||||
response.set('Access-Control-Allow-Credentials', 'true');
|
||||
|
||||
cors(request, response, () => {
|
||||
// batch delete all of the "liveTrainData" collections's documents
|
||||
var db = admin.firestore();
|
||||
admin.firestore().collection('liveTrainData').get().then((snapshot) => {
|
||||
var batchDelete = db.batch();
|
||||
snapshot.forEach(doc => {
|
||||
batchDelete.delete(doc.ref);
|
||||
});
|
||||
// fetch mainland trains
|
||||
axios.get('https://api.irishrail.ie/realtime/realtime.asmx/getCurrentTrainsXML_WithTrainType?TrainType=M').then(res => {
|
||||
// XML to JSON
|
||||
parseString(res.data, function(err, result) {
|
||||
let jsonData = parseJSON(result)
|
||||
|
||||
// fetch data using codes M (mainland), S (suburban), D (dart)
|
||||
batchDelete.commit().then(function() {
|
||||
if (callIrishRail(request, response, db, "M") == false ||
|
||||
callIrishRail(request, response, db, "S") == false ||
|
||||
callIrishRail(request, response, db, "D") == false) {
|
||||
response.send({data: "Error fetching data from the IrishRail API"});
|
||||
}
|
||||
response.send({data: "Successfully fetched and uploaded live train data from Irish Rail"});
|
||||
// batch delete all of the liveTrainData collections's documents
|
||||
var db = admin.firestore();
|
||||
admin.firestore().collection('liveTrainData').get().then((snapshot) => {
|
||||
var batchDelete = db.batch();
|
||||
snapshot.forEach(doc => {
|
||||
batchDelete.delete(doc.ref);
|
||||
});
|
||||
|
||||
batchDelete.commit().then(function() {
|
||||
// batch write all station JSON objects to the liveTrainData collection
|
||||
batchWriteDB(request, response, db, jsonData, "M");
|
||||
|
||||
// fetch suburban trains
|
||||
axios.get('https://api.irishrail.ie/realtime/realtime.asmx/getCurrentTrainsXML_WithTrainType?TrainType=S').then(res => {
|
||||
parseString(res.data, function(err, result) {
|
||||
let jsonData = parseJSON(result)
|
||||
batchWriteDB(request, response, db, jsonData, "S");
|
||||
|
||||
// fetch dart trains
|
||||
axios.get('https://api.irishrail.ie/realtime/realtime.asmx/getCurrentTrainsXML_WithTrainType?TrainType=D').then(res => {
|
||||
parseString(res.data, function(err, result) {
|
||||
let jsonData = parseJSON(result)
|
||||
batchWriteDB(request, response, db, jsonData, "D");
|
||||
response.send({data: "Successfully fetched and uploaded live train data from Irish Rail"});
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
1
functions/package-lock.json
generated
1
functions/package-lock.json
generated
@ -9,6 +9,7 @@
|
||||
"axios": "^1.3.3",
|
||||
"chai": "^4.3.7",
|
||||
"chai-http": "^4.3.0",
|
||||
"cors": "^2.8.5",
|
||||
"firebase": "^9.17.1",
|
||||
"firebase-admin": "^11.5.0",
|
||||
"firebase-functions": "^4.2.0",
|
||||
|
@ -17,6 +17,7 @@
|
||||
"axios": "^1.3.3",
|
||||
"chai": "^4.3.7",
|
||||
"chai-http": "^4.3.0",
|
||||
"cors": "^2.8.5",
|
||||
"firebase": "^9.17.1",
|
||||
"firebase-admin": "^11.5.0",
|
||||
"firebase-functions": "^4.2.0",
|
||||
|
@ -6,7 +6,7 @@ const expect = chai.expect;
|
||||
|
||||
describe('Firebase cloud function tests', function() {
|
||||
this.timeout(100000);
|
||||
it('Test getting live train data', async() => {
|
||||
it('Test getting live train data from the database', async() => {
|
||||
const result = await chai.request('https://us-central1-irishrailtracker.cloudfunctions.net')
|
||||
.get('/getLiveTrainData')
|
||||
expect(result.statusCode).to.equal(200);
|
||||
@ -18,6 +18,20 @@ describe('Firebase cloud function tests', function() {
|
||||
expect(result.body.data[0]).haveOwnProperty('TrainLongitude');
|
||||
expect(result.body.data[0]).haveOwnProperty('TrainCode');
|
||||
expect(result.body.data[0]).haveOwnProperty('TrainDate');
|
||||
expect(result.body.data[0]).haveOwnProperty('TrainType');
|
||||
}),
|
||||
|
||||
this.timeout(100000);
|
||||
it('Test getting station data from the database', async() => {
|
||||
const result = await chai.request('https://us-central1-irishrailtracker.cloudfunctions.net')
|
||||
.get('/getStationData')
|
||||
expect(result.statusCode).to.equal(200);
|
||||
expect(result.body.data).to.be.an('Array');
|
||||
expect(result.body.data[0]).haveOwnProperty('StationDesc');
|
||||
expect(result.body.data[0]).haveOwnProperty('StationLatitude');
|
||||
expect(result.body.data[0]).haveOwnProperty('StationLongitude');
|
||||
expect(result.body.data[0]).haveOwnProperty('StationCode');
|
||||
expect(result.body.data[0]).haveOwnProperty('StationId');
|
||||
}),
|
||||
|
||||
this.timeout(100000);
|
||||
@ -27,19 +41,6 @@ describe('Firebase cloud function tests', function() {
|
||||
expect(result.statusCode).to.equal(200);
|
||||
}),
|
||||
|
||||
this.timeout(100000);
|
||||
it('test getting station data', async() => {
|
||||
const result = await chai.request('https://us-central1-irishrailtracker.cloudfunctions.net')
|
||||
.get('/getStationData')
|
||||
expect(result.statusCode).to.equal(200);
|
||||
expect(result.body.data).to.be.an('Array');
|
||||
expect(result.body.data[0]).haveOwnProperty('StationDesc');
|
||||
expect(result.body.data[0]).haveOwnProperty('StationLatitude');
|
||||
expect(result.body.data[0]).haveOwnProperty('StationLongitude');
|
||||
expect(result.body.data[0]).haveOwnProperty('StationCode');
|
||||
expect(result.body.data[0]).haveOwnProperty('StationId');
|
||||
})
|
||||
|
||||
this.timeout(100000);
|
||||
it('Test updating the database with live station data', async() => {
|
||||
const result = await chai.request('https://us-central1-irishrailtracker.cloudfunctions.net')
|
||||
|
Reference in New Issue
Block a user