These JavaScript code-samples cannot be run directly in a browser (because of cross-site-scripting issue). You can run these examples in node.js copying the following code-snippets in a file test.js and calling "node test.js <your_token>" on the command line.
Alternately you can install a proxy (i.e. Apache with forward- and reverse-proxy module mod_proxy) and run these examples slightly modified in a browser.
Important note: You may not use this example in a production environment. This example is only for demonstration purpose.
Use the following code to create a tour:
console.log("### Start test CreateTour"); var token = process.argv[2]; console.log("token=" + token); // define a trip with two stops var tourData = { "tour": { "stops": [ { "coordinate": { "locationX": 8.61638888888889, "locationY": 49.81833333333333 }, "earliestArrivalTime": "2013-03-07T08:00:00.000+01:00", "latestDepartureTime": "2013-03-07T08:00:00.000+01:00", "serviceTimeAtStop": 0, "useServicePeriodForRecreation": true, "weightWhenLeavingStop": "6908,272" }, { "coordinate": { "locationX": 8.565, "locationY": 49.966111111111104 }, "earliestArrivalTime": "2013-03-07T08:29:08.000+01:00", "latestDepartureTime": "2013-03-07T08:44:08.000+01:00", "serviceTimeAtStop": 900, "useServicePeriodForRecreation": true, "weightWhenLeavingStop": "6835,772" } ], "vehicle": { "vehicleProfileID": "mg-truck-40t" }, "drivers": [ { "driverDescription": "driver 1", "raSTime": { "periodDrivenSinceLastBreak": 0, "periodDrivenSinceLastRest": 0, "currentIdlePeriod": 0, "isSplitBreak": false, "isSplitRest": false } } ], "customData": { "user": "node.js_test", "myid": "1" } } }; // print tour object on console for reference console.log(); console.log(JSON.stringify(tourData)); console.log(); // display response as JSON when response for Tour Create comes back var callbackTourCreated = function (error, response, body) { console.log(JSON.stringify(body,null,1)); }; // create request object in node (if not available, try "npm install request" in command line before) var request = require('request'); var options = { url: 'https://driveandarrive-v1.cloud.ptvgroup.com/em/tour?source=PTVNODEJSCS&token=' + token, method: 'POST', json: tourData, headers: { "content-type":"application/json" } }; // send Tour Create request request(options, callbackTourCreated);
You will get your request as a response, but enriched with the concerning SCEMIDs per stop and per Tour.
Please create a public URL with http://requestb.in/ before. It's a tool to receive HTTP requests in order to analyze them. Put the URL in the property notificationDescription.callBackURL. The requestb.in URL will look similiar to this one which is used in the sample: http://requestb.in/1cifoit1
Use the following code to create a subscription:
console.log("### Start test CreateSubscription"); var stopID = process.argv[2]; console.log("stopID=" + stopID); // create subscription for a callback URL. // callback URL target is request.bin // please refer to online documentation for this sample to understand the requestb.in URL var subscriptionData = { "subscription": { "notificationType": "CALLBACK_URL", "notificationDescription": { "callBackURL":"http://requestb.in/1cifoit1", "ident": 'test'+stopID }, "scemid": stopID } }; // print json of request to console for reference console.log(); console.log("data: "+JSON.stringify(subscriptionData)); console.log(); // display response as JSON when response for Tour Create comes back var callbackSubscriptionCreated = function (error, response, body) { console.log(JSON.stringify(body,null,1)); }; // create request object in node (if not available, try "npm install request" in command line before) var request = require('request'); var options = { url: 'https://driveandarrive-v1.cloud.ptvgroup.com/em/subscription?source=PTVNODEJSCS', method: 'POST', json: subscriptionData, headers: { "content-type":"application/json" } }; // send Tour Create request request(options, callbackSubscriptionCreated);
You will get a SUCCESS message by the API. Now you can watch the requestb.in URL that you defined as callback URL in order to check ETA changes, for this example: http://requestb.in/1cifoit1?inspect
Use the following code to create an event:
console.log("### Start test CreateEvent"); var stopID = process.argv[2]; console.log("stopID=" + stopID); // define a positioning event var eventData = { "event": { "eventType": "POSITION", "eventSubtype": "GPS", "eventSource": "test", "eventPayLoad": { "heading": 0, "coordinate": { "locationX": 8.61660560080855, "locationY": 49.8182790029665 }, "timeStamp": "2013-07-03T07:00:00.000+0000" }, "scemid": stopID } } // print event object to console for reference console.log(); console.log("data: "+JSON.stringify(eventData)); console.log(); // display response as JSON when response for Tour Create comes back var callbackEventCreated = function (error, response, body) { console.log(JSON.stringify(body,null,1)); }; // create request object in node (if not available, try "npm install request" in command line before) var request = require('request'); var options = { url: 'https://driveandarrive-v1.cloud.ptvgroup.com/em/event?source=PTVNODEJSCS', method: 'POST', json: eventData, headers: { "content-type":"application/json" } }; // send Tour Create request request(options, callbackEventCreated);
You will get an immediate SUCCESS message. The event is now processed internally and a ETA calculation is triggered if appropriate. Please note that not every positioning event leads to a new ETA calculation! If there is a new ETA message you will get notified over your callback URL defined above, in our example, you should see all ETA notifications here: http://requestb.in/1cifoit1?inspect
© 2022 PTV Planung Transport Verkehr GmbH | Imprint