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;
|
int conn_error_count = 0;
|
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);
|
conn_error_count = 0;
|
while (true) {
|
try {
|
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++;
|
}
|
}
|
} catch (Exception e) {
|
conn_error_count ++;
|
if(conn_error_count >=6) {
|
break;
|
}
|
//e.printStackTrace();
|
}
|
//²âÊÔ³ÌÐòÒì³£Çé¿öÏ£¬³ÌÐòÄÜ·ñ¼ÌÐøÔËÐÐ
|
//System.out.println(1/0);
|
Thread.sleep(10);
|
}
|
} catch (Exception e) {
|
Thread.sleep(100);
|
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));
|
}
|
|
}
|