mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-06-21 23:59:50 +08:00
update 电单车协议
This commit is contained in:
@@ -27,12 +27,18 @@ public class ChargingPileMessage {
|
|||||||
public static ChargingPileMessage parseMessage(byte[] messageBytes) {
|
public static ChargingPileMessage parseMessage(byte[] messageBytes) {
|
||||||
log.info("parseMessage:{}", BytesUtil.binary(messageBytes, 16));
|
log.info("parseMessage:{}", BytesUtil.binary(messageBytes, 16));
|
||||||
String header = new String(Arrays.copyOfRange(messageBytes, 0, 3));
|
String header = new String(Arrays.copyOfRange(messageBytes, 0, 3));
|
||||||
|
|
||||||
int length = ((messageBytes[3] & 0xFF) << 8) | (messageBytes[4] & 0xFF);
|
int length = ((messageBytes[3] & 0xFF) << 8) | (messageBytes[4] & 0xFF);
|
||||||
|
|
||||||
int physicalId = ((messageBytes[5] & 0xFF) << 24) | ((messageBytes[6] & 0xFF) << 16) |
|
int physicalId = ((messageBytes[5] & 0xFF) << 24) | ((messageBytes[6] & 0xFF) << 16) |
|
||||||
((messageBytes[7] & 0xFF) << 8) | (messageBytes[8] & 0xFF);
|
((messageBytes[7] & 0xFF) << 8) | (messageBytes[8] & 0xFF);
|
||||||
|
|
||||||
int messageId = ((messageBytes[9] & 0xFF) << 8) | (messageBytes[10] & 0xFF);
|
int messageId = ((messageBytes[9] & 0xFF) << 8) | (messageBytes[10] & 0xFF);
|
||||||
|
|
||||||
byte command = messageBytes[11];
|
byte command = messageBytes[11];
|
||||||
|
|
||||||
byte[] data = Arrays.copyOfRange(messageBytes, 12, messageBytes.length - 2);
|
byte[] data = Arrays.copyOfRange(messageBytes, 12, messageBytes.length - 2);
|
||||||
|
|
||||||
int checksum = ((messageBytes[messageBytes.length - 2] & 0xFF) << 8) | (messageBytes[messageBytes.length - 1] & 0xFF);
|
int checksum = ((messageBytes[messageBytes.length - 2] & 0xFF) << 8) | (messageBytes[messageBytes.length - 1] & 0xFF);
|
||||||
|
|
||||||
return new ChargingPileMessage(header, length, physicalId, messageId, command, data, checksum);
|
return new ChargingPileMessage(header, length, physicalId, messageId, command, data, checksum);
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
package com.jsowell.netty.server.electricbicycles;
|
package com.jsowell.netty.server.electricbicycles;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSON;
|
||||||
import com.jsowell.common.util.BytesUtil;
|
import com.jsowell.common.util.BytesUtil;
|
||||||
import com.jsowell.netty.domain.ChargingPileMessage;
|
import com.jsowell.netty.domain.ChargingPileMessage;
|
||||||
import io.netty.channel.ChannelHandler;
|
import io.netty.channel.*;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
|
||||||
import io.netty.channel.ChannelId;
|
|
||||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@@ -20,169 +18,11 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
@ChannelHandler.Sharable
|
@ChannelHandler.Sharable
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
public class ChargingPileHandler extends ChannelInboundHandlerAdapter {
|
public class ChargingPileHandler extends SimpleChannelInboundHandler<ChargingPileMessage> {
|
||||||
|
|
||||||
/**
|
|
||||||
* 管理一个全局map,保存连接进服务端的通道数量
|
|
||||||
*/
|
|
||||||
private static final ConcurrentHashMap<ChannelId, ChannelHandlerContext> CHANNEL_MAP = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 有客户端连接服务器会触发此函数
|
|
||||||
* 连接被建立并且准备进行通信时被调用
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void channelActive(ChannelHandlerContext ctx) {
|
|
||||||
InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress();
|
|
||||||
String clientIp = insocket.getAddress().getHostAddress();
|
|
||||||
int clientPort = insocket.getPort();
|
|
||||||
//获取连接通道唯一标识
|
|
||||||
ChannelId channelId = ctx.channel().id();
|
|
||||||
//如果map中不包含此连接,就保存连接
|
|
||||||
if (CHANNEL_MAP.containsKey(channelId)) {
|
|
||||||
log.info("Handler:{}, 客户端【{}】是连接状态,连接通道数量: {}", this.getClass().getSimpleName(), channelId, CHANNEL_MAP.size());
|
|
||||||
} else {
|
|
||||||
//保存连接
|
|
||||||
CHANNEL_MAP.put(channelId, ctx);
|
|
||||||
log.info("Handler:{}, 客户端【{}】, 连接netty服务器【IP:{}, PORT:{}】, 连接通道数量: {}", this.getClass().getSimpleName(), channelId, clientIp, clientPort, CHANNEL_MAP.size());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelRead(ChannelHandlerContext ctx, Object msg) {
|
protected void channelRead0(ChannelHandlerContext ctx, ChargingPileMessage msg) throws Exception {
|
||||||
log.info("加载客户端报文=== channelId:{}, mag:{}", ctx.channel().id(), BytesUtil.binary((byte[]) msg, 16));
|
log.info("收到消息, channelId:{}, msg:{}", ctx.channel().id().toString(), JSON.toJSONString(msg));
|
||||||
// if (!(msg instanceof ChargingPileMessage)) {
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// ChargingPileMessage message = (ChargingPileMessage) msg;
|
|
||||||
// byte command = message.getCommand();
|
|
||||||
//
|
|
||||||
// switch (command) {
|
|
||||||
// case 0x11:
|
|
||||||
// handleHeartbeat(ctx, message);
|
|
||||||
// break;
|
|
||||||
// case 0x12:
|
|
||||||
// handleTimeRequest(ctx, message);
|
|
||||||
// break;
|
|
||||||
// case 0x15:
|
|
||||||
// handleFirmwareUpgradeRequest(ctx, message);
|
|
||||||
// break;
|
|
||||||
// case (byte) 0xFA:
|
|
||||||
// handleFirmwareUpgradeResponse(ctx, message);
|
|
||||||
// break;
|
|
||||||
// case 0x31:
|
|
||||||
// handleReboot(ctx, message);
|
|
||||||
// break;
|
|
||||||
// case 0x32:
|
|
||||||
// handleCommunicationModuleReboot(ctx, message);
|
|
||||||
// break;
|
|
||||||
// case 0x33:
|
|
||||||
// handleClearUpgradeData(ctx, message);
|
|
||||||
// break;
|
|
||||||
// case 0x34:
|
|
||||||
// handleChangeIPAddress(ctx, message);
|
|
||||||
// break;
|
|
||||||
// case 0x35:
|
|
||||||
// handleSubdeviceVersionUpload(ctx, message);
|
|
||||||
// break;
|
|
||||||
// case 0x3B:
|
|
||||||
// handleFSKParameterRequest(ctx, message);
|
|
||||||
// break;
|
|
||||||
// default:
|
|
||||||
// log.info("Unknown command: " + String.format("0x%02X", command));
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleHeartbeat(ChannelHandlerContext ctx, ChargingPileMessage message) {
|
|
||||||
// 处理心跳包,无需回复
|
|
||||||
log.info("Received heartbeat from device ID: " + message.getPhysicalId());
|
|
||||||
// 可以在这里更新设备状态、记录日志等
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleTimeRequest(ChannelHandlerContext ctx, ChargingPileMessage message) {
|
|
||||||
// 处理时间请求
|
|
||||||
// long currentTime = Instant.now().getEpochSecond();
|
|
||||||
// byte[] timeBytes = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt((int)currentTime).array();
|
|
||||||
//
|
|
||||||
// ChargingPileMessage response = new ChargingPileMessage(
|
|
||||||
// message.getPhysicalId(),
|
|
||||||
// message.getMessageId(),
|
|
||||||
// (byte) 0x12,
|
|
||||||
// timeBytes
|
|
||||||
// );
|
|
||||||
//
|
|
||||||
// ctx.writeAndFlush(response);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleFirmwareUpgradeRequest(ChannelHandlerContext ctx, ChargingPileMessage message) {
|
|
||||||
// 处理固件升级请求
|
|
||||||
// 这里需要实现固件升级的逻辑,包括发送固件数据包等
|
|
||||||
log.info("Firmware upgrade requested from device ID: " + message.getPhysicalId());
|
|
||||||
// TODO: 实现固件升级逻辑
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleFirmwareUpgradeResponse(ChannelHandlerContext ctx, ChargingPileMessage message) {
|
|
||||||
// 处理固件升级响应
|
|
||||||
log.info("Firmware upgrade response from device ID: " + message.getPhysicalId());
|
|
||||||
// TODO: 根据响应继续发送下一个固件包或结束升级过程
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleReboot(ChannelHandlerContext ctx, ChargingPileMessage message) {
|
|
||||||
// 处理重启主机指令
|
|
||||||
log.info("Reboot command received for device ID: " + message.getPhysicalId());
|
|
||||||
// 发送成功响应
|
|
||||||
sendSimpleResponse(ctx, message, (byte) 0x31, (byte) 0x00);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleCommunicationModuleReboot(ChannelHandlerContext ctx, ChargingPileMessage message) {
|
|
||||||
// 处理重启通信模块指令
|
|
||||||
log.info("Communication module reboot command received for device ID: " + message.getPhysicalId());
|
|
||||||
// 发送成功响应
|
|
||||||
sendSimpleResponse(ctx, message, (byte) 0x32, (byte) 0x00);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleClearUpgradeData(ChannelHandlerContext ctx, ChargingPileMessage message) {
|
|
||||||
// 处理清空升级分机数据指令
|
|
||||||
log.info("Clear upgrade data command received for device ID: " + message.getPhysicalId());
|
|
||||||
// 发送成功响应
|
|
||||||
sendSimpleResponse(ctx, message, (byte) 0x33, (byte) 0x00);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleChangeIPAddress(ChannelHandlerContext ctx, ChargingPileMessage message) {
|
|
||||||
// 处理更改IP地址指令
|
|
||||||
log.info("Change IP address command received for device ID: " + message.getPhysicalId());
|
|
||||||
// TODO: 实现IP地址更改逻辑
|
|
||||||
// 发送成功响应
|
|
||||||
sendSimpleResponse(ctx, message, (byte) 0x34, (byte) 0x00);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleSubdeviceVersionUpload(ChannelHandlerContext ctx, ChargingPileMessage message) {
|
|
||||||
// 处理上传分机版本号与设备类型
|
|
||||||
log.info("Subdevice version upload received from device ID: " + message.getPhysicalId());
|
|
||||||
// TODO: 处理上传的分机版本信息
|
|
||||||
// 此命令不需要响应
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleFSKParameterRequest(ChannelHandlerContext ctx, ChargingPileMessage message) {
|
|
||||||
// 处理请求服务器FSK主机参数
|
|
||||||
log.info("FSK parameter request received from device ID: " + message.getPhysicalId());
|
|
||||||
// TODO: 实现发送FSK参数的逻辑(使用0x3A指令)
|
|
||||||
}
|
|
||||||
|
|
||||||
private void sendSimpleResponse(ChannelHandlerContext ctx, ChargingPileMessage originalMessage, byte command, byte result) {
|
|
||||||
// ChargingPileMessage response = new ChargingPileMessage(
|
|
||||||
// originalMessage.getPhysicalId(),
|
|
||||||
// originalMessage.getMessageId(),
|
|
||||||
// command,
|
|
||||||
// new byte[]{result}
|
|
||||||
// );
|
|
||||||
// ctx.writeAndFlush(response);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
|
||||||
cause.printStackTrace();
|
|
||||||
ctx.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user