whyclj
2020-10-20 2a1aa055a75826211327ce37a7ad761bb7016c68
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
package com.dev.data;
 
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Date;
import java.util.List;
 
import com.base.BaseData;
import com.base.Com;
import com.config.AppConfig;
import com.sql.MysqlConnPool;
 
public class Dev_UDPServer_Thread implements Runnable{
    public static final int ZIZHAN_DATATYPE = 1;            //×ÓÕ¾Êý¾Ý
    public static final int QIFA_DATATYPE = 2;                //Æû·¢Êý¾Ý
    public static final int BATTERY_DATA_TYPE = 3;            //Ðîµç³Ø×éÊý¾Ý
    
    
    public MysqlConnPool conn_pool;
    public List<Device_inf> devices;
    public List<ConfigData> configs;
    public static int thread_count = 0;                        //×ÓÏ̸߳öÊý
    public static final int port = 6556;                    //×ÓվͨѶ¶Ë¿Ú 
    public static final String server_ip = "235.0.0.5";        //×é²¥ipµØÖ·
    public static final int MAX_DATA_COUNT = 1024;
    public AppConfig cfg;
    
    public DatagramSocket datagramSocket;
    
    public Dev_UDPServer_Thread(MysqlConnPool gB_MysqlConnPool, List<Device_inf> devices,List<ConfigData> configs,AppConfig cfg) {
        this.conn_pool = gB_MysqlConnPool;
        this.devices = devices;
        this.configs = configs;
        this.cfg = cfg;
    }
    
