Skip to content

Commit

Permalink
traveltimes-a22: add calls.http, minimum request timestamp #673
Browse files Browse the repository at this point in the history
  • Loading branch information
clezag committed Nov 7, 2024
1 parent f6928af commit 600d166
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 22 deletions.
52 changes: 52 additions & 0 deletions data-collectors/traveltimes-a22/calls.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
@host={{$dotenv A22_CONNECTOR_URL}}

### Authorize
# @name login
@token = {{login.response.body.SubscribeResult.sessionId}}
POST {{host}}/token
Content-Type: application/json
User-Agent: NOI/A22TraveltimesConnector
Accept: */*

{
"request": {
"username": "{{$dotenv A22_CONNECTOR_USR}}",
"password": "{{$dotenv A22_CONNECTOR_PWD}}"
}
}


### De-authorize
DELETE {{host}}/token/{{token}}
User-Agent: NOI/A22TraveltimesConnector
Accept: */*

### Get segments (stations)
POST {{host}}/percorrenze/anagrafica
Content-Type: application/json
User-Agent: NOI/A22TraveltimesConnector
Accept: */*

{
"sessionId": "{{token}}"
}

### unix timestamps
@fromTs={{$timestamp -1 h}}
@toTs={{$timestamp}}

### Get travel times (measurements)
POST {{host}}/percorrenze/tempi
Content-Type: application/json
User-Agent: NOI/A22TraveltimesConnector
Accept: */*


