| | |
| | | import io.netty.util.CharsetUtil; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springblade.modules.netty.business.entity.PoliceCarEquipment; |
| | | import org.springblade.modules.netty.business.service.PoliceCarEquipmentService; |
| | | import org.springblade.modules.netty.business.entity.DeviceMobilePositionPoliceCamera; |
| | | import org.springblade.modules.netty.business.service.DeviceChannelPoliceCameraService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | import javax.annotation.PostConstruct; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import static org.springblade.modules.netty.config.MyDecoder.hexToByte; |
| | | import static org.springblade.modules.netty.config.MyDecoder.hexTohort; |
| | | import static org.springblade.modules.netty.util.Hex.getDouble; |
| | |
| | | private static UdpServerHandler udpServerHandler; |
| | | |
| | | @Autowired |
| | | private PoliceCarEquipmentService policeCarEquipmentService; |
| | | private DeviceChannelPoliceCameraService deviceChannelPoliceCameraService; |
| | | |
| | | @PostConstruct |
| | | public void init() { |
| | | udpServerHandler = this; |
| | | udpServerHandler.policeCarEquipmentService = this.policeCarEquipmentService; |
| | | udpServerHandler.deviceChannelPoliceCameraService = this.deviceChannelPoliceCameraService; |
| | | } |
| | | |
| | | @Override |
| | |
| | | // 字节转16进制字符串 |
| | | String s = bytesToHexString(req); |
| | | //获取字符串的长度 |
| | | // int length = s.length(); |
| | | int length = s.length(); |
| | | // System.out.println("数据长度>>>>>> = " + length); |
| | | //数据解析 |
| | | dataHandler(s); |
| | |
| | | * @param s 16位字符串 |
| | | */ |
| | | private void dataHandler(String s) { |
| | | // s = "AAAACCCC220000000033373933343732393100000000000000000000000052D50451F77D5D406C04E275FD723C400000015F0000000507E70306103B0A"; |
| | | System.out.println("s = " + s); |
| | | s = "AAAACCCC220000000033373933343732393100000000000000000000000052D50451F77D5D406C04E275FD723C400000015F0000000507E70306103B0A"; |
| | | System.out.println("s = " + s); |
| | | //创建设备对象 |
| | | PoliceCarEquipment backEquipment = new PoliceCarEquipment(); |
| | | if (s.substring(4,8).equals("CCCC")) { |
| | | backEquipment.setStatus(1); |
| | | } |
| | | //断开连接 |
| | | if (s.substring(4,8).equals("FFFF")) { |
| | | backEquipment.setStatus(0); |
| | | } |
| | | //截取字符串 |
| | | String terminalNumber = hexStr2Str(s.substring(20, 60)); |
| | | // char[20] 后面跟了 \0000 空字符串 |
| | | String trim = terminalNumber.trim(); |
| | | backEquipment.setTerminalNumber(trim); |
| | | |
| | | backEquipment.setLongitude(Double.toString(getDouble(s.substring(60, 76)))); |
| | | |
| | | backEquipment.setLatitude(Double.toString(getDouble(s.substring(76, 92)))); |
| | | |
| | | backEquipment.setSpeed(Short.toString(hexTohort(s.substring(92, 96)))); |
| | | backEquipment.setDirection(Short.toString(hexTohort(s.substring(96, 100)))); |
| | | backEquipment.setElevation(Short.toString(hexTohort(s.substring(100, 104)))); |
| | | backEquipment.setPrecisions(Short.toString(hexTohort(s.substring(104, 108)))); |
| | | |
| | | DeviceMobilePositionPoliceCamera backEquipment = new DeviceMobilePositionPoliceCamera(); |
| | | // if (s.substring(4,8).equals("CCCC")) { |
| | | // backEquipment.setStatus(1); |
| | | // } |
| | | // //断开连接 |
| | | // if (s.substring(4,8).equals("FFFF")) { |
| | | // backEquipment.setStatus(0); |
| | | // } |
| | | //截取字符串,并去除后面多余的空格(如果有) |
| | | backEquipment.setChannelId(hexStr2Str(s.substring(20, 60)).trim()); |
| | | //设置经纬度 |
| | | backEquipment.setLongitude(getDouble(s.substring(60, 76))); |
| | | backEquipment.setLatitude(getDouble(s.substring(76, 92))); |
| | | // 设置速度、方向等 |
| | | backEquipment.setSpeed(hexTohort(s.substring(92, 96))); |
| | | backEquipment.setDirection(hexTohort(s.substring(96, 100))); |
| | | backEquipment.setAltitude(hexTohort(s.substring(100, 104))); |
| | | backEquipment.setReportSource(Short.toString(hexTohort(s.substring(104, 108)))); |
| | | //时间拼接 |
| | | String year = Short.toString(hexTohort(s.substring(108, 112))); |
| | | |
| | | String month = Short.toString(hexToByte(s.substring(112, 114))); |
| | | if (Integer.parseInt(month)<10){ |
| | | month = "0" + month; |
| | |
| | | second = "0" + second; |
| | | } |
| | | String time = year + "-" + month + "-" + day +" " + hour + ":" + minute + ":" + second; |
| | | //时间拼接 |
| | | try { |
| | | backEquipment.setReceiveTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time)); |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | backEquipment.setUpdateTime(new Date()); |
| | | backEquipment.setTime(time); |
| | | // 该处设置成和通道一样 |
| | | backEquipment.setDeviceId(backEquipment.getChannelId()); |
| | | // 新增 |
| | | udpServerHandler.policeCarEquipmentService.save(backEquipment); |
| | | udpServerHandler.deviceChannelPoliceCameraService.save(backEquipment); |
| | | } |
| | | |
| | | /** |
| | |
| | | return sb.toString(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 捕获异常 |
| | | * @param ctx |