1. 增加发送控制器控制命令调试接口

2. 增加控制器控制命令响应协议解析
This commit is contained in:
HuangXin 2023-08-14 20:48:18 +08:00
parent b1ca040ef0
commit e4270b51b8
14 changed files with 346 additions and 103 deletions

View File

@ -31,4 +31,4 @@ mybatis.configuration.default-enum-type-handler=com.zjyr.beidouservice.common.Co
#mybatis.configuration.log-impl=lombok.extern.slf4j.Slf4j #mybatis.configuration.log-impl=lombok.extern.slf4j.Slf4j
#config log #config log
logging.config=file:config/logback.xml logging.config=file:config/logback.xml
log4j.logger.org.mybatis=debug #log4j.logger.org.mybatis=debug

View File

@ -1,3 +1,4 @@
socket.server-host=localhost socket.server-host=localhost
socket.server-port=10000 socket.server-port=10000
socket.server-mode=tcp socket.server-mode=tcp
socket.heart-timeout=60

View File

@ -4,8 +4,6 @@
<property name="LOG_PATH" value="./logs"/> <property name="LOG_PATH" value="./logs"/>
<property name="LOG_LEVEL" value="info"/> <property name="LOG_LEVEL" value="info"/>
<property name="SVR_LOG_LEVEL" value="info"/> <property name="SVR_LOG_LEVEL" value="info"/>
<property name="DEBUG_LOG_LEVEL" value="debug"/>
<property name="INFO_LOG_LEVEL" value="info"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder charset="UTF-8"> <encoder charset="UTF-8">
@ -48,7 +46,7 @@
</encoder> </encoder>
</appender> </appender>
<logger name="com.zjyr.beidouservice.mapper" level="debug" additivity="false"> <logger name="com.zjyr.beidouservice.mapper" level="${LOG_LEVEL}" additivity="false">
<appender-ref ref="DATA"/> <appender-ref ref="DATA"/>
<appender-ref ref="CONSOLE"/> <appender-ref ref="CONSOLE"/>
</logger> </logger>

View File

@ -3,6 +3,7 @@ package com.zjyr.beidouservice.adapter.impl.netty;
import com.zjyr.beidouservice.adapter.impl.netty.decode.YuanRongProtocolDecode; import com.zjyr.beidouservice.adapter.impl.netty.decode.YuanRongProtocolDecode;
import com.zjyr.beidouservice.adapter.impl.netty.encode.ProtocolStartEncode; import com.zjyr.beidouservice.adapter.impl.netty.encode.ProtocolStartEncode;
import com.zjyr.beidouservice.adapter.impl.netty.encode.YuanRongProtocolEncode; import com.zjyr.beidouservice.adapter.impl.netty.encode.YuanRongProtocolEncode;
import com.zjyr.beidouservice.config.NettySocketConfigure;
import com.zjyr.beidouservice.pojo.vo.binary.BaseBinaryProtocol; import com.zjyr.beidouservice.pojo.vo.binary.BaseBinaryProtocol;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
@ -53,19 +54,21 @@ public class ChannelInit<T> extends ChannelInitializer<SocketChannel> {
@Resource @Resource
private MessageHandler<T> messageHandler; private MessageHandler<T> messageHandler;
@Resource
private NettySocketConfigure nettySocketConfigure;
@Override @Override
protected void initChannel(SocketChannel channel) { protected void initChannel(SocketChannel channel) {
channel.pipeline() channel.pipeline()
.addLast("idle", new IdleStateHandler(0, 0, 60, TimeUnit.SECONDS)) .addLast(new IdleStateHandler(0, 0, nettySocketConfigure.getHeartTimeout(), TimeUnit.SECONDS))
.addLast("decode", .addLast(new LengthFieldBasedFrameDecoder(MAX_FRAME_LENGTH,
new LengthFieldBasedFrameDecoder(MAX_FRAME_LENGTH,
LENGTH_FIELD_OFFSET, LENGTH_FIELD_OFFSET,
LENGTH_FIELD_LENGTH, LENGTH_FIELD_LENGTH,
LENGTH_ADJUSTMENT, LENGTH_ADJUSTMENT,
INITIAL_BYTES_TO_STRIP)) INITIAL_BYTES_TO_STRIP))
.addLast("decodeProtocol", new YuanRongProtocolDecode()) .addLast(new YuanRongProtocolDecode())
.addLast("startFlag", new ProtocolStartEncode(2)) .addLast(new ProtocolStartEncode(2))
.addLast("encode", new YuanRongProtocolEncode<T>()) .addLast(new YuanRongProtocolEncode<T>())
.addLast("message", messageHandler); .addLast("message", messageHandler);
} }
} }

