manual config time n rate model

This commit is contained in:
ZZ 2022-05-07 14:33:57 +08:00
parent 843c74b175
commit 75b6fbd078
7 changed files with 101 additions and 30 deletions

View File

@ -40,4 +40,7 @@ public interface PowerPileService {
@PathVariable("rateModelId") @Param("rateModelId") Long rateModelId,
@RequestBody @Param("rateModel") CacheRateModel rateModel);
@PostMapping("/pile/{pileNo}/timeNRateModel")
R configTimeNRateModel(@PathVariable("pileNo") String pileNo);
}

View File

@ -57,6 +57,12 @@ public class PowerPileFallbackFactory implements FallbackFactory<PowerPileServic
return R.fail("设置电站费率模型失败:" + cause.getMessage());
}
@Override
public R configTimeNRateModel(String pileNo) {
return R.fail("校时校费失败:" + cause.getMessage());
}
};
}

View File

@ -77,12 +77,12 @@ public class ChargingController {
return r;
}
private R<Object> getRR(String response) {
public static R<Object> getRR(String response) {
R<Object> r;
JSONObject responseJson = (JSONObject) JSON.parse(response);
if (responseJson == null) {
r = R.fail("Native服务调用失败");
r = R.fail("原生服务调用失败");
} else {
int code = responseJson.getInteger("code");
log.debug(response);
@ -133,7 +133,7 @@ public class ChargingController {
return r;
}
private R<Object> checkPile(Map<String, Object> cachePile) {
public static R<Object> checkPile(Map<String, Object> cachePile) {
if (cachePile == null) {
return R.fail("充电桩未注册");
@ -186,7 +186,7 @@ public class ChargingController {
return r;
}
private String fmt(String svcSrv) {
public static String fmt(String svcSrv) {
String[] split = svcSrv.split("#");
return "http://".concat(split[0]).concat(":").concat(split[1]);

View File

@ -1,17 +1,24 @@
package com.xhpc.pp.controller;
import cn.hutool.core.date.DateUtil;
import com.xhpc.common.api.dto.ChargingStationDto;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.utils.HttpUtils;
import com.xhpc.pp.logic.RateModelRequestLogic;
import com.xhpc.pp.utils.HexUtils;
import lombok.extern.slf4j.Slf4j;
import org.quickserver.net.server.ClientHandler;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.*;
import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMAT;
import static cn.hutool.core.util.NumberUtil.isInteger;
import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;
import static com.xhpc.common.data.redis.StaticBeanUtil.seqHex;
import static com.xhpc.pp.controller.ChargingController.*;
import static com.xhpc.pp.server.ChargingPileBinaryHandler.getTimeBin;
import static com.xhpc.pp.server.ChargingPileServer.getHandler;
import static com.xhpc.pp.server.ChargingPileServer.removeHandler;
@ -47,47 +54,96 @@ public class PileController {
cachePile.put("version", version);
REDIS.setCacheMap(pkey, cachePile);
}
log.info("station [{}] pile whitelist add: [{}]", stationId, Arrays.toString(pileNoSet.toArray()));
return R.ok();
}
@DeleteMapping("pile/whitelist/delete/{stationId}")
public Object deleteWhitelist(@PathVariable("stationId") Long stationId, @RequestBody Set<String> pileNoSet) {
// System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
// System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
REDIS.deleteSetVal("PILE_WHITELIST", pileNoSet.toArray());
String stationKey = "station:".concat(stationId.toString());
// System.out.println(">>>>>>>>>>>>>>>>>>pileNoSet.toArray()>>>>>>>>>>>>>>>>"+stationKey);
ChargingStationDto cacheStation = REDIS.getCacheObject(stationKey);
cacheStation.getPiles().removeAll(pileNoSet);
REDIS.setCacheObject(stationKey, cacheStation);
log.info("station [{}] pile whitelist delete: [{}]", stationId, Arrays.toString(pileNoSet.toArray()));
return R.ok();
}
@PostMapping("native/pile/{pileNo}/rateModel")
public Object configRateModel(@PathVariable("pileNo") String pileNo, @RequestBody String msg) {
@PostMapping("pile/{pileNo}/timeNRateModel")
public R configTimeNRateModel(@PathVariable("pileNo") String pileNo) {
String pkey = "pile:".concat(pileNo);
Map<String, Object> cachePile = REDIS.getCacheMap(pkey);
R<Object> r = checkPile(cachePile);
if (r.getCode() == 200) {
String svcSrv = (String) cachePile.get("svcSrv");
String response = HttpUtils.post(fmt(svcSrv).concat("/native/pile/").concat(pileNo).concat("/timeNRateModel"));
r = getRR(response);
}
return r;
}
@PostMapping("native/pile/{pileNo}/timeNRateModel")
public R nativeConfigTimeNRateModel(@PathVariable("pileNo") String pileNo) {
ClientHandler handler = getHandler(pileNo);
R r;
R r = R.fail("校时校费下发失败,充电桩未注册.");
if (handler != null && handler.isOpen()) {
if (!handler.isOpen()) {
log.error("send message failed. [{}]({}) connection lost", handler.getName(), pileNo);
removeHandler(pileNo);
r = R.fail("充电桩连接已断开,请稍后再试");
r = R.fail("充电桩连接已断开,请稍后再试.");
} else {
Collection<String> stationKeys = REDIS.keys("station:*");
for (String stationKey : stationKeys) {
ChargingStationDto stationDto = REDIS.getCacheObject(stationKey);
Long rateModelIdStation = stationDto.getRateModelId();
if (rateModelIdStation != null) {
Set<String> pns = stationDto.getPiles();
if (pns != null) for (String pn : pns) {
if (pn.equals(pileNo)) {
String pk = "pile:".concat(pileNo);
Long rateModelIdPile = REDIS.getCacheMapValue(pk, "rateModelId");
if (!rateModelIdStation.equals(rateModelIdPile)) {
String gkPattern = ("gun:").concat(pileNo).concat("*");
Collection<String> gks = REDIS.keys(gkPattern);
boolean charging = false;
for (String gk : gks) {
if (gk.length() == 20 && isInteger(REDIS.getCacheMapValue(gk, "status"))) {
charging = true;
break;
}
}
if (!charging) {
String rsmsg = RateModelRequestLogic.translate(pileNo, rateModelIdStation, "0058",
REDIS.getCacheMapValue(pk, "version"));
try {
log.debug("[{}] - server send msg >>>> ({}) |{}|", handler.getName(), pileNo, msg);
handler.sendClientBinary(HexUtils.toBytes(msg));
r = R.ok(null, "网络延迟,请重试");
} catch (IOException e) {
log.error("费率模型下发失败[{}]:".concat(e.getMessage()).concat(".无法启动充电"), pileNo);
removeHandler(pileNo);
r = R.fail("电桩网络通信失败,请重试");
}
handler.sendClientBinary(HexUtils.toBytes(rsmsg));
log.info("pile[{}] rate model[{}] sent ⚪ {}", pileNo, rateModelIdStation, rsmsg);
String skey = pk.concat(".seqhex");
Thread.sleep(500);
Date date = Calendar.getInstance().getTime();
String timebin = getTimeBin(seqHex(skey), pileNo, date);
handler.sendClientBinary(HexUtils.toBytes(timebin));
REDIS.setCacheMapValue(pk, "manConfTime", DateUtil.format(date,
NORM_DATETIME_FORMAT));
r = R.fail("校时校费下发成功.");
} catch (IOException | InterruptedException e) {
r = R.fail("校时|校费下发失败,请重试.");
}
} else {
log.error("费率模型下发失败[{}].无法启动充电", pileNo);
r = R.fail("启动失败,请稍后重试");
r = R.fail("校时校费下发失败,充电桩在充电中.");
}
}
break;
}
}
}
}
}
}
return r;
}

View File

@ -40,7 +40,7 @@ public class PileTimeConfigReplyDataLogic implements ServiceLogic {
String pk = "pile:".concat(pileNo);
String configTime = DateUtil.format(cp56toDate(pileTimeConfigReplyData.getSetTime()), NORM_DATETIME_FORMAT);
Map<String, Object> cachePile = REDIS.getCacheMap(pk);
cachePile.put("configTime", configTime);
cachePile.put("configTimeReply", configTime);
REDIS.setCacheMap(pk, cachePile);
log.debug("({}) set time success√: [{}]", pileNo, configTime);
String remark = "充电桩同步时钟";

View File

@ -1,5 +1,6 @@
package com.xhpc.pp.logic;
import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xhpc.common.api.dto.ChargingStationDto;
import com.xhpc.common.data.up.PileConfigReplyData;
@ -15,8 +16,10 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Map;
import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMAT;
import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;
import static com.xhpc.pp.tx.ServiceResult.*;
@ -53,6 +56,8 @@ public class RateModelConfigReplyDataLogic implements ServiceLogic {
cachePile.put("rateModelId", cacheStation.getRateModelId());
result = OK;
}
String configTime = DateUtil.format(Calendar.getInstance().getTime(), NORM_DATETIME_FORMAT);
cachePile.put("configRmReply", configTime);
REDIS.setCacheMap(pkey, cachePile);
}

View File

@ -23,6 +23,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMAT;
import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;
@ -132,13 +133,14 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler {
if (SERVICE_HB.equals(serviceName)) {
if (OK.equals(resultCode)) {
pileNo = ChargingPileServer.getPileNo(handler);
Boolean tcfg = (Boolean) cachePile.get("tcfg");
String tcfgkey = "pile:".concat(pileNo).concat(".tcfg");
Boolean tcfg = REDIS.getCacheObject(tcfgkey);
if (tcfg == null || !tcfg) {
String skey = pilekey.concat(".seqhex");
Thread.sleep(500);
Date date = Calendar.getInstance().getTime();
String timebin = getTimeBin(seqHex(skey), pileNo, date);
cachePile.put("configTime", DateUtil.format(date, NORM_DATETIME_FORMAT));
cachePile.put("hbConfTime", DateUtil.format(date, NORM_DATETIME_FORMAT));
REDIS.setCacheMap(pilekey, cachePile);
log.debug("server send time config msg >>>> ({}) |{}|", pileNo, timebin);
if (!handler.isOpen()) {
@ -149,8 +151,7 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler {
if (gunId != null) {
hori2(pileNo, gunId);
}
cachePile.put("tcfg", true);
REDIS.setCacheMap(pilekey, cachePile);
REDIS.setCacheObject(tcfgkey, true, 1L, TimeUnit.DAYS);
}
}
} else {
@ -161,7 +162,7 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler {
}
}
private String getTimeBin(String seqhex, String pileNo, Date date) {
public static String getTimeBin(String seqhex, String pileNo, Date date) {
String timebin = "6812".concat(seqhex).concat("0056").concat(pileNo).concat(toCp56Hex(date));
timebin = timebin.concat(CRCCalculator.calcCrc(timebin));