package baowen;
|
import java.math.BigInteger;
|
import java.net.DatagramPacket;
|
import java.net.DatagramSocket;
|
import java.net.SocketException;
|
import DataBase.DatabaseManagement;
|
import data_model.Dell_fangzhuang;
|
import data_model.Dell_tag;
|
import data_model.Tb_fangzhuang;
|
import data_model.Tb_tag;
|
import frame.Index1;
|
import publicclass.GetNowTime;
|
import publicclass.JugeNumber;
|
import publicclass.Open_soft_dialog;
|
import publicclass.Shi_jian_cha_sss;
|
import publicclass.ShowMessage;
|
|
/**¸ÃÀàÓÃÓÚ½ÓÊÕUDP±¨ÎÄ*/
|
|
public class Udp_Receive implements Runnable {
|
static String tagid=null;
|
static int power=-1;
|
static int baoxu;
|
static int stationary_flag;
|
static int tag_hz;
|
static int shijianpian;
|
static int anchornum;
|
static int heart;//ÐÄÂÊ
|
static String nowzhendong;//Õð¶¯×´Ì¬
|
static int taggao ;//±êǩʵʱ¸ß¶È
|
static String ancgasid;//Èýά²Î¿¼»ùÕ¾ID
|
static int angas;
|
static int gascha;
|
static int gasancgao;
|
static String qiya;//ÆøÑ¹¼ÆÖµ
|
static int qiyaji;
|
static StringBuffer data=new StringBuffer();
|
static StringBuffer anchids=new StringBuffer();
|
static StringBuffer distances=new StringBuffer();
|
static DatagramSocket socket;
|
int port=8235;
|
DatagramPacket packet;
|
byte[] byt=null;
|
boolean G4=false;
|
static int bytlenth=1024*3;
|
static String[] hex;
|
int i=1;
|
static Tb_fangzhuang fzdata=null;
|
/**³õʼ»¯¶ÔÏó
|
* @throws NumberFormatException
|
* @throws IOException */
|
public Udp_Receive() {
|
|
try {
|
socket = new DatagramSocket(port);
|
} catch (SocketException e) {
|
ShowMessage.zidingyi_24("°ó¶¨¶Ë¿Ú"+port+"ʧ°Ü");
|
}
|
byte[] data = new byte[bytlenth]; // ´´½¨byteÊý×é
|
packet= new DatagramPacket(data,bytlenth);
|
|
}
|
|
|
/**Æô¶¯Ï̵߳ķ½·¨*/
|
public void startThread() {
|
Thread t=new Thread(this);
|
t.start();
|
Open_soft_dialog.addara("Æô¶¯"+port+"¶Ë¿Ú¼àÌý");
|
}
|
|
|
public void run() {
|
while(true) {
|
getdata();
|
}
|
}
|
|
/**»ñÈ¡Êý¾Ý*/
|
public void getdata() {
|
if(socket !=null) {
|
try {
|
socket.receive(packet); // ½ÓÊÕÊý¾Ý°ü
|
//Èç¹ûÔÝͣûÓÐÆô¶¯
|
int lenth=packet.getLength();
|
byt=subBytes(packet.getData(),0, lenth);
|
//»ñÈ¡·¢ËͶ˵ÄIPµØÖ·¶ÔÏó
|
String ip=packet.getAddress().getHostAddress();
|
int port=packet.getPort();
|
String datas=Tools.Bytes2HexString(byt);
|
String upinfo=datas.toUpperCase();
|
if(Index1.getTagid().equals("ÔʼÊý¾Ý")){
|
if(Index1.isShoware()) {
|
Index1.ara_show(upinfo);
|
}
|
}
|
i=i+1;
|
Index1.getUdpnum().setText(i+"Ìõ");
|
if(i==100000) {
|
i=0;
|
}
|
dell55aa0c_start(upinfo,ip,port);
|
Dell_55AA01.dell55aa01_start(upinfo, ip, port);
|
byt=null;
|
} catch (Exception e) {
|
e.printStackTrace(); // Êä³öÒì³£ÐÅÏ¢
|
}
|
}
|
}
|
|
|
public static DatagramSocket getSocket() {
|
return socket;
|
}
|
|
public static byte[] subBytes(byte[] src, int begin, int count) {
|
byte[] bs = new byte[count];
|
System.arraycopy(src, begin, bs, 0, count);
|
return bs;
|
}
|
|
public static void dell55aa0c_start(String message,String ip,int port) {
|
if(!message.startsWith("55AA0C")) {
|
return;
|
}
|
message=message.replaceAll("\r\n|\r|\n", "");//È¡³öËùÓÐQÐкͻسµ
|
String[] aa0=message.split("55AA");
|
int size=aa0.length;
|
for(int i=0;i<size;i++) {
|
String info="55AA"+aa0[i];
|
dell_55aa0c(info,ip,port);
|
}
|
}
|
|
//´¦Àí55AAOC¿ªÍ·µÄ±¨ÎÄ
|
public static void dell_55aa0c(String message,String ip,int port) {
|
|
int lenth=message.length();
|
int timestamp=GetNowTime.timestamp();
|
String intime=GetNowTime.timestamp2();
|
if(lenth>10) {
|
hex=hex(message);
|
String type= hex[2] ;//Ö¸ÁîÀàÐÍ
|
if(!type.equals("0C")) {
|
return;
|
}
|
int datalenth1=decodeHEX(hex[3]) ;//Êý¾Ý³¤¶È
|
datalenth1=datalenth1*2+8;
|
if(lenth==datalenth1) {
|
tagid=hex[5]+hex[4];//±êÇ©ID
|
power=decodeHEX(hex[8]);//µçÁ¿
|
baoxu=decodeHEX(hex[7]+hex[6]);//°üÐò
|
tag_hz=decodeHEX(hex[12]);//±êǩƵÂÊ
|
anchornum=decodeHEX(hex[16]);//»ùÕ¾ÊýÁ¿
|
String ancrid0=hex[18]+hex[17];//µ±Ç°²â¾àµÄÖ÷»ùÕ¾
|
String[] anchorid=new String[anchornum];//»ùÕ¾ID¼¯ºÏ
|
int[] distance=new int[anchornum];//»ùÕ¾¾àÀ뼯ºÏ
|
int dis1=anchornum*2+17;
|
int dis2=dis1+1;
|
int dis3=dis1+2;
|
int dis4=dis1+3;
|
for(int i=0;i<anchornum;i++) {
|
anchorid[i]=hex[18+i*2]+hex[17+i*2];
|
distance[i]=decodeHEX(hex[dis4+i*4]+hex[dis3+i*4]+hex[dis2+i*4]+hex[dis1+i*4]);
|
addbuf(anchids,i,anchorid[i]);
|
addbuf(distances,i,String.valueOf(distance[i]));
|
dellceju(anchorid[i] ,distance[i]);
|
}
|
showdata();
|
}else {//Èç¹û±¨Îij¤¶È²»µÈÓÚÐèÒªµÄ³¤¶È¿ÉÄÜÁ¬°üÁË
|
Dell_uanshi_byt.intsert(ip,message,intime, timestamp,port);
|
}
|
}else {//Èç¹û±¨Îij¤¶È²»×ã10
|
Dell_uanshi_byt.intsert(ip,message,intime, timestamp,port);
|
}
|
|
}
|
|
/**´¦Àí²â¾àÊý¾Ý·½·¨*/
|
public static void dellceju(String anchorid ,int dis) {
|
fzdata=Dell_fangzhuang.get_anchor(anchorid);
|
if(fzdata !=null) {
|
int dis0=Integer.parseInt(fzdata.getFzDis());//·Àײ¾àÀë
|
String door=fzdata.getDoor();
|
if(!(dis0<dis)) {//Èç¹û·Àײ¾àÀë²»´óÓÚÉ趨Ϊ¾àÀë
|
Tb_tag tag =Dell_tag.get_tag(tagid);
|
if(tag ==null) {//²åÈëÊý¾Ý¿â
|
indatabase(anchorid,dis+"", power+"");
|
}else {
|
String time=tag.getTime();
|
boolean aa=Shi_jian_cha_sss.shijiancha(time,300);
|
if(aa) {//²åÈëÊý¾Ý¿â
|
indatabase(anchorid,dis+"", power+"");
|
}
|
}
|
Dell_tag.addtagoraler(tagid, anchorid, door);
|
}
|
}
|
}
|
|
//½«Êý¾Ý²åÈëÊý¾Ý¿â
|
public static void indatabase(String anchorid,String distance, String power) {
|
if (power.equals("0")) {
|
power="1";
|
}
|
String[] ziduan= {"tagid","anchorid","distance","power","addtime"};
|
String[] zhi= {tagid,anchorid,distance ,power,GetNowTime.now()};
|
DatabaseManagement.insertfast("tb_door", ziduan, zhi);
|
}
|
public static String[] hex(String message) {
|
int size=message.length()/2;
|
String[] hex=new String[size];
|
for(int i=0;i<size;i++) {
|
hex[i]=message.substring(i*2, 2+i*2);
|
}
|
|
return hex;
|
}
|
|
/**16½øÖÆ×ªÎª10½øÖÆ*/
|
public static int decodeHEX(String hexs){
|
String hex=hexs.trim();
|
boolean a=JugeNumber.isLetterDigit(hex);
|
int numb=0;
|
if(a) {
|
BigInteger bigint=new BigInteger(hex,16);
|
numb=bigint.intValue();
|
}else {
|
ShowMessage.zidingyi_24("16½øÖÆ×ª10½øÖƳö´í,ÊÕµ½£º"+hex);
|
}
|
return numb;
|
|
}
|
|
/**±¨ÎÄÏÔʾµ÷ÓÃ*/
|
public static void showdata() {
|
if(Index1.getTagid().equals("ÔʼÊý¾Ý")) {
|
return;
|
}
|
if(Index1.isShoware()) {
|
data.append("ID:"+tagid+",");
|
data.append("°üÐò:"+baoxu+",");
|
data.append("µçÁ¿:"+power+",");
|
data.append("»ùÕ¾Êý:"+anchornum+",");
|
data.append("񅧏:"+anchids+",");
|
data.append("¾àÀë:"+distances+",");
|
String as=data.toString();
|
if(Index1.getTagid().equals("0")) {
|
Index1.ara_show(as);
|
}else {
|
if(Index1.getTagid().equals(tagid)) {
|
Index1.ara_show(as);
|
}
|
}
|
data.setLength(0);
|
anchids.setLength(0);
|
distances.setLength(0);
|
}
|
|
}
|
|
/**Ìí¼Ó»ùÕ¾½øÈ뱨ÎÄ*/
|
public static void addbuf(StringBuffer buf,int i,String anchorid) {
|
if(Index1.isShoware()) {
|
if(anchornum==1) {
|
buf.append("["+anchorid+"]");
|
}else {
|
if(i==0) {
|
buf.append("["+anchorid+",");
|
}else if(i==(anchornum-1)) {
|
buf.append(anchorid+"]");
|
}else {
|
buf.append(anchorid+",");
|
}
|
}
|
}
|
}
|
}
|