View File

@ -1,11 +1,16 @@
package com.zjyr.beidouservice.adapter.impl.netty.decode; package com.zjyr.beidouservice.adapter.impl.netty.decode;
import com.zjyr.beidouservice.common.CommonEnumHandler; import com.zjyr.beidouservice.common.CommonEnumHandler;
import com.zjyr.beidouservice.common.impl.BeidouStatusName;
import com.zjyr.beidouservice.common.impl.ControlCommandName; import com.zjyr.beidouservice.common.impl.ControlCommandName;
import com.zjyr.beidouservice.common.impl.SendStatusName;
import com.zjyr.beidouservice.common.impl.SensorStatusName; import com.zjyr.beidouservice.common.impl.SensorStatusName;
import com.zjyr.beidouservice.common.impl.TelphoneStatusName;
import com.zjyr.beidouservice.common.impl.WirelessStatusName;
import com.zjyr.beidouservice.pojo.vo.binary.BaseBinaryProtocol; import com.zjyr.beidouservice.pojo.vo.binary.BaseBinaryProtocol;
import com.zjyr.beidouservice.pojo.vo.binary.ControllerStatus; import com.zjyr.beidouservice.pojo.vo.binary.ControllerStatus;
import com.zjyr.beidouservice.pojo.vo.binary.MessageContent; import com.zjyr.beidouservice.pojo.vo.binary.MessageContent;
import com.zjyr.beidouservice.pojo.vo.binary.SensorCotrolAck;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil; import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
@ -24,61 +29,95 @@ import java.util.List;
@Slf4j @Slf4j
public class YuanRongProtocolDecode extends ByteToMessageDecoder { public class YuanRongProtocolDecode extends ByteToMessageDecoder {
@Override @Override
protected void decode(ChannelHandlerContext channelHandlerContext, protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf buf, List<Object> list) {
ByteBuf buf,
List<Object> list) throws Exception {
log.info("\n{}", ByteBufUtil.prettyHexDump(buf)); log.info("\n{}", ByteBufUtil.prettyHexDump(buf));
short msgLength = buf.readShort(); try {
byte version = buf.readByte(); short msgLength = buf.readShort();
int recvMajorId = buf.readInt(); byte version = buf.readByte();
int recvMinorId = buf.readInt(); int recvMajorId = buf.readInt();
int sendMajorId = buf.readInt(); int recvMinorId = buf.readInt();
int sendMinorId = buf.readInt(); int sendMajorId = buf.readInt();
byte cryptCytp = buf.readByte(); int sendMinorId = buf.readInt();
int timeStamp = buf.readInt(); byte cryptCytp = buf.readByte();
int statusCode = buf.readInt(); int timeStamp = buf.readInt();
byte msgType = buf.readByte(); int statusCode = buf.readInt();
short msgSize = buf.readShort(); byte msgType = buf.readByte();
short msgSize = buf.readShort();
ControlCommandName cmd = CommonEnumHandler.codeOf(ControlCommandName.class, msgType); ControlCommandName cmd = CommonEnumHandler.codeOf(ControlCommandName.class, msgType);
if (cmd != null) { if (cmd != null) {
switch (cmd) { switch (cmd) {
case COMMAND_REPORT_HEART -> { case COMMAND_REPORT_HEART -> {
byte[] beidouSignal = new byte[10]; byte[] beidouSignal = new byte[10];
buf.readBytes(beidouSignal, 0, 10); buf.readBytes(beidouSignal, 0, 10);
SensorStatusName wireStatus = CommonEnumHandler.codeOf(SensorStatusName.class, buf.readByte()); SensorStatusName wireStatus = CommonEnumHandler.codeOf(SensorStatusName.class, buf.readByte());
SensorStatusName phoneStatus = CommonEnumHandler.codeOf(SensorStatusName.class, buf.readByte()); SensorStatusName phoneStatus = CommonEnumHandler.codeOf(SensorStatusName.class, buf.readByte());
ControllerStatus status = ControllerStatus.builder() ControllerStatus status = ControllerStatus.builder()
.beidouSignalStrength(beidouSignal) .beidouSignalStrength(beidouSignal)
.wirelessStatus(wireStatus) .wirelessStatus(wireStatus)
.telphoneStatus(phoneStatus) .telphoneStatus(phoneStatus)
.build(); .build();
MessageContent<ControllerStatus> msgCtx = MessageContent.<ControllerStatus>builder() MessageContent<ControllerStatus> msgCtx = MessageContent.<ControllerStatus>builder().msgType(
.msgType(msgType) msgType).msgSize(msgSize).msgBody(status).build();
.msgSize(msgSize)
.msgBody(status)
.build();
list.add(BaseBinaryProtocol.<ControllerStatus>builder() list.add(BaseBinaryProtocol.<ControllerStatus>builder()
.msgLength(msgLength) .msgLength(msgLength)
.version(version) .version(version)
.recvMajorId(recvMajorId) .recvMajorId(recvMajorId)
.recvMinorId(recvMinorId) .recvMinorId(recvMinorId)
.sendMajorId(sendMajorId) .sendMajorId(sendMajorId)
.sendMinorId(sendMinorId) .sendMinorId(sendMinorId)
.cryptoType(cryptCytp) .cryptoType(cryptCytp)
.timeStamp(timeStamp) .timeStamp(timeStamp)
.statusCode(statusCode) .statusCode(statusCode)
.msgContent(msgCtx) .msgContent(msgCtx)
.build()); .build());
}
case COMMAND_REPORT_SENSOR -> {
SendStatusName sendStatus = CommonEnumHandler.codeOf(SendStatusName.class, buf.readByte());
BeidouStatusName beidouStatus = CommonEnumHandler.codeOf(BeidouStatusName.class,
buf.readByte());
WirelessStatusName wireStatus = CommonEnumHandler.codeOf(WirelessStatusName.class,
buf.readByte());
TelphoneStatusName phoneStatus = CommonEnumHandler.codeOf(TelphoneStatusName.class,
buf.readByte());
byte[] beidouSignle = new byte[3];
buf.readBytes(beidouSignle, 0, 3);
SensorCotrolAck ack = SensorCotrolAck.builder()
.sendStatus(sendStatus)
.beidouStatus(beidouStatus)
.wirelessStatus(wireStatus)
.telphoneStatus(phoneStatus)
.beidouFreq(beidouSignle)
.build();
MessageContent<SensorCotrolAck> msgCtx = MessageContent.<SensorCotrolAck>builder().msgType(
msgType).msgSize(msgSize).msgBody(ack).build();
list.add(BaseBinaryProtocol.<SensorCotrolAck>builder()
.msgLength(msgLength)
.version(version)
.recvMajorId(recvMajorId)
.recvMinorId(recvMinorId)
.sendMajorId(sendMajorId)
.sendMinorId(sendMinorId)
.cryptoType(cryptCytp)
.timeStamp(timeStamp)
.statusCode(statusCode)
.msgContent(msgCtx)
.build());
}
case COMMAND_REPORT_QUERY_SENSOR -> {
}
default -> log.error("Unsupport Command: {}({})", cmd, msgType);
} }
case COMMAND_REPORT_SENSOR -> {
}
case COMMAND_REPORT_QUERY_SENSOR -> {
}
default -> log.error("Unsupport Command: {}({})", cmd, msgType);
} }
} catch (Exception ex) {
log.error("Clear buffer because error data");
buf.resetReaderIndex();
buf.resetWriterIndex();
} }
} }
} }

