对未注册的连接和消息进行无情抛弃

This commit is contained in:
ZZ 2022-03-04 13:56:43 +08:00
parent 18e4f0a99d
commit fc568b6542

View File

@ -63,10 +63,10 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler {
public void handleBinary(ClientHandler handler, byte[] data) throws IOException { public void handleBinary(ClientHandler handler, byte[] data) throws IOException {
try { try {
String pileNo = ChargingPileServer.getPileNo(handler);
List<byte[]> dataList = parseDataList(data); List<byte[]> dataList = parseDataList(data);
for (byte[] d : dataList) { for (byte[] d : dataList) {
String dataStr = toHex(d); String dataStr = toHex(d);
String pileNo = ChargingPileServer.getPileNo(handler);
log.debug("received data <<<< |{}| from pile <- ({})", dataStr, pileNo); log.debug("received data <<<< |{}| from pile <- ({})", dataStr, pileNo);
if (d.length <= 2 || !dataStr.startsWith("68")) { if (d.length <= 2 || !dataStr.startsWith("68")) {
log.debug("received invalid data <<<< |{}|, len[{}]", dataStr, d.length); log.debug("received invalid data <<<< |{}|, len[{}]", dataStr, d.length);
@ -88,7 +88,8 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler {
} }
} }
private void process(ClientHandler handler, byte[] data) throws TxException, IOException, NacosException, InterruptedException { private void process(ClientHandler handler, byte[] data) throws TxException, IOException, NacosException,
InterruptedException {
String serviceName = toHex(data, 5, 6); String serviceName = toHex(data, 5, 6);
String version = ChargingPileServer.getVersion(handler.getName()); String version = ChargingPileServer.getVersion(handler.getName());
@ -105,8 +106,7 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler {
cachePile.put("tcfg", false); cachePile.put("tcfg", false);
REDIS.setCacheMap(pilekey, cachePile); REDIS.setCacheMap(pilekey, cachePile);
regHandler(handler, pileNo, req); regHandler(handler, pileNo, req);
} } else if (SERVICE_RMR.equals(serviceName) && OK.equals(resultCode)) {
if (SERVICE_RMR.equals(serviceName) && OK.equals(resultCode)) {
setCachePileRM(pilekey); setCachePileRM(pilekey);
} }
if (result.getBinary() != null) { if (result.getBinary() != null) {
@ -130,25 +130,30 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler {
xhpcDeviceMessageMapper.insertByDomain(deviceMessage); xhpcDeviceMessageMapper.insertByDomain(deviceMessage);
} }
if (SERVICE_HB.equals(serviceName) && OK.equals(resultCode)) { if (SERVICE_HB.equals(serviceName) && OK.equals(resultCode)) {
Boolean tcfg = (Boolean) cachePile.get("tcfg"); pileNo = ChargingPileServer.getPileNo(handler);
if (tcfg == null || !tcfg) { if (null == pileNo) {
String skey = pilekey.concat(".seqhex"); handler.forceClose();
Thread.sleep(500); } else {
Date date = Calendar.getInstance().getTime(); Boolean tcfg = (Boolean) cachePile.get("tcfg");
String timebin = getTimeBin(seqHex(skey), pileNo, date); if (tcfg == null || !tcfg) {
cachePile.put("configTime", DateUtil.format(date, NORM_DATETIME_FORMAT)); String skey = pilekey.concat(".seqhex");
REDIS.setCacheMap(pilekey, cachePile); Thread.sleep(500);
log.debug("server send time config msg >>>> ({}) |{}|", pileNo, timebin); Date date = Calendar.getInstance().getTime();
if (!handler.isOpen()) { String timebin = getTimeBin(seqHex(skey), pileNo, date);
log.error("send message failed. [{}]({}) connection lost", handler.getName(), pileNo); cachePile.put("configTime", DateUtil.format(date, NORM_DATETIME_FORMAT));
removeHandler(pileNo);
} else {
handler.sendClientBinary(HexUtils.toBytes(timebin));
if (gunId != null) {
hori2(pileNo, gunId);
}
cachePile.put("tcfg", true);
REDIS.setCacheMap(pilekey, cachePile); REDIS.setCacheMap(pilekey, cachePile);
log.debug("server send time config msg >>>> ({}) |{}|", pileNo, timebin);
if (!handler.isOpen()) {
log.error("send message failed. [{}]({}) connection lost", handler.getName(), pileNo);
removeHandler(pileNo);
} else {
handler.sendClientBinary(HexUtils.toBytes(timebin));
if (gunId != null) {
hori2(pileNo, gunId);
}
cachePile.put("tcfg", true);
REDIS.setCacheMap(pilekey, cachePile);
}
} }
} }
} }