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
#config log
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-port=10000
socket.server-mode=tcp
socket.heart-timeout=60

View File

@ -4,8 +4,6 @@
<property name="LOG_PATH" value="./logs"/>
<property name="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">
<encoder charset="UTF-8">
@ -48,7 +46,7 @@
</encoder>
</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="CONSOLE"/>
</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.encode.ProtocolStartEncode;
import com.zjyr.beidouservice.adapter.impl.netty.encode.YuanRongProtocolEncode;
import com.zjyr.beidouservice.config.NettySocketConfigure;
import com.zjyr.beidouservice.pojo.vo.binary.BaseBinaryProtocol;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
@ -53,19 +54,21 @@ public class ChannelInit<T> extends ChannelInitializer<SocketChannel> {
@Resource
private MessageHandler<T> messageHandler;
@Resource
private NettySocketConfigure nettySocketConfigure;
@Override
protected void initChannel(SocketChannel channel) {
channel.pipeline()
.addLast("idle", new IdleStateHandler(0, 0, 60, TimeUnit.SECONDS))
.addLast("decode",
new LengthFieldBasedFrameDecoder(MAX_FRAME_LENGTH,
.addLast(new IdleStateHandler(0, 0, nettySocketConfigure.getHeartTimeout(), TimeUnit.SECONDS))
.addLast(new LengthFieldBasedFrameDecoder(MAX_FRAME_LENGTH,
LENGTH_FIELD_OFFSET,
LENGTH_FIELD_LENGTH,
LENGTH_ADJUSTMENT,
INITIAL_BYTES_TO_STRIP))
.addLast("decodeProtocol", new YuanRongProtocolDecode())
.addLast("startFlag", new ProtocolStartEncode(2))
.addLast("encode", new YuanRongProtocolEncode<T>())
.addLast(new YuanRongProtocolDecode())
.addLast(new ProtocolStartEncode(2))
.addLast(new YuanRongProtocolEncode<T>())
.addLast("message", messageHandler);
}
}

View File

@ -1,11 +1,16 @@
package com.zjyr.beidouservice.adapter.impl.netty.decode;
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.SendStatusName;
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.ControllerStatus;
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.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
@ -24,61 +29,95 @@ import java.util.List;
@Slf4j
public class YuanRongProtocolDecode extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext channelHandlerContext,
ByteBuf buf,
List<Object> list) throws Exception {
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf buf, List<Object> list) {
log.info("\n{}", ByteBufUtil.prettyHexDump(buf));
short msgLength = buf.readShort();
byte version = buf.readByte();
int recvMajorId = buf.readInt();
int recvMinorId = buf.readInt();
int sendMajorId = buf.readInt();
int sendMinorId = buf.readInt();
byte cryptCytp = buf.readByte();
int timeStamp = buf.readInt();
int statusCode = buf.readInt();
byte msgType = buf.readByte();
short msgSize = buf.readShort();
try {
short msgLength = buf.readShort();
byte version = buf.readByte();
int recvMajorId = buf.readInt();
int recvMinorId = buf.readInt();
int sendMajorId = buf.readInt();
int sendMinorId = buf.readInt();
byte cryptCytp = buf.readByte();
int timeStamp = buf.readInt();
int statusCode = buf.readInt();
byte msgType = buf.readByte();
short msgSize = buf.readShort();
ControlCommandName cmd = CommonEnumHandler.codeOf(ControlCommandName.class, msgType);
if (cmd != null) {
switch (cmd) {
case COMMAND_REPORT_HEART -> {
byte[] beidouSignal = new byte[10];
buf.readBytes(beidouSignal, 0, 10);
SensorStatusName wireStatus = CommonEnumHandler.codeOf(SensorStatusName.class, buf.readByte());
SensorStatusName phoneStatus = CommonEnumHandler.codeOf(SensorStatusName.class, buf.readByte());
ControlCommandName cmd = CommonEnumHandler.codeOf(ControlCommandName.class, msgType);
if (cmd != null) {
switch (cmd) {
case COMMAND_REPORT_HEART -> {
byte[] beidouSignal = new byte[10];
buf.readBytes(beidouSignal, 0, 10);
SensorStatusName wireStatus = CommonEnumHandler.codeOf(SensorStatusName.class, buf.readByte());
SensorStatusName phoneStatus = CommonEnumHandler.codeOf(SensorStatusName.class, buf.readByte());
ControllerStatus status = ControllerStatus.builder()
.beidouSignalStrength(beidouSignal)
.wirelessStatus(wireStatus)
.telphoneStatus(phoneStatus)
.build();
MessageContent<ControllerStatus> msgCtx = MessageContent.<ControllerStatus>builder()
.msgType(msgType)
.msgSize(msgSize)
.msgBody(status)
.build();
ControllerStatus status = ControllerStatus.builder()
.beidouSignalStrength(beidouSignal)
.wirelessStatus(wireStatus)
.telphoneStatus(phoneStatus)
.build();
MessageContent<ControllerStatus> msgCtx = MessageContent.<ControllerStatus>builder().msgType(
msgType).msgSize(msgSize).msgBody(status).build();
list.add(BaseBinaryProtocol.<ControllerStatus>builder()
.msgLength(msgLength)
.version(version)
.recvMajorId(recvMajorId)
.recvMinorId(recvMinorId)
.sendMajorId(sendMajorId)
.sendMinorId(sendMinorId)
.cryptoType(cryptCytp)
.timeStamp(timeStamp)
.statusCode(statusCode)
.msgContent(msgCtx)
.build());
list.add(BaseBinaryProtocol.<ControllerStatus>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_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 Integer heartTimeout;
/**
* Init global value.
*/

View File

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

View File

@ -23,6 +23,6 @@ public class ControlAdapterSocketCtx {
public ControlAdapterSocketCtx(Long id, Channel ch) {
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.vo.ControlAdapterSocketCtx;
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.BaidouAdapterService;
import io.netty.channel.Channel;
@ -59,9 +59,9 @@ public class BaidouSocketAdapterServiceImpl<T extends ApplicationEvent> implemen
@Override
public void sendCommond(Channel ch, Object command) {
if (command instanceof HeartProtocol heart) {
tcpServer.sendData(ch, adapterProtocolService.createTransmissionProtocol(heart));
}
//if (command instanceof HeartProtocol heart) {
tcpServer.sendData(ch, adapterProtocolService.createTransmissionProtocol(command));
//}
}
@Override
@ -99,6 +99,13 @@ public class BaidouSocketAdapterServiceImpl<T extends ApplicationEvent> implemen
status.getWirelessStatus(),
status.getTelphoneStatus(),
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()));
}
}
}