View File

@ -0,0 +1,40 @@
package com.zjyr.beidouservice.common.impl;
import com.zjyr.beidouservice.common.EnumerationBase;
public enum BeidouStatusName implements EnumerationBase {
BEIDOU_NORMAL(0, "BEIDOU_NORMAL"),
BEIDOU_MODULE_EXCEPTION(1, "BEIDOU_MODULE_EXCEPTION"),
BEIDOU_SATELLITE_EXCEPTION(2, "BEIDOU_SATELLITE_EXCEPTION"),
;
/**
* The Code.
*/
private final Integer code;
/**
* The Desc.
*/
private final String desc;
/**
* Instantiates a new Beidou adapter type name.
*
* @param val the val
* @param desc the desc
*/
BeidouStatusName(int val, String desc) {
this.code = val;
this.desc = desc;
}
@Override
public Integer getValue() {
return this.code;
}
@Override
public String getDescription() {
return this.desc;
}
}

View File

@ -0,0 +1,41 @@
package com.zjyr.beidouservice.common.impl;
import com.zjyr.beidouservice.common.EnumerationBase;
public enum SendStatusName implements EnumerationBase {
SEND_SUCCESS(0, "SEND_SUCCESS"),
SEND_WAIT_IDLE(1, "SEND_WAIT_IDLE"),
SEND_DEVICE_UNEXISTS(2, "SEND_DEVICE_UNEXISTS"),
SEND_CONTROL_LOCKED(3, "SEND_CONTROL_LOCKED"),
;
/**
* The Code.
*/
private final Integer code;
/**
* The Desc.
*/
private final String desc;
/**
* Instantiates a new Beidou adapter type name.
*
* @param val the val
* @param desc the desc
*/
SendStatusName(int val, String desc) {
this.code = val;
this.desc = desc;
}
@Override
public Integer getValue() {
return this.code;
}
@Override
public String getDescription() {
return this.desc;
}
}

