fix cp56time2a alg

This commit is contained in:
ZZ 2021-08-17 10:46:00 +08:00
parent d48ba4bb9f
commit ec47440976
4 changed files with 51 additions and 51 deletions

View File

@ -15,8 +15,7 @@ import java.util.Map;
import static cn.hutool.core.date.DatePattern.UTC_FORMAT; import static cn.hutool.core.date.DatePattern.UTC_FORMAT;
import static com.xhpc.pp.server.ChargingPileServer.REDIS; import static com.xhpc.pp.server.ChargingPileServer.REDIS;
import static com.xhpc.pp.utils.security.CP56Time2a.toDate; import static com.xhpc.pp.utils.security.CP56Time2a.cp56toDate;
import static com.xhpc.pp.utils.security.HexUtils.toBytes;
@Lazy @Lazy
@Component("PileTimeConfigReplyDataLogic") @Component("PileTimeConfigReplyDataLogic")
@ -39,9 +38,18 @@ public class PileTimeConfigReplyDataLogic implements ServiceLogic {
log.info("({}) set time success", pileNo); log.info("({}) set time success", pileNo);
} else { } else {
// todo may call back mgmt backend // 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); 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;
}
} }

View File

@ -23,7 +23,7 @@ import java.util.*;
import static com.xhpc.common.data.redis.SeqUtil.seqHex; import static com.xhpc.common.data.redis.SeqUtil.seqHex;
import static com.xhpc.pp.server.ChargingPileServer.REDIS; import static com.xhpc.pp.server.ChargingPileServer.REDIS;
import static com.xhpc.pp.tx.ServiceResult.OK; 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.CRCCalculator.calcCrc;
import static com.xhpc.pp.utils.security.HexUtils.toHex; import static com.xhpc.pp.utils.security.HexUtils.toHex;
@ -95,7 +95,7 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
String timebin = getTimeBin(seqHex(pilekey.concat("seqhex")), pileNo, calendar); String timebin = getTimeBin(seqHex(pilekey.concat("seqhex")), pileNo, calendar);
Map<String, Object> cachePile = REDIS.getCacheMap(pilekey); Map<String, Object> cachePile = REDIS.getCacheMap(pilekey);
cachePile.put("setTime", toHex(toBytes(calendar.getTime()))); cachePile.put("setTime", toCp56Hex(calendar.getTime()));
REDIS.setCacheMap(pilekey, cachePile); REDIS.setCacheMap(pilekey, cachePile);
log.info("server send time config msg >>>> ({}) |{}|", pileNo, timebin); log.info("server send time config msg >>>> ({}) |{}|", pileNo, timebin);
handler.sendClientBinary(HexUtils.toBytes(timebin)); handler.sendClientBinary(HexUtils.toBytes(timebin));
@ -113,7 +113,7 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler {
private String getTimeBin(String seqhex, String pileNo, Calendar calendar) { 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)); timebin = timebin.concat(CRCCalculator.calcCrc(timebin));
return timebin; return timebin;
} }

View File

