String[] requestProximity(String message, RouteStop[] rs, RouteProximityOptions ro, MapSize ms, String dataSource) throws Exception {
String[] ret = new String[2];
String response = message;
StringBuilder request = new StringBuilder();
TCPClientDecarta tcpDecarta = new TCPClientDecarta(_server, _port);
try {
String[] part = message.split(Pattern.quote("%"));
if (message.toLowerCase().indexOf("%vr7=") > 0) {
ret[0] = message;
ret[0] = "";
return ret;
}
int reqNumber = 0;
Point ptCheck = new Point();
if (response.toLowerCase().indexOf("find route") > 0) {
ret[0] = response;
ret[1] = request.toString();
return ret;
}
while (reqNumber < 25 && (response.toLowerCase().indexOf("cannot") > 0)) {
if (response.toLowerCase().indexOf("origin") > 0) {
ptCheck.setX(rs[0].getPoint().getX());
ptCheck.setY(rs[0].getPoint().getY());
}
if (response.toLowerCase().indexOf("destination") > 0) {
ptCheck.setX(rs[rs.length - 1].getPoint().getX());
ptCheck.setY(rs[rs.length - 1].getPoint().getY());
}
if (response.toLowerCase().indexOf("waypoint") > 0) {
part = response.split(Pattern.quote("%"));
ptCheck.setX(rs[Integer.parseInt(part[1].replace("WP=", "")) + 1].getPoint().getX());
ptCheck.setY(rs[Integer.parseInt(part[1].replace("WP=", "")) + 1].getPoint().getY());
}
// PNB1|%LL=-23.63874,-46.62146|%S1FIELD0=?
// *%RAD=m1000%TIMEOUT=500%DS=lbslocal_rotas%LL||
request = new StringBuilder();
request.append("PNB1");
request.append("|");
request.append("%LL=" + String.valueOf(ptCheck.getY()).replace(",", ".") + "," + String.valueOf(ptCheck.getX()).replace(",", "."));
request.append("|");
request.append("%S1FIELD0=? *%RAD=m2500");
request.append("%TIMEOUT=500");
request.append("%DS=" + dataSource + "%LL");
request.append("||");
String teste = tcpDecarta.Send(request.toString());
if (teste.toLowerCase().indexOf("records") >= 0) {
request = new StringBuilder();
request.append("PNB1");
request.append("|");
request.append("%LL=" + String.valueOf(ptCheck.getY()).replace(",", ".") + "," + String.valueOf(ptCheck.getX()).replace(",", "."));
request.append("|");
request.append("%S1FIELD0=? *%RAD=k10");
request.append("%TIMEOUT=500");
request.append("%DS=" + dataSource + "%LL");
request.append("||");
teste = tcpDecarta.Send(request.toString());
if (teste.toLowerCase().indexOf("records") >= 0) {
request = new StringBuilder();
request.append("PNB1");
request.append("|");
request.append("%LL=" + String.valueOf(ptCheck.getY()).replace(",", ".") + ","
+ String.valueOf(ptCheck.getX()).replace(",", "."));
request.append("|");
request.append("%S1FIELD0=? *%RAD=k100");
request.append("%TIMEOUT=500");
request.append("%DS=" + dataSource + "%LL");
request.append("||");
teste = tcpDecarta.Send(request.toString());
}
if (teste.toLowerCase().indexOf("records") >= 0) {
if (response.indexOf("Cannot find path destination") > 0) {
throw (new Exception("Não foi possível encontrar ligação com o ponto " + (rs.length) + " ("
+ rs[rs.length - 1].getDescription() + ") "));
}
if (response.indexOf("Cannot find path origin") > 0) {
throw (new Exception("Não foi possível encontrar ligação com o ponto " + (1) + " (" + rs[0].getDescription() + ") "));
}
if (response.toLowerCase().indexOf("wp") > 0) {
throw (new Exception("Não foi possível encontrar ligação com o ponto "
+ (Integer.parseInt(response.substring(response.indexOf("WP=") + 3,
response.substring(response.indexOf("WP=") + 3).indexOf("%"))) + 1)
+ " ("
+ rs[Integer.parseInt(response.substring(response.indexOf("WP=") + 3,
response.substring(response.indexOf("WP=") + 3).indexOf("%"))) + 1].getDescription() + ") "));
}
}
}
if (teste.indexOf("Plugin error") >= 0) {
request = new StringBuilder();
request.append("PNB1");
request.append("|");
request.append("%LL=" + String.valueOf(ptCheck.getY()).replace(",", ".") + "," + String.valueOf(ptCheck.getX()).replace(",", "."));
request.append("|");
request.append("%S1FIELD0=? *%RAD=m500");
request.append("%TIMEOUT=500");
request.append("%DS=" + dataSource + "%LL");
request.append("||");
teste = tcpDecarta.Send(request.toString());
}
if (teste.toLowerCase().indexOf("feature") >= 0) {
request = new StringBuilder();
request.append("PNB1");
request.append("|");
request.append("%LL=" + String.valueOf(ptCheck.getY()).replace(",", ".") + "," + String.valueOf(ptCheck.getX()).replace(",", "."));
request.append("|");
request.append("%S1FIELD0=? *%RAD=m5000");
request.append("%TIMEOUT=500");
request.append("%DS=" + dataSource + "%LL");
request.append("||");
teste = tcpDecarta.Send(request.toString());
}
Point newPoint = new Point();
newPoint.setX(Double.parseDouble(teste.substring(teste.indexOf("LL=") + 3, teste.indexOf("LL=") + 3 + 21).split(Pattern.quote(","))[1]
.replace("|", "").replace("%", "")));
newPoint.setY(Double.parseDouble(teste.substring(teste.indexOf("LL=") + 3, teste.indexOf("LL=") + 3 + 21).split(Pattern.quote(","))[0]
.replace("|", "").replace("%", "")));
if ((ptCheck.getX() == newPoint.getX()) && (ptCheck.getY() == newPoint.getY())) {
if (response.toLowerCase().indexOf("wp") > 0) {
throw (new Exception("Não foi possível encontrar ligação com o ponto "
+ (Integer.parseInt(response.substring(response.indexOf("WP=") + 3, response.substring(response.indexOf("WP=") + 3)
.indexOf("%"))) + 1)
+ " ("
+ rs[Integer.parseInt(response.substring(response.indexOf("WP=") + 3, response.substring(response.indexOf("WP=") + 3)
.indexOf("%"))) + 1].getDescription() + ") "));
}
}
if (response.toLowerCase().indexOf("origin") > 0) {
rs[0].getPoint().setX(newPoint.getX());
rs[0].getPoint().setY(newPoint.getY());
}
if (response.toLowerCase().indexOf("destination") > 0) {
rs[rs.length - 1].getPoint().setX(newPoint.getX());
rs[rs.length - 1].getPoint().setY(newPoint.getY());
}
if (response.toLowerCase().indexOf("waypoint") > 0) {
rs[Integer.parseInt(part[1].replace("WP=", "")) + 1].getPoint().setX(newPoint.getX());
rs[Integer.parseInt(part[1].replace("WP=", "")) + 1].getPoint().setY(newPoint.getY());
}
request = new StringBuilder();
request.append("RMAN");
request.append("|");
request.append("%OLL=" + String.valueOf(rs[0].getPoint().getY()).replace(",", ".") + ","
+ String.valueOf(rs[0].getPoint().getX()).replace(",", "."));
for (int x = 1; x < rs.length - 1; x++) {
request.append("%WP");
request.append("%WDLL=" + String.valueOf(rs[x].getPoint().getY()).replace(",", ".") + ","
+ String.valueOf(rs[x].getPoint().getX()).replace(",", "."));
request.append("%WOLL=" + String.valueOf(rs[x].getPoint().getY()).replace(",", ".") + ","
+ String.valueOf(rs[x].getPoint().getX()).replace(",", "."));
}
request.append("%DLL=" + String.valueOf(rs[rs.length - 1].getPoint().getY()).replace(",", ".") + ","
+ String.valueOf(rs[rs.length - 1].getPoint().getX()).replace(",", "."));
request.append("|%TIME");
if (rs.length < 10 && ro.getRouteDetails().isOptimizeRoute())
request.append("%OPT=1");
else
request.append("%OPT=0");
switch (ro.getRouteDetails().getRouteType()) {
case 0:
request.append("%RS=F");
break;
case 1:
request.append("%RS=S");
break;
case 2:
request.append("%RS=P");
break;
/*
* case 3: { request.append("%RS=F"); setTraffic(dataSource); }
* break; case 4: { request.append("%RS=S");
* setTraffic(dataSource); } break;
*/
case 5:
request.append("%RS=F");
break;
case 6: // 6 Rotas evitando balsas
request.append("%ADJUSTCOST=FERRYBOAT,INF");
break;
case 7: // 7 Rotas evitando estradas de terra
request.append("%ADJUSTCOST=TUNNEL,INF");
break;
case 8: // 8 Rotas evitando balsas e estradas de terra
request.append("%ADJUSTCOST=FERRYBOAT,INF%ADJUSTCOST=TUNNEL,INF");
break;
case 9: // 9 Rotas evitando estradas com condi��es
// prec�rias
request.append("%ADJUSTCOST=SKYWAY,INF");
break;
case 10: // 10 Rotas evitando ped�gios
request.append("%ADJUSTCOST=TOLLROAD,INF");
break;
case 12:
request.append("%RS=F");
break;
case 13:
request.append("%RS=F");
break;
}
request.append("%S2");
request.append("%AC");
request.append("%TDIST=K");
request.append("%DIST=K");
request.append("%LLMAX");
request.append("%LLMIN");
if (ms.getHeight() != 0 || ro.getRouteDetails().getPoiRoute() != null || ro.getVehicle().getTollFeeCat() > 0)
request.append("%RG");
request.append("%VR7");
request.append("%TURN");
request.append("%LL");
switch (ro.getRouteDetails().getRouteType()) {
case 5:
request.append("%DS=ccr_rotas");
break;
case 12:
request.append("%DS=telefonica_rotas");
break;
case 13:
request.append("%DS=telefonica_pf");
break;
/*
* case 3: if (setTraffic(dataSource)) request.append("%EDS=" +
* dataSource + "_edit"); else request.append("%DS=" +
* dataSource); break; case 4: if (setTraffic(dataSource))
* request.append("%EDS=" + dataSource + "_edit"); else
* request.append("%DS=" + dataSource); break;
*/
case 6: // 6 Rotas evitando balsas
request.append("%DS=lbslocal_rodoviaria_rotas_evitando_balsas");
break;
case 7: // 7 Rotas evitando estradas de terra
request.append("%DS=lbslocal_rodoviaria_rotas_evitando_terra");
break;
case 8: // 8 Rotas evitando balsas e estradas de terra
request.append("%DS=lbslocal_rodoviaria_rotas_evitando_balsa_terra");
break;
case 9: // 9 Rotas evitando estradas com condi��es
// prec�rias
request.append("%DS=lbslocal_rodoviaria_rotas_evitando_condicoes_precarias");
break;
case 10: // 10 Rotas evitando ped�gios
request.append("%DS=lbslocal_rodoviaria_rotas_evitando_pedagios");
break;
default:
request.append("%DS=" + dataSource);
break;
}
request.append("%TIMEOUT=50000");
request.append("||");
response = tcpDecarta.Send(request.toString());
reqNumber++;
}
} catch (Exception ex) {
throw ex;