View File

@ -0,0 +1,40 @@
package com.zjyr.beidouservice.common.impl;
import com.zjyr.beidouservice.common.EnumerationBase;
public enum TelphoneStatusName implements EnumerationBase {
TELPHONE_NORMAL(0, "TELPHONE_NORMAL"),
TELPHONE_NOT_DETECTION(1, "TELPHONE_NOT_DETECTION"),
TELPHONE_BREAKDOWN(2, "TELPHONE_BREAKDOWN"),
;
/**
* The Code.
*/
private final Integer code;
/**
* The Desc.
*/
private final String desc;
/**
* Instantiates a new Beidou adapter type name.
*
* @param val the val
* @param desc the desc
*/
TelphoneStatusName(int val, String desc) {
this.code = val;
this.desc = desc;
}
@Override
public Integer getValue() {
return this.code;
}
@Override
public String getDescription() {
return this.desc;
}
}

View File

@ -0,0 +1,40 @@
package com.zjyr.beidouservice.common.impl;
import com.zjyr.beidouservice.common.EnumerationBase;
public enum WirelessStatusName implements EnumerationBase {
WIRELESS_NORMAL(0, "WIRELESS_NORMAL"),
WIRELESS_BREAKDOWN(0, "WIRELESS_BREAKDOWN"),
;
/**
* The Code.
*/
private final Integer code;
/**
* The Desc.
*/
private final String desc;
/**
* Instantiates a new Beidou adapter type name.
*
* @param val the val
* @param desc the desc
*/
WirelessStatusName(int val, String desc) {
this.code = val;
this.desc = desc;
}
@Override
public Integer getValue() {
return this.code;
}
@Override
public String getDescription() {
return this.desc;
}
}

View File

