diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/PileTimeConfigReplyDataLogic.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/PileTimeConfigReplyDataLogic.java index ca9ff2d6..1b2224ca 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/PileTimeConfigReplyDataLogic.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/PileTimeConfigReplyDataLogic.java @@ -15,8 +15,7 @@ import java.util.Map; import static cn.hutool.core.date.DatePattern.UTC_FORMAT; import static com.xhpc.pp.server.ChargingPileServer.REDIS; -import static com.xhpc.pp.utils.security.CP56Time2a.toDate; -import static com.xhpc.pp.utils.security.HexUtils.toBytes; +import static com.xhpc.pp.utils.security.CP56Time2a.cp56toDate; @Lazy @Component("PileTimeConfigReplyDataLogic") @@ -39,9 +38,18 @@ public class PileTimeConfigReplyDataLogic implements ServiceLogic { log.info("({}) set time success", pileNo); } else { // todo may call back mgmt backend - log.error("({}) set time failed: √[{}] ×[{}]", pileNo, DateUtil.format(toDate(toBytes(setTime)), UTC_FORMAT), DateUtil.format(toDate(toBytes(configTime)), UTC_FORMAT)); + log.error("({}) set time failed: √[{}] ×[{}]", pileNo, DateUtil.format(cp56toDate(setTime), UTC_FORMAT), DateUtil.format(cp56toDate(configTime), UTC_FORMAT)); } return new ServiceResult(false); } + private static String getCP56time2a(String str) { + + return "20" + Integer.parseInt(str.substring(12, 14), 16) + "-" + Integer.parseInt(str.substring(10, 12), 16) + + "-" + Integer.parseInt(str.substring(8, 10), 16) + " " + Integer.parseInt(str.substring(6, 8), 16) + + ":" + Integer.parseInt(str.substring(4, 6), 16) + ":" + + Integer.parseInt(str.substring(2, 4) + "" + str.substring(0, 2), 16) / 1000; + + } + } diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/server/ChargingPileBinaryHandler.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/server/ChargingPileBinaryHandler.java index 14e5cea1..897f6a8b 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/server/ChargingPileBinaryHandler.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/server/ChargingPileBinaryHandler.java @@ -23,7 +23,7 @@ import java.util.*; import static com.xhpc.common.data.redis.SeqUtil.seqHex; import static com.xhpc.pp.server.ChargingPileServer.REDIS; import static com.xhpc.pp.tx.ServiceResult.OK; -import static com.xhpc.pp.utils.security.CP56Time2a.toBytes; +import static com.xhpc.pp.utils.security.CP56Time2a.toCp56Hex; import static com.xhpc.pp.utils.security.CRCCalculator.calcCrc; import static com.xhpc.pp.utils.security.HexUtils.toHex; @@ -95,7 +95,7 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler { Calendar calendar = Calendar.getInstance(); String timebin = getTimeBin(seqHex(pilekey.concat("seqhex")), pileNo, calendar); Map cachePile = REDIS.getCacheMap(pilekey); - cachePile.put("setTime", toHex(toBytes(calendar.getTime()))); + cachePile.put("setTime", toCp56Hex(calendar.getTime())); REDIS.setCacheMap(pilekey, cachePile); log.info("server send time config msg >>>> ({}) |{}|", pileNo, timebin); handler.sendClientBinary(HexUtils.toBytes(timebin)); @@ -113,7 +113,7 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler { private String getTimeBin(String seqhex, String pileNo, Calendar calendar) { - String timebin = "6812".concat(seqhex).concat("0056").concat(pileNo).concat(toHex(toBytes(calendar.getTime()))); + String timebin = "6812".concat(seqhex).concat("0056").concat(pileNo).concat(toCp56Hex(calendar.getTime())); timebin = timebin.concat(CRCCalculator.calcCrc(timebin)); return timebin; } diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/utils/security/CP56Time2a.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/utils/security/CP56Time2a.java index 5f180366..372cf3ec 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/utils/security/CP56Time2a.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/utils/security/CP56Time2a.java @@ -1,63 +1,56 @@ package com.xhpc.pp.utils.security; +import cn.hutool.core.date.DateField; +import cn.hutool.core.date.DateTime; + import java.util.Calendar; import java.util.Date; -/** - * CP56time2a - * - * @author drebander - * @since 2020-09-11 11:35 上午 - **/ public class CP56Time2a { - public static Date toDate(byte[] bytes) { + public static Date cp56toDate(String hex) { - int milliseconds1 = bytes[0] < 0 ? 256 + bytes[0] : bytes[0]; - int milliseconds2 = bytes[1] < 0 ? 256 + bytes[1] : bytes[1]; - int milliseconds = milliseconds1 + milliseconds2 * 256; - // 位于 0011 1111 + byte[] bytes = HexUtils.toBytes(hex); + int milliseconds1 = bytes[0]; + int milliseconds2 = bytes[1]; + int milliseconds = milliseconds1 + milliseconds2 * 1000; int minutes = bytes[2] & 0x3f; - // 位于 0001 1111 int hours = bytes[3] & 0x1f; - // 位于 0000 1111 int days = bytes[4] & 0x3f; - // 位于 0001 1111 int months = bytes[5] & 0x0f; - // 位于 0111 1111 int years = bytes[6] & 0x7f; - Calendar aTime = Calendar.getInstance(); - aTime.set(Calendar.MILLISECOND, milliseconds); - aTime.set(Calendar.MINUTE, minutes); - aTime.set(Calendar.HOUR_OF_DAY, hours + 8); - aTime.set(Calendar.DAY_OF_MONTH, days); - aTime.set(Calendar.MONTH, months - 1); - aTime.set(Calendar.YEAR, years + 2000); - return aTime.getTime(); + DateTime dt = new DateTime(); + dt.setField(DateField.MILLISECOND, milliseconds % 1000); + dt.setField(DateField.SECOND, milliseconds / 1000); + dt.setField(DateField.MINUTE, minutes); + dt.setField(DateField.HOUR_OF_DAY, hours + 8); + dt.setField(DateField.DAY_OF_MONTH, days); + dt.setField(DateField.MONTH, months - 1); + dt.setField(DateField.YEAR, years + 2000); + return dt.toCalendar().getTime(); } - public static byte[] toBytes(Date aDate) { + public static String toCp56Hex(Date d) { byte[] result = new byte[7]; - Calendar aTime = Calendar.getInstance(); - aTime.setTime(aDate); - final int milliseconds = aTime.get(Calendar.MILLISECOND); - result[0] = (byte) (milliseconds % 256); - result[1] = (byte) (milliseconds / 256); - result[2] = (byte) aTime.get(Calendar.MINUTE); - result[3] = (byte) aTime.get(Calendar.HOUR_OF_DAY); - result[4] = (byte) aTime.get(Calendar.DAY_OF_MONTH); - result[5] = (byte) aTime.get(Calendar.MONTH); - result[6] = (byte) (aTime.get(Calendar.YEAR) % 100); - return result; + Calendar date = Calendar.getInstance(); + date.setTime(d); + final int milliseconds = date.get(Calendar.SECOND) * 1000 + date.get(Calendar.MILLISECOND); + result[0] = (byte) (milliseconds % 1000); + result[1] = (byte) (milliseconds / 1000); + result[2] = (byte) date.get(Calendar.MINUTE); + result[3] = (byte) (date.get(Calendar.HOUR_OF_DAY)); + result[4] = (byte) (date.get(Calendar.DAY_OF_MONTH)); + result[5] = (byte) (date.get(Calendar.MONTH) + 1); + result[6] = (byte) (date.get(Calendar.YEAR) % 100); + return HexUtils.toHex(result); } -// public static void main(String[] args) { -// Date aDate = new Date(); -// System.out.println(aDate); -// final byte[] bytes = toBytes(aDate); -// final Date date = toDate(bytes); -// System.out.println(date); -// } + public static void main(String[] args) { + + String hex = toCp56Hex(Calendar.getInstance().getTime()); + System.out.println(hex); + System.out.println(cp56toDate(hex)); + } } diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/utils/security/CacheDataUtils.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/utils/security/CacheDataUtils.java index b622e2ac..fdec6a82 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/utils/security/CacheDataUtils.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/utils/security/CacheDataUtils.java @@ -7,8 +7,7 @@ import java.lang.reflect.InvocationTargetException; import static cn.hutool.core.date.DatePattern.UTC_FORMAT; import static com.xhpc.common.core.utils.StringUtils.capitalize; -import static com.xhpc.pp.utils.security.CP56Time2a.toDate; -import static com.xhpc.pp.utils.security.HexUtils.toBytes; +import static com.xhpc.pp.utils.security.CP56Time2a.cp56toDate; public class CacheDataUtils { @@ -22,7 +21,7 @@ public class CacheDataUtils { if (tarfield.getType().getSimpleName().equals("Integer")) { tarval = HexUtils.reverseHexInt(srcval); } else if (tarFieldName.contains("ime")) { - tarval = DateUtil.format(toDate(toBytes(srcval)), UTC_FORMAT); + tarval = DateUtil.format(cp56toDate(srcval), UTC_FORMAT); } else { tarval = srcval; }