package com.hxzkoa.udp;
|
|
import java.io.IOException;
|
import java.lang.Thread.State;
|
import java.net.DatagramPacket;
|
import java.net.DatagramSocket;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Set;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import com.hxzkoa.json.tb_system;
|
import com.hxzkoa.json.tb_tag;
|
import com.hxzkoa.services.SysSettingService;
|
import com.hxzkoa.util.Config;
|
import com.hxzkoa.util.HttpUtil;
|
import com.hxzkoa.util.ModifyConfig;
|
|
import net.sf.json.JSONObject;
|
|
/** 该类用于接收UDP报文 */
|
public class Udp_Receive implements Runnable {
|
static DatagramSocket socket;
|
public static List<String> allMessage = new ArrayList<String>();
|
public static List<String> zhuceMessage = new ArrayList<String>();
|
public static List<String> debugMessage = new ArrayList<String>();
|
public static List<String> mysqlMessage = new ArrayList<String>();
|
public static List<String> hexMessage = new ArrayList<String>();
|
public static List<String> gasMessage = new ArrayList<String>();
|
public static List<String> tcpMessage = new ArrayList<String>();
|
public static List<String> yuanshiMessage = new ArrayList<String>();
|
public static List<String> udpMessage = new ArrayList<String>();
|
DatagramPacket packet;
|
int times = 5;
|
Thread t = new Thread(this);
|
|
|
/**
|
* 初始化对象
|
*
|
* @throws NumberFormatException
|
* @throws IOException
|
*/
|
public Udp_Receive(String udpPort) throws NumberFormatException, IOException {
|
socket = new DatagramSocket(Integer.parseInt(udpPort));
|
byte[] data = new byte[40960]; // 创建byte数组
|
packet = new DatagramPacket(data, data.length);
|
|
}
|
|
/** 启动线程的方法 */
|
public void startThread() {
|
t.start();
|
|
}
|
|
/** 停止线程的方法
|
* @throws InterruptedException */
|
public void stopThread() throws InterruptedException {
|
try {
|
Thread.sleep(2000);
|
t.interrupt();
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
|
// /** 查看线程状态的方法 */
|
public State getState() {
|
State state = t.getState();
|
return state;
|
}
|
|
@SuppressWarnings("unused")
|
public void run() {
|
while (true) {
|
try {
|
//udp接收
|
socket.receive(packet); // 接收数据包
|
int lenth = packet.getLength();
|
byte[] byt = subBytes(packet.getData(), 0, lenth);
|
// // 获取发送端的IP地址对象
|
String ip = packet.getAddress().getHostAddress();
|
String datas = Tools.Bytes2HexString(byt);
|
String message = new String(packet.getData(), 0, lenth);
|
Date d = new Date();
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
System.out.println(sdf.format(d));
|
System.out.println("message"+message);
|
|
//模拟接收
|
// String message = null;
|
// String datas=null;
|
// Thread.sleep(300);
|
// message = "$GNGGA,102319.60,3413.31707,N,10853.33826,E,2,12,0.97,443.6,M,-27.8,M,0.6,0654*4C,1002,41";
|
// byte[] byt = null;
|
// if ( (System.currentTimeMillis() % 100) == 10) {
|
// message = "display,64,1018,70,2020-12-14 13:08:33.479,0,648,367,150,32,0,0,1";
|
// datas = null;
|
// } else if ((System.currentTimeMillis() % 10) == 1){
|
// message = "$GNGGA,043310.44,3951.73773,N,11615.40420,E,4,12,0.71,53.0,M,-8.9,M,1.4,1935*41,1234,2d";
|
// datas = null;
|
// }else if ((System.currentTimeMillis() % 10) == 2){
|
// message = "anchor_info,2,1000,1,1,1,0,0,1,1,127.0.0.1,v1,0";
|
// datas = null;
|
// }else if ((System.currentTimeMillis() %10) == 10){
|
// message = "anchor_near,1, 1000, 1,2,3,4, 5, 6, 7,8, 1,0";
|
// datas = null;
|
// }else if ((System.currentTimeMillis() % 10) == 4){
|
// message = "anchor_tongbu,1,1000,1001,1,0,0";
|
// datas = null;
|
// }else if ((System.currentTimeMillis() % 10) == 5){
|
// message = "tag_info,1,1001,1,0,0,0,0,v1,0,0,0,0,0,0";
|
// datas = null;
|
// }else if ((System.currentTimeMillis() % 10) == 6){
|
// message = "hxzk_heart,1,0,0";
|
// datas = null;
|
// }else if ((System.currentTimeMillis() % 10) == 7){
|
// message = "gas_colector,1,0,0,1,127.1.1.0,0,0,0,0";
|
// datas = null;
|
// }else if ((System.currentTimeMillis() % 10) == 8){
|
// message = "gas_sensor,0,0,0,0,0,0,0,0,0,0,0";
|
// datas = null;
|
// }else if ((System.currentTimeMillis() % 10) == 9){
|
// message = "01 03 48 00 08 00 00 00 00 00 00 00 00 00 03 BF 80 00 00 00 00 00 03 BF 80 00 00 00 00 00 03 BF 80 00 00 00 00 00 03 BF 80 00 00 00 00 00 03 BF 80 00 00 00 00 00 03 BF 80 00 00 00 00 00 03 BF 80 00 00 00 00 00 03 BF 80 00 00 08 2A";
|
// datas = "01 03 48 00 08 00 00 00 00 00 00 00 00 00 03 BF 80 00 00 00 00 00 03 BF 80 00 00 00 00 00 03 BF 80 00 00 00 00 00 03 BF 80 00 00 00 00 00 03 BF 80 00 00 00 00 00 03 BF 80 00 00 00 00 00 03 BF 80 00 00 00 00 00 03 BF 80 00 00 08 2A";
|
// message=message.replace(" ", "");
|
// datas=datas.replace(" ", "");
|
// }else if ((System.currentTimeMillis() % 10) == 0){
|
// message = "55 AA 0A 14 19 98 13 10 05 15 02 01 1E 02 00 0A 00 53 85 94 FC 00 5E FC";
|
// datas = "55 AA 0A 14 19 98 13 10 05 15 02 01 1E 02 00 0A 00 53 85 94 FC 00 5E FC";
|
// message=message.replace(" ", "");
|
// datas=datas.replace(" ", "");
|
// }else if ((System.currentTimeMillis() % 10) == 3){
|
// message = "55 AA 02 0C 18 98 01 0000000000000040FF";
|
// datas = "55 AA 02 0C 18 98 01 0000000000000040FF";
|
// message=message.replace(" ", "");
|
// datas=datas.replace(" ", "");
|
// }
|
// String ip = "127.0.0.1";
|
// System.out.println("message"+message);
|
|
//报文处理
|
String BaowenPath = Config.getBaowenConfig();
|
System.out.println("baowenswitch"+ModifyConfig.readData(BaowenPath, "baowenSwitch"));
|
String baowenStatus = ModifyConfig.readData(BaowenPath, "baowenSwitch");
|
String baowenContent = ModifyConfig.readData(BaowenPath, "allMessage");
|
if(message!=null){
|
//
|
// int lenth = message.length();
|
String messageJson = GetNowTime.timestamp2()+ " 收:" +message;
|
if (baowenStatus.equals("1")){
|
Udp_Receive.allMessage.add(messageJson);
|
}
|
|
// 处理以display开头的数据
|
if (message.startsWith("display")) {
|
// Baowen_Receive.insert(message);
|
// //定位数据转发
|
Transfer_Receive.insert(message);
|
Dell_display.display(message);
|
} else if (message.startsWith("anchor_info")) {
|
// Baowen_Receive.insert(message);
|
// Anchor_info.anchor_info(message);
|
} else if (message.startsWith("anchor_near")) {
|
// Baowen_Receive.insert(message);
|
// Anchor_near.anchor_near(message);
|
} else if (message.startsWith("anchor_tongbu")) {
|
// Baowen_Receive.insert(message);
|
// Anchor_tongbu.anchor_tongbu(message);
|
} else if (message.startsWith("tag_info")) {
|
// Baowen_Receive.insert(message);
|
// Tag_info.tag_info(message);
|
} else if (message.startsWith("hxzk_heart")) {
|
// Baowen_Receive.insert(message);
|
// Hxzk_heart.hxzk_heart(message);
|
} else if (message.startsWith("get_anchor_info")) {
|
// Baowen_Receive.insert(message);
|
// Anchor_info.get_anchor_info(message);
|
} else if (message.startsWith("get_anchor_nearinfo")) {
|
// Baowen_Receive.insert(message);
|
// Anchor_near.get_anchor_nearinfo(message);
|
} else if(message.startsWith("get_taginfo")){
|
// Baowen_Receive.insert(message);
|
// Tag_info.get_tag_info(message);
|
} else if (message.startsWith("gas_colector")) {
|
// Baowen_Receive.insert(message);
|
// gasCollector.collector(message);
|
} else if (message.startsWith("gas_sensor")) {
|
// Baowen_Receive.insert(message);
|
// gasSensor.sensor(message);
|
} else if (message.startsWith("$GNGGA")) {
|
//定位数据转发
|
Transfer_Receive.insert(message);
|
// Gps_Receive.insert(message);
|
Dell_GNGGA.dell_gps(message);
|
} else if (message.startsWith("status2")) {
|
// String AnchorPath = Config.getAnchorConfig();
|
// ModifyConfig.writeData(AnchorPath, "huceju", message);
|
}else if (message.startsWith("SOS")) {
|
//转发
|
Transfer_Receive.insert(message);
|
}else {
|
// String time=GetNowTime.timestamp2();
|
// int timestamp=GetNowTime.timestamp();
|
// //原始数据转发
|
// Transfer_Receive.insert(message);
|
// // 原始报文
|
// String hex = datas.toUpperCase();
|
// // 如果是合法的55AA01数据
|
// if (hex.startsWith("55AA01") && lenth == 42) {
|
// Yuanshi_Receive.insert(hex, ip);
|
// }
|
// // 如果是心跳包
|
// if (hex.startsWith("55AA02") && lenth == 32) {
|
// Yuanshi_Receive.insert(hex, ip);
|
// }
|
// // 如果是注册信息
|
// if (hex.startsWith("55AA0A") && lenth == 48) {
|
// // 调用处理注册信息的方法
|
// Zhuce_Receive.insert(hex, ip);
|
// // 处理返回的读取配置信息成功
|
// }
|
// if (hex.startsWith("55AA03") && lenth == 202) {
|
// Yuanshi_Receive.insert(hex, ip);
|
// }
|
// if(hex.startsWith("0103")|| hex.startsWith("0203")|| hex.startsWith("0303") ) {
|
// Gas_Receive.insert(hex, ip);
|
// }
|
}
|
}
|
|
//
|
// ip = null;
|
// message = null;
|
// byt = null;
|
// datas = 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 main(String[] args) {
|
|
}
|
}
|