package com.cari.sys.biz;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class SendGpsTask {
private String path; // 發(fā)送的地址
private String tel; // 終端號碼
private Double[] xx; // 經(jīng)度
private Double[] yy; // 緯度
private String[] time; // 時(shí)間
private String[] vv; //速度
public SendGpsTask() {
}
public SendGpsTask(String path, String tel, Double[] xx, Double[] yy,
String[] time,String[] vv) {
this.path = path;
this.tel = tel;
this.xx = xx;
this.yy = yy;
this.time = time;
}
public static void sendToGIS(String path,int port,String tel, Double[] xx,
Double[] yy, String[] time,String[] vv) {
OutputStream outputStream;
Socket socket=null;
try {
InetAddress address = InetAddress.getByName(path);
//socket = new Socket(path,10003);
socket = new Socket();
System.out.println("前置機IP:"+path+",端口號:"+port);
socket.connect(new InetSocketAddress(address,port), 30000);
outputStream = socket.getOutputStream();
//與接收的流要相同
ObjectOutputStream objStream = new ObjectOutputStream(outputStream);
//將之前設置的好的對象傳入給服務(wù)器
String info = getMessage(tel, xx, yy, time,vv);
System.out.println("寫(xiě)入數據:"+info);
objStream.writeObject(info);
objStream.flush();
objStream.close();
outputStream.close();
}catch(ConnectException e){
e.printStackTrace();
for (int i = 0; i < time.length; i++) {
System.out.println("信息轉發(fā)失敗"+tel+" {X:"+xx[i]+",Y:"+yy[i]+",TT:"+time[i]+",VV:"+vv[i]+"}");
}
} catch (UnknownHostException e) {
e.printStackTrace();
System.out.println("UnKnown host:"+path);
}catch (InterruptedIOException e) {
e.printStackTrace();
System.out.println("沒(méi)有讀取數據,時(shí)間超時(shí)");
} catch (IOException e) {
e.printStackTrace();
System.out.println("連接失敗");
} finally{
if(socket!=null){
try {
socket.close();
socket=null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static String getMessage(String phone, Double[] longitude,
Double[] latitude, String[] tt,String[] vv) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(phone);
stringBuffer.append(":");
int index = longitude.length;
for (int i = 0; i < index; i++) {
stringBuffer.append(longitude[i]);
stringBuffer.append("-");
stringBuffer.append(latitude[i]);
stringBuffer.append("-");
stringBuffer.append(tt[i]);
stringBuffer.append("-");
stringBuffer.append(vv[i]);
if (i != index - 1) {
stringBuffer.append(",");
}
}
return stringBuffer.toString();
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public Double[] getXx() {
return xx;
}
public void setXx(Double[] xx) {
this.xx = xx;
}
public Double[] getYy() {
return yy;
}
public void setYy(Double[] yy) {
this.yy = yy;
}
public String[] getTime() {
return time;
}
public void setTime(String[] time) {
this.time = time;
}
public String[] getVv() {
return vv;
}
public void setVv(String[] vv) {
this.vv = vv;
}
}