Search in sources :

Example 1 with NodesAndTimestamp

use of org.onebusaway.transit_data_federation.impl.realtime.SiriLikeRealtimeSource.NodesAndTimestamp in project onebusaway-application-modules by camsys.

the class SiriLikeRealtimeSourceTest method testParseRoute2Multi.

// @Test
public void testParseRoute2Multi() throws Exception {
    URL url = getClass().getResource("SiriLike_2_multi.xml").toURI().toURL();
    NodesAndTimestamp vehicles = source.parseVehicles(url);
    assertNotNull(vehicles);
    assertEquals(4, vehicles.getNodes().size());
    // as we don't have predictions, there isn't much we can do with this
    VehicleLocationRecord vlr = source.parse(vehicles.getNodes().get(0), 0);
    assertNotNull(vlr);
    assertEquals("UTA", vlr.getTripId().getAgencyId());
    assertEquals("2726063", vlr.getTripId().getId());
    assertNull(vlr.getBlockId());
    assertEquals(1470204000000l, vlr.getServiceDate());
    assertEquals("13034", vlr.getVehicleId().getId());
    assertEquals(40.76492, vlr.getCurrentLocationLat(), 0.0001);
    assertEquals(-111.8756, vlr.getCurrentLocationLon(), 0.0001);
    assertEquals(-2.0, vlr.getScheduleDeviation(), 0.01);
}
Also used : VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord) URL(java.net.URL) NodesAndTimestamp(org.onebusaway.transit_data_federation.impl.realtime.SiriLikeRealtimeSource.NodesAndTimestamp)

Example 2 with NodesAndTimestamp

use of org.onebusaway.transit_data_federation.impl.realtime.SiriLikeRealtimeSource.NodesAndTimestamp in project onebusaway-application-modules by camsys.

the class SiriLikeRealtimeSourceTest method testParseEmpty.

@Test
public void testParseEmpty() throws Exception {
    URL url = getClass().getResource("SiriLike_Empty.xml").toURI().toURL();
    NodesAndTimestamp vehicles = source.parseVehicles(url);
    assertNotNull(vehicles);
    assertEquals(0, vehicles.getNodes().size());
}
Also used : URL(java.net.URL) NodesAndTimestamp(org.onebusaway.transit_data_federation.impl.realtime.SiriLikeRealtimeSource.NodesAndTimestamp) Test(org.junit.Test)

Example 3 with NodesAndTimestamp

use of org.onebusaway.transit_data_federation.impl.realtime.SiriLikeRealtimeSource.NodesAndTimestamp in project onebusaway-application-modules by camsys.

the class SiriLikeRealtimeSourceTest method testParseRoute2.

// @Test
public void testParseRoute2() throws Exception {
    URL url = getClass().getResource("SiriLike_2.xml").toURI().toURL();
    NodesAndTimestamp vehicles = source.parseVehicles(url);
    assertNotNull(vehicles);
    assertEquals(1, vehicles.getNodes().size());
    // as we don't have predictions, there isn't much we can do with this
    VehicleLocationRecord vlr = source.parse(vehicles.getNodes().get(0), 0);
    assertNotNull(vlr);
    assertEquals("UTA", vlr.getTripId().getAgencyId());
    assertEquals("2726094", vlr.getTripId().getId());
    assertNull(vlr.getBlockId());
    assertEquals(1470204000000l, vlr.getServiceDate());
    assertEquals("07011", vlr.getVehicleId().getId());
    assertEquals(40.76589, vlr.getCurrentLocationLat(), 0.0001);
    assertEquals(-111.90987, vlr.getCurrentLocationLon(), 0.0001);
    assertEquals(-2.0, vlr.getScheduleDeviation(), 0.01);
}
Also used : VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord) URL(java.net.URL) NodesAndTimestamp(org.onebusaway.transit_data_federation.impl.realtime.SiriLikeRealtimeSource.NodesAndTimestamp)

Example 4 with NodesAndTimestamp

use of org.onebusaway.transit_data_federation.impl.realtime.SiriLikeRealtimeSource.NodesAndTimestamp in project onebusaway-application-modules by camsys.

the class SiriLikeRealtimeSource method parseVehicles.

// process URL into a series of fragments representing vehicle activity
public NodesAndTimestamp parseVehicles(URL url) throws Exception {
    List<Node> vehicles = new ArrayList<Node>();
    Document doc = builder.parse(url.toString());
    String recordedAtStr = (String) recordedAtExpression.evaluate(doc, XPathConstants.STRING);
    long timestamp = parseDate(recordedAtStr).getTime();
    _log.debug("timestamp=" + new Date(timestamp) + " for date " + recordedAtStr);
    NodeList nl = (NodeList) this.mvjExpression.evaluate(doc, XPathConstants.NODESET);
    if (nl == null || nl.getLength() == 0) {
        _log.debug("no nodes found");
        return new NodesAndTimestamp(vehicles, timestamp);
    }
    for (int i = 0; i < nl.getLength(); i++) {
        vehicles.add(nl.item(i));
    }
    return new NodesAndTimestamp(vehicles, timestamp);
}
Also used : Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) Date(java.util.Date) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) NodesAndTimestamp(org.onebusaway.transit_data_federation.impl.realtime.SiriLikeRealtimeSource.NodesAndTimestamp) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint)

Example 5 with NodesAndTimestamp

use of org.onebusaway.transit_data_federation.impl.realtime.SiriLikeRealtimeSource.NodesAndTimestamp in project onebusaway-application-modules by camsys.

the class SiriLikeRealtimeSource method handleUpdates.

private synchronized void handleUpdates(MonitoredResult result) throws Exception {
    int vehicles = 0;
    int trips = 0;
    for (String route : getRoutes()) {
        NodesAndTimestamp nodesAndTimestamp = parseVehicles(new URL(constructUrl(route, getApiKey(), getUrl())));
        _log.debug("found " + nodesAndTimestamp.getNodes().size() + " nodes");
        for (Node n : nodesAndTimestamp.getNodes()) {
            vehicles++;
            VehicleLocationRecord vlr = parse(n, nodesAndTimestamp.getTimestamp());
            if (vlr != null) {
                trips++;
                result.addMatchedTripId(vlr.getTripId().getId());
                try {
                    _vehicleLocationListener.handleVehicleLocationRecord(vlr);
                } catch (Exception any) {
                // bury
                }
            }
        }
    }
    _log.info("updated " + trips + " of " + vehicles + " for agency " + getAgency());
}
Also used : Node(org.w3c.dom.Node) VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) NodesAndTimestamp(org.onebusaway.transit_data_federation.impl.realtime.SiriLikeRealtimeSource.NodesAndTimestamp) URL(java.net.URL)

Aggregations

NodesAndTimestamp (org.onebusaway.transit_data_federation.impl.realtime.SiriLikeRealtimeSource.NodesAndTimestamp)5 URL (java.net.URL)4 VehicleLocationRecord (org.onebusaway.realtime.api.VehicleLocationRecord)3 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)2 Node (org.w3c.dom.Node)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Test (org.junit.Test)1 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)1 Document (org.w3c.dom.Document)1 NodeList (org.w3c.dom.NodeList)1