Я написал грубое и готовое консольное приложение java для реализации клиента telnet и чтения из файла координат.Это не самый красивый код, но он работает:
import org.apache.commons.net.telnet.TelnetClient;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class AutomatedTelnetClient {
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private String prompt = "#";
public AutomatedTelnetClient(String server, String user, String password) {
try {
// Connect to the specified server
telnet.connect("127.0.0.1", 5554);
// Get input and output stream references
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());
readUntil("OK");
sendGeoFixes("c:\\junk\\esk.txt");
System.out.println("Finished");
} catch (Exception e) {
e.printStackTrace();
}
}
private void delay() {
long t = System.currentTimeMillis();
long finish = t + 15000;
boolean loop = true;
while (loop) {
t = System.currentTimeMillis();
if (t > finish)
loop = false;
}
}
private void sendGeoFixes(String fn) {
boolean loop = true;
String line;
BufferedReader br = null;
String lat, lon;
String formatStr = "000.000000";
NumberFormat formatter = new DecimalFormat(formatStr);
String gf = "geo fix ";
try {
br = new BufferedReader(new FileReader(fn));
do {
try {
line = br.readLine();
if (line == null)
break;
String sa[] = line.split(",");
if (sa.length < 2)
break;
lat = sa[0];
String slat = formatter.format(Float.parseFloat(lat));
lon = sa[1];
String slon = formatter.format(Float.parseFloat(lon));
String sFix = slat + " " + slon;
write(gf + sFix);
String ans = readUntil("OK");
System.out.println(ans);
delay();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} while (loop);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String readUntil(String pattern) {
try {
char lastChar = pattern.charAt(pattern.length() - 1);
StringBuffer sb = new StringBuffer();
char ch = (char) in.read();
while (true) {
System.out.print(ch);
sb.append(ch);
if (ch == lastChar) {
if (sb.toString().endsWith(pattern)) {
return sb.toString();
}
}
ch = (char) in.read();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void write(String value) {
try {
out.println(value);
out.flush();
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
AutomatedTelnetClient telnet = new AutomatedTelnetClient(
"127.0.0.1", "username", "password");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Файл "esk.txt" имеет следующий формат:
-1.059408,54.476764
-1.056685,54.477731
-1.048233,54.479462
-1.044054,54.479968
-1.038213,54.482259
-1.034194,54.482496
-1.027904,54.480873
-1.01868,54.479361
-1.017364,54.47953
и т. Д.