From fbb9b4374821b43d0b85aa569977415fd8d77cd9 Mon Sep 17 00:00:00 2001 From: ‘liusuyi’ <1951119284@qq.com> Date: 星期一, 03 七月 2023 16:01:39 +0800 Subject: [PATCH] 增加雷达抽油机停机推送mqtt --- /dev/null | 197 ------------ src/main/java/com/ard/utils/DateUtils.java | 201 ++++++++++++ src/main/java/com/ard/utils/SpringTool.java | 19 - src/main/java/com/ard/utils/tcp/NettyTcpClientHandler.java | 282 +++++++---------- src/main/java/com/ard/utils/GisUtils.java | 29 + src/main/java/com/ard/utils/LonlatConver.java | 7 src/main/java/com/ard/utils/mqtt/MqttConsumer.java | 2 src/main/java/com/ard/utils/tcp/MessageParsing.java | 96 ++++++ src/main/java/com/ard/hiksdk/service/impl/FMSGCallBack.java | 2 src/main/java/com/ard/utils/ByteUtils.java | 82 +++++ src/main/resources/application.yml | 12 11 files changed, 537 insertions(+), 392 deletions(-) diff --git a/src/main/java/com/ard/hiksdk/service/impl/FMSGCallBack.java b/src/main/java/com/ard/hiksdk/service/impl/FMSGCallBack.java index b345927..2803831 100644 --- a/src/main/java/com/ard/hiksdk/service/impl/FMSGCallBack.java +++ b/src/main/java/com/ard/hiksdk/service/impl/FMSGCallBack.java @@ -7,9 +7,9 @@ import com.ard.config.MinioClientSingleton; import com.ard.hiksdk.common.GlobalVariable; import com.ard.hiksdk.domain.alarmEventInfo; -import com.ard.hiksdk.util.DateUtils; import com.ard.hiksdk.util.hikSdkUtil.HCNetSDK; import com.ard.hiksdk.util.minio.MinioUtils; +import com.ard.utils.DateUtils; import com.ard.utils.SpringTool; import com.ard.utils.mqtt.MqttConsumer; import com.sun.jna.Pointer; diff --git a/src/main/java/com/ard/hiksdk/util/DateUtils.java b/src/main/java/com/ard/hiksdk/util/DateUtils.java deleted file mode 100644 index 2bb715f..0000000 --- a/src/main/java/com/ard/hiksdk/util/DateUtils.java +++ /dev/null @@ -1,197 +0,0 @@ -package com.ard.hiksdk.util; -import org.apache.commons.lang3.time.DateFormatUtils; - -import java.lang.management.ManagementFactory; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.time.*; -import java.util.Date; -/** - * @ClassName DateUtils - * @Description: 杩欐槸涓�涓椂闂村伐鍏风被 - * @Author 鍒樿嫃涔� - * @Date 2023/2/16 21:58 - * @Version 1.0 - */ -public class DateUtils extends org.apache.commons.lang3.time.DateUtils -{ - public static String YYYY = "yyyy"; - - public static String YYYY_MM = "yyyy-MM"; - - public static String YYYY_MM_DD = "yyyy-MM-dd"; - - public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss"; - - public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; - - private static String[] parsePatterns = { - "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", - "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", - "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"}; - - /** - * 鑾峰彇褰撳墠Date鍨嬫棩鏈� - * - * @return Date() 褰撳墠鏃ユ湡 - */ - public static Date getNowDate() - { - return new Date(); - } - - /** - * 鑾峰彇褰撳墠鏃ユ湡, 榛樿鏍煎紡涓簓yyy-MM-dd - * - * @return String - */ - public static String getDate() - { - return dateTimeNow(YYYY_MM_DD); - } - - public static final String getTime() - { - return dateTimeNow(YYYY_MM_DD_HH_MM_SS); - } - - public static final String dateTimeNow() - { - return dateTimeNow(YYYYMMDDHHMMSS); - } - - public static final String dateTimeNow(final String format) - { - return parseDateToStr(format, new Date()); - } - - public static final String dateTime(final Date date) - { - return parseDateToStr(YYYY_MM_DD, date); - } - - public static final String parseDateToStr(final String format, final Date date) - { - return new SimpleDateFormat(format).format(date); - } - - public static final Date dateTime(final String format, final String ts) - { - try - { - return new SimpleDateFormat(format).parse(ts); - } - catch (ParseException e) - { - throw new RuntimeException(e); - } - } - - /** - * 鏃ユ湡璺緞 鍗冲勾/鏈�/鏃� 濡�2018/08/08 - */ - public static final String datePath() - { - Date now = new Date(); - return DateFormatUtils.format(now, "yyyy/MM/dd"); - } - - /** - * 鏃ユ湡璺緞 鍗冲勾/鏈�/鏃� 濡�20180808 - */ - public static final String dateTime() - { - Date now = new Date(); - return DateFormatUtils.format(now, "yyyyMMdd"); - } - - /** - * 鏃ユ湡鍨嬪瓧绗︿覆杞寲涓烘棩鏈� 鏍煎紡 - */ - public static Date parseDate(Object str) - { - if (str == null) - { - return null; - } - try - { - return parseDate(str.toString(), parsePatterns); - } - catch (ParseException e) - { - return null; - } - } - - /** - * 鑾峰彇鏈嶅姟鍣ㄥ惎鍔ㄦ椂闂� - */ - public static Date getServerStartDate() - { - long time = ManagementFactory.getRuntimeMXBean().getStartTime(); - return new Date(time); - } - - /** - * 璁$畻鐩稿樊澶╂暟 - */ - public static int differentDaysByMillisecond(Date date1, Date date2) - { - return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24))); - } - - /** - * 璁$畻涓や釜鏃堕棿宸� - */ - public static String getDatePoor(Date endDate, Date nowDate) - { - long nd = 1000 * 24 * 60 * 60; - long nh = 1000 * 60 * 60; - long nm = 1000 * 60; - // long ns = 1000; - // 鑾峰緱涓や釜鏃堕棿鐨勬绉掓椂闂村樊寮� - long diff = endDate.getTime() - nowDate.getTime(); - // 璁$畻宸灏戝ぉ - long day = diff / nd; - // 璁$畻宸灏戝皬鏃� - long hour = diff % nd / nh; - // 璁$畻宸灏戝垎閽� - long min = diff % nd % nh / nm; - // 璁$畻宸灏戠//杈撳嚭缁撴灉 - // long sec = diff % nd % nh % nm / ns; - return day + "澶�" + hour + "灏忔椂" + min + "鍒嗛挓"; - } - - /** - * 澧炲姞 LocalDateTime ==> Date - */ - public static Date toDate(LocalDateTime temporalAccessor) - { - ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault()); - return Date.from(zdt.toInstant()); - } - - /** - * 澧炲姞 LocalDate ==> Date - */ - public static Date toDate(LocalDate temporalAccessor) - { - LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0)); - ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault()); - return Date.from(zdt.toInstant()); - } - - //SDK鏃堕棿瑙f瀽 - public static String parseTime(int time) - { - int year=(time>>26)+2000; - int month=(time>>22)&15; - int day=(time>>17)&31; - int hour=(time>>12)&31; - int min=(time>>6)&63; - int second=(time>>0)&63; - String sTime=String.format("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, min, second); - return sTime; - } -} diff --git a/src/main/java/com/ard/utils/ByteUtils.java b/src/main/java/com/ard/utils/ByteUtils.java new file mode 100644 index 0000000..507ddcc --- /dev/null +++ b/src/main/java/com/ard/utils/ByteUtils.java @@ -0,0 +1,82 @@ +package com.ard.utils; + +import javax.xml.bind.DatatypeConverter; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @Description: 瀛楄妭宸ュ叿绫� + * @ClassName: byteUtils + * @Author: 鍒樿嫃涔� + * @Date: 2023骞�07鏈�03鏃�15:18 + * @Version: 1.0 + **/ +public class ByteUtils { + /** + * Byte瀛楄妭杞琀ex + * @param b 瀛楄妭 + * @return Hex + */ + public static String byteToHex(byte b) + { + String hexString = Integer.toHexString(b & 0xFF); + //鐢变簬鍗佸叚杩涘埗鏄敱0~9銆丄~F鏉ヨ〃绀�1~16锛屾墍浠ュ鏋淏yte杞崲鎴怘ex鍚庡鏋滄槸<16,灏变細鏄竴涓瓧绗︼紙姣斿A=10锛夛紝閫氬父鏄娇鐢ㄤ袱涓瓧绗︽潵琛ㄧず16杩涘埗浣嶇殑, + //鍋囧涓�涓瓧绗︾殑璇濓紝閬囧埌瀛楃涓�11锛岃繖鍒板簳鏄�1涓瓧鑺傦紝杩樻槸1鍜�1涓や釜瀛楄妭锛屽鏄撴贩娣嗭紝濡傛灉鏄ˉ0锛岄偅涔�1鍜�1琛ュ厖鍚庡氨鏄�0101锛�11灏辫〃绀虹函绮圭殑11 + if (hexString.length() < 2) + { + hexString = new StringBuilder(String.valueOf(0)).append(hexString).toString(); + } + return hexString.toUpperCase(); + } + /** + * byte鏁扮粍杞琭loat + */ + public static float bytesToFloat(byte[] bytes) { + ByteBuffer buffer = ByteBuffer.wrap(bytes); + return buffer.getFloat(); + } + /** + * byte鏁扮粍杞暣鍨� + */ + public static int bytesToDecimal(byte[] byteArray) { + int decimalValue = 0; + + for (int i = 0; i < byteArray.length; i++) { + decimalValue = (decimalValue << 8) | (byteArray[i] & 0xFF); + } + + return decimalValue; + } + + /** + * byte鏁扮粍杞珼ouble + */ + public static double bytesToDouble(byte[] byteArray) { + long longBits = 0; + + // 鏍规嵁瀛楄妭鏁扮粍鐨勯暱搴﹀拰瀛楄妭椤哄簭锛屽皢瀛楄妭鏁扮粍杞崲涓洪暱鏁村瀷 + for (int i = 0; i < byteArray.length; i++) { + longBits |= (long) (byteArray[i] & 0xFF) << (8 * (byteArray.length - 1 - i)); + } + + // 浣跨敤Double.longBitsToDouble鏂规硶灏嗛暱鏁村瀷杞崲涓篋ouble绫诲瀷 + return Double.longBitsToDouble(longBits); + } + + /** + * 澶х杞皬绔� + */ + public static byte[] toLittleEndian(byte[] bigEndianBytes) { + byte[] littleEndianBytes = new byte[bigEndianBytes.length]; + + for (int i = 0; i < bigEndianBytes.length; i++) { + int j = bigEndianBytes.length - i - 1; + littleEndianBytes[i] = bigEndianBytes[j]; + } + + return littleEndianBytes; + } + +} diff --git a/src/main/java/com/ard/utils/DateUtils.java b/src/main/java/com/ard/utils/DateUtils.java index 41b20ed..00c25d9 100644 --- a/src/main/java/com/ard/utils/DateUtils.java +++ b/src/main/java/com/ard/utils/DateUtils.java @@ -1,16 +1,201 @@ package com.ard.utils; +import org.apache.commons.lang3.time.DateFormatUtils; -import java.time.LocalDateTime; +import java.lang.management.ManagementFactory; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.*; import java.time.format.DateTimeFormatter; +import java.util.Date; /** - * @Description: 鏃堕棿澶勭悊宸ュ叿 - * @ClassName: DateUtils - * @Author: 鍒樿嫃涔� - * @Date: 2023骞�06鏈�06鏃�10:04 - * @Version: 1.0 - **/ -public class DateUtils { + * @ClassName DateUtils + * @Description: 杩欐槸涓�涓椂闂村伐鍏风被 + * @Author 鍒樿嫃涔� + * @Date 2023/2/16 21:58 + * @Version 1.0 + */ +public class DateUtils extends org.apache.commons.lang3.time.DateUtils +{ + public static String YYYY = "yyyy"; + + public static String YYYY_MM = "yyyy-MM"; + + public static String YYYY_MM_DD = "yyyy-MM-dd"; + + public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss"; + + public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; + + private static String[] parsePatterns = { + "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", + "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", + "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"}; + + /** + * 鑾峰彇褰撳墠Date鍨嬫棩鏈� + * + * @return Date() 褰撳墠鏃ユ湡 + */ + public static Date getNowDate() + { + return new Date(); + } + + /** + * 鑾峰彇褰撳墠鏃ユ湡, 榛樿鏍煎紡涓簓yyy-MM-dd + * + * @return String + */ + public static String getDate() + { + return dateTimeNow(YYYY_MM_DD); + } + + public static final String getTime() + { + return dateTimeNow(YYYY_MM_DD_HH_MM_SS); + } + + public static final String dateTimeNow() + { + return dateTimeNow(YYYYMMDDHHMMSS); + } + + public static final String dateTimeNow(final String format) + { + return parseDateToStr(format, new Date()); + } + + public static final String dateTime(final Date date) + { + return parseDateToStr(YYYY_MM_DD, date); + } + + public static final String parseDateToStr(final String format, final Date date) + { + return new SimpleDateFormat(format).format(date); + } + + public static final Date dateTime(final String format, final String ts) + { + try + { + return new SimpleDateFormat(format).parse(ts); + } + catch (ParseException e) + { + throw new RuntimeException(e); + } + } + + /** + * 鏃ユ湡璺緞 鍗冲勾/鏈�/鏃� 濡�2018/08/08 + */ + public static final String datePath() + { + Date now = new Date(); + return DateFormatUtils.format(now, "yyyy/MM/dd"); + } + + /** + * 鏃ユ湡璺緞 鍗冲勾/鏈�/鏃� 濡�20180808 + */ + public static final String dateTime() + { + Date now = new Date(); + return DateFormatUtils.format(now, "yyyyMMdd"); + } + + /** + * 鏃ユ湡鍨嬪瓧绗︿覆杞寲涓烘棩鏈� 鏍煎紡 + */ + public static Date parseDate(Object str) + { + if (str == null) + { + return null; + } + try + { + return parseDate(str.toString(), parsePatterns); + } + catch (ParseException e) + { + return null; + } + } + + /** + * 鑾峰彇鏈嶅姟鍣ㄥ惎鍔ㄦ椂闂� + */ + public static Date getServerStartDate() + { + long time = ManagementFactory.getRuntimeMXBean().getStartTime(); + return new Date(time); + } + + /** + * 璁$畻鐩稿樊澶╂暟 + */ + public static int differentDaysByMillisecond(Date date1, Date date2) + { + return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24))); + } + + /** + * 璁$畻涓や釜鏃堕棿宸� + */ + public static String getDatePoor(Date endDate, Date nowDate) + { + long nd = 1000 * 24 * 60 * 60; + long nh = 1000 * 60 * 60; + long nm = 1000 * 60; + // long ns = 1000; + // 鑾峰緱涓や釜鏃堕棿鐨勬绉掓椂闂村樊寮� + long diff = endDate.getTime() - nowDate.getTime(); + // 璁$畻宸灏戝ぉ + long day = diff / nd; + // 璁$畻宸灏戝皬鏃� + long hour = diff % nd / nh; + // 璁$畻宸灏戝垎閽� + long min = diff % nd % nh / nm; + // 璁$畻宸灏戠//杈撳嚭缁撴灉 + // long sec = diff % nd % nh % nm / ns; + return day + "澶�" + hour + "灏忔椂" + min + "鍒嗛挓"; + } + + /** + * 澧炲姞 LocalDateTime ==> Date + */ + public static Date toDate(LocalDateTime temporalAccessor) + { + ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault()); + return Date.from(zdt.toInstant()); + } + + /** + * 澧炲姞 LocalDate ==> Date + */ + public static Date toDate(LocalDate temporalAccessor) + { + LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0)); + ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault()); + return Date.from(zdt.toInstant()); + } + + //SDK鏃堕棿瑙f瀽 + public static String parseTime(int time) + { + int year=(time>>26)+2000; + int month=(time>>22)&15; + int day=(time>>17)&31; + int hour=(time>>12)&31; + int min=(time>>6)&63; + int second=(time>>0)&63; + String sTime=String.format("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, min, second); + return sTime; + } /** * 鏃ユ湡瀛楃涓茬殑鏈堟棩鏃跺垎绉掕嚜鍔ㄨˉ闆�,骞跺皢/鏇挎崲鎴�- diff --git a/src/main/java/com/ard/utils/GisUtils.java b/src/main/java/com/ard/utils/GisUtils.java new file mode 100644 index 0000000..df21a12 --- /dev/null +++ b/src/main/java/com/ard/utils/GisUtils.java @@ -0,0 +1,29 @@ +package com.ard.utils; + +import org.gavaghan.geodesy.Ellipsoid; +import org.gavaghan.geodesy.GeodeticCalculator; +import org.gavaghan.geodesy.GlobalCoordinates; + +/** + * @Description: gis宸ュ叿绫� + * @ClassName: GisUtils + * @Author: 鍒樿嫃涔� + * @Date: 2023骞�07鏈�03鏃�15:23 + * @Version: 1.0 + **/ +public class GisUtils { + /** + * 閫氳繃A鐐瑰潗鏍囷紝闀垮害鍜孻杞磋搴﹁绠桞鐐瑰潗鏍� + */ + public static Double[] CalculateCoordinates(Double[] radarCoordinates, Double distance, Double angle) { + double[] to_wgs84 = LonlatConver.gcj02_To_Wgs84(radarCoordinates[0], radarCoordinates[1]); + double Ax = to_wgs84[0]; // A 鐐圭殑 X 鍧愭爣 + double Ay = to_wgs84[1]; // A 鐐圭殑 Y 鍧愭爣 + double AB = distance; // AB 鐨勯暱搴� + double angleWithYAxisDegrees = angle; // AB 涓� Y 杞寸殑瑙掑害锛堜互搴︽暟琛ㄧず锛� + GeodeticCalculator calculator = new GeodeticCalculator(); + GlobalCoordinates globalCoordinates = new GlobalCoordinates(Ay, Ax); + GlobalCoordinates globalCoordinates1 = calculator.calculateEndingGlobalCoordinates(Ellipsoid.WGS84, globalCoordinates, angleWithYAxisDegrees, AB); + return new Double[]{globalCoordinates1.getLongitude(), globalCoordinates1.getLatitude()}; + } +} diff --git a/src/main/java/com/ard/utils/LonlatConver.java b/src/main/java/com/ard/utils/LonlatConver.java index ff02333..cd8c092 100644 --- a/src/main/java/com/ard/utils/LonlatConver.java +++ b/src/main/java/com/ard/utils/LonlatConver.java @@ -1,6 +1,11 @@ package com.ard.utils; - +/** + * 缁忕含搴﹁浆鎹㈠伐鍏� + * 鍒樿嫃涔� + * 2023/7/3 15:15 + */ public class LonlatConver { + /** * a */ diff --git a/src/main/java/com/ard/utils/SpringTool.java b/src/main/java/com/ard/utils/SpringTool.java index e152dde..0a7acfb 100644 --- a/src/main/java/com/ard/utils/SpringTool.java +++ b/src/main/java/com/ard/utils/SpringTool.java @@ -4,6 +4,9 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; + +import java.nio.ByteBuffer; + /** * @Description: * @ClassName: SpringTool @@ -33,20 +36,4 @@ return getApplicationContext().getBean(name); } - /** - * Byte瀛楄妭杞琀ex - * @param b 瀛楄妭 - * @return Hex - */ - public static String byteToHex(byte b) - { - String hexString = Integer.toHexString(b & 0xFF); - //鐢变簬鍗佸叚杩涘埗鏄敱0~9銆丄~F鏉ヨ〃绀�1~16锛屾墍浠ュ鏋淏yte杞崲鎴怘ex鍚庡鏋滄槸<16,灏变細鏄竴涓瓧绗︼紙姣斿A=10锛夛紝閫氬父鏄娇鐢ㄤ袱涓瓧绗︽潵琛ㄧず16杩涘埗浣嶇殑, - //鍋囧涓�涓瓧绗︾殑璇濓紝閬囧埌瀛楃涓�11锛岃繖鍒板簳鏄�1涓瓧鑺傦紝杩樻槸1鍜�1涓や釜瀛楄妭锛屽鏄撴贩娣嗭紝濡傛灉鏄ˉ0锛岄偅涔�1鍜�1琛ュ厖鍚庡氨鏄�0101锛�11灏辫〃绀虹函绮圭殑11 - if (hexString.length() < 2) - { - hexString = new StringBuilder(String.valueOf(0)).append(hexString).toString(); - } - return hexString.toUpperCase(); - } } \ No newline at end of file diff --git a/src/main/java/com/ard/utils/mqtt/MqttConsumer.java b/src/main/java/com/ard/utils/mqtt/MqttConsumer.java index 1c07850..ea2b31a 100644 --- a/src/main/java/com/ard/utils/mqtt/MqttConsumer.java +++ b/src/main/java/com/ard/utils/mqtt/MqttConsumer.java @@ -75,7 +75,7 @@ // 璁剧疆浼氳瘽蹇冭烦鏃堕棿 options.setKeepAliveInterval(PropertiesUtil.MQTT_KEEP_ALIVE); // 鏄惁娓呴櫎session - options.setCleanSession(false); + options.setCleanSession(true); log.info("--鐢熸垚mqtt閰嶇疆瀵硅薄"); return options; } diff --git a/src/main/java/com/ard/utils/tcp/MessageParsing.java b/src/main/java/com/ard/utils/tcp/MessageParsing.java new file mode 100644 index 0000000..14aa3c5 --- /dev/null +++ b/src/main/java/com/ard/utils/tcp/MessageParsing.java @@ -0,0 +1,96 @@ +package com.ard.utils.tcp; + +import javax.xml.bind.DatatypeConverter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @Description: 鎶ユ枃瑙f瀽 + * @ClassName: MessageParsing + * @Author: 鍒樿嫃涔� + * @Date: 2023骞�07鏈�03鏃�15:30 + * @Version: 1.0 + **/ +public class MessageParsing { + // 鍒涘缓缂撳啿鍖哄垪琛� + private static List<Byte> buffer = new ArrayList<>(); + + /** + * 鎺ユ敹瀹屾暣鍖� + */ + public static byte[] receiveCompletePacket(byte[] receivedData) { + // 瀹氫箟鍖呭熬瀛楄妭搴忓垪 + byte[] packetEnd = {0x01, 0x02, 0x00}; + // 娣诲姞宸叉帴鏀剁殑鏁版嵁鍒扮紦鍐插尯 + for (byte data : receivedData) { + buffer.add(data); + } + // 妫�鏌ョ紦鍐插尯涓殑鏁版嵁鏄惁鍖呭惈瀹屾暣鐨勫寘 + while (buffer.size() >= packetEnd.length) { + int endIndex = findPacketEndIndex(buffer, packetEnd); + if (endIndex != -1) { + // 鎵惧埌瀹屾暣鐨勫寘 + byte[] packet = extractPacketFromBuffer(buffer, endIndex + packetEnd.length); + return packet; + } else { + // 鏈壘鍒板寘灏撅紝缁х画鎺ユ敹鏁版嵁 + break; + } + } + + // 鏈壘鍒板畬鏁寸殑鍖� + return null; + } + + /** + * 鑾峰彇鍖呯粨鏉熺储寮� + */ + public static int findPacketEndIndex(List<Byte> buffer, byte[] packetEnd) { + for (int i = 0; i <= buffer.size() - packetEnd.length; i++) { + boolean isMatch = true; + for (int j = 0; j < packetEnd.length; j++) { + if (buffer.get(i + j) != packetEnd[j]) { + isMatch = false; + break; + } + } + if (isMatch) { + return i; + } + } + return -1; + } + + /** + * 浠庣紦鍐插尯鎻愬彇鏁版嵁鍖� + */ + public static byte[] extractPacketFromBuffer(List<Byte> buffer, int endIndex) { + byte[] packet = new byte[endIndex]; + for (int i = 0; i < endIndex; i++) { + packet[i] = buffer.get(i); + } + buffer.subList(0, endIndex).clear(); + return packet; + } + + /** + * 鍘绘帀鍖呭ご鍜屽寘灏炬牎楠屽強杞箟 + */ + public static byte[] transferData(byte[] data) { + data = Arrays.copyOfRange(data, 3, data.length); + data = Arrays.copyOfRange(data, 0, data.length - 7); + String dataStr = DatatypeConverter.printHexBinary(data); + if (dataStr.contains("01020201")) {//杞箟01020201 + dataStr = dataStr.replaceAll("01020201", "010201"); + } + if (dataStr.contains("01020200")) {//杞箟01020200 + dataStr = dataStr.replaceAll("01020200", "010200"); + } + if (dataStr.contains("01020202")) {//杞箟01020202 + dataStr = dataStr.replaceAll("01020202", "010202"); + } + data = DatatypeConverter.parseHexBinary(dataStr); + return data; + } +} diff --git a/src/main/java/com/ard/utils/tcp/NettyTcpClientHandler.java b/src/main/java/com/ard/utils/tcp/NettyTcpClientHandler.java index bb05e68..fa6016c 100644 --- a/src/main/java/com/ard/utils/tcp/NettyTcpClientHandler.java +++ b/src/main/java/com/ard/utils/tcp/NettyTcpClientHandler.java @@ -4,18 +4,14 @@ import com.ard.alarm.radar.domain.ArdEquipRadar; import com.ard.alarm.radar.domain.RadarAlarmData; import com.ard.alarm.radar.domain.ArdAlarmRadar; -import com.ard.utils.LonlatConver; +import com.ard.utils.ByteUtils; +import com.ard.utils.GisUtils; import com.ard.utils.mqtt.MqttConsumer; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import lombok.extern.slf4j.Slf4j; -import org.gavaghan.geodesy.Ellipsoid; -import org.gavaghan.geodesy.GeodeticCalculator; -import org.gavaghan.geodesy.GlobalCoordinates; - import javax.xml.bind.DatatypeConverter; -import java.nio.ByteBuffer; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.ScheduledFuture; @@ -35,6 +31,7 @@ private Integer port; private Double longitude; private Double lagitude; + private Double altitude; private String name; private String id; @@ -45,6 +42,7 @@ this.lagitude = ardEquipRadar.getLatitude(); this.name = ardEquipRadar.getName(); this.id = ardEquipRadar.getId(); + this.altitude = ardEquipRadar.getAltitude(); } private ChannelHandlerContext context; @@ -55,10 +53,8 @@ // 澶勭悊鎺ユ敹鍒扮殑娑堟伅 byte[] byteArray = new byte[msg.readableBytes()]; msg.getBytes(msg.readerIndex(), byteArray); - byte[] bytes = receiveCompletePacket(byteArray); + byte[] bytes = MessageParsing.receiveCompletePacket(byteArray); if (bytes != null) { - // String hexString = DatatypeConverter.printHexBinary(bytes); - // log.info(hexString); processData(bytes); } } @@ -109,7 +105,9 @@ */ public void processData(byte[] data) { try { - data = transferData(data);//鍘绘帀鍖呭ご鍜屽寘灏俱�佹牎楠屽強杞箟 + //log.info("鍘熷鏁版嵁:" + DatatypeConverter.printHexBinary(data)); + log.info("闆疯揪淇℃伅锛�" + host + "銆恜ort銆�" + port + "銆怷銆�" + longitude + "銆怸銆�" + lagitude + "銆怹銆�" + altitude); + data = MessageParsing.transferData(data);//鍘绘帀鍖呭ご鍜屽寘灏俱�佹牎楠屽強杞箟 byte[] type = Arrays.copyOfRange(data, 0, 1);//鍛戒护绫诲瀷 // log.info("鍛戒护绫诲瀷:" + DatatypeConverter.printHexBinary(type)); @@ -118,40 +116,41 @@ // log.info("鍛戒护ID:" + DatatypeConverter.printHexBinary(cmdId)); byte[] payloadSize = Arrays.copyOfRange(data, 2, 4);//鏈夋晥璐熻浇澶у皬 - payloadSize = toLittleEndian(payloadSize); - int payloadSizeToDecimal = byteArrayToDecimal(payloadSize); + payloadSize = ByteUtils.toLittleEndian(payloadSize); + int payloadSizeToDecimal = ByteUtils.bytesToDecimal(payloadSize); // log.info("鏈夋晥璐熻浇澶у皬(杞暣鍨�):" + payloadSizeToDecimal); - + List<ArdAlarmRadar> radarAlarmInfos = new ArrayList<>(); + String alarmTime=""; if (Arrays.equals(cmdId, new byte[]{0x01})) { byte[] dwTim = Arrays.copyOfRange(data, 4, 8); - dwTim = toLittleEndian(dwTim); + dwTim = ByteUtils.toLittleEndian(dwTim); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - long l = byteArrayToDecimal(dwTim); - String alarmTime = sdf.format(l * 1000); + long l = ByteUtils.bytesToDecimal(dwTim); + alarmTime = sdf.format(l * 1000); // log.info("鍛ㄨ鍥惧儚鐨勫嚭鐜版椂闂�(杞琩ate):" + alarmTime); byte[] wTargetNum = Arrays.copyOfRange(data, 8, 10); - wTargetNum = toLittleEndian(wTargetNum); - int targetNum = byteArrayToDecimal(wTargetNum); + wTargetNum = ByteUtils.toLittleEndian(wTargetNum); + int targetNum = ByteUtils.bytesToDecimal(wTargetNum); //log.info("鐩爣鎬荤偣鏁�(杞暣鍨�):" + targetNum); //瑙f瀽NET_TARGET_UNIT(64鏄疦ET_TARGET_HEAD鐨勫瓧鑺傛暟) int uintSize = (payloadSizeToDecimal - 64) / targetNum; // log.info("鍗曟潯鎶ヨ澶у皬:" + uintSize); - List<ArdAlarmRadar> radarAlarmInfos = new ArrayList<>(); + for (int i = 0; i < targetNum; i++) { Integer index = 68 + uintSize * i; byte[] dwID = Arrays.copyOfRange(data, index, index + 4); // log.info("鐩爣ID:" + DatatypeConverter.printHexBinary(cmdId)); - dwID = toLittleEndian(dwID); - int id = byteArrayToDecimal(dwID); + dwID = ByteUtils.toLittleEndian(dwID); + int id = ByteUtils.bytesToDecimal(dwID); // log.info("鐩爣ID鍙凤細" + id); byte[] iDistance = Arrays.copyOfRange(data, index + 8, index + 12); - iDistance = toLittleEndian(iDistance); - int Distance = byteArrayToDecimal(iDistance); + iDistance = ByteUtils.toLittleEndian(iDistance); + Double Distance = ByteUtils.bytesToDouble(iDistance); // log.info("鐩爣褰撳墠璺濈(m):" + Distance); //region 涓嶉渶瑕佺殑瀛楁 // byte[] dwGSum = Arrays.copyOfRange(data, index + 4, index + 8); @@ -200,7 +199,7 @@ //endregion String alarmType = ""; byte[] cStat = Arrays.copyOfRange(data, index + 23, index + 24); - cStat = toLittleEndian(cStat); + cStat = ByteUtils.toLittleEndian(cStat); String binaryString = String.format("%8s", Integer.toBinaryString(cStat[0] & 0xFF)).replace(' ', '0'); // log.info("鐩爣褰撳墠鐘舵��:" + binaryString); // 鎻愬彇绗�4浣嶈嚦绗�6浣嶇殑鍊� @@ -215,7 +214,7 @@ byte[] szName = Arrays.copyOfRange(data, index + 64, index + 96); int position = findIndexOfDoubleZero(szName); - String alarmPointName = ""; + String alarmPointName; if (position != -1) { byte[] result = new byte[position]; System.arraycopy(szName, 0, result, 0, position); @@ -225,16 +224,15 @@ } // log.info("鎵�灞炲憡璀﹀尯鍩熷悕绉�:" + alarmPointName); byte[] afTx = Arrays.copyOfRange(data, index + 96, index + 100); - afTx = toLittleEndian(afTx); - float fTx = bytesToFloat(afTx); + afTx = ByteUtils.toLittleEndian(afTx); + float fTx = ByteUtils.bytesToFloat(afTx); // log.info("姘村钩瑙掑害:" + fTx); byte[] afTy = Arrays.copyOfRange(data, index + 112, index + 116); - afTy = toLittleEndian(afTy); - float fTy = bytesToFloat(afTy); + afTy = ByteUtils.toLittleEndian(afTy); + float fTy = ByteUtils.bytesToFloat(afTy); // log.info("鍨傜洿瑙掑害:" + fTy); - // log.info("闆疯揪淇℃伅锛�" + host + "銆恜ort銆�" + port + "銆怷銆�" + longitude + "銆怸銆�" + lagitude); Double[] radarXY = {longitude, lagitude}; - Double[] alarmXY = CalculateCoordinates(radarXY, Distance, (double) fTx); + Double[] alarmXY = GisUtils.CalculateCoordinates(radarXY, Distance, (double) fTx); // log.info("鎶ヨ淇℃伅锛�" + "銆恑d銆�" + id + "銆恘ame銆�" + alarmPointName + "銆恆larmType銆�" + alarmType + "銆恆larmTime銆�" + alarmTime + "銆恉istance銆�" + Distance + "銆怭銆�" + fTx + "銆怲銆�" + fTy + "銆怷銆�" + alarmXY[0] + "銆怸銆�" + alarmXY[1]); ArdAlarmRadar ardAlarmRadar = new ArdAlarmRadar(); ardAlarmRadar.setTargetId(id); @@ -244,134 +242,109 @@ ardAlarmRadar.setAlarmType(alarmType); radarAlarmInfos.add(ardAlarmRadar); } - RadarAlarmData radarAlarmData = new RadarAlarmData(); - radarAlarmData.setRadarId(id); - radarAlarmData.setRadarName(name); - radarAlarmData.setAlarmTime(alarmTime); - radarAlarmData.setArdAlarmRadars(radarAlarmInfos); - MqttConsumer.publish(2, false, "radar", JSON.toJSONString(radarAlarmData)); + } - } catch (Exception ex) { - log.error(ex.getMessage()); - } - } + if (Arrays.equals(cmdId, new byte[]{0x04})) { + //String hexString = DatatypeConverter.printHexBinary(data); + //log.info(hexString); - /** - * byte鏁扮粍杞琭loat - */ - private float bytesToFloat(byte[] bytes) { - ByteBuffer buffer = ByteBuffer.wrap(bytes); - return buffer.getFloat(); - } + byte[] dwTim = Arrays.copyOfRange(data, 4, 8); + dwTim = ByteUtils.toLittleEndian(dwTim); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + long l = ByteUtils.bytesToDecimal(dwTim); + alarmTime = sdf.format(l * 1000); + //log.info("鍛ㄨ鍥惧儚鐨勫嚭鐜版椂闂�(杞琩ate):" + alarmTime); + byte[] wTargetNum = Arrays.copyOfRange(data, 8, 10); + wTargetNum = ByteUtils.toLittleEndian(wTargetNum); + int targetNum = ByteUtils.bytesToDecimal(wTargetNum); + //log.info("鐩爣鎬荤偣鏁�(杞暣鍨�):" + targetNum); - /** - * byte鏁扮粍杞暣鍨� - */ - public int byteArrayToDecimal(byte[] byteArray) { - int decimalValue = 0; + //瑙f瀽NET_TARGET_UNIT(64鏄疦ET_TARGET_HEAD鐨勫瓧鑺傛暟) + int uintSize = (payloadSizeToDecimal - 64) / targetNum; + //log.info("鍗曟潯鎶ヨ澶у皬:" + uintSize); + for (int i = 0; i < targetNum; i++) { + Integer index = 68 + uintSize * i; + byte[] dwID = Arrays.copyOfRange(data, index, index + 4); + //log.info("鐩爣ID:" + DatatypeConverter.printHexBinary(dwID)); + dwID = ByteUtils.toLittleEndian(dwID); + int id = ByteUtils.bytesToDecimal(dwID); + //log.info("鐩爣ID鍙凤細" + id); + //region 涓嶉渶瑕佺殑瀛楁 + byte[] iTw = Arrays.copyOfRange(data, index + 4, index + 8); + iTw = ByteUtils.toLittleEndian(iTw); + int Tw = ByteUtils.bytesToDecimal(iTw); + // log.info("鐩爣褰撳墠鐨勫儚绱犲搴�:" + Tw); - for (int i = 0; i < byteArray.length; i++) { - decimalValue = (decimalValue << 8) | (byteArray[i] & 0xFF); - } + byte[] iTh = Arrays.copyOfRange(data, index + 8, index + 12); + iTh = ByteUtils.toLittleEndian(iTh); + int Th = ByteUtils.bytesToDecimal(iTh); + //log.info("鐩爣褰撳墠鐨勫儚绱犻珮搴�:" + Th); - return decimalValue; - } + byte[] fTx = Arrays.copyOfRange(data, index + 12, index + 16); + fTx = ByteUtils.toLittleEndian(fTx); + float fTxAngle = ByteUtils.bytesToFloat(fTx); + //log.info("p瑙掑害:" + fTxAngle); + byte[] fTy = Arrays.copyOfRange(data, index + 16, index + 20); + fTy = ByteUtils.toLittleEndian(fTy); + float fTyAngle = ByteUtils.bytesToFloat(fTy); + //log.info("t瑙掑害:" + fTyAngle); - /** - * 澶х杞皬绔� - */ - public byte[] toLittleEndian(byte[] bigEndianBytes) { - byte[] littleEndianBytes = new byte[bigEndianBytes.length]; + byte[] sAreaNo = Arrays.copyOfRange(data, index + 20, index + 22); + sAreaNo = ByteUtils.toLittleEndian(sAreaNo); + int AreaNo = ByteUtils.bytesToDecimal(sAreaNo); + //log.info("鐩爣褰掑睘鐨勫憡璀﹀尯鍩熷彿:" + AreaNo); - for (int i = 0; i < bigEndianBytes.length; i++) { - int j = bigEndianBytes.length - i - 1; - littleEndianBytes[i] = bigEndianBytes[j]; - } + byte[] cGrp = Arrays.copyOfRange(data, index + 22, index + 23); + cGrp = ByteUtils.toLittleEndian(cGrp); + int Grp = ByteUtils.bytesToDecimal(cGrp); + //log.info("鎵�灞炵粍:" + Grp); + //endregion + String alarmType; + byte[] cStat = Arrays.copyOfRange(data, index + 23, index + 24); + cStat = ByteUtils.toLittleEndian(cStat); + //String binaryString = String.format("%8s", Integer.toBinaryString(cStat[0] & 0xFF)).replace(' ', '0'); + //log.info("鐩爣褰撳墠鐘舵��:" + binaryString); + // 鎻愬彇绗�0浣嶅�� + // 浣跨敤浣嶈繍绠楁搷浣滃垽鏂0浣嶆槸鍚︿负1 + boolean isBitSet = (cStat[0] & 0x01) == 0x00; + // 鍒ゆ柇鎻愬彇鐨勫�� + if (isBitSet) { + alarmType = "闆疯揪鎶芥补鏈哄仠鏈�"; + } else { + continue; + } + //log.info("鎶ヨ绫诲瀷:" + alarmType); - return littleEndianBytes; - } - - - // 鍒涘缓缂撳啿鍖哄垪琛� - List<Byte> buffer = new ArrayList<>(); - - /** - * 鎺ユ敹瀹屾暣鍖� - */ - public byte[] receiveCompletePacket(byte[] receivedData) { - // 瀹氫箟鍖呭熬瀛楄妭搴忓垪 - byte[] packetEnd = {0x01, 0x02, 0x00}; - // 娣诲姞宸叉帴鏀剁殑鏁版嵁鍒扮紦鍐插尯 - for (byte data : receivedData) { - buffer.add(data); - } - // 妫�鏌ョ紦鍐插尯涓殑鏁版嵁鏄惁鍖呭惈瀹屾暣鐨勫寘 - while (buffer.size() >= packetEnd.length) { - int endIndex = findPacketEndIndex(buffer, packetEnd); - if (endIndex != -1) { - // 鎵惧埌瀹屾暣鐨勫寘 - byte[] packet = extractPacketFromBuffer(buffer, endIndex + packetEnd.length); - return packet; - } else { - // 鏈壘鍒板寘灏撅紝缁х画鎺ユ敹鏁版嵁 - break; - } - } - - // 鏈壘鍒板畬鏁寸殑鍖� - return null; - } - - /** - * 鑾峰彇鍖呯粨鏉熺储寮� - */ - public int findPacketEndIndex(List<Byte> buffer, byte[] packetEnd) { - for (int i = 0; i <= buffer.size() - packetEnd.length; i++) { - boolean isMatch = true; - for (int j = 0; j < packetEnd.length; j++) { - if (buffer.get(i + j) != packetEnd[j]) { - isMatch = false; - break; + byte[] szName = Arrays.copyOfRange(data, index + 32, index + 64); + //log.info("鎵�灞炲憡璀﹀尯鍩熷悕绉�:" + DatatypeConverter.printHexBinary(szName)); + int position = findIndexOfDoubleZero(szName); + String alarmPointName; + if (position != -1) { + byte[] result = new byte[position]; + System.arraycopy(szName, 0, result, 0, position); + alarmPointName = new String(result, "GBK"); + } else { + alarmPointName = new String(szName, "GBK"); + } + // log.info("鎵�灞炲憡璀﹀尯鍩熷悕绉�:" + alarmPointName); + //log.info("鎶ヨ淇℃伅锛�" + "銆恑d銆�" + id + "銆恘ame銆�" + alarmPointName + "銆恆larmType銆�" + alarmType + "銆恆larmTime銆�" + alarmTime); + ArdAlarmRadar ardAlarmRadar = new ArdAlarmRadar(); + ardAlarmRadar.setTargetId(id); + ardAlarmRadar.setName(alarmPointName); + ardAlarmRadar.setAlarmType(alarmType); + radarAlarmInfos.add(ardAlarmRadar); } } - if (isMatch) { - return i; - } + RadarAlarmData radarAlarmData = new RadarAlarmData(); + radarAlarmData.setRadarId(id); + radarAlarmData.setRadarName(name); + radarAlarmData.setAlarmTime(alarmTime); + radarAlarmData.setArdAlarmRadars(radarAlarmInfos); + MqttConsumer.publish(2, false, "radar", JSON.toJSONString(radarAlarmData)); + } catch (Exception ex) { + log.error("闆疯揪鎶ユ枃瑙f瀽寮傚父:" + ex.getMessage()); } - return -1; - } - - /** - * 浠庣紦鍐插尯鎻愬彇鏁版嵁鍖� - */ - public byte[] extractPacketFromBuffer(List<Byte> buffer, int endIndex) { - byte[] packet = new byte[endIndex]; - for (int i = 0; i < endIndex; i++) { - packet[i] = buffer.get(i); - } - buffer.subList(0, endIndex).clear(); - return packet; - } - - /** - * 鍘绘帀鍖呭ご鍜屽寘灏炬牎楠屽強杞箟 - */ - public byte[] transferData(byte[] data) { - data = Arrays.copyOfRange(data, 3, data.length); - data = Arrays.copyOfRange(data, 0, data.length - 7); - String dataStr = DatatypeConverter.printHexBinary(data); - if (dataStr.contains("01020201")) {//杞箟01020201 - dataStr = dataStr.replaceAll("01020201", "010201"); - } - if (dataStr.contains("01020200")) {//杞箟01020200 - dataStr = dataStr.replaceAll("01020200", "010200"); - } - if (dataStr.contains("01020202")) {//杞箟01020202 - dataStr = dataStr.replaceAll("01020202", "010202"); - } - data = DatatypeConverter.parseHexBinary(dataStr); - return data; } /** @@ -384,21 +357,6 @@ } } return -1; - } - - /** - * 閫氳繃A鐐瑰潗鏍囷紝闀垮害鍜孻杞磋搴﹁绠桞鐐瑰潗鏍� - */ - public Double[] CalculateCoordinates(Double[] radarCoordinates, Integer distance, Double angle) { - double[] to_wgs84 = LonlatConver.gcj02_To_Wgs84(radarCoordinates[0], radarCoordinates[1]); - double Ax = to_wgs84[0]; // A 鐐圭殑 X 鍧愭爣 - double Ay = to_wgs84[1]; // A 鐐圭殑 Y 鍧愭爣 - double AB = distance; // AB 鐨勯暱搴� - double angleWithYAxisDegrees = angle; // AB 涓� Y 杞寸殑瑙掑害锛堜互搴︽暟琛ㄧず锛� - GeodeticCalculator calculator = new GeodeticCalculator(); - GlobalCoordinates globalCoordinates = new GlobalCoordinates(Ay, Ax); - GlobalCoordinates globalCoordinates1 = calculator.calculateEndingGlobalCoordinates(Ellipsoid.WGS84, globalCoordinates, angleWithYAxisDegrees, AB); - return new Double[]{globalCoordinates1.getLongitude(), globalCoordinates1.getLatitude()}; } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 2e6ddc4..50a9b71 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -9,7 +9,7 @@ enabled: true mqtt: host: tcp://192.168.1.15:1883 - clientId: c3 + clientId: cc3 username: admin password: xzx12345 topic: tube @@ -20,12 +20,12 @@ url: http://iot.zhdk.net:8090/Warning/GetWarning?userName=cy4oil datasource: driver-class-name: org.postgresql.Driver - url: jdbc:postgresql://111.40.46.199:15432/ry-vue?stringtype=unspecified - username: postgres - password: Yykj.2021 -# url: jdbc:postgresql://192.168.1.15:5432/ry-vue?stringtype=unspecified +# url: jdbc:postgresql://111.40.46.199:15432/ry-vue?stringtype=unspecified # username: postgres -# password: postgres +# password: Yykj.2021 + url: jdbc:postgresql://192.168.1.15:5432/ry-vue?stringtype=unspecified + username: postgres + password: postgres mybatis: typeAliasesPackage: com.ard.alarm.**.domain mapperLocations: classpath:/mapper/*.xml -- Gitblit v1.9.3