打印日志

This commit is contained in:
Guoqs
2024-09-04 11:59:05 +08:00
parent 26ea5822d8
commit 3da69c7954
3 changed files with 106 additions and 9 deletions

View File

@@ -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<String> 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;
}
}

View File

@@ -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) {

View File

@@ -51,13 +51,14 @@ public enum EBikeCommandEnum {
return BytesUtil.intToBytesLittle(code, 1);
}
// public static Class<? extends AbsEBikeMessage> 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;
}
}