feat(pbf): read pbf

This commit is contained in:
dancingCycle 2022-09-13 22:59:29 +02:00
parent 575461e037
commit f93734790c
1 changed files with 29 additions and 2 deletions

View File

@ -1,6 +1,8 @@
import React, { useState } from 'react';
import axios from 'axios';
import Pbf from 'pbf';
import Hello from '../components/hello';
import { FeedMessage } from '../../proto2js/js/gtfs-rt.js';
import '../style.css';
const Home = () => {
/*storage*/
@ -10,10 +12,35 @@ const Home = () => {
try {
/*TODO handle errors: https://www.valentinog.com/blog/await-react/*/
let url = 'https://soll.vbn.de/vehicle-positions';
const res = await axios.get(url);
//let url = 'http://localhost:8080/vehicle-positions';
const res = await axios.get(url,
{
responseType: 'arraybuffer'
//responseType: 'blob'
});
//TODO remove debugging
if (res.data) {
console.log('getVehPos() res.data: ', res.data);
//console.log('getVehPos() JSON res: ', JSON.stringify(res));
//console.log('getVehPos() res.data: ', res.data);
const pbf = new Pbf(res.data);
//console.log('getVehPos() JSON pbf: ', JSON.stringify(pbf));
const feed = FeedMessage.read(pbf);
//console.log('getVehPos() feed: ', feed);
//console.log('getVehPos() JSON feed: ', JSON.stringify(feed));
feed.entity.forEach(entity => {
const vehiclePos = entity.vehicle;
if (vehiclePos) {
console.log('getVehPos() vehiclePos available');
const { trip, position, vehicle } = vehiclePos;
if (trip && position && vehicle) {
console.log('getVehPos() trip, position & vehicle available');
} else {
console.error('getVehPos() trip, position & vehicle NOT unavailable ');
}
} else {
console.error('getVehPos() vehiclePos NOT available');
}
});
} else {
console.error('getVehPos() res.data unavailable ');
}