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
package com.dev.data;
 
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.UnknownHostException;
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.base.ComBase;
import com.base.ComFn;
import com.config.AppConfig;
import com.sql.MysqlConnPool;
 
/**
 *     Äæ±äµçÔ´UDP±¨ÎĽÓÊÕÏß³Ì
 * @author LiJun
 *
 */
public class InverterPowerUDPServer_Thread implements Runnable{
    
    public MysqlConnPool conn_pool;
    public List<Device_inf> devices;
    public AppConfig cfg;
    
    public static final int server_port = 6600;
    public static final int MAX_DATA_COUNT = 512;                    //×î´óÊý¾Ý°ü³¤¶È
    
    public InverterPowerUDPServer_Thread(MysqlConnPool conn_pool,List<Device_inf> devices,AppConfig cfg) {
        this.conn_pool = conn_pool;
        this.devices = devices;
        this.cfg = cfg;
    }
 
    @Override
    public void run() {
        System.out.println("InverterPowerUDPServer_Thread start at "+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
        try {
            byte[] data = null;
            while(true) {
                DatagramSocket ds = null;
                try {
                    data = new byte[MAX_DATA_COUNT];
                    DatagramPacket packet = new DatagramPacket(data, data.length);
                    //¼àÌýÖ¸¶¨µÄ¶Ë¿ÚºÅ
                    ds = new DatagramSocket(server_port);
                    //ds.setSoTimeout(5000);
                    while (true) {
                        ds.receive(packet);                        
                        byte[] headCount = new byte[2];
                        
                        System.arraycopy(data, 0, headCount, 0, headCount.length);
                        BaseData basedata = new BaseData();
                        ByteBuffer bf = ByteBuffer.allocate(createPackHeadCount(data));
                        if(createPackHeadCount(data) > data.length) {
                            continue;            //Êý¾ÝÖ¡³¬±êÅжÏ
                        }
                        bf.order(ByteOrder.LITTLE_ENDIAN);
                        bf.put(data,0,createPackHeadCount(data));
                        bf.flip();
                        //BaseData.udpCheck(bf.array());
                        //System.out.println(packet.getSocketAddress());
                        //System.out.println(ds.getLocalSocketAddress().toString());
                        //System.out.println("½ÓÊÕµ½Äæ±äµçԴϵͳ:"+ComFn.bytesToHexString(data, createPackHeadCount(data)));
                        //System.out.println("");
                        if(basedata.putNiBianByteBuffer(bf,cfg.isEquie_device_udppackage_cheak())) {
                            //System.out.println(basedata.data.length);
                            String client_ip = packet.getSocketAddress().toString();
                            if(basedata.packtype == BaseData.NiBianPower_DevType) {
                                RecordUDPPackage.RecordUDPPackageData(RecordUDPPackage.PackageType_InverPower, client_ip, bf.array());
                                
                                InverterPower_Thread power_thread = new InverterPower_Thread(conn_pool, devices, basedata,client_ip);
                                new Thread(power_thread).start();
                                Dev_UDPServer_Thread.thread_count++;
                            }
                        }
                        //²âÊÔ³ÌÐòÒì³£Çé¿öÏ£¬³ÌÐòÄÜ·ñ¼ÌÐøÔËÐÐ
                        //System.out.println(1/0);
                        Thread.sleep(10);
                    }
                } catch (Exception e) {
                    Thread.sleep(1000);
                    e.printStackTrace();
                } finally {
                    if(ds != null) {
                        try {
                            ds.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            
        }
    }
    
    /**
     *     »ñÈ¡Êý¾ÝÍ·µÄ³¤¶È
     * @param b
     * @return
     */
    public static int createPackHeadCount(byte[] b) {
        ByteBuffer bf = ByteBuffer.allocate(b.length);
        bf.order(ByteOrder.LITTLE_ENDIAN);
        bf.put(b);
        bf.flip();
        return ComBase.changeShortToInt(bf.getShort());
        
    }
    
    public static void main(String[] args) {
        int i = 0x0001;
        System.out.println(i+"==="+(~i));
        
        byte[] b = ComFn.hex2Bytes("5600");
        System.out.println(ComFn.bytesToHexString(b, b.length)+"=="+createPackHeadCount(b));
    }
    
}