stop charging

This commit is contained in:
ZZ 2021-08-05 13:14:50 +08:00
parent 18940b3bc9
commit e48e29a776
6 changed files with 78 additions and 18 deletions

View File

@ -18,6 +18,9 @@ public interface PowerPileService {
@PostMapping("/charging/start") @PostMapping("/charging/start")
R startCharging(@Validated @RequestBody StartChargingData startChargingData); R startCharging(@Validated @RequestBody StartChargingData startChargingData);
@PutMapping("charging/stop/{pileNo}/{gunId}/{version}")
R stopCharging(@PathVariable("pileNo") String pileNo, @PathVariable("gunId") String gunId, @PathVariable("version") String version);
@PostMapping("/pile/whitelist/add/{stationId}") @PostMapping("/pile/whitelist/add/{stationId}")
R addPileWhitelist(@PathVariable("stationId") Long stationId, @RequestBody Set<String> pileNoSet); R addPileWhitelist(@PathVariable("stationId") Long stationId, @RequestBody Set<String> pileNoSet);

View File

@ -28,6 +28,12 @@ public class PowerPileFallbackFactory implements FallbackFactory<PowerPileServic
return R.fail("启动充电失败:" + cause.getMessage()); return R.fail("启动充电失败:" + cause.getMessage());
} }
@Override
public R stopCharging(String pileNo, String gunId, String version) {
return R.fail("停止充电失败:" + cause.getMessage());
}
@Override @Override
public R addPileWhitelist(Long stationId, Set<String> pileNoSet) { public R addPileWhitelist(Long stationId, Set<String> pileNoSet) {

View File

@ -16,9 +16,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
@ -69,6 +67,32 @@ public class ChargingController {
return R.ok(responseJson.get("data"), responseJson.getString("msg")); return R.ok(responseJson.get("data"), responseJson.getString("msg"));
} }
@PutMapping("charging/stop/{pileNo}/{gunId}/{version}")
public Object stopCharging(@PathVariable("pileNo") String pileNo, @PathVariable("gunId") String gunId, @PathVariable("version") String version) {
String pkey = "pile:".concat(pileNo);
Map<String, String> cachePile = REDIS.getCacheMap(pkey);
if (cachePile.isEmpty()) {
return R.fail("充电桩未注册");
}
String status = cachePile.get("status");
if (!REGISTERED.equals(status)) {
return R.fail("充电桩离线");
}
String svcSrv = cachePile.get("svcSrv");
String response = HttpUtils.get(fmt(svcSrv)
.concat("/native/charging/stop/")
.concat(pileNo).concat("/")
.concat(gunId).concat("/")
.concat(version));
JSONObject responseJson = (JSONObject) JSON.parse(response);
int code = responseJson.getInteger("code");
if (code != 200) {
return R.fail(code, responseJson.getString("msg"));
}
return R.ok(responseJson.get("data"), responseJson.getString("msg"));
}
private String fmt(String svcSrv) { private String fmt(String svcSrv) {
String[] split = svcSrv.split("#"); String[] split = svcSrv.split("#");
@ -85,7 +109,7 @@ public class ChargingController {
String gunkey = "gun:".concat(pileNo).concat(startChargingData.getGunId()); String gunkey = "gun:".concat(pileNo).concat(startChargingData.getGunId());
String skey = gunkey.concat(".seqhex"); String skey = gunkey.concat(".seqhex");
String seq = seqHex(skey); String seq = seqHex(skey);
byte[] msg = translateSucc(startChargingData, seq); byte[] msg = translateStart(startChargingData, seq);
clientHandler.sendClientBinary(msg); clientHandler.sendClientBinary(msg);
return R.ok(); return R.ok();
} catch (IOException e) { } catch (IOException e) {
@ -94,8 +118,25 @@ public class ChargingController {
} }
} }
@GetMapping("native/charging/stop/{pileNo}/{gunId}/{version}")
public Object nativeStopCharging(@PathVariable("pileNo") String pileNo, @PathVariable("gunId") String gunId, @PathVariable("version") String version) {
private byte[] translateSucc(StartChargingData startChargingData, String seq) { ClientHandler clientHandler = ChargingPileServer.getHandler(pileNo);
if (clientHandler == null) return R.fail("充电桩没有连接到上次注册的服务器");
try {
String gunkey = "gun:".concat(pileNo).concat(gunId);
String skey = gunkey.concat(".seqhex");
String seq = seqHex(skey);
byte[] msg = translateStop(pileNo, gunId, version, seq);
clientHandler.sendClientBinary(msg);
return R.ok();
} catch (IOException e) {
log.error("send message failed. " + e.getMessage(), e);
return R.fail(e.getMessage());
}
}
private byte[] translateStart(StartChargingData startChargingData, String seq) {
byte[] data = new byte[0]; byte[] data = new byte[0];
data = ArrayUtils.addAll(data, HexUtils.toBytes("6830".concat(seq).concat("0034"))); data = ArrayUtils.addAll(data, HexUtils.toBytes("6830".concat(seq).concat("0034")));
@ -112,8 +153,21 @@ public class ChargingController {
} }
String msg = HexUtils.toHex(data).concat(ServiceResult.HEX_OK); String msg = HexUtils.toHex(data).concat(ServiceResult.HEX_OK);
msg = msg.concat(CRCCalculator.calcCrc(msg)); msg = msg.concat(CRCCalculator.calcCrc(msg));
log.info("charging order[{}], send msg to pile: {}", startChargingData.getOrderNo(), msg); log.info("start charging order[{}], send msg >>>> |{}|", startChargingData.getOrderNo(), msg);
return HexUtils.toBytes(msg); return HexUtils.toBytes(msg);
} }
private byte[] translateStop(String pileNo, String gunId, String version, String seq) {
byte[] data = new byte[0];
data = ArrayUtils.addAll(data, HexUtils.toBytes("680C".concat(seq).concat("0036")));
if (default_version.equals(version)) {
data = ArrayUtils.addAll(data, HexUtils.toBytes(pileNo));
data = ArrayUtils.addAll(data, HexUtils.toBytes(gunId));
}
String msg = HexUtils.toHex(data).concat(ServiceResult.HEX_OK);
msg = msg.concat(CRCCalculator.calcCrc(msg));
log.info("stop charging, send msg to terminal ({}{}) >>>> {}", pileNo, gunId, msg);
return HexUtils.toBytes(msg);
}
} }

