Giter VIP home page Giter VIP logo

Comments (4)

dancesWithCycles avatar dancesWithCycles commented on June 28, 2024 1

Hi folks,
I found the error and solution myself. Anyhow, I am still wondering how can I verify that encoding finished successful without decoding the buffer and looking at the message content. Does anyone has a little advice for me?

Here is the code example:

const GtfsRealtimeBindings = require('gtfs-realtime-bindings');
const debug = require('debug')('gtfs');

var feedHeader = GtfsRealtimeBindings.transit_realtime.FeedHeader.create({
    gtfsRealtimeVersion:'2.0'
});

var pos = GtfsRealtimeBindings.transit_realtime.Position.create({
    latitude:36,longitude:-79});

var vehiclePosition = GtfsRealtimeBindings.transit_realtime.VehiclePosition.create({
position:pos});

var feedEntity = GtfsRealtimeBindings.transit_realtime.FeedEntity.create({
    id:'uuid-foo-bar',vehicle:vehiclePosition
});

var feedMessage = GtfsRealtimeBindings.transit_realtime.FeedMessage.create({
    header:feedHeader,
    entity:[feedEntity]
});

var errMsg = GtfsRealtimeBindings.transit_realtime.FeedMessage.verify(feedMessage);
if (errMsg){
    debug('feedMessage invalid')
    throw Error(errMsg);
}else{
    debug('feedMessage valid')
}

debug("JSON: %s", JSON.stringify(feedMessage));

var encodedFeedMsg = GtfsRealtimeBindings.transit_realtime.FeedMessage.encode(feedMessage).finish();
debug('feedMessage encoded')
var decodedFeedMsg = GtfsRealtimeBindings.transit_realtime.FeedMessage.decode(encodedFeedMsg);
debug('feedMessage decoded')
debug("JSON: %s", JSON.stringify(decodedFeedMsg));

And here is the respective CLI output:

[nodemon] starting `node index.js`
  gtfs feedMessage valid +0ms
  gtfs JSON: {"header":{"gtfsRealtimeVersion":"2.0"},"entity":[{"id":"uuid-foo-bar","vehicle":{"position":{"latitude":36,"longitude":-79}}}]} +4ms
  gtfs feedMessage encoded +3ms
  gtfs feedMessage decoded +1ms
  gtfs JSON: {"header":{"gtfsRealtimeVersion":"2.0"},"entity":[{"id":"uuid-foo-bar","vehicle":{"position":{"latitude":36,"longitude":-79}}}]} +0ms
[nodemon] clean exit - waiting for changes before restart

from gtfs-realtime-bindings.

dancesWithCycles avatar dancesWithCycles commented on June 28, 2024

Hi folks,
my next try ended in a message that is valid according to the library but can not be encoded.

Source code:

const GtfsRealtimeBindings = require('gtfs-realtime-bindings');
const debug = require('debug')('gtfs');

var feedHeader = GtfsRealtimeBindings.transit_realtime.FeedHeader.create(); // or use .fromObject if conversion is necessary
debug('feedHeader created')

feedHeader.gtfs_realtime_version="2.0"

var feedMessage = GtfsRealtimeBindings.transit_realtime.FeedMessage.create(); // or use .fromObject if conversion is necessary
debug('feedMessage created')

feedMessage.header=feedHeader;

var feedEntity = GtfsRealtimeBindings.transit_realtime.FeedEntity.create(); // or use .fromObject if conversion is necessary
debug('feedEntity created')

var vehiclePosition = GtfsRealtimeBindings.transit_realtime.VehiclePosition.create(); // or use .fromObject if conversion is necessary
debug('vehiclePosition created')

var pos = GtfsRealtimeBindings.transit_realtime.Position.create(); // or use .fromObject if conversion is necessary
debug('pos created')

pos.latitude=36;
pos.longitude=-79;

var errMsg = GtfsRealtimeBindings.transit_realtime.Position.verify(pos);
if (errMsg){
    debug('pos invalid')
    throw Error(errMsg);
}else{
    debug('pos valid')
}

vehiclePosition.position=pos;
feedEntity.vehicle=vehiclePosition;
feedMessage.entity=[feedEntity];

var errMsg = GtfsRealtimeBindings.transit_realtime.FeedMessage.verify(feedMessage);
if (errMsg){
    debug('feedMessage invalid')
    throw Error(errMsg);
}else{
    debug('feedMessage valid')
}

var errMsg = GtfsRealtimeBindings.transit_realtime.FeedMessage.encode(feedMessage);
if (errMsg){
    debug('encode error: %s',errMsg.toString())
    throw Error(errMsg);
}else{
    debug('encode success')
}

Error:

[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
  gtfs feedHeader created +0ms
  gtfs feedHeader valid +3ms
  gtfs feedMessage created +1ms
  gtfs feedMessage valid +0ms
  gtfs feedEntity created +0ms
  gtfs vehiclePosition created +0ms
  gtfs vehiclePosition valid +0ms
  gtfs pos created +1ms
  gtfs pos valid +0ms
  gtfs feedEntity valid +0ms
  gtfs feedMessage valid +0ms
  gtfs encode error: [object Object] +2ms
/home/user/Desktop/sandbox/gtfsRtBindingsEncode/index.js:82
    throw Error(errMsg);
    ^

Error: [object Object]
    at Object.<anonymous> (/home/user/Desktop/sandbox/gtfsRtBindingsEncode/index.js:82:11)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
[nodemon] app crashed - waiting for file changes before starting...

Do you guys have any clue what is the reason for the error?

Cheers!

from gtfs-realtime-bindings.

jameslinjl avatar jameslinjl commented on June 28, 2024

@dancesWithCycles Protobufs are simply a way to serialize data (typically into a packed, binary format), so a successful encoding output is only verifiable by performing a corresponding decode on that encoded message. If you wanted to ensure that the output is truly language and platform neutral, you could take the resulting output and deserialize it using another language. The expectation is that the encoded message could still be decoded.

from gtfs-realtime-bindings.

jameslinjl avatar jameslinjl commented on June 28, 2024

proto3 version does have a canonical JSON format (as opposed to binary) for serialization, which can help with debugging and triaging these sorts of issues via manual human inspection, but GTFS-Realtime currently uses the proto2 version, so that isn't applicable here

from gtfs-realtime-bindings.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.