diff --git a/jsowell-common/src/main/java/com/jsowell/common/enums/ebike/PortStatusEnum.java b/jsowell-common/src/main/java/com/jsowell/common/enums/ebike/PortStatusEnum.java index b254d0200..9eadd30ed 100644 --- a/jsowell-common/src/main/java/com/jsowell/common/enums/ebike/PortStatusEnum.java +++ b/jsowell-common/src/main/java/com/jsowell/common/enums/ebike/PortStatusEnum.java @@ -1,5 +1,8 @@ package com.jsowell.common.enums.ebike; +import com.jsowell.common.constant.Constants; +import com.jsowell.common.util.StringUtils; + /** * 电单车端口状态 */ @@ -64,4 +67,20 @@ public enum PortStatusEnum { } return null; } + + /** + * 电单车协议状态转换为数据库状态 + * @param eBikeStatus + * @return + */ + public static String eBikeStatusTransformDBStatus(String eBikeStatus) { + String dbStatus = Constants.ZERO; // 默认0-离网 + if (StringUtils.isNotBlank(eBikeStatus)) { + PortStatusEnum enumByValue = PortStatusEnum.getEnumByValue(Integer.parseInt(eBikeStatus)); + if (enumByValue != null) { + dbStatus = enumByValue.getDbStatus(); + } + } + return dbStatus; + } } \ No newline at end of file diff --git a/jsowell-netty/src/main/java/com/jsowell/netty/handler/electricbicycles/HeartbeatHandler.java b/jsowell-netty/src/main/java/com/jsowell/netty/handler/electricbicycles/HeartbeatHandler.java index 3c57fa995..a4d141b48 100644 --- a/jsowell-netty/src/main/java/com/jsowell/netty/handler/electricbicycles/HeartbeatHandler.java +++ b/jsowell-netty/src/main/java/com/jsowell/netty/handler/electricbicycles/HeartbeatHandler.java @@ -4,7 +4,6 @@ import com.alibaba.fastjson2.JSON; import com.jsowell.common.constant.Constants; import com.jsowell.common.core.domain.ebike.EBikeDataProtocol; import com.jsowell.common.enums.ebike.PortStatusEnum; -import com.jsowell.common.util.StringUtils; import com.jsowell.common.util.YKCUtils; import com.jsowell.netty.factory.EBikeOperateFactory; import com.jsowell.pile.domain.ebike.AbsEBikeMessage; @@ -75,23 +74,8 @@ public class HeartbeatHandler extends AbstractEBikeHandler { String pileConnectorCode = pileSn + connectorCode; // 电单车协议状态 String eBikeStatus = portStatus.get(i); - pileConnectorInfoService.updateConnectorStatus(pileConnectorCode, eBikeStatusTransformDBStatus(eBikeStatus)); + pileConnectorInfoService.updateConnectorStatus(pileConnectorCode, PortStatusEnum.eBikeStatusTransformDBStatus(eBikeStatus)); } } - /** - * 电单车协议状态转换为数据库状态 - * @param eBikeStatus - * @return - */ - private String eBikeStatusTransformDBStatus(String eBikeStatus) { - String dbStatus = Constants.ZERO; // 默认0-离网 - if (StringUtils.isNotBlank(eBikeStatus)) { - PortStatusEnum enumByValue = PortStatusEnum.getEnumByValue(Integer.parseInt(eBikeStatus)); - if (enumByValue != null) { - dbStatus = enumByValue.getDbStatus(); - } - } - return dbStatus; - } } diff --git a/jsowell-netty/src/main/java/com/jsowell/netty/handler/electricbicycles/PowerHeartbeatHandler.java b/jsowell-netty/src/main/java/com/jsowell/netty/handler/electricbicycles/PowerHeartbeatHandler.java index c1d0d385e..2929abfd0 100644 --- a/jsowell-netty/src/main/java/com/jsowell/netty/handler/electricbicycles/PowerHeartbeatHandler.java +++ b/jsowell-netty/src/main/java/com/jsowell/netty/handler/electricbicycles/PowerHeartbeatHandler.java @@ -6,13 +6,11 @@ import com.jsowell.common.core.domain.ebike.EBikeDataProtocol; import com.jsowell.common.core.domain.ykc.RealTimeMonitorData; import com.jsowell.common.enums.ebike.PortStatusEnum; import com.jsowell.common.util.DateUtils; -import com.jsowell.common.util.StringUtils; import com.jsowell.common.util.YKCUtils; import com.jsowell.netty.factory.EBikeOperateFactory; import com.jsowell.pile.domain.ebike.AbsEBikeMessage; import com.jsowell.pile.domain.ebike.EBikeCommandEnum; import com.jsowell.pile.domain.ebike.deviceupload.EBikeMessageCmd06; -import com.jsowell.pile.domain.ebike.deviceupload.EBikeMessageCmd21; import com.jsowell.pile.service.PileBasicInfoService; import com.jsowell.pile.service.PileConnectorInfoService; import io.netty.channel.ChannelHandlerContext; @@ -20,8 +18,6 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.util.List; - /** * 端口充电时功率心跳包 */ @@ -58,8 +54,10 @@ public class PowerHeartbeatHandler extends AbstractEBikeHandler { EBikeMessageCmd06.PowerHeartbeat powerHeartbeat = message.getPowerHeartbeat(); log.info("端口充电时功率心跳包:{}", JSON.toJSONString(message)); - // updatePileStatus(message); + // 更新枪口状态 + updatePileStatus(message); + // 保存实时监控数据 saveRealTimeMonitorData(message); return getResult(dataProtocol, Constants.zeroByteArray); @@ -80,7 +78,7 @@ public class PowerHeartbeatHandler extends AbstractEBikeHandler { realTimeMonitorData.setConnectorStatus(powerHeartbeat.getPortStatus()); realTimeMonitorData.setOutputVoltage(powerHeartbeat.getVoltage()); realTimeMonitorData.setOutputCurrent(powerHeartbeat.getCurrent()); - realTimeMonitorData.setOutputPower(""); + // realTimeMonitorData.setOutputPower(""); realTimeMonitorData.setDateTime(DateUtils.getDateTime()); pileBasicInfoService.saveRealTimeMonitorData2Redis(realTimeMonitorData); @@ -90,34 +88,12 @@ public class PowerHeartbeatHandler extends AbstractEBikeHandler { * 更新充电桩状态 * @param message */ - private void updatePileStatus(EBikeMessageCmd21 message) { + private void updatePileStatus(EBikeMessageCmd06 message) { String pileSn = message.getPhysicalId() + ""; - EBikeMessageCmd21.DeviceHeartbeat deviceHeartbeat = message.getDeviceHeartbeat(); - int portNumber = deviceHeartbeat.getPortNumber(); - List portStatus = deviceHeartbeat.getPortStatus(); - for (int i = 0; i < portNumber; i++) { - // 组装pile_connector_info表数据 - String connectorCode = String.format("%1$02d", i + 1); - String pileConnectorCode = pileSn + connectorCode; - // 电单车协议状态 - String eBikeStatus = portStatus.get(i); - pileConnectorInfoService.updateConnectorStatus(pileConnectorCode, eBikeStatusTransformDBStatus(eBikeStatus)); - } + EBikeMessageCmd06.PowerHeartbeat powerHeartbeat = message.getPowerHeartbeat(); + String connectorCode = powerHeartbeat.getPort(); + String portStatus = powerHeartbeat.getPortStatus(); + pileConnectorInfoService.updateConnectorStatus(pileSn + connectorCode, PortStatusEnum.eBikeStatusTransformDBStatus(portStatus)); } - /** - * 电单车协议状态转换为数据库状态 - * @param eBikeStatus - * @return - */ - private String eBikeStatusTransformDBStatus(String eBikeStatus) { - String dbStatus = Constants.ZERO; // 默认0-离网 - if (StringUtils.isNotBlank(eBikeStatus)) { - PortStatusEnum enumByValue = PortStatusEnum.getEnumByValue(Integer.parseInt(eBikeStatus)); - if (enumByValue != null) { - dbStatus = enumByValue.getDbStatus(); - } - } - return dbStatus; - } } diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/domain/ebike/serversend/EBikeMessageCmd82.java b/jsowell-pile/src/main/java/com/jsowell/pile/domain/ebike/serversend/EBikeMessageCmd82.java index b82c28e5d..b02eb952b 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/domain/ebike/serversend/EBikeMessageCmd82.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/domain/ebike/serversend/EBikeMessageCmd82.java @@ -42,7 +42,6 @@ public class EBikeMessageCmd82 extends AbsEBikeMessage { // 消息ID byte[] messageIdBytes = BytesUtil.intToBytesLittle(messageId, 2); // 命令 - // byte[] commandBytes = BytesUtil.intToBytesLittle(Integer.parseInt(command), 1); byte[] commandBytes = new byte[]{(byte) 0x82}; // 数据 byte[] payloadBytes = data.getBytes(); diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/service/EBikeSendCommandService.java b/jsowell-pile/src/main/java/com/jsowell/pile/service/EBikeSendCommandService.java index 901f46515..57761bfb2 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/service/EBikeSendCommandService.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/service/EBikeSendCommandService.java @@ -12,12 +12,12 @@ public interface EBikeSendCommandService { * 启动充电 * @param command */ - void sendStartChargingCommand(StartChargingCommand command); + void sendStartChargingCommand(StartChargingCommand command) throws Exception; /** * 停止充电 * @param command */ - void sendStopChargingCommand(StopChargingCommand command); + void sendStopChargingCommand(StopChargingCommand command) throws Exception; } diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/service/PileRemoteService.java b/jsowell-pile/src/main/java/com/jsowell/pile/service/PileRemoteService.java index 32a3eb4b5..5700f3599 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/service/PileRemoteService.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/service/PileRemoteService.java @@ -126,8 +126,11 @@ public class PileRemoteService { .transactionCode(transactionCode) .chargeAmount(chargeAmount) .build(); - // ykcPushCommandService.pushStartChargingCommand(startChargingCommand); - eBikeSendCommandService.sendStartChargingCommand(startChargingCommand); + try { + eBikeSendCommandService.sendStartChargingCommand(startChargingCommand); + } catch (Exception e) { + log.error("电单车远程启动充电error", e); + } } /** diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/EBikeSendCommandServiceImpl.java b/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/EBikeSendCommandServiceImpl.java index cc0c9adfb..d3788d146 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/EBikeSendCommandServiceImpl.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/EBikeSendCommandServiceImpl.java @@ -1,8 +1,10 @@ package com.jsowell.pile.service.impl; import com.jsowell.common.enums.ykc.PileChannelEntity; +import com.jsowell.common.protocol.SyncPromise; import com.jsowell.common.util.BytesUtil; import com.jsowell.common.util.RandomUtil; +import com.jsowell.common.util.RpcUtil; import com.jsowell.pile.domain.ebike.AbsEBikeMessage; import com.jsowell.pile.domain.ebike.serversend.EBikeMessageCmd82; import com.jsowell.pile.domain.ykcCommond.StartChargingCommand; @@ -17,6 +19,7 @@ import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.util.Objects; +import java.util.concurrent.TimeUnit; @Slf4j @Service @@ -29,7 +32,7 @@ public class EBikeSendCommandServiceImpl implements EBikeSendCommandService { * 电单车发送启动充电指令 */ @Override - public void sendStartChargingCommand(StartChargingCommand command) { + public void sendStartChargingCommand(StartChargingCommand command) throws Exception { String pileSn = command.getPileSn(); String connectorCode = command.getConnectorCode(); String transactionCode = command.getTransactionCode(); @@ -81,7 +84,7 @@ public class EBikeSendCommandServiceImpl implements EBikeSendCommandService { * @param command */ @Override - public void sendStopChargingCommand(StopChargingCommand command) { + public void sendStopChargingCommand(StopChargingCommand command) throws Exception { String pileSn = command.getPileSn(); String connectorCode = command.getConnectorCode(); String transactionCode = command.getTransactionCode(); @@ -128,7 +131,10 @@ public class EBikeSendCommandServiceImpl implements EBikeSendCommandService { * 公共方法, 发送指令 * @param msg */ - private void send(AbsEBikeMessage msg) { + private void send(AbsEBikeMessage msg) throws Exception { + this.send(msg, 3, TimeUnit.SECONDS); + } + private void send(AbsEBikeMessage msg, long timeout, TimeUnit unit) throws Exception { String pileSn = msg.getPhysicalId() + ""; byte[] messageBytes = msg.getMessageBytes(); // PileChannelEntity.output(); @@ -139,6 +145,14 @@ public class EBikeSendCommandServiceImpl implements EBikeSendCommandService { log.error("电单车send命令失败, 桩号:{}无法获取到长连接, 请检查充电桩连接状态!", pileSn); throw new NullPointerException("channel"); } + + // 创造一个容器,用于存放当前线程与rpcClient中的线程交互 + SyncPromise syncPromise = new SyncPromise(); + // 消息id = channelId + 帧类型(例如: "0x34") + String msgId = ctx.channel().id().toString() + "_" + msg.getCommand(); + log.info("同步获取响应数据-发送消息, msgId:{}", msgId); + RpcUtil.getSyncPromiseMap().put(msgId, syncPromise); + ByteBuf byteBuf = ctx.channel().alloc().buffer().writeBytes(messageBytes); ChannelFuture channelFuture = ctx.channel().writeAndFlush(byteBuf); channelFuture.addListener((ChannelFutureListener) channelFutureListener -> { @@ -154,6 +168,19 @@ public class EBikeSendCommandServiceImpl implements EBikeSendCommandService { log.error("电单车send发送命令失败, pileSn:{}", pileSn, cause); } }); + + // 等待获取结果 + byte[] rpcResponse = syncPromise.get(timeout, unit); + if(rpcResponse == null) { + if(syncPromise.isTimeout()) { + // throw new TimeoutException("等待响应结果超时"); + log.error("发送[{}]指令后, 等待响应结果超时", msg.getCommand()); + } else{ + log.error("发送[{}]指令后, 发生其他异常", msg.getCommand()); + } + } + // 移除容器 + RpcUtil.getSyncPromiseMap().remove(msgId); } } diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/OrderBasicInfoServiceImpl.java b/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/OrderBasicInfoServiceImpl.java index a9a099c3f..51bc806a1 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/OrderBasicInfoServiceImpl.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/OrderBasicInfoServiceImpl.java @@ -1954,13 +1954,13 @@ public class OrderBasicInfoServiceImpl implements OrderBasicInfoService { String redisKey = CacheConstants.PILE_REAL_TIME_MONITOR_DATA + pileConnectorCode + "_" + transactionCode; // 拿到所有数据 Map map = redisCache.hmget(redisKey); - // logger.info("查redis中的实时数据, pileConnectorCode:{}, transactionCode:{}, map:{}", pileConnectorCode, transactionCode, map); + // logger.debug("查redis中的实时数据, pileConnectorCode:{}, transactionCode:{}, map:{}", pileConnectorCode, transactionCode, map); if (map == null || map.isEmpty()) { // 按照电单车重新查 pileConnectorCode = transactionCode.substring(0, 10); // 电单车车桩 redisKey = CacheConstants.PILE_REAL_TIME_MONITOR_DATA + pileConnectorCode + "_" + transactionCode; map = redisCache.hmget(redisKey); - // logger.info("按照电单车重新查, pileConnectorCode:{}, transactionCode:{}, map:{}", pileConnectorCode, transactionCode, map); + // logger.debug("按照电单车重新查, pileConnectorCode:{}, transactionCode:{}, map:{}", pileConnectorCode, transactionCode, map); } if (map != null && !map.isEmpty()) { diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/YKCPushCommandServiceImpl.java b/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/YKCPushCommandServiceImpl.java index 033c52b39..80870266a 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/YKCPushCommandServiceImpl.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/YKCPushCommandServiceImpl.java @@ -154,7 +154,6 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService { // 创造一个容器,用于存放当前线程与rpcClient中的线程交互 SyncPromise syncPromise = new SyncPromise(); - // 消息id = channelId + 帧类型(例如: "0x34") String msgId = ctx.channel().id().toString() + "_" + YKCUtils.frameType2Str(((YKCFrameTypeCode) frameTypeCode).getBytes()); // log.info("同步获取响应数据-发送消息, msgId:{}", msgId); @@ -208,7 +207,6 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService { // 等待获取结果 byte[] rpcResponse = syncPromise.get(timeout, unit); - if(rpcResponse == null) { if(syncPromise.isTimeout()) { // throw new TimeoutException("等待响应结果超时"); @@ -217,7 +215,6 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService { log.error("发送[{}]指令后, 发生其他异常", value); } } - // 移除容器 RpcUtil.getSyncPromiseMap().remove(msgId);