View File

@ -54,7 +54,7 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler {
String pileNo = ChargingPileServer.getPileNo(handler); String pileNo = ChargingPileServer.getPileNo(handler);
log.info("received data <<<< {}, from pile <<<< {}", dataStr, pileNo); log.info("received data <<<< {}, from pile <<<< {}", dataStr, pileNo);
if (d.length <= 2 || !dataStr.startsWith("68")) { if (d.length <= 2 || !dataStr.startsWith("68")) {
log.info("received invalid data <<<< {}, len[{}]", dataStr, d.length); log.info("received invalid data <<<< |{}|, len[{}]", dataStr, d.length);
continue; continue;
} }
int len = HexUtils.toInteger(d, 1, 2); int len = HexUtils.toInteger(d, 1, 2);
@ -91,7 +91,7 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler {
// TODO: 2021/7/28 // TODO: 2021/7/28
}*/ }*/
if (result.getBinary() != null) { if (result.getBinary() != null) {
log.info("server send msg >>>> [{}] |{}|", pileNo, HexUtils.toHex(result.getBinary())); log.info("server send msg >>>> ({}) |{}|", pileNo, HexUtils.toHex(result.getBinary()));
handler.sendClientBinary(result.getBinary()); handler.sendClientBinary(result.getBinary());
} }
} }
@ -99,7 +99,7 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler {
private void reg(ClientHandler handler, String pileNo, Map<String, Object> req) throws NacosException { private void reg(ClientHandler handler, String pileNo, Map<String, Object> req) throws NacosException {
if (!EarlierBeanConf.ifreg(pileNo)) { if (!EarlierBeanConf.ifreg(pileNo)) {
log.info("pile already registered >>>> [{}] ", pileNo); log.info("pile already registered >>>> ({}) ", pileNo);
return; return;
} }
ChargingPileServer.putHandler(pileNo, handler); ChargingPileServer.putHandler(pileNo, handler);
@ -116,7 +116,7 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler {
String gunkey = pileNo.concat(gunId); String gunkey = pileNo.concat(gunId);
cachePileGunSvcSrv("svcSrvGun:", gunkey); cachePileGunSvcSrv("svcSrvGun:", gunkey);
} }
log.info("pile registering >>>> [{}] ", pileNo); log.info("pile registering >>>> ({}) ", pileNo);
} }
private void cachePileGunSvcSrv(String prefix, String key) { private void cachePileGunSvcSrv(String prefix, String key) {
@ -135,7 +135,7 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler {
int start = 0; int start = 0;
while (start < data.length) { while (start < data.length) {
if (len > data.length) { if (len > data.length) {
log.error("incorrect input data[{}] len[{}]", data, data.length); log.error("incorrect input data|{}| len[{}]", data, data.length);
break; break;
} }
dataList.add(ArrayUtils.subarray(data, start, start + len)); dataList.add(ArrayUtils.subarray(data, start, start + len));

View File

@ -27,10 +27,7 @@ public class ChargingPileEventHandler implements ClientEventHandler {
public void lostConnection(ClientHandler handler) { public void lostConnection(ClientHandler handler) {
String pileNo = ChargingPileServer.getPileNo(handler); String pileNo = ChargingPileServer.getPileNo(handler);
if (pileNo != null) { log.info("lost connection -> ({}) [{}] <- {}",
ChargingPileServer.removeHandler(pileNo);
}
log.info("lost connection -> [{}] {} <- {}",
pileNo, handler.getName(), handler.getSocket().getRemoteSocketAddress().toString()); pileNo, handler.getName(), handler.getSocket().getRemoteSocketAddress().toString());
} }

View File

@ -92,7 +92,7 @@ public class ChargingPileServer {
cacheMap.put("status", DISCONNECTED); cacheMap.put("status", DISCONNECTED);
REDIS.setCacheMap(pkey, cacheMap); REDIS.setCacheMap(pkey, cacheMap);
if (handler != null) { if (handler != null) {
log.info("remove handler [{}] for [{}]", handler.getName(), pileNo); log.info("remove handler [{}] for ({})", handler.getName(), pileNo);
pileMap.remove(handler.getName()); pileMap.remove(handler.getName());
versionMapper.remove(handler.getName()); versionMapper.remove(handler.getName());
} }
@ -100,14 +100,14 @@ public class ChargingPileServer {
public static void sendClientMsg(String pileNo, byte[] msg) { public static void sendClientMsg(String pileNo, byte[] msg) {
log.info("server send msg >>>> [{}] |{}|", pileNo, HexUtils.toHex(msg)); log.info("server send msg >>>> ({}) |{}|", pileNo, HexUtils.toHex(msg));
if (pileNo.length() < 14) { if (pileNo.length() < 14) {
pileNo = "0000000000000000" + pileNo; pileNo = "0000000000000000" + pileNo;
pileNo = pileNo.substring(pileNo.length() - 14); pileNo = pileNo.substring(pileNo.length() - 14);
} }
ClientHandler handler = handlerMap.get(pileNo); ClientHandler handler = handlerMap.get(pileNo);
if (handler == null || !handler.isOpen()) { if (handler == null || !handler.isOpen()) {
log.error("send message failed. [{}] connection lost", pileNo); log.error("send message failed. ({}) connection lost", pileNo);
removeHandler(pileNo); removeHandler(pileNo);
return; return;
} }