From 3da69c7954d6df151a28e5f2e7e0992ea9fe2aa6 Mon Sep 17 00:00:00 2001 From: Guoqs <123@jsowell.com> Date: Wed, 4 Sep 2024 11:59:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=93=E5=8D=B0=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PowerHeartbeatHandler.java | 94 +++++++++++++++++++ .../impl/EBikeBusinessServiceImpl.java | 4 +- .../pile/domain/ebike/EBikeCommandEnum.java | 17 ++-- 3 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 jsowell-netty/src/main/java/com/jsowell/netty/handler/electricbicycles/PowerHeartbeatHandler.java 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 new file mode 100644 index 000000000..35d4d942e --- /dev/null +++ b/jsowell-netty/src/main/java/com/jsowell/netty/handler/electricbicycles/PowerHeartbeatHandler.java @@ -0,0 +1,94 @@ +package com.jsowell.netty.handler.electricbicycles; + +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; +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.PileConnectorInfoService; +import io.netty.channel.ChannelHandlerContext; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * 端口充电时功率心跳包 + */ +@Slf4j +@Component +public class PowerHeartbeatHandler extends AbstractEBikeHandler { + private final String type = YKCUtils.frameType2Str(EBikeCommandEnum.POWER_HEARTBEAT.getBytes()); + + @Autowired + private PileConnectorInfoService pileConnectorInfoService; + + @Override + public void afterPropertiesSet() throws Exception { + EBikeOperateFactory.register(type, this); + } + + /** + * 执行逻辑 + * 有应答 + * + * @param dataProtocol + * @param ctx + */ + @Override + public byte[] supplyProcess(EBikeDataProtocol dataProtocol, ChannelHandlerContext ctx) { + // 解析字节数组 + EBikeMessageCmd06 message = (EBikeMessageCmd06) AbsEBikeMessage.parseMessage(dataProtocol.getBytes()); + // 保存时间 + saveLastTimeAndCheckChannel(message.getPhysicalId() + "", ctx); + + EBikeMessageCmd06.PowerHeartbeat powerHeartbeat = message.getPowerHeartbeat(); + log.debug("端口充电时功率心跳包:{}", JSON.toJSONString(message)); + + // updatePileStatus(message); + + return getResult(dataProtocol, Constants.zeroByteArray); + } + + /** + * 更新充电桩状态 + * @param message + */ + private void updatePileStatus(EBikeMessageCmd21 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)); + } + } + + /** + * 电单车协议状态转换为数据库状态 + * @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/service/electricbicycles/impl/EBikeBusinessServiceImpl.java b/jsowell-netty/src/main/java/com/jsowell/netty/service/electricbicycles/impl/EBikeBusinessServiceImpl.java index 2de48fd0f..690666cd4 100644 --- a/jsowell-netty/src/main/java/com/jsowell/netty/service/electricbicycles/impl/EBikeBusinessServiceImpl.java +++ b/jsowell-netty/src/main/java/com/jsowell/netty/service/electricbicycles/impl/EBikeBusinessServiceImpl.java @@ -10,6 +10,7 @@ import com.jsowell.common.util.YKCUtils; import com.jsowell.netty.factory.EBikeOperateFactory; import com.jsowell.netty.handler.electricbicycles.AbstractEBikeHandler; import com.jsowell.netty.service.electricbicycles.EBikeBusinessService; +import com.jsowell.pile.domain.ebike.EBikeCommandEnum; import com.jsowell.pile.service.OrderBasicInfoService; import com.jsowell.pile.service.PileConnectorInfoService; import com.jsowell.pile.service.PileMsgRecordService; @@ -37,7 +38,8 @@ public class EBikeBusinessServiceImpl implements EBikeBusinessService { EBikeDataProtocol eBikeDataProtocol = new EBikeDataProtocol(msg); // 获取帧类型 String command = YKCUtils.frameType2Str(eBikeDataProtocol.getCommand()); - log.info("电单车===>收到消息, channelId:{}, 指令:{}, msg:{}", ctx.channel().id().toString(), command, BytesUtil.binary(msg, 16)); + log.info("电单车===>收到消息, channelId:{}, 指令:{}, 指令名称:{}, msg:{}", ctx.channel().id().toString(), command, + EBikeCommandEnum.getDescByCode(BytesUtil.bytesToIntLittle(eBikeDataProtocol.getCommand())), BytesUtil.binary(msg, 16)); // 获取业务处理handler AbstractEBikeHandler invokeStrategy = EBikeOperateFactory.getInvokeStrategy(command); if (invokeStrategy != null) { diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/domain/ebike/EBikeCommandEnum.java b/jsowell-pile/src/main/java/com/jsowell/pile/domain/ebike/EBikeCommandEnum.java index eac6caf35..626977eb3 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/domain/ebike/EBikeCommandEnum.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/domain/ebike/EBikeCommandEnum.java @@ -51,13 +51,14 @@ public enum EBikeCommandEnum { return BytesUtil.intToBytesLittle(code, 1); } - // public static Class getMsgClassByCode(String code) { - // for (EBikeCommandEnum e : EBikeCommandEnum.values()) { - // if (e.getCode().equals(code)) { - // return e.getMsgClass(); - // } - // } - // return null; - // } + // 根据code获取desc + public static String getDescByCode(int code) { + for (EBikeCommandEnum e : EBikeCommandEnum.values()) { + if (e.getCode() == code) { + return e.getDesc(); + } + } + return null; + } } \ No newline at end of file