wenzheng.yang
2023-06-13 76017226c92e01ead75b24e10d36477dd631c2cd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
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+",");
                }
            }
        }
    }
}