1. 修改心跳协议以及控制器状态回复协议

This commit is contained in:
黄昕 2023-08-21 14:55:09 +08:00
parent 28c904076a
commit 1faa9ff0ca
17 changed files with 223 additions and 377 deletions

View File

@ -1,20 +1,12 @@
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.ControlChannelStatus;
import com.zjyr.beidouservice.common.impl.ControlCommandName;
import com.zjyr.beidouservice.common.impl.SendStatusName;
import com.zjyr.beidouservice.common.impl.SensorControlTunnelName;
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.misc.HelperUtils;
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 com.zjyr.beidouservice.pojo.vo.binary.SensorStatusAck;
import com.zjyr.beidouservice.pojo.vo.binary.SensorStatusInfo;
import com.zjyr.beidouservice.pojo.vo.binary.*;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
@ -50,16 +42,24 @@ public class YuanRongProtocolDecode extends ByteToMessageDecoder {
if (cmd != null) {
switch (cmd) {
case COMMAND_REPORT_HEART -> {
SendStatusName sendStatus = CommonEnumHandler.codeOf(SendStatusName.class, buf.readByte());
ControlChannelStatus beidouStatus = CommonEnumHandler.codeOf(ControlChannelStatus.class, buf.readByte());
byte[] beidouCommFreq = new byte[3];
buf.readBytes(beidouCommFreq, 0, 3);
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());
ControlChannelStatus wireStatus = CommonEnumHandler.codeOf(ControlChannelStatus.class, buf.readByte());
ControlChannelStatus phoneStatus = CommonEnumHandler.codeOf(ControlChannelStatus.class, buf.readByte());
ControllerStatus status = ControllerStatus.builder()
.sendStatus(sendStatus)
.beidouStatus(beidouStatus)
.beidouFreq(beidouCommFreq)
.beidouSignalStrength(beidouSignal)
.wirelessStatus(wireStatus)
.telphoneStatus(phoneStatus)
.build();
MessageContent<ControllerStatus> msgCtx = MessageContent.<ControllerStatus>builder().msgType(
msgType).msgSize(msgSize).msgBody(status).build();
@ -74,35 +74,35 @@ public class YuanRongProtocolDecode extends ByteToMessageDecoder {
.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)
.commandId(commandId)
.cryptoType(cryptCytp)
.timeStamp(timeStamp)
.statusCode(statusCode)
.msgContent(msgCtx)
.build());
// 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)
// .commandId(commandId)
// .cryptoType(cryptCytp)
// .timeStamp(timeStamp)
// .statusCode(statusCode)
// .msgContent(msgCtx)
// .build());
}
case COMMAND_REPORT_QUERY_SENSOR -> {
int nItems = buf.readByte();

View File

@ -62,7 +62,7 @@ public class YuanRongProtocolEncode<T> extends MessageToByteEncoder<BaseBinaryPr
byteBuf.writeByte(msg.getNControlInfo());
for (var c : msg.getControllContents()) {
byteBuf.writeByte(c.getDistrictsCode());
byteBuf.writeByte(c.getControlAction().getValue().byteValue());
//byteBuf.writeByte(c.getControlAction().getValue().byteValue());
byteBuf.writeShort(c.getSensorId());
}
}
@ -76,7 +76,7 @@ public class YuanRongProtocolEncode<T> extends MessageToByteEncoder<BaseBinaryPr
byteBuf.writeByte(msg.getNControlInfo());
for (var c : msg.getControllContents()) {
byteBuf.writeByte(c.getDistrictsCode());
byteBuf.writeByte(c.getControlAction().getValue().byteValue());
//byteBuf.writeByte(c.getControlAction().getValue().byteValue());
byteBuf.writeShort(c.getSensorId());
}
}

View File

