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 Gps_Receive implements Runnable {
|
static DatagramSocket socket;
|
DatagramPacket packet;
|
int times = 5;
|
Thread t = new Thread(this);
|
public static List<String> gpsMessage = new ArrayList<String>();
|
|
public static void insert(String hex) {
|
gpsMessage.add(hex);
|
}
|
|
/**
|
* 初始化对象
|
*
|
* @throws NumberFormatException
|
* @throws IOException
|
*/
|
|
/** 启动线程的方法 */
|
public void startThread() {
|
t.start();
|
|
}
|
|
/** 停止线程的方法 */
|
public void stopThread() {
|
try {
|
Thread.sleep(2000);
|
t.interrupt();
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
|
// /** 查看线程状态的方法 */
|
public State getState() {
|
State state = t.getState();
|
return state;
|
}
|
|
public void run() {
|
while (true) {
|
try {
|
|
if (gpsMessage.size() >0){
|
String message = gpsMessage.get(0);
|
|
//报文处理
|
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){
|
// 处理以display开头的数据
|
if (message.startsWith("$GNGGA")) {
|
if (baowenStatus.equals("1")){
|
String messageJson = GetNowTime.timestamp2()+ " 收:" +message;
|
/* Udp_Receive.hexMessage.add(messageJson); */
|
}
|
new Dell_GNGGA().dell_gps(message);
|
}
|
}
|
|
|
message = null;
|
// byt = null;
|
|
gpsMessage.remove(0);
|
Thread.sleep(0);
|
} else {
|
Thread.sleep(1000);
|
}
|
|
|
|
|
|
} 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) {
|
// try {
|
// Udp_Receive receive = new Udp_Receive("8081");
|
// receive.run();
|
// } catch (Exception e) {
|
// e.printStackTrace();
|
// }
|
// String result = "{\"result\":{\"addtime\":\"2021-02-24 09:41:06\",\"dong_status\":\"\",\"gaodu\":\"155\",\"gongfang\":\"\",\"id\":35,\"imu\":\"\",\"pinglv\":\"2\",\"power\":\"100\",\"sleep_satus\":\"\",\"sleep_time\":\"\",\"state\":\"未绑定\",\"status\":\"status\",\"sudu\":\"无\",\"tag_id\":\"1013\",\"version\":\"V2.21\"}}";
|
// // String result = "{\"result\":\"1103\"}";
|
// JSONObject re_tag_bw = JSONObject.fromObject(result);
|
// System.out.println("----------response:" + re_tag_bw.toString());
|
// tb_tag re_tag = (tb_tag) JSONObject.toBean(JSONObject.fromObject(re_tag_bw.get("result").toString()),
|
// tb_tag.class);
|
//
|
// System.out.println(re_tag.toString());
|
// System.out.println(re_tag.getTag_id());
|
// JSONObject json = JSONObject.fromObject(result);
|
// System.out.println(json.toString());
|
}
|
}
|