update 电单车协议

This commit is contained in:
Guoqs
2024-08-29 15:46:27 +08:00
parent 741c37cf5e
commit d8ff7c4361
6 changed files with 88 additions and 13 deletions

View File

@@ -16,8 +16,8 @@ import org.springframework.stereotype.Component;
*/
@Slf4j
@Component
public class GetServerTimeHandler extends AbstractEBikeHandler {
private final String type = EBikeCommandEnum.GET_SERVER_TIME.getCode();
public class DeviceGetServerTimeHandler extends AbstractEBikeHandler {
private final String type = EBikeCommandEnum.DEVICE_GET_SERVER_TIME.getCode();
@Override
public void afterPropertiesSet() throws Exception {
@@ -35,7 +35,7 @@ public class GetServerTimeHandler extends AbstractEBikeHandler {
public byte[] supplyProcess(EBikeDataProtocol dataProtocol, ChannelHandlerContext ctx) {
// 解析字节数组
EBikeMessageCmd22 message = (EBikeMessageCmd22) AbsEBikeMessage.parseMessage(dataProtocol.getBytes());
log.info("设备获取服务器时间:{}", JSON.toJSONString(message));
log.info("设备 获取服务器时间:{}", JSON.toJSONString(message));
// 保存时间
saveLastTimeAndCheckChannel(message.getPhysicalId() + "", ctx);

View File

@@ -40,15 +40,6 @@ public class HeartbeatHandler extends AbstractEBikeHandler {
// 保存时间
saveLastTimeAndCheckChannel(message.getPhysicalId() + "", ctx);
try {
byte[] serialize = SerializationUtil.serialize(message);
log.info("心跳包序列化:{}", BytesUtil.binary(serialize, 16));
EBikeMessageCmd21 deserialize = SerializationUtil.deserialize(EBikeMessageCmd21.class, serialize);
log.info("心跳包反序列化:{}", JSON.toJSONString(deserialize));
} catch (Exception e) {
log.info("error", e);
}
EBikeMessageCmd21.DeviceHeartbeat deviceHeartbeat = message.getDeviceHeartbeat();
log.info("设备心跳包:{}", JSON.toJSONString(message));
return getResult(dataProtocol, Constants.zeroByteArray);

View File

@@ -0,0 +1,47 @@
package com.jsowell.netty.handler.electricbicycles;
import com.alibaba.fastjson2.JSON;
import com.jsowell.common.core.domain.ebike.EBikeDataProtocol;
import com.jsowell.common.util.BytesUtil;
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.EBikeMessageCmd12;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* 设备获取服务器时间
*/
@Slf4j
@Component
public class HostGetServerTimeHandler extends AbstractEBikeHandler {
private final String type = EBikeCommandEnum.HOST_GET_SERVER_TIME.getCode();
@Override
public void afterPropertiesSet() throws Exception {
EBikeOperateFactory.register(type, this);
}
/**
* 执行逻辑
* 有应答
*
* @param dataProtocol
* @param ctx
*/
@Override
public byte[] supplyProcess(EBikeDataProtocol dataProtocol, ChannelHandlerContext ctx) {
// 解析字节数组
EBikeMessageCmd12 message = (EBikeMessageCmd12) AbsEBikeMessage.parseMessage(dataProtocol.getBytes());
log.info("主机 获取服务器时间:{}", JSON.toJSONString(message));
// 保存时间
saveLastTimeAndCheckChannel(message.getPhysicalId() + "", ctx);
// 获取当前服务器10位时间戳
byte[] timeBytes = BytesUtil.getIntBytes((int) (System.currentTimeMillis() / 1000));
// System.out.println("data: " + BytesUtil.bytesToIntLittle(timeBytes));
return getResult(dataProtocol, timeBytes);
}
}

View File

@@ -42,6 +42,8 @@ public abstract class AbsEBikeMessage {
return new EBikeMessageCmd04(header, length, physicalId, messageId, command, null, checksum, new EBikeMessageCmd04.ConfirmOrder(dataBytes));
case "06":
return new EBikeMessageCmd06(header, length, physicalId, messageId, command, null, checksum, new EBikeMessageCmd06.PowerHeartbeat(dataBytes));
case "12":
return new EBikeMessageCmd12(header, length, physicalId, messageId, command, null, checksum);
case "20":
return new EBikeMessageCmd20(header, length, physicalId, messageId, command, null, checksum, new EBikeMessageCmd20.DeviceRegister(dataBytes));
case "21":

View File

@@ -9,8 +9,9 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public enum EBikeCommandEnum {
// HEARTBEAT("01", "设备心跳包"),
HOST_GET_SERVER_TIME("12", "主机获取服务器时间", EBikeMessageCmd12.class),
REGISTRATION("20", "设备注册包", EBikeMessageCmd20.class),
GET_SERVER_TIME("22", "设备获取服务器时间", EBikeMessageCmd22.class),
DEVICE_GET_SERVER_TIME("22", "设备获取服务器时间", EBikeMessageCmd22.class),
HEARTBEAT_2("21", "设备心跳包", EBikeMessageCmd21.class),
CARD_OPERATION("02", "刷卡操作", EBikeMessageCmd02.class),
SETTLEMENT_UPLOAD("03", "结算消费信息上传", EBikeMessageCmd03.class),

View File

@@ -0,0 +1,34 @@
package com.jsowell.pile.domain.ebike.deviceupload;
import com.jsowell.common.util.bean.SerializationUtil;
import com.jsowell.pile.domain.ebike.AbsEBikeMessage;
import lombok.Getter;
import lombok.Setter;
/**
* 主机获取服务器时间0x12
* 此命令主机每次上电后就会发送直至服务器应答后就停止发送。如服务器无应答则每隔3分钟发送一次请求
* 每12小时从服务器获取一次时间如服务器不应答则每隔3分钟发送一次请求
* 不带RTC模块的主机不会发送此命令
* DE 5C A9 5F转小端模式=0x5FA95CDE=1604934878=2020-11-09 23:14:38
*/
@Getter
@Setter
public class EBikeMessageCmd12 extends AbsEBikeMessage {
public EBikeMessageCmd12(String header, int length, int physicalId, int messageId, String command, Object payload, int checksum) {
super(header, length, physicalId, messageId, command, payload, checksum);
}
@Override
public void parsePayload(byte[] dataBytes) {
}
@Override
public byte[] getMessageBytes() {
byte[] serialize = SerializationUtil.serialize(this);
return serialize;
}
}