@ -2,10 +2,12 @@ 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"),
public enum ControlChannelStatus implements EnumerationBase {
CHANNEL_NORMAL(0, "CHANNEL_NORMAL"),
CHANNEL_MODULE_UNEXISTS(1, "CHANNEL_MODULE_UNEXISTS"),
CHANNEL_MODULE_EXCEPTION(2, "CHANNEL_MODULE_EXCEPTION"),
BEIDOU_MODULE_EXCEPTION(3, "BEIDOU_MODULE_EXCEPTION"),
BEIDOU_SATELLITE_EXCEPTION(4, "BEIDOU_SATELLITE_EXCEPTION"),
;
/**
@ -23,7 +25,7 @@ public enum BeidouStatusName implements EnumerationBase {
* @param val the val
* @param desc the desc
*/
BeidouStatusName(int val, String desc) {
ControlChannelStatus(int val, String desc) {
this.code = val;
this.desc = desc;
}

View File

@ -1,54 +0,0 @@
package com.zjyr.beidouservice.common.impl;
import com.zjyr.beidouservice.common.EnumerationBase;
/**
* The enum Sensor control action name.
*/
public enum SensorControlActionName implements EnumerationBase {
/**
* Action single point sensor control action name.
*/
ACTION_SINGLE_POINT(0, "ACTION_SINGLE_POINT"),
/**
* Action city based sensor control action name.
*/
ACTION_CITY_BASED(1, "ACTION_CITY_BASED"),
/**
* Action districts based sensor control action name.
*/
ACTION_DISTRICTS_BASED(2, "ACTION_DISTRICTS_BASED"),
;
/**
* The Code.
*/
private final Integer code;
/**
* The Desc.
*/
private final String desc;
/**
* Instantiates a new Sensor control action name.
*
* @param val the val
* @param desc the desc
*/
SensorControlActionName(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

@ -1,54 +0,0 @@
package com.zjyr.beidouservice.common.impl;
import com.zjyr.beidouservice.common.EnumerationBase;
/**
* The enum Sensor status name.
*/
public enum SensorStatusName implements EnumerationBase {
/**
* Wireless noexists sensor status name.
*/
WIRELESS_NOEXISTS(0, "WIRELESS_NOEXISTS"),
/**
* Wireless normal sensor status name.
*/
WIRELESS_NORMAL(1, "WIRELESS_NORMAL"),
/**
* Wireless exception sensor status name.
*/
WIRELESS_EXCEPTION(2, "WIRELESS_EXCEPTION"),
;
/**
* The Code.
*/
private final Integer code;
/**
* The Desc.
*/
private final String desc;
/**
* Instantiates a new Sensor status name.
*
* @param val the val
* @param desc the desc
*/
SensorStatusName(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

@ -1,40 +0,0 @@
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

@ -1,40 +0,0 @@
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

@ -1,6 +1,5 @@
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;
@ -62,11 +61,11 @@ public class AuthUser {
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());
// adapterInfo.add(BeidouAdapterControlContent.builder()
// .districtsCode(0x10 + i)
// .sensorId(0x3f00 + i)
// .controlAction(SensorControlActionName.ACTION_DISTRICTS_BASED)
// .build());
}
SensorControlProtocol sp =

View File

@ -41,6 +41,12 @@ public class ControlDevice {
*/
private String deviceAddr;
private Integer sendEnabled;
private Integer beidouStatus;
private String beidouChannelStatus;
/**
* The Beidou signal strength.
*/

View File

@ -1,6 +1,5 @@
package com.zjyr.beidouservice.pojo.po;
import com.zjyr.beidouservice.common.impl.SensorControlActionName;
import lombok.Builder;
import lombok.Data;
@ -14,7 +13,7 @@ public class BeidouAdapterControlContent {
/**
* The Control action.
*/
private SensorControlActionName controlAction;
//private SensorControlActionName controlAction;
/**
* The Sensor id.
*/

View File

@ -13,7 +13,7 @@ public class BeidouAdapterDevice {
/**
* The Id.
*/
private Long id;
private long id;
/**
* The Adapter type.
*/
@ -22,6 +22,9 @@ public class BeidouAdapterDevice {
* The Adapter addr.
*/
private String adapterAddr;
private int sendEnabled;
private int beidouStatus;
private String beidouChannelStatus;
/**
* The Beidou signal strength.
*/
@ -29,9 +32,9 @@ public class BeidouAdapterDevice {
/**
* The Wireless status.
*/
private Integer wirelessStatus;
private int wirelessStatus;
/**
* The Telphone status.
*/
private Integer telphoneStatus;
private int telphoneStatus;
}

View File

@ -1,6 +1,6 @@
package com.zjyr.beidouservice.pojo.vo.binary;
import com.zjyr.beidouservice.common.impl.SensorStatusName;
import com.zjyr.beidouservice.common.impl.*;
import lombok.Builder;
import lombok.Data;
@ -10,16 +10,14 @@ import lombok.Data;
@Data
@Builder
public class ControllerStatus {
private byte[] beidouFreq;
/**
* The Beidou signal strength.
*/
private byte[] beidouSignalStrength;
/**
* The Wireless status.
*/
private SensorStatusName wirelessStatus;
/**
* The Telphone status.
*/
private SensorStatusName telphoneStatus;
private SendStatusName sendStatus;
private ControlChannelStatus beidouStatus;
private ControlChannelStatus wirelessStatus;
private ControlChannelStatus telphoneStatus;
}

View File

@ -1,6 +1,6 @@
package com.zjyr.beidouservice.pojo.vo.binary;
import com.zjyr.beidouservice.common.impl.SensorControlActionName;
//import com.zjyr.beidouservice.common.impl.SensorControlActionName;
import lombok.Builder;
import lombok.Data;
@ -17,7 +17,7 @@ public class SensorControllContent {
/**
* The Control action.
*/
private SensorControlActionName controlAction;
//private SensorControlActionName controlAction;
/**
* The Sensor id.
*/

View File

@ -1,19 +0,0 @@
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,6 @@ 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.SensorCotrolAck;
import com.zjyr.beidouservice.pojo.vo.binary.SensorStatusAck;
import com.zjyr.beidouservice.service.AdapterProtocolService;
import com.zjyr.beidouservice.service.BaidouAdapterService;
@ -74,11 +73,10 @@ public class BaidouSocketAdapterServiceImpl<T extends ApplicationEvent> implemen
if (evtContent instanceof SocketNotifyEvent event) {
InetSocketAddress sa = (InetSocketAddress) event.getCtxChannel().remoteAddress();
switch (event.getSocketEvent()) {
case SOCKET_EVT_CONNECT, SOCKET_EVT_IDLE_TIMEOUT -> {
case SOCKET_EVT_CONNECT -> {
sendCommond(event.getCtxChannel(), adapterProtocolService.createHeartProtocol());
if (event.getSocketEvent() == SOCKET_EVT_CONNECT) {
BeidouAdapterDevice a = BeidouAdapterDevice.builder()
.adapterType(BeidouAdapterTypeName.ADAPTER_SOCKET_TCP)
.adapterAddr(sa.getAddress().getHostAddress())
@ -90,6 +88,10 @@ public class BaidouSocketAdapterServiceImpl<T extends ApplicationEvent> implemen
}
}
case SOCKET_EVT_IDLE_TIMEOUT -> {
sendCommond(event.getCtxChannel(), adapterProtocolService.createHeartProtocol());
}
case SOCKET_EVT_DISCONNECT -> {
for (ConcurrentHashMap.Entry<Long, ControlAdapterSocketCtx> entry : ctxMap.entrySet()) {
if (entry.getValue().getChannel().id() == event.getCtxChannel().id()) {
@ -104,14 +106,25 @@ public class BaidouSocketAdapterServiceImpl<T extends ApplicationEvent> implemen
status.getWirelessStatus(),
status.getTelphoneStatus(),
HelperUtils.bytesToHexString(status.getBeidouSignalStrength()));
} else if (event.getEvtMessage() instanceof SensorCotrolAck ack) {
BeidouAdapterDevice a = BeidouAdapterDevice.builder()
.adapterType(BeidouAdapterTypeName.ADAPTER_SOCKET_TCP)
.adapterAddr(sa.getAddress().getHostAddress())
.sendEnabled(status.getSendStatus().getValue())
.beidouStatus(status.getBeidouStatus().getValue())
.beidouChannelStatus(HelperUtils.bytesToHexString(status.getBeidouFreq()))
.beidouSignalStrength(HelperUtils.bytesToHexString(status.getBeidouSignalStrength()))
.wirelessStatus(status.getWirelessStatus().getValue())
.telphoneStatus(status.getTelphoneStatus().getValue())
.build();
addNewBeidouAdapter(a);
} /*else if (event.getEvtMessage() instanceof SensorCotrolAck ack) {
log.info("+++{}, {}, {}, {}, {}",
ack.getSendStatus(),
ack.getBeidouStatus(),
ack.getWirelessStatus(),
ack.getTelphoneStatus(),
HelperUtils.bytesToHexString(ack.getBeidouFreq()));
} else if (event.getEvtMessage() instanceof SensorStatusAck ack) {
}*/ else if (event.getEvtMessage() instanceof SensorStatusAck ack) {
log.info("+++Total Sensors: {}", ack.getSensorStatus().size());
sensorDataService.addNewSensor(ack.getSensorStatus());
}
@ -150,6 +163,9 @@ public class BaidouSocketAdapterServiceImpl<T extends ApplicationEvent> implemen
.id(adapter.getId())
.deviceType(adapter.getAdapterType())
.deviceAddr(adapter.getAdapterAddr())
.beidouStatus(adapter.getBeidouStatus())
.sendEnabled(adapter.getSendEnabled())
.beidouChannelStatus(adapter.getBeidouChannelStatus())
.beidouSignalStrength(adapter.getBeidouSignalStrength())
.wirelessStatus(adapter.getWirelessStatus())
.telphoneStatus(adapter.getTelphoneStatus())

View File

@ -31,7 +31,7 @@ public class BeidouSocketProtocolServiceImpl implements AdapterProtocolService {
for (BeidouAdapterControlContent v : ctxList) {
list.add(SensorControllContent.builder()
.districtsCode(v.getDistrictsCode().byteValue())
.controlAction(v.getControlAction())
//.controlAction(v.getControlAction())
.sensorId(v.getSensorId().shortValue())
.build());
}

View File

@ -5,29 +5,59 @@
<id column="id" property="id"/>
<result column="deviceType" property="deviceType"/>
<result column="deviceAddr" property="deviceAddr"/>
<result column="sendEnabled" property="sendEnabled"/>
<result column="beidouStatus" property="beidouStatus"/>
<result column="beidouChannelStatus" property="beidouChannelStatus"/>
<result column="beidouSignalStrength" property="beidouSignalStrength"/>
<result column="wirelessStatus" property="wirelessStatus"/>
<result column="telphoneStatus" property="telphoneStatus"/>
</resultMap>
<select id="selectAll" resultMap="control_device">
SELECT id, deviceAddr, deviceType, beidouSignalStrength, wirelessStatus, telphoneStatus
SELECT id,
deviceAddr,
deviceType,
sendEnabled,
beidouStatus,
beidouChannelStatus,
beidouSignalStrength,
wirelessStatus,
telphoneStatus
FROM control_device
</select>
<update id="upgradeControlDevice" parameterType="com.zjyr.beidouservice.pojo.entry.ControlDevice">
UPDATE control_device
SET beidouSignalStrength = #{dev.beidouSignalStrength},
wirelessStatus = #{dev.wirelessStatus},
telphoneStatus = #{dev.telphoneStatus}
WHERE deviceAddr = #{dev.deviceAddr}
INSERT IGNORE INTO control_device(deviceAddr, deviceType, sendEnabled, beidouStatus, beidouChannelStatus,
beidouSignalStrength, wirelessStatus, telphoneStatus)
SELECT #{device.deviceAddr},
#{device.deviceType},
#{device.sendEnabled},
#{device.beidouStatus},
#{device.beidouChannelStatus},
#{device.beidouSignalStrength},
#{device.wirelessStatus},
#{device.telphoneStatus}
FROM DUAL
WHERE NOT EXISTS (SELECT deviceAddr
FROM control_device
WHERE deviceAddr = #{device.deviceAddr})
</update>
<insert id="addControlDevice" useGeneratedKeys="true" keyProperty="id"
parameterType="com.zjyr.beidouservice.pojo.entry.ControlDevice">
INSERT IGNORE INTO control_device(deviceAddr, deviceType, beidouSignalStrength, wirelessStatus, telphoneStatus)
SELECT #{device.deviceAddr}, #{device.deviceType}, #{device.beidouSignalStrength}, #{device.wirelessStatus},
#{device.telphoneStatus} FROM DUAL WHERE NOT EXISTS (SELECT deviceAddr FROM control_device
INSERT IGNORE INTO control_device(deviceAddr, deviceType, sendEnabled, beidouStatus, beidouChannelStatus,
beidouSignalStrength, wirelessStatus, telphoneStatus)
SELECT #{device.deviceAddr},
#{device.deviceType},
#{device.sendEnabled},
#{device.beidouStatus},
#{device.beidouChannelStatus},
#{device.beidouSignalStrength},
#{device.wirelessStatus},
#{device.telphoneStatus}
FROM DUAL
WHERE NOT EXISTS (SELECT deviceAddr
FROM control_device
WHERE deviceAddr = #{device.deviceAddr})
</insert>
</mapper>