@ -27,6 +27,8 @@ public class NettySocketConfigure {
*/ */
private String serverMode; private String serverMode;
private Integer heartTimeout;
/** /**
* Init global value. * Init global value.
*/ */

View File

@ -1,11 +1,26 @@
package com.zjyr.beidouservice.controller; package com.zjyr.beidouservice.controller;
import com.zjyr.beidouservice.common.impl.SensorControlActionName;
import com.zjyr.beidouservice.common.impl.SensorControlModeName;
import com.zjyr.beidouservice.common.impl.SensorControlTunnelName;
import com.zjyr.beidouservice.pojo.dto.BaseProtocolDTO; import com.zjyr.beidouservice.pojo.dto.BaseProtocolDTO;
import com.zjyr.beidouservice.pojo.dto.GetUserConfig; import com.zjyr.beidouservice.pojo.dto.GetUserConfig;
import com.zjyr.beidouservice.pojo.po.BeidouAdapterControlContent;
import com.zjyr.beidouservice.pojo.vo.binary.SensorControlProtocol;
import com.zjyr.beidouservice.service.AdapterProtocolService;
import com.zjyr.beidouservice.service.BaidouAdapterService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
/** /**
* The type Auth user. * The type Auth user.
@ -14,6 +29,12 @@ import org.springframework.web.bind.annotation.*;
//@RequestMapping(value = "/auth") //@RequestMapping(value = "/auth")
@Slf4j @Slf4j
public class AuthUser { public class AuthUser {
@Resource
BaidouAdapterService baidouAdapterService;
@Resource
AdapterProtocolService adapterProtocolService;
/** /**
* Third string. * Third string.
* *
@ -35,46 +56,38 @@ public class AuthUser {
* @param headers the headers * @param headers the headers
* @return the user config * @return the user config
*/ */
@PostMapping("/getuserconfig") @PostMapping("/sensor/control")
@ResponseBody @ResponseBody
public String getUserConfig(@RequestBody BaseProtocolDTO<GetUserConfig> mr, @RequestHeader HttpHeaders headers) { public String getUserConfig(@RequestBody BaseProtocolDTO<GetUserConfig> mr, @RequestHeader HttpHeaders headers) {
return """ List<BeidouAdapterControlContent> adapterInfo = new ArrayList<>();
{
"ver": 1, for (int i = 0; i < 2; i++) {
"cryptoType": 0, adapterInfo.add(BeidouAdapterControlContent.builder()
"timeStamp": 1688954003, .districtsCode(0x10 + i)
"code": 0, .sensorId(0x3f00 + i)
"msgContent": { .controlAction(SensorControlActionName.ACTION_DISTRICTS_BASED)
"scgCtrlAppId": 7, .build());
"scgTunnelAppId": 8, }
"cliPrivateKey": "WGAlqvys3O83VmWug6Z8NzUrxGr/PNHSeOVFnKLSe2k=",
"cliPublicKey": "6BWnmkCJqJC5iNoCEZWTxwGNG7qwkxFoVgAk4DoIKCk=", SensorControlProtocol sp =
"cliAddress": "10.10.10.1/32", adapterProtocolService.createSensorControlProtocol(SensorControlTunnelName.TUNNEL_BEIDOU,
"vmConfig": [ new byte[]{0x01, 0x02, 0x03},
{ 8,
"vmId": 1, 14,
"vmName": "Windows10_1", 20,
"svrPublicKey": "8KEaqtWM5U35SR8S3QJriGHPr0VIqvk8A7BEuOjjp1M=", 35,
"vmNetwork": "10.0.4.16/30", 3,
"scgGateway": "101.35.234.160:10010" 5,
}, SensorControlModeName.CONTROL_TYPE_QUIET,
{ 0,
"vmId": 2, adapterProtocolService.createSensorCtrlInfo(
"vmName": "Windows10_2", adapterInfo));
"svrPublicKey": "q3ep8hN2v3VpHbcru+rTmvyBt13iH0DkEsVAyT2LpVo=", Long id = baidouAdapterService.getAllAdapter().get(0).getId();
"vmNetwork": "192.168.100.210/24", if (id != null) {
"scgGateway": "efc.xajhuang.com:10000" baidouAdapterService.sendCommond(id, sp);
}, }
{
"vmId": 3, return "{\"result\": 0}";
"vmName": "Windows10_3",
"svrPublicKey": "q3ep8hN2v3VpHbcru+rTmvyBt13iH0DkEsVAyT2LpVo=",
"vmNetwork": "172.18.2.152/26",
"scgGateway": "localhost:10000"
}
]
}
}""";
} }
/** /**

View File

@ -23,6 +23,6 @@ public class ControlAdapterSocketCtx {
public ControlAdapterSocketCtx(Long id, Channel ch) { public ControlAdapterSocketCtx(Long id, Channel ch) {
this.controlAdapterId = id; this.controlAdapterId = id;
this.channel = ch; this.channel = ch;
} }
} }

View File

@ -0,0 +1,19 @@
package com.zjyr.beidouservice.pojo.vo.binary;
import com.zjyr.beidouservice.common.impl.BeidouStatusName;
import com.zjyr.beidouservice.common.impl.SendStatusName;
import com.zjyr.beidouservice.common.impl.TelphoneStatusName;
import com.zjyr.beidouservice.common.impl.WirelessStatusName;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class SensorCotrolAck {
private byte[] beidouFreq;
private SendStatusName sendStatus;
private BeidouStatusName beidouStatus;
private WirelessStatusName wirelessStatus;
private TelphoneStatusName telphoneStatus;
}

View File

@ -9,7 +9,7 @@ import com.zjyr.beidouservice.pojo.entry.ControlDevice;
import com.zjyr.beidouservice.pojo.po.BeidouAdapterDevice; import com.zjyr.beidouservice.pojo.po.BeidouAdapterDevice;
import com.zjyr.beidouservice.pojo.vo.ControlAdapterSocketCtx; import com.zjyr.beidouservice.pojo.vo.ControlAdapterSocketCtx;
import com.zjyr.beidouservice.pojo.vo.binary.ControllerStatus; import com.zjyr.beidouservice.pojo.vo.binary.ControllerStatus;
import com.zjyr.beidouservice.pojo.vo.binary.HeartProtocol; import com.zjyr.beidouservice.pojo.vo.binary.SensorCotrolAck;
import com.zjyr.beidouservice.service.AdapterProtocolService; import com.zjyr.beidouservice.service.AdapterProtocolService;
import com.zjyr.beidouservice.service.BaidouAdapterService; import com.zjyr.beidouservice.service.BaidouAdapterService;
import io.netty.channel.Channel; import io.netty.channel.Channel;
@ -59,9 +59,9 @@ public class BaidouSocketAdapterServiceImpl<T extends ApplicationEvent> implemen
@Override @Override
public void sendCommond(Channel ch, Object command) { public void sendCommond(Channel ch, Object command) {
if (command instanceof HeartProtocol heart) { //if (command instanceof HeartProtocol heart) {
tcpServer.sendData(ch, adapterProtocolService.createTransmissionProtocol(heart)); tcpServer.sendData(ch, adapterProtocolService.createTransmissionProtocol(command));
} //}
} }
@Override @Override
@ -99,6 +99,13 @@ public class BaidouSocketAdapterServiceImpl<T extends ApplicationEvent> implemen
status.getWirelessStatus(), status.getWirelessStatus(),
status.getTelphoneStatus(), status.getTelphoneStatus(),
HelperUtils.bytesToHexString(status.getBeidouSignalStrength())); HelperUtils.bytesToHexString(status.getBeidouSignalStrength()));
} else if (event.getEvtMessage() instanceof SensorCotrolAck ack) {
log.info("+++{}, {}, {}, {}, {}",
ack.getSendStatus(),
ack.getBeidouStatus(),
ack.getWirelessStatus(),
ack.getTelphoneStatus(),
HelperUtils.bytesToHexString(ack.getBeidouFreq()));
} }
} }
} }