mirror of
https://gitee.com/pan648540858/wvp-GB28181-pro.git
synced 2026-05-26 15:07:49 +08:00
1078-简化设备注册取值写法
This commit is contained in:
parent
23a72e94e6
commit
93946407e8
@ -1,5 +1,6 @@
|
|||||||
package com.genersoft.iot.vmp.jt1078.codec.netty;
|
package com.genersoft.iot.vmp.jt1078.codec.netty;
|
||||||
|
|
||||||
|
import com.genersoft.iot.vmp.jt1078.event.ConnectChangeEvent;
|
||||||
import com.genersoft.iot.vmp.jt1078.proc.response.Rs;
|
import com.genersoft.iot.vmp.jt1078.proc.response.Rs;
|
||||||
import com.genersoft.iot.vmp.jt1078.session.Session;
|
import com.genersoft.iot.vmp.jt1078.session.Session;
|
||||||
import com.genersoft.iot.vmp.jt1078.session.SessionManager;
|
import com.genersoft.iot.vmp.jt1078.session.SessionManager;
|
||||||
@ -43,6 +44,13 @@ public class Jt808Handler extends ChannelInboundHandlerAdapter {
|
|||||||
Session session = SessionManager.INSTANCE.newSession(channel);
|
Session session = SessionManager.INSTANCE.newSession(channel);
|
||||||
channel.attr(Session.KEY).set(session);
|
channel.attr(Session.KEY).set(session);
|
||||||
log.info("> Tcp connect {}", session);
|
log.info("> Tcp connect {}", session);
|
||||||
|
if (session.getDevId() == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ConnectChangeEvent event = new ConnectChangeEvent(this);
|
||||||
|
event.setConnected(true);
|
||||||
|
event.setTerminalId(session.getDevId());
|
||||||
|
applicationEventPublisher.publishEvent(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -50,8 +58,14 @@ public class Jt808Handler extends ChannelInboundHandlerAdapter {
|
|||||||
Session session = ctx.channel().attr(Session.KEY).get();
|
Session session = ctx.channel().attr(Session.KEY).get();
|
||||||
log.info("< Tcp disconnect {}", session);
|
log.info("< Tcp disconnect {}", session);
|
||||||
ctx.close();
|
ctx.close();
|
||||||
|
if (session.getDevId() == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ConnectChangeEvent event = new ConnectChangeEvent(this);
|
||||||
|
event.setConnected(false);
|
||||||
|
event.setTerminalId(session.getDevId());
|
||||||
|
applicationEventPublisher.publishEvent(event);
|
||||||
|
|
||||||
applicationEventPublisher.publishEvent();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -88,4 +88,11 @@ public interface JTDeviceMapper {
|
|||||||
|
|
||||||
@Delete("delete from wvp_jt_device where terminal_id = #{terminalId}")
|
@Delete("delete from wvp_jt_device where terminal_id = #{terminalId}")
|
||||||
void deleteDeviceByTerminalId(@Param("terminalId") String terminalId);
|
void deleteDeviceByTerminalId(@Param("terminalId") String terminalId);
|
||||||
|
|
||||||
|
@Update(value = {" <script>" +
|
||||||
|
"UPDATE wvp_jt_device " +
|
||||||
|
"SET status=#{connected} " +
|
||||||
|
"WHERE terminal_id=#{terminalId}"+
|
||||||
|
" </script>"})
|
||||||
|
void updateDeviceStatus(@Param("connected") boolean connected, @Param("terminalId") String terminalId);
|
||||||
}
|
}
|
||||||
|
|||||||
42
src/main/java/com/genersoft/iot/vmp/jt1078/event/ConnectChangeEvent.java
Executable file
42
src/main/java/com/genersoft/iot/vmp/jt1078/event/ConnectChangeEvent.java
Executable file
@ -0,0 +1,42 @@
|
|||||||
|
package com.genersoft.iot.vmp.jt1078.event;
|
||||||
|
|
||||||
|
import com.genersoft.iot.vmp.jt1078.bean.JTDevice;
|
||||||
|
import org.springframework.context.ApplicationEvent;
|
||||||
|
|
||||||
|
import java.time.Clock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 链接断或者连接的事件
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class ConnectChangeEvent extends ApplicationEvent {
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public ConnectChangeEvent(Object source) {
|
||||||
|
super(source);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private boolean connected;
|
||||||
|
|
||||||
|
private String terminalId;
|
||||||
|
|
||||||
|
public boolean isConnected() {
|
||||||
|
return connected;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConnected(boolean connected) {
|
||||||
|
this.connected = connected;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTerminalId() {
|
||||||
|
return terminalId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTerminalId(String terminalId) {
|
||||||
|
this.terminalId = terminalId;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
package com.genersoft.iot.vmp.jt1078.event.eventListener;
|
||||||
|
|
||||||
|
import com.genersoft.iot.vmp.jt1078.bean.JTDevice;
|
||||||
|
import com.genersoft.iot.vmp.jt1078.event.ConnectChangeEvent;
|
||||||
|
import com.genersoft.iot.vmp.jt1078.event.RegisterEvent;
|
||||||
|
import com.genersoft.iot.vmp.jt1078.proc.request.J0003;
|
||||||
|
import com.genersoft.iot.vmp.jt1078.service.Ijt1078Service;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.ApplicationListener;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class ConnectChangeEventListener implements ApplicationListener<ConnectChangeEvent> {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(ConnectChangeEventListener.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Ijt1078Service service;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onApplicationEvent(ConnectChangeEvent event) {
|
||||||
|
if (event.isConnected()) {
|
||||||
|
log.info("[JT-设备已连接] 终端ID: {}", event.getTerminalId());
|
||||||
|
}else{
|
||||||
|
log.info("[JT-设备连接已断开] 终端ID: {}", event.getTerminalId());
|
||||||
|
}
|
||||||
|
JTDevice device = service.getDevice(event.getTerminalId());
|
||||||
|
if (device != null) {
|
||||||
|
service.updateDeviceStatus(event.isConnected(), event.getTerminalId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -17,6 +17,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
import org.springframework.context.ApplicationEvent;
|
import org.springframework.context.ApplicationEvent;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -40,25 +41,18 @@ public class J0100 extends Re {
|
|||||||
if (version >= 1) {
|
if (version >= 1) {
|
||||||
device.setCityId(buf.readUnsignedShort() + "");
|
device.setCityId(buf.readUnsignedShort() + "");
|
||||||
// decode as 2019
|
// decode as 2019
|
||||||
byte[] bytes11 = new byte[11];
|
device.setMakerId(buf.readCharSequence(11, Charset.forName("GBK"))
|
||||||
buf.readBytes(bytes11);
|
.toString().trim());
|
||||||
device.setMakerId(new String(bytes11).trim());
|
|
||||||
|
|
||||||
byte[] bytes30 = new byte[30];
|
device.setDeviceModel(buf.readCharSequence(30, Charset.forName("GBK"))
|
||||||
buf.readBytes(bytes30);
|
.toString().trim());
|
||||||
device.setDeviceModel(new String(bytes30).trim());
|
|
||||||
|
|
||||||
buf.readBytes(bytes30);
|
device.setDeviceId(buf.readCharSequence(30, Charset.forName("GBK"))
|
||||||
device.setDeviceId(new String(bytes30).trim());
|
.toString().trim());
|
||||||
|
|
||||||
device.setPlateColor(buf.readByte());
|
device.setPlateColor(buf.readByte());
|
||||||
byte[] plateColorBytes = new byte[buf.readableBytes()];
|
device.setPlateNo(buf.readCharSequence(buf.readableBytes(), Charset.forName("GBK"))
|
||||||
buf.readBytes(plateColorBytes);
|
.toString().trim());
|
||||||
try {
|
|
||||||
device.setPlateNo(new String(plateColorBytes, "GBK").trim());
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// decode as 2013
|
// decode as 2013
|
||||||
device.setCityId(buf.readUnsignedShort() + "");
|
device.setCityId(buf.readUnsignedShort() + "");
|
||||||
|
|||||||
@ -1,14 +1,18 @@
|
|||||||
package com.genersoft.iot.vmp.jt1078.proc.request;
|
package com.genersoft.iot.vmp.jt1078.proc.request;
|
||||||
|
|
||||||
import com.genersoft.iot.vmp.jt1078.annotation.MsgId;
|
import com.genersoft.iot.vmp.jt1078.annotation.MsgId;
|
||||||
|
import com.genersoft.iot.vmp.jt1078.bean.JTDevice;
|
||||||
import com.genersoft.iot.vmp.jt1078.proc.Header;
|
import com.genersoft.iot.vmp.jt1078.proc.Header;
|
||||||
import com.genersoft.iot.vmp.jt1078.proc.response.J8001;
|
import com.genersoft.iot.vmp.jt1078.proc.response.J8001;
|
||||||
import com.genersoft.iot.vmp.jt1078.proc.response.Rs;
|
import com.genersoft.iot.vmp.jt1078.proc.response.Rs;
|
||||||
import com.genersoft.iot.vmp.jt1078.service.Ijt1078Service;
|
import com.genersoft.iot.vmp.jt1078.service.Ijt1078Service;
|
||||||
import com.genersoft.iot.vmp.jt1078.session.Session;
|
import com.genersoft.iot.vmp.jt1078.session.Session;
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.util.CharsetUtil;
|
||||||
import org.springframework.context.ApplicationEvent;
|
import org.springframework.context.ApplicationEvent;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 终端鉴权
|
* 终端鉴权
|
||||||
*
|
*
|
||||||
@ -18,20 +22,31 @@ import org.springframework.context.ApplicationEvent;
|
|||||||
*/
|
*/
|
||||||
@MsgId(id = "0102")
|
@MsgId(id = "0102")
|
||||||
public class J0102 extends Re {
|
public class J0102 extends Re {
|
||||||
|
|
||||||
|
private String authenticationCode;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Rs decode0(ByteBuf buf, Header header, Session session) {
|
protected Rs decode0(ByteBuf buf, Header header, Session session) {
|
||||||
int lenCode = buf.readUnsignedByte();
|
int lenCode = buf.readUnsignedByte();
|
||||||
// String code = buf.readCharSequence(lenCode, CharsetUtil.UTF_8).toString();
|
byte[] authenticationCodeBytes = new byte[lenCode];
|
||||||
|
// ByteBuf byteBuf = buf.readBytes(authenticationCodeBytes);
|
||||||
|
authenticationCode = buf.readCharSequence(lenCode, Charset.forName("GBK")).toString();
|
||||||
|
System.out.println("设备鉴权: authenticationCode: " + authenticationCode);
|
||||||
// if 2019 to decode next
|
// if 2019 to decode next
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Rs handler(Header header, Session session, Ijt1078Service service) {
|
protected Rs handler(Header header, Session session, Ijt1078Service service) {
|
||||||
|
JTDevice device = service.getDevice(header.getTerminalId());
|
||||||
J8001 j8001 = new J8001();
|
J8001 j8001 = new J8001();
|
||||||
j8001.setRespNo(header.getSn());
|
j8001.setRespNo(header.getSn());
|
||||||
j8001.setRespId(header.getMsgId());
|
j8001.setRespId(header.getMsgId());
|
||||||
|
if (device == null || !device.getAuthenticationCode().equals(authenticationCode)) {
|
||||||
|
j8001.setResult(J8001.FAIL);
|
||||||
|
}else {
|
||||||
j8001.setResult(J8001.SUCCESS);
|
j8001.setResult(J8001.SUCCESS);
|
||||||
|
}
|
||||||
return j8001;
|
return j8001;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -20,6 +20,7 @@ import org.springframework.context.ApplicationEvent;
|
|||||||
public class J0200 extends Re {
|
public class J0200 extends Re {
|
||||||
@Override
|
@Override
|
||||||
protected Rs decode0(ByteBuf buf, Header header, Session session) {
|
protected Rs decode0(ByteBuf buf, Header header, Session session) {
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -12,8 +12,15 @@ import io.netty.buffer.Unpooled;
|
|||||||
*/
|
*/
|
||||||
@MsgId(id = "8001")
|
@MsgId(id = "8001")
|
||||||
public class J8001 extends Rs {
|
public class J8001 extends Rs {
|
||||||
|
|
||||||
public static final Integer SUCCESS = 0;
|
public static final Integer SUCCESS = 0;
|
||||||
|
|
||||||
|
public static final Integer FAIL = 1;
|
||||||
|
|
||||||
|
public static final Integer ERROR = 2;
|
||||||
|
public static final Integer NOT_SUPPORTED = 3;
|
||||||
|
public static final Integer ALARM_ACK = 3;
|
||||||
|
|
||||||
Integer respNo;
|
Integer respNo;
|
||||||
String respId;
|
String respId;
|
||||||
Integer result;
|
Integer result;
|
||||||
|
|||||||
@ -15,4 +15,6 @@ public interface Ijt1078Service {
|
|||||||
void addDevice(JTDevice device);
|
void addDevice(JTDevice device);
|
||||||
|
|
||||||
void deleteDeviceByDeviceId(String deviceId);
|
void deleteDeviceByDeviceId(String deviceId);
|
||||||
|
|
||||||
|
void updateDeviceStatus(boolean connected, String terminalId);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -47,4 +47,9 @@ public class jt1078ServiceImpl implements Ijt1078Service {
|
|||||||
public void deleteDeviceByDeviceId(String deviceId) {
|
public void deleteDeviceByDeviceId(String deviceId) {
|
||||||
jtDeviceMapper.deleteDeviceByTerminalId(deviceId);
|
jtDeviceMapper.deleteDeviceByTerminalId(deviceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateDeviceStatus(boolean connected, String terminalId) {
|
||||||
|
jtDeviceMapper.updateDeviceStatus(connected, terminalId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user