update 电单车协议

This commit is contained in:
Guoqs
2024-07-15 16:05:15 +08:00
parent 61b948aea9
commit b9ec56a202
5 changed files with 259 additions and 2 deletions

View File

@@ -36,7 +36,7 @@ public class NettyServerManager implements CommandLineRunner {
public void run(String... args) throws Exception {
startNettyServer(Constants.SOCKET_IP, 9011);
startElectricBikeNettyServer(Constants.SOCKET_IP, 9012);
startMqttSever(Constants.SOCKET_IP, 1883);
// startMqttSever(Constants.SOCKET_IP, 1883);
}
public void startNettyServer(String host, int port) {

View File

@@ -0,0 +1,155 @@
package com.jsowell.netty.server.electricbicycles;
import com.jsowell.netty.domain.ChargingPileMessage;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.time.Instant;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@ChannelHandler.Sharable
@Slf4j
@Component
public class ChargingPileHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object 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();
}
}

View File

@@ -1,5 +1,6 @@
package com.jsowell.netty.server.electricbicycles;
import com.jsowell.netty.decoder.ChargingPileDecoder;
import com.jsowell.netty.decoder.ProtocolDnyDecoder;
import com.jsowell.netty.decoder.StartAndLengthFieldFrameDecoder;
import io.netty.channel.ChannelInitializer;
@@ -18,11 +19,14 @@ public class ElectricBicyclesServerChannelInitializer extends ChannelInitializer
@Resource
ElectricBicyclesServerHandler electricBicyclesServerHandler;
@Resource
ChargingPileHandler chargingPileHandler;
@Override
protected void initChannel(SocketChannel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
// pipeline.addLast("frameDecoder",new CustomDecoder());
pipeline.addLast("frameDecoder", new ProtocolDnyDecoder());
pipeline.addLast("frameDecoder", new ChargingPileDecoder());
pipeline.addLast("decoder", new ByteArrayDecoder());
pipeline.addLast("encoder", new ByteArrayDecoder());
//读超时时间设置为10s0表示不监控