加入连接检查

This commit is contained in:
ZZ 2021-09-09 16:16:42 +08:00
parent c095057923
commit c2527da0f9
4 changed files with 68 additions and 35 deletions

View File

@ -31,8 +31,7 @@ import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMAT;
import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;
import static com.xhpc.common.data.redis.StaticBeanUtil.seqHex;
import static com.xhpc.pp.logic.RegisterLogic.REGISTERED;
import static com.xhpc.pp.server.ChargingPileServer.default_version;
import static com.xhpc.pp.server.ChargingPileServer.getHandler;
import static com.xhpc.pp.server.ChargingPileServer.*;
import static com.xhpc.pp.utils.security.HexUtils.toHexInt;
@RestController
@ -183,12 +182,18 @@ public class ChargingController {
ClientHandler handler = getHandler(pileNo);
R<Object> r;
if (handler != null) {
try {
log.info("native refresh balance >>> {}", refreshBalanceMsg);
handler.sendClientBinary(HexUtils.toBytes(refreshBalanceMsg));
r = R.ok("余额更新已下发");
} catch (IOException e) {
r = R.fail("余额更新下发失败:".concat(e.getMessage()));
if (!handler.isOpen()) {
log.error("[{}]({}) connection lost", handler.getName(), pileNo);
removeHandler(pileNo);
r = R.fail("充电桩连接已断开");
} else {
try {
log.info("native refresh balance >>> {}", refreshBalanceMsg);
handler.sendClientBinary(HexUtils.toBytes(refreshBalanceMsg));
r = R.ok("余额更新已下发");
} catch (IOException e) {
r = R.fail("余额更新下发失败:".concat(e.getMessage()));
}
}
} else {
r = R.fail("余额更新下发失败,充电桩离线");
@ -200,10 +205,13 @@ public class ChargingController {
public R nativeStartCharging(@Validated @RequestBody StartChargingData startChargingData) {
String pileNo = startChargingData.getPileNo();
ClientHandler clientHandler = ChargingPileServer.getHandler(pileNo);
if (clientHandler == null) {
// ChargingPileServer.removeHandler(pileNo);
ClientHandler handler = ChargingPileServer.getHandler(pileNo);
if (handler == null) {
return R.fail("充电桩没有连接到上次注册的服务器,请稍后再试一次");
} else if (!handler.isOpen()) {
log.error("[{}]({}) connection lost", handler.getName(), pileNo);
removeHandler(pileNo);
return R.fail("充电桩连接已断开,请稍后再试一次");
}
try {
String gunkey = "gun:".concat(pileNo).concat(startChargingData.getGunId());
@ -216,7 +224,7 @@ public class ChargingController {
String skey = gunkey.concat(".seqhex");
String seq = seqHex(skey);
byte[] msg = translateStart(startChargingData, seq);
clientHandler.sendClientBinary(msg);
handler.sendClientBinary(msg);
Integer balance = startChargingData.getBalance();
String orderkey = "order:".concat(startChargingData.getOrderNo());
Map<String, Object> cacheOrder = REDIS.getCacheMap(orderkey);
@ -248,8 +256,8 @@ public class ChargingController {
@GetMapping("native/charging/stop/{pileNo}/{gunId}/{version}")
public R nativeStopCharging(@PathVariable("pileNo") String pileNo, @PathVariable("gunId") String gunId, @PathVariable("version") String version) {
ClientHandler clientHandler = ChargingPileServer.getHandler(pileNo);
if (clientHandler == null) {
ClientHandler handler = ChargingPileServer.getHandler(pileNo);
if (handler == null) {
// ChargingPileServer.removeHandler(pileNo);
return R.fail("充电桩没有连接到上次注册的服务器,请稍后再试一次");
}
@ -259,10 +267,16 @@ public class ChargingController {
String skey = gunkey.concat(".seqhex");
String seq = seqHex(skey);
byte[] msg = translateStop(pileNo, gunId, version, seq);
clientHandler.sendClientBinary(msg);
Map<String, Object> cacheGun = REDIS.getCacheMap(gunkey);
REDIS.setCacheMap(gunkey, cacheGun);
return R.ok("指令已下发至充电桩");
if (!handler.isOpen()) {
log.error("send message failed. [{}]({}) connection lost", handler.getName(), pileNo);
removeHandler(pileNo);
return R.ok("充电桩已离线");
} else {
handler.sendClientBinary(msg);
Map<String, Object> cacheGun = REDIS.getCacheMap(gunkey);
REDIS.setCacheMap(gunkey, cacheGun);
return R.ok("停止充电指令已下发");
}
} catch (IOException e) {
log.error("send message failed. " + e.getMessage(), e);
return R.fail(e.getMessage());

View File

@ -16,6 +16,7 @@ import java.util.Set;
import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;
import static com.xhpc.pp.server.ChargingPileServer.getHandler;
import static com.xhpc.pp.server.ChargingPileServer.removeHandler;
@RestController
@Slf4j
@ -66,13 +67,20 @@ public class PileController {
ClientHandler handler = getHandler(pileNo);
R r;
if (handler != null) {
try {
log.info("[{}] - server send msg >>>> ({}) |{}|", handler.getName(), pileNo, msg);
handler.sendClientBinary(HexUtils.toBytes(msg));
r = R.ok(null, "费率模型已下发,请再次启动充电");
} catch (IOException e) {
log.error("send message failed. " + e.getMessage(), e);
r = R.fail("费率模型下发失败:".concat(e.getMessage()).concat(".无法启动充电"));
if (!handler.isOpen()) {
log.error("send message failed. [{}]({}) connection lost", handler.getName(), pileNo);
removeHandler(pileNo);
r = R.fail("充电桩连接已断开,请稍后再试");
} else {
try {
log.info("[{}] - server send msg >>>> ({}) |{}|", handler.getName(), pileNo, msg);
handler.sendClientBinary(HexUtils.toBytes(msg));
r = R.ok(null, "费率模型已下发,请再次启动充电");
} catch (IOException e) {
log.error("send message failed. " + e.getMessage(), e);
removeHandler(pileNo);
r = R.fail("费率模型下发失败:".concat(e.getMessage()).concat(".无法启动充电"));
}
}
} else {
r = R.fail("费率模型下发失败,充电桩离线");

View File

@ -24,6 +24,7 @@ import java.util.*;
import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMAT;
import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;
import static com.xhpc.common.data.redis.StaticBeanUtil.seqHex;
import static com.xhpc.pp.server.ChargingPileServer.removeHandler;
import static com.xhpc.pp.tx.ServiceResult.OK;
import static com.xhpc.pp.utils.security.CP56Time2a.toCp56Hex;
import static com.xhpc.pp.utils.security.CRCCalculator.calcCrc;
@ -109,8 +110,13 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler {
if (gunId != null) {
hori2(pileNo, gunId);
}
log.info("server send msg >>>> ({}) |{}|", pileNo, toHex(result.getBinary()));
handler.sendClientBinary(result.getBinary());
if (!handler.isOpen()) {
log.error("send message failed. [{}]({}) connection lost", handler.getName(), pileNo);
removeHandler(pileNo);
} else {
log.info("server send msg >>>> ({}) |{}|", pileNo, toHex(result.getBinary()));
handler.sendClientBinary(result.getBinary());
}
if (SERVICE_HB.equals(serviceName) && OK.equals(resultCode)) {
Boolean tcfg = (Boolean) cachePile.get("tcfg");
if (!tcfg) {
@ -121,12 +127,17 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler {
cachePile.put("configTime", DateUtil.format(date, NORM_DATETIME_FORMAT));
REDIS.setCacheMap(pilekey, cachePile);
log.info("server send time config msg >>>> ({}) |{}|", pileNo, timebin);
handler.sendClientBinary(HexUtils.toBytes(timebin));
if (gunId != null) {
hori2(pileNo, gunId);
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);
}
cachePile.put("tcfg", true);
REDIS.setCacheMap(pilekey, cachePile);
}
}
}

View File

@ -111,8 +111,8 @@ public class ChargingPileServer {
public static void removeHandler(String pileNo) {
ClientHandler handler = handlerMap.remove(pileNo);
handler.closeConnection();
log.info("handler [{}] for ({}) close connection", handler.getName(), pileNo);
// handler.closeConnection();
// log.info("handler [{}] for ({}) close connection", handler.getName(), pileNo);
String pkey = "pile:".concat(pileNo);
Map<String, Object> cachePile = REDIS.getCacheMap(pkey);
cachePile.put("status", DISCONNECTED);
@ -132,7 +132,7 @@ public class ChargingPileServer {
}
ClientHandler handler = handlerMap.get(pileNo);
if (handler == null || !handler.isOpen()) {
log.error("send message failed. ({}) connection lost", pileNo);
log.error("send message failed. [{}]({}) connection lost", handler.getName(), pileNo);
removeHandler(pileNo);
return;
}