if hori, sleep; if disconnected unreg handler

This commit is contained in:
ZZ 2021-08-20 14:48:40 +08:00
parent 5842d55b09
commit 78d9a4ccc1
6 changed files with 45 additions and 26 deletions

View File

@ -12,6 +12,7 @@ import com.xhpc.common.data.redis.CacheRateModel;
import com.xhpc.pp.logic.RateModelRequestLogic;
import com.xhpc.pp.server.ChargingPileServer;
import com.xhpc.pp.utils.security.CRCCalculator;
import com.xhpc.pp.utils.security.CacheDataUtils;
import com.xhpc.pp.utils.security.HexUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.quickserver.net.server.ClientHandler;
@ -24,7 +25,6 @@ import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.Calendar;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMAT;
import static com.xhpc.common.data.redis.SeqUtil.seqHex;
@ -173,10 +173,11 @@ public class ChargingController {
}
@PostMapping("native/charging/balance/refresh")
public R nativeRefreshBalance(@Validated @RequestBody StartChargingData refreshBalanceData) {
public R nativeRefreshBalance(@Validated @RequestBody StartChargingData refreshBalanceData) throws InterruptedException {
String pileNo = refreshBalanceData.getPileNo();
String gunkey = "gun:".concat(pileNo).concat(refreshBalanceData.getGunId());
if (CacheDataUtils.hori(gunkey)) Thread.sleep(500L);// return R.fail("等待上一条指令响应");
String skey = gunkey.concat(".seqhex");
String seq = seqHex(skey);
String refreshBalanceMsg = "6817".concat(seq).concat("0042").concat(pileNo)
@ -211,7 +212,7 @@ public class ChargingController {
if (!"空闲".equals(gunstatus)) {
return R.fail("充电桩不在空闲正常状态,稍后再试");
}
if (hori(gunkey)) return R.fail("等待上一条指令响应");
if (CacheDataUtils.hori(gunkey)) Thread.sleep(500L); //return R.fail("等待上一条指令响应");
String skey = gunkey.concat(".seqhex");
String seq = seqHex(skey);
byte[] msg = translateStart(startChargingData, seq);
@ -231,23 +232,12 @@ public class ChargingController {
cacheGun.put("orderkey", orderkey);
REDIS.setCacheMap(gunkey, cacheGun);
return R.ok("指令已下发至充电桩");
} catch (IOException e) {
} catch (IOException | InterruptedException e) {
log.error("send message failed. " + e.getMessage(), e);
return R.fail(e.getMessage());
}
}
private boolean hori(String gunkey) {
String hori = gunkey.concat(".hori");
String HORI = REDIS.getCacheObject(hori);
if (HORI != null) {
return true;
}
REDIS.setCacheObject(hori, "protection film", 300L, TimeUnit.MILLISECONDS);
return false;
}
@GetMapping("native/charging/stop/{pileNo}/{gunId}/{version}")
public R nativeStopCharging(@PathVariable("pileNo") String pileNo, @PathVariable("gunId") String gunId, @PathVariable("version") String version) {
@ -255,15 +245,15 @@ public class ChargingController {
if (clientHandler == null) return R.fail("充电桩没有连接到上次注册的服务器");
try {
String gunkey = "gun:".concat(pileNo).concat(gunId);
Map<String, Object> cacheGun = REDIS.getCacheMap(gunkey);
if (hori(gunkey)) return R.fail("等待上一条指令响应");
if (CacheDataUtils.hori(gunkey)) Thread.sleep(500L);//return R.fail("等待上一条指令响应");
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("指令已下发至充电桩");
} catch (IOException e) {
} catch (IOException | InterruptedException e) {
log.error("send message failed. " + e.getMessage(), e);
return R.fail(e.getMessage());
}

View File

@ -60,7 +60,7 @@ public class OrderDataLogic implements ServiceLogic {
return new ServiceResult(HexUtils.toBytes(resultStr), ServiceResult.OK);
}
private CacheOrderData translate(OrderData orderData) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
private CacheOrderData translate(OrderData orderData) throws InvocationTargetException, IllegalAccessException, InstantiationException {
Class<CacheOrderData> codclz = CacheOrderData.class;
Class<OrderData> odclz = OrderData.class;

View File

@ -27,6 +27,7 @@ import static com.xhpc.pp.server.ChargingPileServer.REDIS;
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;
import static com.xhpc.pp.utils.security.CacheDataUtils.hori2;
import static com.xhpc.pp.utils.security.HexUtils.toHex;
public class ChargingPileBinaryHandler implements ClientBinaryHandler {
@ -103,6 +104,11 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler {
setCachePileRM(pilekey);
}
if (result.getBinary() != null) {
String gunId = (String) req.get("gunId");
if (gunId != null) {
String gunkey = "gun:".concat(pileNo).concat(gunId);
hori2(gunkey);
}
log.info("server send msg >>>> ({}) |{}|", pileNo, toHex(result.getBinary()));
handler.sendClientBinary(result.getBinary());
if (SERVICE_RMR.equals(serviceName) && OK.equals(resultCode)) {

View File

@ -33,10 +33,7 @@ public class ChargingPileEventHandler implements ClientEventHandler {
String pileNo = ChargingPileServer.getPileNo(handler);
if (pileNo != null) {
String pkey = "pile:".concat(pileNo);
Map<String, Object> cachePile = REDIS.getCacheMap(pkey);
cachePile.put("status", RegisterLogic.DISCONNECTED);
REDIS.setCacheMap(pkey, cachePile);
ChargingPileServer.removeHandler(pileNo);
}
log.info("-> ({}) - [{}] <- {}",
pileNo, handler.getName(), handler.getSocket().getRemoteSocketAddress().toString());

View File

@ -88,9 +88,17 @@ public class ChargingPileServer {
ClientHandler handler = handlerMap.remove(pileNo);
String pkey = "pile:".concat(pileNo);
Map<String, Object> cacheMap = REDIS.getCacheMap(pkey);
cacheMap.put("status", DISCONNECTED);
REDIS.setCacheMap(pkey, cacheMap);
Map<String, Object> cachePile = REDIS.getCacheMap(pkey);
cachePile.put("status", DISCONNECTED);
REDIS.setCacheMap(pkey, cachePile);
for (int i = 1; i < 10; i++) {
String gunkey = "gun:".concat(pileNo).concat(String.format("%02d", i));
Map<String, Object> cacheGun = REDIS.getCacheMap(gunkey);
if (cacheGun != null) {
cacheGun.put("status", "离线");
REDIS.setCacheMap(gunkey, cacheGun);
}
}
if (handler != null) {
log.info("remove handler [{}] for ({})", handler.getName(), pileNo);
pileMap.remove(handler.getName());

View File

@ -4,9 +4,11 @@ import cn.hutool.core.date.DateUtil;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.TimeUnit;
import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMAT;
import static com.xhpc.common.core.utils.StringUtils.capitalize;
import static com.xhpc.pp.server.ChargingPileServer.REDIS;
import static com.xhpc.pp.utils.security.CP56Time2a.cp56toDate;
public class CacheDataUtils {
@ -38,5 +40,21 @@ public class CacheDataUtils {
return tarobj;
}
public static void hori2(String gunkey) {
String hori = gunkey.concat(".hori");
REDIS.setCacheObject(hori, "protection film", 300L, TimeUnit.MILLISECONDS);
}
public static boolean hori(String gunkey) {
String hori = gunkey.concat(".hori");
String HORI = REDIS.getCacheObject(hori);
if (HORI != null) {
return true;
}
REDIS.setCacheObject(hori, "protection film", 300L, TimeUnit.MILLISECONDS);
return false;
}
}