{
"request": {
"sessionId": "{{token}}",
"idtratto": "1840-1841",
"fromData": "/Date({{fromTs}}000+0000)/",
"toData": "/Date({{toTs}}999+0000)/"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
package it.bz.noi.a22.traveltimes;

import org.apache.commons.io.IOUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
Expand All @@ -30,6 +31,7 @@
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -42,7 +44,7 @@ public class Connector {
private static final String USER_AGENT = "NOI/A22TravelTimesConnector";
private static final int WS_CONN_TIMEOUT_MSEC = 30000;
private static final int WS_READ_TIMEOUT_MSEC = 1800000;
private static final boolean DEBUG = false;
private static final boolean DEBUG = true;

private String token = null;
private String url = null;
Expand Down Expand Up @@ -188,7 +190,7 @@ public List<HashMap<String, String>> getTravelTimeSegments() throws IOException
}

HttpURLConnection conn = (HttpURLConnection) (new URL(url + "/percorrenze/anagrafica")).openConnection();
conn.setRequestMethod("GET");
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept", "*/*");
Expand Down Expand Up @@ -286,15 +288,16 @@ public List<HashMap<String, String>> getTravelTimes(long fr, long to, String id)

// retrieve events
HttpURLConnection conn = (HttpURLConnection) (new URL(url + "/percorrenze/tempi")).openConnection();
conn.setRequestMethod("GET");
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept", "*/*");
conn.setConnectTimeout(WS_CONN_TIMEOUT_MSEC);
conn.setReadTimeout(WS_READ_TIMEOUT_MSEC);
conn.setDoOutput(true);
OutputStreamWriter os = new OutputStreamWriter(conn.getOutputStream());
os.write("{\"request\":{\"sessionId\":\"" + token + "\",\"idtratto\":\"" + id + "\",\"fromData\":\"/Date(" + frTS + ")/\",\"toData\":\"/Date(" + toTS + ")/\"}}\n");
String reqBody = "{\"request\":{\"sessionId\":\"" + token + "\",\"idtratto\":\"" + id + "\",\"fromData\":\"/Date(" + frTS + ")/\",\"toData\":\"/Date(" + toTS + ")/\"}}\n";
os.write(reqBody);
os.flush();
int status = conn.getResponseCode();
if (http_codes.containsKey(status)) {
Expand All @@ -303,6 +306,9 @@ public List<HashMap<String, String>> getTravelTimes(long fr, long to, String id)
http_codes.put(status, 1);
}
if (status != 200) {
try{
System.err.println("Request body: " + reqBody);
} catch (Exception e) {} //don't care
throw new RuntimeException("could not get travel times (response code was " + status + ")");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,17 @@ private void setupDataType() {
}

private long getLastTimestampOfStationInSeconds(String stationId) {

if(stationIdLastTimestampMap == null) {
readLastTimestampsForAllStations();
}
try {
long ret = stationIdLastTimestampMap.getOrDefault(stationId,
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(a22TraveltimesProperties.getProperty("lastTimestamp")).getTime());
long defaultTs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.parse(a22TraveltimesProperties.getProperty("lastTimestamp")).getTime();

long ret = stationIdLastTimestampMap.getOrDefault(stationId, defaultTs);

// Use default time as latest starting point. Remote API might not have data before that time
ret = Math.max(ret, defaultTs);

LOG.debug("getLastTimestampOfStationInSeconds(" + stationId + "): " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(ret));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
origin=A22
stationtype=LinkStation
lastTimestamp=2020-01-01 00:00:00
lastTimestamp=2024-07-10 00:00:00
scanWindowSeconds=604800

Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ a22traveltimes.datatype.lds_pesanti.unit=
a22traveltimes.datatype.lds_pesanti.description=Livello di servizio mezzi pesanti
a22traveltimes.datatype.lds_pesanti.rtype=Mean
a22traveltimes.datatype.lds_pesanti.mapping=true
a22traveltimes.datatype.lds_leggeri.mapping.A.desc=traffico scorrevole
a22traveltimes.datatype.lds_leggeri.mapping.A.val=1
a22traveltimes.datatype.lds_leggeri.mapping.B.desc=traffico scorrevole
a22traveltimes.datatype.lds_leggeri.mapping.B.val=2
a22traveltimes.datatype.lds_leggeri.mapping.C.desc=rallentamenti
a22traveltimes.datatype.lds_leggeri.mapping.C.val=3
a22traveltimes.datatype.lds_leggeri.mapping.D.desc=rallentamenti
a22traveltimes.datatype.lds_leggeri.mapping.D.val=4
a22traveltimes.datatype.lds_leggeri.mapping.E.desc=traffico rallentato con code
a22traveltimes.datatype.lds_leggeri.mapping.E.val=5
a22traveltimes.datatype.lds_leggeri.mapping.F.desc=code a tratti
a22traveltimes.datatype.lds_leggeri.mapping.F.val=6
a22traveltimes.datatype.lds_leggeri.mapping.Z.desc=traffico critico
a22traveltimes.datatype.lds_leggeri.mapping.Z.val=99
a22traveltimes.datatype.lds_pesanti.mapping.A.desc=traffico scorrevole
a22traveltimes.datatype.lds_pesanti.mapping.A.val=1
a22traveltimes.datatype.lds_pesanti.mapping.B.desc=traffico scorrevole
a22traveltimes.datatype.lds_pesanti.mapping.B.val=2
a22traveltimes.datatype.lds_pesanti.mapping.C.desc=rallentamenti
a22traveltimes.datatype.lds_pesanti.mapping.C.val=3
a22traveltimes.datatype.lds_pesanti.mapping.D.desc=rallentamenti
a22traveltimes.datatype.lds_pesanti.mapping.D.val=4
a22traveltimes.datatype.lds_pesanti.mapping.E.desc=traffico rallentato con code
a22traveltimes.datatype.lds_pesanti.mapping.E.val=5
a22traveltimes.datatype.lds_pesanti.mapping.F.desc=code a tratti
a22traveltimes.datatype.lds_pesanti.mapping.F.val=6
a22traveltimes.datatype.lds_pesanti.mapping.Z.desc=traffico critico
a22traveltimes.datatype.lds_pesanti.mapping.Z.val=99
a22traveltimes.datatype.tempo_pesanti.key=tempo_pesanti
a22traveltimes.datatype.tempo_pesanti.unit=[s]
a22traveltimes.datatype.tempo_pesanti.description=Tempo di percorrenza mezzi pesanti
Expand Down

0 comments on commit 600d166

Please sign in to comment.