mocking danych

This commit is contained in:
2023-10-01 00:23:17 +02:00
parent 6aec1a75c9
commit f60263c923
3 changed files with 17074 additions and 55 deletions
File diff suppressed because it is too large Load Diff
+2 -3
View File
@@ -2,8 +2,7 @@ import { ScheduledTrain, StopStatus } from '../interfaces/ScheduledTrain';
import Train from '../interfaces/Train';
import TrainStop from '../interfaces/TrainStop';
export const getLocoURL = (locoType: string): string =>
`https://rj.td2.info.pl/dist/img/thumbnails/${locoType.includes('EN') ? locoType + 'rb' : locoType}.png`;
export const getLocoURL = (locoType: string): string => `https://rj.td2.info.pl/dist/img/thumbnails/${locoType.includes('EN') ? locoType + 'rb' : locoType}.png`;
export const getStatusID = (stationStatus: any): string => {
if (!stationStatus) return 'unknown';
@@ -58,7 +57,7 @@ export const getStatusTimestamp = (stationStatus: any): number => {
return -1;
};
export const parseSpawns = (spawnString: string) => {
export const parseSpawns = (spawnString: string | null) => {
if (!spawnString) return [];
if (spawnString === 'NO_SPAWN') return [];
+27 -52
View File
@@ -8,13 +8,7 @@ import Station from '../scripts/interfaces/Station';
import StationRoutes from '../scripts/interfaces/StationRoutes';
import Train from '../scripts/interfaces/Train';
import { URLs } from '../scripts/utils/apiURLs';
import {
getLocoURL,
getStatusTimestamp,
getStatusID,
getScheduledTrain,
parseSpawns,
} from '../scripts/utils/storeUtils';
import { getLocoURL, getStatusTimestamp, getStatusID, getScheduledTrain, parseSpawns } from '../scripts/utils/storeUtils';
import { APIData, StationJSONData, StoreState } from '../scripts/interfaces/store/storeTypes';
import packageInfo from '../../package.json';
@@ -69,10 +63,7 @@ export const useStore = defineStore('store', {
if (!trains) return [];
this.trainList = trains
.filter(
(train) =>
train.region === this.region.id && (train.online || train.timetable || train.lastSeen > Date.now() - 180000)
)
.filter((train) => train.region === this.region.id && (train.online || train.timetable || train.lastSeen > Date.now() - 180000))
.map((train) => {
const stock = train.stockString.split(';');
const locoType = stock ? stock[0] : train.stockString;
@@ -96,9 +87,8 @@ export const useStore = defineStore('store', {
currentStationName: train.currentStationName,
currentStationHash: train.currentStationHash,
connectedTrack: train.connectedTrack,
stockList: stock,
locoType,
locoURL: getLocoURL(locoType),
cars: stock.slice(1),
lastSeen: train.lastSeen,
isTimeout: train.isTimeout,
@@ -125,20 +115,12 @@ export const useStore = defineStore('store', {
getDispatcherStatus(onlineStationData: StationAPIData) {
const { dispatchers } = this.apiData;
const prevDispatcherStatus = this.lastDispatcherStatuses.find(
(dispatcher) => dispatcher.hash === onlineStationData.stationHash
);
const prevDispatcherStatus = this.lastDispatcherStatuses.find((dispatcher) => dispatcher.hash === onlineStationData.stationHash);
const stationStatus = !dispatchers
? undefined
: dispatchers.find(
(status: string[]) => status[0] == onlineStationData.stationHash && status[1] == this.region.id
) || -1;
const stationStatus = !dispatchers ? undefined : dispatchers.find((status: string[]) => status[0] == onlineStationData.stationHash && status[1] == this.region.id) || -1;
const statusTimestamp =
prevDispatcherStatus && !dispatchers ? prevDispatcherStatus.statusTimestamp : getStatusTimestamp(stationStatus);
const statusID =
prevDispatcherStatus && !dispatchers ? prevDispatcherStatus.statusID : getStatusID(stationStatus);
const statusTimestamp = prevDispatcherStatus && !dispatchers ? prevDispatcherStatus.statusTimestamp : getStatusTimestamp(stationStatus);
const statusID = prevDispatcherStatus && !dispatchers ? prevDispatcherStatus.statusID : getStatusID(stationStatus);
return {
hash: onlineStationData.stationHash,
@@ -162,26 +144,17 @@ export const useStore = defineStore('store', {
const stopName = stop.stopNameRAW.toLowerCase();
if (stationName === stopName) return true;
if (stopName.includes(stationName) && !stop.stopName.includes('po.') && !stop.stopName.includes('podg.'))
return true;
if (stopName.includes(stationName) && !stop.stopName.includes('po.') && !stop.stopName.includes('podg.')) return true;
if (stationName.includes(stopName) && !stop.stopName.includes('po.') && !stop.stopName.includes('podg.'))
return true;
if (stationName.includes(stopName) && !stop.stopName.includes('po.') && !stop.stopName.includes('podg.')) return true;
if (
stopName.includes('podg.') &&
stopName.split(', podg.')[0] &&
stationName.includes(stopName.split(', podg.')[0])
)
return true;
if (stopName.includes('podg.') && stopName.split(', podg.')[0] && stationName.includes(stopName.split(', podg.')[0])) return true;
if (
stationGeneralInfo &&
stationGeneralInfo.checkpoints &&
stationGeneralInfo.checkpoints.length > 0 &&
stationGeneralInfo.checkpoints.some((cp) =>
cp.checkpointName.toLowerCase().includes(stop.stopNameRAW.toLowerCase())
)
stationGeneralInfo.checkpoints.some((cp) => cp.checkpointName.toLowerCase().includes(stop.stopNameRAW.toLowerCase()))
)
return true;
@@ -194,9 +167,7 @@ export const useStore = defineStore('store', {
if (stationGeneralInfo?.checkpoints) {
for (const checkpoint of stationGeneralInfo.checkpoints) {
const index = timetable.followingStops.findIndex(
(stop) => stop.stopNameRAW.toLowerCase() == checkpoint.checkpointName.toLowerCase()
);
const index = timetable.followingStops.findIndex((stop) => stop.stopNameRAW.toLowerCase() == checkpoint.checkpointName.toLowerCase());
if (index == -1) continue;
@@ -212,10 +183,7 @@ export const useStore = defineStore('store', {
getStationTrains(stationAPIData: StationAPIData) {
return this.trainList
.filter(
(train) =>
train?.region === this.region.id && train.online && train.currentStationName === stationAPIData.stationName
)
.filter((train) => train?.region === this.region.id && train.online && train.currentStationName === stationAPIData.stationName)
.map((train) => ({
driverName: train.driverName,
driverId: train.driverId,
@@ -305,9 +273,7 @@ export const useStore = defineStore('store', {
routes:
scenery.routesInfo.reduce(
(acc, route) => {
const propName: keyof StationRoutes = `${route.routeTracks == 2 ? 'twoWay' : 'oneWay'}${
route.isElectric ? '' : 'No'
}CatenaryRouteNames`;
const propName: keyof StationRoutes = `${route.routeTracks == 2 ? 'twoWay' : 'oneWay'}${route.isElectric ? '' : 'No'}CatenaryRouteNames`;
acc[route.routeTracks == 2 ? 'twoWay' : 'oneWay'].push({
name: route.routeName,
@@ -336,15 +302,24 @@ export const useStore = defineStore('store', {
twoWayNoCatenaryRouteNames: [],
} as StationRoutes
) || {},
checkpoints: scenery.checkpoints
? scenery.checkpoints.split(';').map((sub) => ({ checkpointName: sub, scheduledTrains: [] }))
: [],
checkpoints: scenery.checkpoints ? scenery.checkpoints.split(';').map((sub) => ({ checkpointName: sub, scheduledTrains: [] })) : [],
},
};
});
},
connectToWebsocket() {
async connectToWebsocket() {
if (import.meta.env.VITE_APP_WS_DEV === '1') {
const mockWebsocketData = await import('../data/mockWebsocketData.json');
this.dataStatuses.connection = DataStatus.Loaded;
this.apiData = mockWebsocketData as any;
this.setOnlineData();
console.warn('Stacjownik działa w trybie mockowania danych z WS');
return;
}
const socket = io(URLs.stacjownikAPI, {
// transports: ['websocket', 'polling'],
rememberUpgrade: true,