@ -1,63 +1,56 @@
package com.xhpc.pp.utils.security; 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.Calendar;
import java.util.Date; import java.util.Date;
/**
* CP56time2a
*
* @author drebander
* @since 2020-09-11 11:35 上午
**/
public class CP56Time2a { 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]; byte[] bytes = HexUtils.toBytes(hex);
int milliseconds2 = bytes[1] < 0 ? 256 + bytes[1] : bytes[1]; int milliseconds1 = bytes[0];
int milliseconds = milliseconds1 + milliseconds2 * 256; int milliseconds2 = bytes[1];
// 位于 0011 1111 int milliseconds = milliseconds1 + milliseconds2 * 1000;
int minutes = bytes[2] & 0x3f; int minutes = bytes[2] & 0x3f;
// 位于 0001 1111
int hours = bytes[3] & 0x1f; int hours = bytes[3] & 0x1f;
// 位于 0000 1111
int days = bytes[4] & 0x3f; int days = bytes[4] & 0x3f;
// 位于 0001 1111
int months = bytes[5] & 0x0f; int months = bytes[5] & 0x0f;
// 位于 0111 1111
int years = bytes[6] & 0x7f; int years = bytes[6] & 0x7f;
Calendar aTime = Calendar.getInstance(); DateTime dt = new DateTime();
aTime.set(Calendar.MILLISECOND, milliseconds); dt.setField(DateField.MILLISECOND, milliseconds % 1000);
aTime.set(Calendar.MINUTE, minutes); dt.setField(DateField.SECOND, milliseconds / 1000);
aTime.set(Calendar.HOUR_OF_DAY, hours + 8); dt.setField(DateField.MINUTE, minutes);
aTime.set(Calendar.DAY_OF_MONTH, days); dt.setField(DateField.HOUR_OF_DAY, hours + 8);
aTime.set(Calendar.MONTH, months - 1); dt.setField(DateField.DAY_OF_MONTH, days);
aTime.set(Calendar.YEAR, years + 2000); dt.setField(DateField.MONTH, months - 1);
return aTime.getTime(); 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]; byte[] result = new byte[7];
Calendar aTime = Calendar.getInstance(); Calendar date = Calendar.getInstance();
aTime.setTime(aDate); date.setTime(d);
final int milliseconds = aTime.get(Calendar.MILLISECOND); final int milliseconds = date.get(Calendar.SECOND) * 1000 + date.get(Calendar.MILLISECOND);
result[0] = (byte) (milliseconds % 256); result[0] = (byte) (milliseconds % 1000);
result[1] = (byte) (milliseconds / 256); result[1] = (byte) (milliseconds / 1000);
result[2] = (byte) aTime.get(Calendar.MINUTE); result[2] = (byte) date.get(Calendar.MINUTE);
result[3] = (byte) aTime.get(Calendar.HOUR_OF_DAY); result[3] = (byte) (date.get(Calendar.HOUR_OF_DAY));
result[4] = (byte) aTime.get(Calendar.DAY_OF_MONTH); result[4] = (byte) (date.get(Calendar.DAY_OF_MONTH));
result[5] = (byte) aTime.get(Calendar.MONTH); result[5] = (byte) (date.get(Calendar.MONTH) + 1);
result[6] = (byte) (aTime.get(Calendar.YEAR) % 100); result[6] = (byte) (date.get(Calendar.YEAR) % 100);
return result; return HexUtils.toHex(result);
} }
// public static void main(String[] args) { public static void main(String[] args) {
// Date aDate = new Date();
// System.out.println(aDate); String hex = toCp56Hex(Calendar.getInstance().getTime());
// final byte[] bytes = toBytes(aDate); System.out.println(hex);
// final Date date = toDate(bytes); System.out.println(cp56toDate(hex));
// System.out.println(date); }
// }
} }

View File

@ -7,8 +7,7 @@ import java.lang.reflect.InvocationTargetException;
import static cn.hutool.core.date.DatePattern.UTC_FORMAT; import static cn.hutool.core.date.DatePattern.UTC_FORMAT;
import static com.xhpc.common.core.utils.StringUtils.capitalize; 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.CP56Time2a.cp56toDate;
import static com.xhpc.pp.utils.security.HexUtils.toBytes;
public class CacheDataUtils { public class CacheDataUtils {
@ -22,7 +21,7 @@ public class CacheDataUtils {
if (tarfield.getType().getSimpleName().equals("Integer")) { if (tarfield.getType().getSimpleName().equals("Integer")) {
tarval = HexUtils.reverseHexInt(srcval); tarval = HexUtils.reverseHexInt(srcval);
} else if (tarFieldName.contains("ime")) { } else if (tarFieldName.contains("ime")) {
tarval = DateUtil.format(toDate(toBytes(srcval)), UTC_FORMAT); tarval = DateUtil.format(cp56toDate(srcval), UTC_FORMAT);
} else { } else {
tarval = srcval; tarval = srcval;
} }