adjust cp56time2a alg, add two methods with ms retained

This commit is contained in:
ZZ 2021-08-17 13:29:38 +08:00
parent e7ae59dad2
commit e5cbce5fd8
4 changed files with 79 additions and 23 deletions

View File

@ -188,7 +188,7 @@ public class ChargingController {
r = R.fail("余额更新下发失败:".concat(e.getMessage()));
}
} else {
r = R.fail("费率模型下发失败,充电桩离线");
r = R.fail("余额更新下发失败,充电桩离线");
}
return r;
}
@ -213,6 +213,10 @@ public class ChargingController {
Integer balance = startChargingData.getBalance();
String orderkey = "order:".concat(startChargingData.getOrderNo());
Map<String, Object> cacheOrder = REDIS.getCacheMap(orderkey);
String pkey = "pile:".concat(pileNo);
Map<String, Object> cachePile = REDIS.getCacheMap(pkey);
Long pileRateModelId = (Long) cachePile.get("rateModelId");
cacheOrder.put("rateModelId", pileRateModelId);
cacheOrder.put("balance", balance);
cacheOrder.put("tel", startChargingData.getTel());
REDIS.setCacheMap(orderkey, cacheOrder);

View File

@ -14,6 +14,7 @@ import org.springframework.stereotype.Component;
import java.util.Map;
import static cn.hutool.core.date.DatePattern.UTC_FORMAT;
import static cn.hutool.core.date.DatePattern.UTC_PATTERN;
import static com.xhpc.pp.server.ChargingPileServer.REDIS;
import static com.xhpc.pp.utils.security.CP56Time2a.cp56toDate;
@ -35,10 +36,10 @@ public class PileTimeConfigReplyDataLogic implements ServiceLogic {
String setTime = (String) cachePile.get("setTime");
String configTime = pileTimeConfigReplyData.getSetTime();
if (configTime.equals(setTime)) {
log.info("({}) set time success", pileNo);
log.info("({}) set time success: √[{}]", pileNo, DateUtil.format(cp56toDate(setTime), UTC_PATTERN));
} else {
// todo may call back mgmt backend
log.error("({}) set time failed: √[{}] ×[{}]", pileNo, DateUtil.format(cp56toDate(setTime), UTC_FORMAT), DateUtil.format(cp56toDate(configTime), UTC_FORMAT));
log.error("({}) set time failed: √[{}] ×[{}]", pileNo, DateUtil.format(cp56toDate(setTime), UTC_PATTERN), DateUtil.format(cp56toDate(configTime), UTC_FORMAT));
}
return new ServiceResult(false);
}

View File

@ -11,19 +11,17 @@ public class CP56Time2a {
public static Date cp56toDate(String hex) {
byte[] bytes = HexUtils.toBytes(hex);
int milliseconds1 = bytes[0];
int milliseconds2 = bytes[1];
int milliseconds = milliseconds1 + milliseconds2 * 1000;
int milliseconds = HexUtils.reverseHexInt(hex.substring(0, 4));
int minutes = bytes[2] & 0x3f;
int hours = bytes[3] & 0x1f;
int days = bytes[4] & 0x3f;
int months = bytes[5] & 0x0f;
int years = bytes[6] & 0x7f;
DateTime dt = new DateTime();
dt.setField(DateField.MILLISECOND, milliseconds % 1000);
dt.setField(DateField.MILLISECOND, 0);
dt.setField(DateField.SECOND, milliseconds / 1000);
dt.setField(DateField.MINUTE, minutes);
dt.setField(DateField.HOUR_OF_DAY, hours + 8);
dt.setField(DateField.HOUR_OF_DAY, hours);
dt.setField(DateField.DAY_OF_MONTH, days);
dt.setField(DateField.MONTH, months - 1);
dt.setField(DateField.YEAR, years + 2000);
@ -35,22 +33,75 @@ public class CP56Time2a {
byte[] result = new byte[7];
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);
String milliSecond = String.format("%04X", (date.get(Calendar.SECOND) * 1000));
String reversehilo = milliSecond.substring(2, 4).concat(milliSecond.substring(0, 2));
result[0] = (byte) date.get(Calendar.MINUTE);
result[1] = (byte) (date.get(Calendar.HOUR_OF_DAY));
result[2] = (byte) (date.get(Calendar.DAY_OF_MONTH));
result[3] = (byte) (date.get(Calendar.MONTH) + 1);
result[4] = (byte) (date.get(Calendar.YEAR) % 100);
return reversehilo.concat(HexUtils.toHex(result)).toString();
}
public static void main(String[] args) {
public static String decode(byte b[]) {
String hex = toCp56Hex(Calendar.getInstance().getTime());
System.out.println(hex);
System.out.println(cp56toDate(hex));
int year = b[6] & 0x7F;
int month = b[5] & 0x0F;
int day = b[4] & 0x1F;
int week = (b[4] & 0xE0) / 32;
// int week = (b[4] & 0xE0) >> 5;
int hour = b[3] & 0x1F;
int minute = b[2] & 0x3F;
int second = (b[1] << 8) + b[0];
String str = "20" + year + "-"
+ String.format("%02d", month) + "-"
+ String.format("%02d", day) + "T"
+ String.format("%02d", hour) + ":"
+ String.format("%02d", minute) + ":"
+ String.format("%02d", second / 1000) + "."
+ String.format("%03d", second % 1000);
return str;
}
public static String encode(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
StringBuilder builder = new StringBuilder();
String milliSecond = String.format("%04X", (calendar.get(Calendar.SECOND) * 1000) + calendar.get(Calendar.MILLISECOND));
builder.append(milliSecond.substring(2, 4));
builder.append(milliSecond.substring(0, 2));
builder.append(String.format("%02X", calendar.get(Calendar.MINUTE) & 0x3F));
builder.append(String.format("%02X", calendar.get(Calendar.HOUR_OF_DAY) & 0x1F));
int week = calendar.get(Calendar.DAY_OF_WEEK);
if (week == Calendar.SUNDAY)
week = 7;
else week--;
builder.append(String.format("%02X", (week << 5) + (calendar.get(Calendar.DAY_OF_MONTH) & 0x1F)));
builder.append(String.format("%02X", calendar.get(Calendar.MONTH) + 1));
builder.append(String.format("%02X", calendar.get(Calendar.YEAR) - 2000));
return builder.toString();
}
public static void main(String[] args) throws InterruptedException {
Date time = Calendar.getInstance().getTime();
System.out.println(String.format("--未编码--: %s", time));
String encode = encode(time);
System.out.println("--解码--:" + decode(HexUtils.toBytes(encode)));
// String hex = toCp56Hex(time);
// System.out.println(String.format("--未编码--: %s", time));
// System.out.println("--编码--:"+hex);
// System.out.println("--解码--:"+cp56toDate(hex));
// Thread.sleep(10000);
// System.out.println("-----------");
// System.out.println("-----------");
// time = Calendar.getInstance().getTime();
// hex = toCp56Hex(time);
// System.out.println(String.format("--未编码--: %s", time));
// System.out.println("--编码--:"+hex);
// System.out.println("--解码--:"+cp56toDate(hex));
}
}

View File

@ -11,10 +11,10 @@ import static com.xhpc.pp.utils.security.CP56Time2a.cp56toDate;
public class CacheDataUtils {
public static Object reflectTranslate(Object srcobj, Class tarclz, Class srcclz, Field[] tarfields) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException {
public static Object reflectTranslate(Object srcobj, Class tarclz, Class srcclz, Field[] targetfields) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException {
Object tarobj = tarclz.getConstructors()[0].newInstance();
for (Field tarfield : tarfields) {
for (Field tarfield : targetfields) {
String tarFieldName = tarfield.getName();
String srcval = (String) srcclz.getMethod("get".concat(capitalize(tarFieldName))).invoke(srcobj);
Object tarval;