Now supports handling events for TRIPS as well as ALERTS. Need to manually add xBOTs to queue using API 9853
This commit is contained in:
parent
3ae52206fc
commit
6260cc5d63
@ -28,10 +28,33 @@ public final class Helpers {
|
||||
return new JSONObject(string).get("alertId").toString();
|
||||
}
|
||||
|
||||
public static String getTripId(String string) throws IOException {
|
||||
return new JSONObject(string).get("tripId").toString();
|
||||
}
|
||||
|
||||
public static String getXbot(String string) throws IOException {
|
||||
return new JSONObject(string).get("xbot").toString();
|
||||
}
|
||||
|
||||
public static void getTripDetails(String tripId, String xbot, String gboBearerToken) throws Exception {
|
||||
SSLContext sc = SSLContext.getInstance("SSL");
|
||||
sc.init(null, DummyX509TrustManager.getDummyArray(), new java.security.SecureRandom());
|
||||
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
|
||||
|
||||
URL url = new URL("https://api.sbx.idbt.translink.nl/api/v3/id-media/tokens/xbot/" + xbot + "/trips/details/" + tripId);
|
||||
URLConnection con = url.openConnection();
|
||||
HttpURLConnection http = (HttpURLConnection)con;
|
||||
http.setRequestMethod("GET");
|
||||
http.setDoOutput(true);
|
||||
http.setRequestProperty("Authorization", "Bearer " + gboBearerToken);
|
||||
http.connect();
|
||||
|
||||
try(InputStream is = http.getInputStream()) {
|
||||
String response = new String(is.readAllBytes(), StandardCharsets.UTF_8);
|
||||
LOGGER.info("GBO API 8659 trip details response for xBOT " + xbot + " and tripId " + tripId + ": \n" + new JSONObject(response).toString(2));
|
||||
}
|
||||
}
|
||||
|
||||
public static void getAlertDetails(String alertId, String xBot, String gboBearerToken) throws Exception {
|
||||
SSLContext sc = SSLContext.getInstance("SSL");
|
||||
sc.init(null, DummyX509TrustManager.getDummyArray(), new java.security.SecureRandom());
|
||||
@ -48,7 +71,7 @@ public final class Helpers {
|
||||
|
||||
try(InputStream is = http.getInputStream()) {
|
||||
String response = new String(is.readAllBytes(), StandardCharsets.UTF_8);
|
||||
LOGGER.info("GBO API 8851 alert details response for xBOT " + xBot + ": \n" + new JSONObject(response).toString(2));
|
||||
LOGGER.info("GBO API 8851 alert details response for xBOT " + xBot + " and alertId " + alertId + ": \n" + new JSONObject(response).toString(2));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -16,14 +16,28 @@ public class RabbitConnector {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(RabbitConnector.class);
|
||||
|
||||
// TRIPS
|
||||
// SubscriptionId = 3e246de5-d3ad-468f-834b-1aaebf52244c
|
||||
// Use API 9853 to manually add xBOT to queue
|
||||
private static final String QUEUE_NAME = "BEID_3.TRIPS";
|
||||
private static final String USER_NAME = "BEID_3_TRIPS_HlTT";
|
||||
private static final String PASSWORD = "xJR4C8hIqhHQw0sn";
|
||||
|
||||
// ALERTS
|
||||
// SubscriptionId = 17c8100b-88a2-4cef-b40d-8dca4f93d311
|
||||
// Use API 9853 to manually add xBOT to queue
|
||||
// private static final String QUEUE_NAME = "BEID_3.ALERTS";
|
||||
// private static final String USER_NAME = "BEID_3_ALERTS_nZs3";
|
||||
// private static final String PASSWORD = "VyubhPnczKgTB2zJ";
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setVirtualHost("/");
|
||||
factory.setAutomaticRecoveryEnabled(true);
|
||||
factory.setPort(443);
|
||||
factory.setHost("not.sbx.idbt.translink.nl");
|
||||
factory.setUsername("BEID_3_ALERTS_nZs3");
|
||||
factory.setPassword("VyubhPnczKgTB2zJ");
|
||||
factory.setUsername(USER_NAME);
|
||||
factory.setPassword(PASSWORD);
|
||||
factory.useSslProtocol("TLSv1.2");
|
||||
factory.setExceptionHandler(new ForgivingExceptionHandler());
|
||||
Map<String, Object> configs = factory.getClientProperties();
|
||||
@ -33,7 +47,7 @@ public class RabbitConnector {
|
||||
Channel channel = connection.createChannel();
|
||||
DeliverCallback deliverCallback = initDeliverCallback(channel);
|
||||
|
||||
AMQP.Queue.DeclareOk queue = channel.queueDeclarePassive("BEID_3.ALERTS");
|
||||
AMQP.Queue.DeclareOk queue = channel.queueDeclarePassive(QUEUE_NAME);
|
||||
LOGGER.info(
|
||||
"Declared queue: " + queue.getQueue() + ", consumer count: " + queue.getConsumerCount() + ", message count: " +
|
||||
queue.getMessageCount());
|
||||
@ -52,17 +66,39 @@ public class RabbitConnector {
|
||||
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
|
||||
LOGGER.info("Successfully acknowledged message with delivery tag: " + delivery.getEnvelope().getDeliveryTag());
|
||||
|
||||
LOGGER.info("Getting alert details via GBO API 8851...");
|
||||
if (QUEUE_NAME.equals("BEID_3.TRIPS")) {
|
||||
getTripDetails(message);
|
||||
} else if (QUEUE_NAME.equals("BEID_3.ALERTS")) {
|
||||
getAlertDetails(message);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static void getAlertDetails(String message) {
|
||||
try {
|
||||
String alertId = Helpers.getAlertId(message);
|
||||
String xBot = Helpers.getXbot(message);
|
||||
String gboBearerToken = Helpers.getGboBearerToken();
|
||||
|
||||
LOGGER.info("Getting alert details for xBOT {} and alertId {} via GBO API 8851...", xBot, alertId);
|
||||
Helpers.getAlertDetails(alertId, xBot, gboBearerToken);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static void getTripDetails(String message) {
|
||||
try {
|
||||
String tripId = Helpers.getTripId(message);
|
||||
String xBot = Helpers.getXbot(message);
|
||||
String gboBearerToken = Helpers.getGboBearerToken();
|
||||
|
||||
LOGGER.info("Getting trip details for xBOT {} and tripId {} via GBO API 8659...", xBot, tripId);
|
||||
Helpers.getTripDetails(tripId, xBot, gboBearerToken);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user