    @Override
    public void run() {
        System.out.println("Dev_UDPServer_Thread start at "+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
        InetAddress group = null;
        MulticastSocket socket = null;
        try {            
            new Thread(new Runnable() {  
                @Override  
                public void run() {
                    // Ö÷Ï߳̽ÓÊÜ¿ØÖÆÌ¨ÊäÈë²¢¹ã²¥³öÈ¥
                    InetAddress heartgroup = null;
                    DatagramPacket heartpack = null;
                    MulticastSocket socket1 = null;
                    while(true) {
                        try {
                            heartgroup = InetAddress.getByName(server_ip);
                            heartpack = new DatagramPacket(new byte[0], 0, heartgroup, port);                 // Ä¿µÄ¶Ë¿ÚºÍMulticastSocket¶Ë¿ÚÒ»Ñù£¡£¡
                            socket1 = createMulticastGroupAndJoin(heartgroup,port);
                            boolean isNewThread = true;
                            String last = Com.getDateTimeFormat(new Date(2000,1,1), Com.DTF_Y_M_D);            //ÉÏÒ»´Î½¨±íµÄymd
                            while(true) {
                                //·¢ËÍÐÄÌø°ü
                                try {
                                    
                                    /*if(isNewThread || !last.equals(nowdate)) {
                                        if(isNewThread) {
                                        }
                                        isNewThread = false;
                                        last = nowdate;
                                    }*/
                                    //System.out.println(Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
                                    
                                    
                                    //System.out.println("Ïß³Ì×ÜÊý£º"+thread_count);
                                    //²âÊÔÒì³£Çé¿öϳÌÐòÊÇ·ñÄÜÕý³£¿ªÆô¶Ë¿Ú¼àÌý
                                    //System.out.println(1/0);
                                    //¹¹ÔìÐÄÌø°üÒÔ¼°·¢ËÍÐÄÌø°ü
                                    byte[] buf = BaseData.createHeartData();
                                    heartpack.setData(buf);
                                    socket1.send(heartpack);
                                    //System.err.println(Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
                                    Thread.sleep(5000);
                                } catch (Exception e) {
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e1) {
                                        e1.printStackTrace();
                                    }
                                    e.printStackTrace();
                                } 
                            }
                        } catch (Exception e1) {
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            e1.printStackTrace();
                        } finally {
                            try {
                                socket1.leaveGroup(heartgroup);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            if(socket1 != null) {
                                socket1.close();
                            }
                        }
                    }
                }  
            }).start(); 
            byte[] data = null;
            DatagramPacket packet= null;
            while(true) {
                try {
                    data = new byte[MAX_DATA_COUNT];
                    group = InetAddress.getByName(server_ip);         
                    socket = createMulticastGroupAndJoin(group,port);  //¼ÓÈë×é²¥×飬ÉèÖÃ×é²¥×éµÄ¼àÌý¶Ë¿ÚΪ6556  
                    DatagramPacket outpack = new DatagramPacket(new byte[0], 0, group, port); // Ä¿µÄ¶Ë¿ÚºÍMulticastSocket¶Ë¿ÚÒ»Ñù£¡£¡
                    packet = new DatagramPacket(data, data.length);  
                    
                    
                    while(true) {                        
                        socket.receive(packet);         // Í¨¹ýMulticastSocketʵÀý¶Ë¿Ú´Ó×é²¥×é½ÓÊÕÊý¾Ý 
                        byte[] headCount = new byte[2];
                        System.arraycopy(data, 0, headCount, 0, headCount.length);
                        //System.out.println("Êý¾Ý°ü³¤¶È"+createPackHeadCount(data));
                        int dataCount = BaseData.createPackHeadCount(headCount);
                        if(dataCount > data.length) {
                            continue;            //Êý¾ÝÁ¿³¬±êÊý¾ÝÖ¡
                        }
                        ByteBuffer bf = ByteBuffer.allocate(dataCount);
                        bf.order(ByteOrder.LITTLE_ENDIAN);
                        bf.put(data,0,dataCount);
                        bf.flip();
                        
                        //System.out.println(ComBase.calchecksum(bf.array()));
                        //System.out.println("½ÓÊÕµ½Êý¾Ý:"+ComFn.bytesToHexString(bf.array(), bf.array().length));
                        
                        
                        BaseData baseData = new BaseData();
                        if(baseData.putByteBuffer(bf,cfg.isEquie_device_udppackage_cheak())) {
                            //System.err.println("packtype:"+baseData.packtype);
                            if(BaseData.ClildStood_DevType == baseData.packtype) {
                                //×ÓÕ¾ÀàÐÍÊý¾Ý°ü
                                if(baseData.devident >= 0x0701 && baseData.devident <= 0x0707) {
                                    String client_ip = packet.getSocketAddress().toString();
                                    RecordUDPPackage.RecordUDPPackageData(RecordUDPPackage.PackageType_ChildStood, client_ip, bf.array());
                                    
                                    ChildStoodParse_Thread childthread = new ChildStoodParse_Thread(conn_pool, devices,configs, baseData,ZIZHAN_DATATYPE);
                                    new Thread(childthread).start();
                                }
                                //ChildStoodParse_Thread childthread = new ChildStoodParse_Thread(conn_pool, devices,configs, baseData,ZIZHAN_DATATYPE);
                                //new Thread(childthread).start();
                                thread_count ++;
                            }else if(BaseData.Battery_DevType == baseData.packtype) {
                                //Ðîµç³Ø×éÀàÐÍÊý¾Ý°ü
                                String client_ip = packet.getSocketAddress().toString();
                                RecordUDPPackage.RecordUDPPackageData(RecordUDPPackage.PackageType_Battery, client_ip, bf.array());
                                
                                BatteryParse_Thread battthread = new BatteryParse_Thread(conn_pool, devices, baseData);
                                new Thread(battthread).start();
                                thread_count ++;
                            }else if(BaseData.SwitchBoard_DevType == baseData.packtype) {
                                String client_ip = packet.getSocketAddress().toString();
                                RecordUDPPackage.RecordUDPPackageData(RecordUDPPackage.PackageType_SwitchBoard, client_ip, bf.array(),baseData.getDateFromDate());
                                //¿ª¹ØÁ¿°åÀàÐÍÊý¾Ý°ü
                                SwitchBoardParse_Thread switchthread = new SwitchBoardParse_Thread(conn_pool, devices, baseData);
                                new Thread(switchthread).start();
                                thread_count ++;
                            }
                        }
                        //²âÊÔ³ÌÐòÕý³£ÔËÐÐÒì³£Çé¿öÏÂÒì³£Çé¿ö
                        //System.out.println(1/0);
                        Thread.sleep(10);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    socket.leaveGroup(group);
                    if(socket != null) {
                        try {
                            socket.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        } catch (Exception e1) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                //e.printStackTrace();
            }
            e1.printStackTrace();
        } finally {
            System.out.println("Ïß³ÌÍ˳ö¡£¡£¡£¡£¡£¡£¡£¡£¡£");
//            if (socket != null) {
//                try {
//                    socket.leaveGroup(group);
//                } catch (Exception e1) {
//                    e1.printStackTrace();
//                }
//                socket.close();
//            }
        }
    }
 
    /**
     * ´´½¨ºÍ¼ÓÈëÖ¸¶¨×é²¥
     * @param groupurl
     * @param port
     * @return
     */
    private MulticastSocket createMulticastGroupAndJoin(InetAddress group,int port) {
        try {  
            MulticastSocket socket = new MulticastSocket(port); // ³õʼ»¯MulticastSocketÀಢ½«¶Ë¿ÚºÅÓëÖ®¹ØÁª  
            socket.setLoopbackMode(false);
            socket.joinGroup(group); // ¼ÓÈë´Ë×é²¥×é  
            return socket;  
        } catch (Exception e1) {  
            System.err.println("Error: " + e1 +"\n ¼ÓÈë×鲥ʧ°Ü at "+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms)); // ²¶×½Òì³£Çé¿ö  
            return null;  
        }
    }
    
    
    
    
   
    
    public static void main(String[] args) {
        Short[] b = {0x44,0x00,0x07,0x78,0x03,0x0d,0x08,0x01,0x00,0x00,0x04,0x02,0x81,0x08,0x00,0x00,
                  0x80,0x3f,0x00,0x00,0x00,0x40,0x00,0x00,0x40,0x40,0x00,0x00,0x80,0x40,0x00,0x00,
                  0xa0,0x40,0x00,0x00,0xc0,0x40,0x00,0x00,0xe0,0x40,0x00,0x00,0x00,0x41,0x00,0x00,
                  0x10,0x41,0x00,0x00,0x20,0x41,0x00,0x00,0x30,0x41,0x00,0x00,0x40,0x41,0x01,0x00,
                  0x07,0x00,0xfc,0x66};
    }
}