Restrukturyzacja pobierania danych, zmiany w wyglądzie

This commit is contained in:
2020-09-10 21:25:10 +02:00
parent 5adb990361
commit 4ef90e655d
17 changed files with 553 additions and 225 deletions
+5 -6
View File
@@ -1,13 +1,12 @@
import Vue from "vue";
import Vuex from "vuex";
import Vue from 'vue';
import Vuex from 'vuex';
import Store from '@/store/store';
import StationsModule from "@/store/modules/stationsModule";
import TrainsModule from "@/store/modules/trainsModule";
Vue.use(Vuex);
const store = new Vuex.Store({
modules: {
StationsModule,
TrainsModule,
Store,
},
});
export default store;
+13 -41
View File
@@ -4,11 +4,9 @@ import axios from 'axios';
import data from '@/data/stations.json';
import Station from '@/scripts/interfaces/Station';
const stationsOnlineURL =
'https://api.td2.info.pl:9640/?method=getStationsOnline';
const stationsOnlineURL = 'https://api.td2.info.pl:9640/?method=getStationsOnline';
const trainsOnlineURL = 'https://api.td2.info.pl:9640/?method=getTrainsOnline';
const dispatchersOnlineURL =
'https://api.td2.info.pl:9640/?method=readFromSWDR&value=getDispatcherStatusList%3B1';
const dispatchersOnlineURL = 'https://api.td2.info.pl:9640/?method=readFromSWDR&value=getDispatcherStatusList%3B1';
enum ConnState {
Loading = 0,
@@ -17,8 +15,7 @@ enum ConnState {
}
interface TimetableResponseData {
stopPoints:
{
stopPoints: {
arrivalTime: string;
arrivalDelay: number;
departureTime: string;
@@ -57,10 +54,6 @@ let onlineTrainsData: {
};
}[];
const queryStations = axios.get(stationsOnlineURL);
const queryTrains = axios.get(trainsOnlineURL);
const queryDispatchers = axios.get(dispatchersOnlineURL);
const getStationLabel = (stationStatus: any) => {
if (!stationStatus) return 'NIEZALOGOWANY';
@@ -97,9 +90,7 @@ const getStationLabel = (stationStatus: any) => {
const getOpenSpawns = (spawnString: string) => {
if (!spawnString) return '';
return spawnString
.split(';')
.map(v => (v.split(',')[6] ? v.split(',')[6] : v.split(',')[0]));
return spawnString.split(';').map(v => (v.split(',')[6] ? v.split(',')[6] : v.split(',')[0]));
};
@Module
@@ -134,9 +125,7 @@ export default class StationsModule extends VuexModule {
@Mutation
private updateStations(updatedStations) {
this.stations = this.stations.reduce((acc, station) => {
const onlineStationData = updatedStations.find(
uStation => uStation.stationName === station.stationName
);
const onlineStationData = updatedStations.find(uStation => uStation.stationName === station.stationName);
if (!onlineStationData) {
acc.push({
@@ -152,7 +141,6 @@ export default class StationsModule extends VuexModule {
dispatcherId: 0,
occupiedTo: 'WOLNA',
statusTimestamp: 0,
scheduledTrains: [],
online: false,
});
@@ -174,9 +162,7 @@ export default class StationsModule extends VuexModule {
// Dodawanie do listy online potencjalnych scenerii niewpisanych do bazy
updatedStations.forEach((updated: any) => {
const alreadyInList: any = this.stations.find(
station => station.stationName === updated.stationName
);
const alreadyInList: any = this.stations.find(station => station.stationName === updated.stationName);
if (!alreadyInList) {
this.stations.push({
@@ -189,9 +175,7 @@ export default class StationsModule extends VuexModule {
this.stationCount = this.stations.filter(station => station.online).length;
this.trainCount = onlineTrainsData.filter(
train => train.isOnline && train.region === 'eu'
).length;
this.trainCount = onlineTrainsData.filter(train => train.isOnline && train.region === 'eu').length;
this.stationsConnectionState = ConnState.Connected;
}
@@ -233,11 +217,7 @@ export default class StationsModule extends VuexModule {
commit: 'updateStations',
})
async fetchOnlineStations() {
return await Promise.all([
axios.get(stationsOnlineURL),
axios.get(trainsOnlineURL),
axios.get(dispatchersOnlineURL),
])
return await Promise.all([axios.get(stationsOnlineURL), axios.get(trainsOnlineURL), axios.get(dispatchersOnlineURL)])
.then(async response => {
onlineStationsData = response[0].data.message;
onlineTrainsData = await response[1].data.message;
@@ -247,22 +227,13 @@ export default class StationsModule extends VuexModule {
onlineStationsData
.filter(station => station.region === 'eu' && station.isOnline)
.map(async station => {
const stationStatus = onlineDispatchersData.find(
status => status[0] == station.stationHash && status[1] == 'eu'
);
const stationStatus = onlineDispatchersData.find(status => status[0] == station.stationHash && status[1] == 'eu');
const statusLabel = getStationLabel(stationStatus);
const statusTimestamp = stationStatus ? stationStatus[3] : -1;
const trains = onlineTrainsData.filter(
train =>
train.region === 'eu' &&
train.isOnline &&
train.station.stationName === station.stationName
);
const trains = onlineTrainsData.filter(train => train.region === 'eu' && train.isOnline && train.station.stationName === station.stationName);
const stationData = data.find(
s => s.stationName === station.stationName
) || {
const stationData = data.find(s => s.stationName === station.stationName) || {
stationName: station.stationName,
stationURL: '',
};
@@ -285,8 +256,9 @@ export default class StationsModule extends VuexModule {
);
return updatedStations;
})
.catch(() => {
.catch(err => {
this.context.commit('setConnectionState', ConnState.Error);
console.log(err);
});
}
}
+9 -26
View File
@@ -65,13 +65,9 @@ interface TimetableData {
const getTimestamp = (date: string) => (date ? new Date(date).getTime() : 0);
const getTimetableURL = (trainNo: number) =>
`https://api.td2.info.pl:9640/?method=readFromSWDR&value=getTimetable%3B${trainNo}%3Beu`;
const getTimetableURL = (trainNo: number) => `https://api.td2.info.pl:9640/?method=readFromSWDR&value=getTimetable%3B${trainNo}%3Beu`;
const getLocoURL = (locoType: string) =>
`https://rj.td2.info.pl/dist/img/thumbnails/${
locoType.includes('EN') ? locoType + 'rb' : locoType
}.png`;
const getLocoURL = (locoType: string) => `https://rj.td2.info.pl/dist/img/thumbnails/${locoType.includes('EN') ? locoType + 'rb' : locoType}.png`;
@Module
export default class TrainsModule extends VuexModule {
@@ -93,17 +89,12 @@ export default class TrainsModule extends VuexModule {
return await Promise.all(
onlineTrainsData.map(async train => {
const timetableResponseData: TimetableResponseData | null = (
await axios.get(getTimetableURL(train.trainNo))
).data.message;
const timetableResponseData: TimetableResponseData | null = (await axios.get(getTimetableURL(train.trainNo))).data.message;
let timetableData: TimetableData | null = null;
if (timetableResponseData && timetableResponseData.trainInfo) {
const routeDistance: number =
timetableResponseData.stopPoints[
timetableResponseData.stopPoints.length - 1
].pointDistance;
const routeDistance: number = timetableResponseData.stopPoints[timetableResponseData.stopPoints.length - 1].pointDistance;
timetableData = {
...timetableResponseData.trainInfo,
@@ -112,18 +103,12 @@ export default class TrainsModule extends VuexModule {
};
}
const locoType = train.dataCon.split(';')
? train.dataCon.split(';')[0]
: train.dataCon;
const locoType = train.dataCon.split(';') ? train.dataCon.split(';')[0] : train.dataCon;
const followingStops = timetableResponseData?.stopPoints.reduce(
(acc, point) => {
const stopObj: any = {};
if (
!point.pointName.includes('Południowy') &&
(point.pointName.includes('strong') ||
point.pointName.includes('podg.'))
) {
if (!point.pointName.includes('Południowy') && (point.pointName.includes('strong') || point.pointName.includes('podg.'))) {
if (point.pointName.includes('strong')) {
stopObj.stopName = point.pointNameRAW;
@@ -137,13 +122,11 @@ export default class TrainsModule extends VuexModule {
stopObj.departureTime = getTimestamp(point.departureTime);
stopObj.arrivalDelay = point.arrivalDelay;
stopObj.departureDelay = point.departureDelay;
stopObj.beginsHere =
getTimestamp(point.arrivalTime) == 0 ? true : false;
stopObj.terminatesHere =
getTimestamp(point.departureTime) == 0 ? true : false;
stopObj.beginsHere = getTimestamp(point.arrivalTime) == 0 ? true : false;
stopObj.terminatesHere = getTimestamp(point.departureTime) == 0 ? true : false;
stopObj.confirmed = point.confirmed;
stopObj.stopped = point.stopped;
stopObj.stopTime = point.pointStopTime;
stopObj.currentStationName = train.station.stationName;
acc.push(stopObj);
}
+361
View File
@@ -0,0 +1,361 @@
import { Module, VuexModule, Mutation, Action } from 'vuex-module-decorators';
import axios from 'axios';
import JSONStationData from '@/data/stations.json';
import Station from '@/scripts/interfaces/Station';
import Train from '@/scripts/interfaces/Train';
import Timetable from '@/scripts/interfaces/Timetable';
enum Status {
Loading = 0,
Error = 1,
Loaded = 2,
}
const URLs = {
stations: 'https://api.td2.info.pl:9640/?method=getStationsOnline',
trains: 'https://api.td2.info.pl:9640/?method=getTrainsOnline',
dispatchers: 'https://api.td2.info.pl:9640/?method=readFromSWDR&value=getDispatcherStatusList%3B1',
};
const timetableURL = (trainNo: number) => `https://api.td2.info.pl:9640/?method=readFromSWDR&value=getTimetable%3B${trainNo}%3Beu`;
const getLocoURL = (locoType: string) => `https://rj.td2.info.pl/dist/img/thumbnails/${locoType.includes('EN') ? locoType + 'rb' : locoType}.png`;
const getStationLabel = (stationStatus: any) => {
if (!stationStatus) return 'NIEZALOGOWANY';
const statusCode = stationStatus[2];
const statusTimestamp = stationStatus[3];
switch (statusCode) {
case 0:
if (statusTimestamp - Date.now() > 21000000) return 'BEZ LIMITU';
return `DO ${new Date(statusTimestamp).toLocaleTimeString('en-US', {
hour12: false,
hour: '2-digit',
minute: '2-digit',
})}`;
case 1:
return 'Z/W';
case 2:
if (statusTimestamp == 0) return 'KOŃCZY';
break;
case 3:
return 'BRAK MIEJSCA';
default:
break;
}
return 'NIEDOSTĘPNY';
};
const getOpenSpawns = (spawnString: string) => (spawnString ? spawnString.split(';').map(v => (v.split(',')[6] ? v.split(',')[6] : v.split(',')[0])) : '');
const getTimestamp = (date: string) => (date ? new Date(date).getTime() : 0);
@Module
export default class Store extends VuexModule {
private trainCount: number = 0;
private stationCount: number = 0;
// private stationsConnectionStatus: Status = Status.Loading;
// private trainsConnectionStatus: Status = Status.Loading;
// private dataConnectionStatus: Status = Status.Loading;
private dataConnectionStatus: Status = Status.Loading;
private stationList: Station[] = [];
private trainList: Train[] = [];
//GETTERS
get getAllData() {
return {
stationList: this.stationList,
trainList: this.trainList,
trainCount: this.trainCount,
stationCount: this.stationCount,
dataConnectionStatus: this.dataConnectionStatus,
};
}
get getStationList() {
return this.stationList;
}
get getTrainList() {
return this.trainList;
}
//ACTIONS
@Action
async synchronizeData() {
this.context.commit('setJSONData');
this.context.dispatch('fetchOnlineData');
setInterval(() => this.context.dispatch('fetchOnlineData'), 20000);
}
@Action({ commit: 'updateTimetableData' })
async fetchTimetableData() {
return await Promise.all(
this.trainList.map(async train => {
const timetable = await (await axios.get(timetableURL(train.trainNo))).data.message;
const trainInfo = timetable.trainInfo;
let timetableData;
if (timetable && trainInfo) {
timetableData = {};
const followingStops = timetable.stopPoints.reduce((acc, point) => {
const stopObj: any = {};
if (!point.pointName.includes('Południowy') && (point.pointName.includes('strong') || point.pointName.includes('podg.'))) {
if (point.pointName.includes('strong')) {
stopObj.stopName = point.pointNameRAW;
stopObj.stopType = point.pointStopType;
} else {
stopObj.stopName = point.pointNameRAW.split(',')[0];
stopObj.stopType = 'podg.';
}
stopObj.arrivalTime = getTimestamp(point.arrivalTime);
stopObj.departureTime = getTimestamp(point.departureTime);
stopObj.arrivalDelay = point.arrivalDelay;
stopObj.departureDelay = point.departureDelay;
stopObj.beginsHere = getTimestamp(point.arrivalTime) == 0 ? true : false;
stopObj.terminatesHere = getTimestamp(point.departureTime) == 0 ? true : false;
stopObj.confirmed = point.confirmed;
stopObj.stopped = point.stopped;
acc.push(stopObj);
}
return acc;
}, []);
timetableData = {
trainNo: train.trainNo,
driverName: train.driverName,
driverId: train.driverId,
currentStationName: train.currentStationName,
timetableId: trainInfo.timetableId,
category: trainInfo.trainCategoryCode,
route: trainInfo.route,
TWR: trainInfo.twr,
SKR: trainInfo.skr,
routeDistance: timetable.stopPoints[timetable.stopPoints.length - 1].pointDistance,
followingStops,
};
}
return timetableData;
})
);
}
@Action
async fetchOnlineData() {
Promise.all([axios.get(URLs.stations), axios.get(URLs.trains), axios.get(URLs.dispatchers)])
.then(async response => {
const onlineStationsData = response[0].data.message;
const onlineTrainsData = await response[1].data.message;
const onlineDispatchersData = await response[2].data.message;
let updatedStationList = onlineStationsData.reduce((acc, station) => {
if (station.region !== 'eu' || !station.isOnline) return acc;
const stationStatus = onlineDispatchersData.find(status => status[0] == station.stationHash && status[1] == 'eu');
const statusLabel = getStationLabel(stationStatus);
const statusTimestamp = stationStatus ? stationStatus[3] : -1;
const stationTrains = onlineTrainsData.filter(train => train.region === 'eu' && train.isOnline && train.station.stationName === station.stationName);
acc.push({
stationName: station.stationName,
stationHash: station.stationHash,
maxUsers: station.maxUsers,
currentUsers: station.currentUsers,
spawnString: getOpenSpawns(station.spawnString),
dispatcherName: station.dispatcherName,
dispatcherRate: station.dispatcherRate,
dispatcherId: station.dispatcherId,
dispatcherExp: station.dispatcherExp,
occupiedTo: statusLabel,
stationTrains,
statusTimestamp,
});
return acc;
}, []);
let updatedTrainList = await Promise.all(
onlineTrainsData
.filter(train => train.region === 'eu')
.map(async train => {
const locoType = train.dataCon.split(';') ? train.dataCon.split(';')[0] : train.dataCon;
return {
trainNo: train.trainNo,
mass: train.dataMass,
length: train.dataLength,
speed: train.dataSpeed,
distance: train.dataDistance,
signal: train.dataSignal,
online: train.isOnline,
driverId: train.driverId,
driverName: train.driverName,
currentStationName: train.station.stationName,
connectedTrack: train.dataSceneryConnection,
locoType,
locoURL: getLocoURL(locoType),
};
})
);
this.context.commit('updateOnlineStations', updatedStationList);
this.context.commit('updateOnlineTrains', updatedTrainList);
this.context.dispatch('fetchTimetableData');
})
.catch(err => {
this.context.commit('setDataConnectionStatus', Status.Error);
});
}
//MUTATIONS
@Mutation
private setDataConnectionStatus(status: Status) {
this.dataConnectionStatus = status;
}
@Mutation setJSONData() {
this.stationList = JSONStationData.map(stationData => ({
stationProject: '',
spawnString: '',
stationHash: '',
maxUsers: 0,
currentUsers: 0,
dispatcherName: '',
dispatcherRate: 0,
dispatcherExp: -1,
dispatcherId: 0,
online: false,
occupiedTo: 'WOLNA',
statusTimestamp: 0,
stationTrains: [],
scheduledTrains: [],
...stationData,
}));
}
@Mutation
private updateOnlineStations(updatedStationList: any[]) {
this.stationList = this.stationList.reduce((acc, station) => {
const onlineStationData = updatedStationList.find(updatedStation => updatedStation.stationName === station.stationName);
if (!onlineStationData) {
acc.push({
...station,
stationProject: '',
spawnString: '',
stationHash: '',
maxUsers: 0,
currentUsers: 0,
dispatcherName: '',
dispatcherRate: 0,
dispatcherExp: -1,
dispatcherId: 0,
occupiedTo: 'WOLNA',
statusTimestamp: 0,
online: false,
});
} else
acc.push({
...station,
...onlineStationData,
online: true,
});
return acc;
}, [] as Station[]);
// Dodawanie do listy online potencjalnych scenerii niewpisanych do bazy
updatedStationList.forEach(updatedStation => {
const alreadyInList: any = this.stationList.find(station => station.stationName === updatedStation.stationName);
if (!alreadyInList) {
this.stationList.push({
...updatedStation,
online: true,
reqLevel: '-1',
});
}
});
this.stationCount = this.stationList.filter(station => station.online).length;
this.dataConnectionStatus = Status.Loaded;
}
@Mutation
private updateOnlineTrains(updatedTrainList) {
this.trainList =
this.trainList.length == 0
? updatedTrainList
: this.trainList.reduce((acc, train) => {
const onlineTrainData = updatedTrainList.find(updatedTrain => train.trainNo === updatedTrain.trainNo);
if (onlineTrainData) acc.push({ ...train, ...onlineTrainData });
return acc;
}, [] as Train[]);
this.trainCount = this.trainList.filter(train => train.online).length;
this.dataConnectionStatus = Status.Loaded;
}
@Mutation
private updateTimetableData(timetableList: any[]) {
this.stationList = this.stationList.map(station => {
const scheduledTrains = timetableList.reduce((acc, timetableData: any) => {
const scheduledIndex = timetableData
? timetableData.followingStops.findIndex(
(stop: any) =>
station.stationName.toLowerCase().includes(stop.stopName) ||
station.stationName.toLowerCase().includes(stop.stopName.toLowerCase().split(',')[0]) ||
(station.stationName.toLowerCase().includes(stop.stopName.toLowerCase().split(' ')[0]) && station.stationName.toLowerCase().includes('lcs'))
)
: -1;
if (scheduledIndex >= 0) {
const scheduledData = timetableData.followingStops[scheduledIndex];
acc.push({
...scheduledData,
trainNo: timetableData.trainNo,
driverName: timetableData.driverName,
driverId: timetableData.driverId,
currentStationName: timetableData.currentStationName,
});
}
return acc;
}, []);
return { ...station, scheduledTrains };
});
this.trainList = this.trainList.reduce((acc, train) => {
const timetableData = timetableList.find(data => data && data.trainNo === train.trainNo);
if (timetableData) acc.push({ ...train, timetableData });
return acc;
}, [] as Train[]);
}
}