优化架构,获取设备注册时的设备相关信息

This commit is contained in:
648540858
2024-03-12 17:32:10 +08:00
parent 79dc7e79d2
commit 9494b6dc85
15 changed files with 209 additions and 6 deletions

View File

@@ -13,6 +13,8 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import java.util.ArrayList;
import java.util.List;
@@ -25,6 +27,12 @@ import java.util.List;
public class Jt808Decoder extends ByteToMessageDecoder {
private final static Logger log = LoggerFactory.getLogger(Jt808Decoder.class);
private ApplicationEventPublisher applicationEventPublisher = null;
public Jt808Decoder(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
Session session = ctx.channel().attr(Session.KEY).get();
@@ -51,6 +59,10 @@ public class Jt808Decoder extends ByteToMessageDecoder {
return;
}
Rs decode = handler.decode(buf, header, session);
ApplicationEvent applicationEvent = handler.getEvent();
if (applicationEvent != null) {
applicationEventPublisher.publishEvent(applicationEvent);
}
if (decode != null) {
out.add(decode);
}

View File

@@ -19,6 +19,7 @@ import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.concurrent.Future;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher;
import java.util.concurrent.TimeUnit;
@@ -35,11 +36,13 @@ public class TcpServer {
private boolean isRunning = false;
private EventLoopGroup bossGroup = null;
private EventLoopGroup workerGroup = null;
private ApplicationEventPublisher applicationEventPublisher = null;
private final ByteBuf DECODER_JT808 = Unpooled.wrappedBuffer(new byte[]{0x7e});
public TcpServer(Integer port) {
public TcpServer(Integer port, ApplicationEventPublisher applicationEventPublisher) {
this.port = port;
this.applicationEventPublisher = applicationEventPublisher;
}
private void startTcpServer() {
@@ -60,7 +63,7 @@ public class TcpServer {
channel.pipeline()
.addLast(new IdleStateHandler(10, 0, 0, TimeUnit.MINUTES))
.addLast(new DelimiterBasedFrameDecoder(1024 * 2, DECODER_JT808))
.addLast(new Jt808Decoder())
.addLast(new Jt808Decoder(applicationEventPublisher))
.addLast(new Jt808Encoder())
.addLast(new Jt808EncoderCmd())
.addLast(new Jt808Handler());

View File

@@ -2,8 +2,10 @@ package com.genersoft.iot.vmp.jt1078.config;
import com.genersoft.iot.vmp.jt1078.cmd.JT1078Template;
import com.genersoft.iot.vmp.jt1078.codec.netty.TcpServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
@@ -18,9 +20,12 @@ import org.springframework.core.annotation.Order;
@ConditionalOnProperty(value = "jt1078.enable", havingValue = "true")
public class JT1078AutoConfiguration {
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
@Bean(initMethod = "start", destroyMethod = "stop")
public TcpServer jt1078Server(@Value("${jt1078.port}") Integer port) {
return new TcpServer(port);
return new TcpServer(port, applicationEventPublisher);
}
@Bean

View File

@@ -33,7 +33,7 @@ public class JT1078Controller {
public WVPResult<?> startLive(@PathVariable String deviceId, @PathVariable String channelId) {
J9101 j9101 = new J9101();
j9101.setChannel(Integer.valueOf(channelId));
j9101.setIp("192.168.1.1");
j9101.setIp("192.168.1.3");
j9101.setRate(1);
j9101.setTcpPort(7618);
j9101.setUdpPort(7618);

View File

@@ -0,0 +1,91 @@
package com.genersoft.iot.vmp.jt1078.event;
import com.genersoft.iot.vmp.gb28181.bean.DeviceAlarm;
import org.springframework.context.ApplicationEvent;
/**
* 注册事件
*/
public class RegisterEvent extends ApplicationEvent {
/**
*
*/
private static final long serialVersionUID = 1L;
public RegisterEvent(Object source) {
super(source);
}
private int provinceId;
private int cityId;
private String makerId;
private String deviceModel;
private String deviceId;
private int plateColor;
private String plateNo;
public int getProvinceId() {
return provinceId;
}
public void setProvinceId(int provinceId) {
this.provinceId = provinceId;
}
public int getCityId() {
return cityId;
}
public void setCityId(int cityId) {
this.cityId = cityId;
}
public String getMakerId() {
return makerId;
}
public void setMakerId(String makerId) {
this.makerId = makerId;
}
public String getDeviceModel() {
return deviceModel;
}
public void setDeviceModel(String deviceModel) {
this.deviceModel = deviceModel;
}
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
public int getPlateColor() {
return plateColor;
}
public void setPlateColor(int plateColor) {
this.plateColor = plateColor;
}
public String getPlateNo() {
return plateNo;
}
public void setPlateNo(String plateNo) {
this.plateNo = plateNo;
}
}

View File

@@ -0,0 +1,15 @@
package com.genersoft.iot.vmp.jt1078.event.eventListener;
import com.genersoft.iot.vmp.gb28181.event.alarm.AlarmEvent;
import com.genersoft.iot.vmp.jt1078.event.RegisterEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class RegisterEventListener implements ApplicationListener<RegisterEvent> {
@Override
public void onApplicationEvent(RegisterEvent event) {
System.out.println("收到设备注册: "+ event.getDeviceId());
}
}

View File

@@ -8,6 +8,7 @@ import com.genersoft.iot.vmp.jt1078.session.Session;
import com.genersoft.iot.vmp.jt1078.session.SessionManager;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import org.springframework.context.ApplicationEvent;
/**
* 终端通用应答
@@ -47,4 +48,9 @@ public class J0001 extends Re {
public int getResult() {
return result;
}
@Override
public ApplicationEvent getEvent() {
return null;
}
}

View File

@@ -6,6 +6,7 @@ import com.genersoft.iot.vmp.jt1078.proc.response.J8001;
import com.genersoft.iot.vmp.jt1078.proc.response.Rs;
import com.genersoft.iot.vmp.jt1078.session.Session;
import io.netty.buffer.ByteBuf;
import org.springframework.context.ApplicationEvent;
/**
* 终端心跳
@@ -29,4 +30,9 @@ public class J0002 extends Re {
j8001.setResult(J8001.SUCCESS);
return j8001;
}
@Override
public ApplicationEvent getEvent() {
return null;
}
}

View File

@@ -5,6 +5,7 @@ import com.genersoft.iot.vmp.jt1078.proc.Header;
import com.genersoft.iot.vmp.jt1078.proc.response.Rs;
import com.genersoft.iot.vmp.jt1078.session.Session;
import io.netty.buffer.ByteBuf;
import org.springframework.context.ApplicationEvent;
/**
* 查询服务器时间
@@ -24,4 +25,9 @@ public class J0004 extends Re {
protected Rs handler(Header header, Session session) {
return null;
}
@Override
public ApplicationEvent getEvent() {
return null;
}
}

View File

@@ -1,11 +1,15 @@
package com.genersoft.iot.vmp.jt1078.proc.request;
import com.genersoft.iot.vmp.jt1078.annotation.MsgId;
import com.genersoft.iot.vmp.jt1078.event.RegisterEvent;
import com.genersoft.iot.vmp.jt1078.proc.Header;
import com.genersoft.iot.vmp.jt1078.proc.response.J8100;
import com.genersoft.iot.vmp.jt1078.proc.response.Rs;
import com.genersoft.iot.vmp.jt1078.session.Session;
import io.netty.buffer.ByteBuf;
import org.springframework.context.ApplicationEvent;
import java.io.UnsupportedEncodingException;
/**
* 终端注册
@@ -35,13 +39,34 @@ public class J0100 extends Re {
protected Rs decode0(ByteBuf buf, Header header, Session session) {
Short version = header.getVersion();
provinceId = buf.readUnsignedShort();
if (version > 1) {
if (version >= 1) {
cityId = buf.readUnsignedShort();
// decode as 2019
byte[] bytes11 = new byte[11];
buf.readBytes(bytes11);
makerId = new String(bytes11).trim();
byte[] bytes30 = new byte[30];
buf.readBytes(bytes30);
deviceModel = new String(bytes30).trim();
buf.readBytes(bytes30);
deviceId = new String(bytes30).trim();
plateColor = buf.readByte();
byte[] plateColorBytes = new byte[buf.readableBytes()];
buf.readBytes(plateColorBytes);
try {
plateNo = new String(plateColorBytes, "GBK").trim();
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
} else {
int i = buf.readUnsignedShort();
// decode as 2013
}
// 发送终端注册消息
return null;
}
@@ -53,4 +78,17 @@ public class J0100 extends Re {
j8100.setCode("WVP_YYDS");
return j8100;
}
@Override
public ApplicationEvent getEvent() {
RegisterEvent registerEvent = new RegisterEvent(this);
registerEvent.setProvinceId(provinceId);
registerEvent.setCityId(cityId);
registerEvent.setDeviceId(deviceId);
registerEvent.setDeviceModel(deviceModel);
registerEvent.setMakerId(makerId);
registerEvent.setPlateColor(plateColor);
registerEvent.setPlateNo(plateNo);
return registerEvent;
}
}

View File

@@ -6,6 +6,7 @@ import com.genersoft.iot.vmp.jt1078.proc.response.J8001;
import com.genersoft.iot.vmp.jt1078.proc.response.Rs;
import com.genersoft.iot.vmp.jt1078.session.Session;
import io.netty.buffer.ByteBuf;
import org.springframework.context.ApplicationEvent;
/**
* 终端鉴权
@@ -33,4 +34,9 @@ public class J0102 extends Re {
return j8001;
}
@Override
public ApplicationEvent getEvent() {
return null;
}
}

View File

@@ -6,6 +6,7 @@ import com.genersoft.iot.vmp.jt1078.proc.response.J8001;
import com.genersoft.iot.vmp.jt1078.proc.response.Rs;
import com.genersoft.iot.vmp.jt1078.session.Session;
import io.netty.buffer.ByteBuf;
import org.springframework.context.ApplicationEvent;
/**
* 实时消息上报
@@ -29,4 +30,9 @@ public class J0200 extends Re {
j8001.setResult(J8001.SUCCESS);
return j8001;
}
@Override
public ApplicationEvent getEvent() {
return null;
}
}

View File

@@ -9,6 +9,7 @@ import com.genersoft.iot.vmp.jt1078.session.Session;
import com.genersoft.iot.vmp.jt1078.session.SessionManager;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import org.springframework.context.ApplicationEvent;
import java.util.ArrayList;
import java.util.List;
@@ -187,4 +188,9 @@ public class J1205 extends Re {
", recordList=" + recordList +
'}';
}
@Override
public ApplicationEvent getEvent() {
return null;
}
}

View File

@@ -6,6 +6,7 @@ import com.genersoft.iot.vmp.jt1078.session.Session;
import io.netty.buffer.ByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.util.StringUtils;
/**
@@ -37,4 +38,6 @@ public abstract class Re {
return rs;
}
public abstract ApplicationEvent getEvent();
}