1078-简化设备注册取值写法
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
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.session.Session;
|
||||
import com.genersoft.iot.vmp.jt1078.session.SessionManager;
|
||||
@@ -43,6 +44,13 @@ public class Jt808Handler extends ChannelInboundHandlerAdapter {
|
||||
Session session = SessionManager.INSTANCE.newSession(channel);
|
||||
channel.attr(Session.KEY).set(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
|
||||
@@ -50,8 +58,14 @@ public class Jt808Handler extends ChannelInboundHandlerAdapter {
|
||||
Session session = ctx.channel().attr(Session.KEY).get();
|
||||
log.info("< Tcp disconnect {}", session);
|
||||
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
|
||||
|
||||
@@ -88,4 +88,11 @@ public interface JTDeviceMapper {
|
||||
|
||||
@Delete("delete from wvp_jt_device where terminal_id = #{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 java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
@@ -40,25 +41,18 @@ public class J0100 extends Re {
|
||||
if (version >= 1) {
|
||||
device.setCityId(buf.readUnsignedShort() + "");
|
||||
// decode as 2019
|
||||
byte[] bytes11 = new byte[11];
|
||||
buf.readBytes(bytes11);
|
||||
device.setMakerId(new String(bytes11).trim());
|
||||
device.setMakerId(buf.readCharSequence(11, Charset.forName("GBK"))
|
||||
.toString().trim());
|
||||
|
||||
byte[] bytes30 = new byte[30];
|
||||
buf.readBytes(bytes30);
|
||||
device.setDeviceModel(new String(bytes30).trim());
|
||||
device.setDeviceModel(buf.readCharSequence(30, Charset.forName("GBK"))
|
||||
.toString().trim());
|
||||
|
||||
buf.readBytes(bytes30);
|
||||
device.setDeviceId(new String(bytes30).trim());
|
||||
device.setDeviceId(buf.readCharSequence(30, Charset.forName("GBK"))
|
||||
.toString().trim());
|
||||
|
||||
device.setPlateColor(buf.readByte());
|
||||
byte[] plateColorBytes = new byte[buf.readableBytes()];
|
||||
buf.readBytes(plateColorBytes);
|
||||
try {
|
||||
device.setPlateNo(new String(plateColorBytes, "GBK").trim());
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
device.setPlateNo(buf.readCharSequence(buf.readableBytes(), Charset.forName("GBK"))
|
||||
.toString().trim());
|
||||
} else {
|
||||
// decode as 2013
|
||||
device.setCityId(buf.readUnsignedShort() + "");
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
package com.genersoft.iot.vmp.jt1078.proc.request;
|
||||
|
||||
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.response.J8001;
|
||||
import com.genersoft.iot.vmp.jt1078.proc.response.Rs;
|
||||
import com.genersoft.iot.vmp.jt1078.service.Ijt1078Service;
|
||||
import com.genersoft.iot.vmp.jt1078.session.Session;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* 终端鉴权
|
||||
*
|
||||
@@ -18,20 +22,31 @@ import org.springframework.context.ApplicationEvent;
|
||||
*/
|
||||
@MsgId(id = "0102")
|
||||
public class J0102 extends Re {
|
||||
|
||||
private String authenticationCode;
|
||||
|
||||
@Override
|
||||
protected Rs decode0(ByteBuf buf, Header header, Session session) {
|
||||
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
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Rs handler(Header header, Session session, Ijt1078Service service) {
|
||||
JTDevice device = service.getDevice(header.getTerminalId());
|
||||
J8001 j8001 = new J8001();
|
||||
j8001.setRespNo(header.getSn());
|
||||
j8001.setRespId(header.getMsgId());
|
||||
j8001.setResult(J8001.SUCCESS);
|
||||
if (device == null || !device.getAuthenticationCode().equals(authenticationCode)) {
|
||||
j8001.setResult(J8001.FAIL);
|
||||
}else {
|
||||
j8001.setResult(J8001.SUCCESS);
|
||||
}
|
||||
return j8001;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import org.springframework.context.ApplicationEvent;
|
||||
public class J0200 extends Re {
|
||||
@Override
|
||||
protected Rs decode0(ByteBuf buf, Header header, Session session) {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,8 +12,15 @@ import io.netty.buffer.Unpooled;
|
||||
*/
|
||||
@MsgId(id = "8001")
|
||||
public class J8001 extends Rs {
|
||||
|
||||
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;
|
||||
String respId;
|
||||
Integer result;
|
||||
|
||||
@@ -15,4 +15,6 @@ public interface Ijt1078Service {
|
||||
void addDevice(JTDevice device);
|
||||
|
||||
void deleteDeviceByDeviceId(String deviceId);
|
||||
|
||||
void updateDeviceStatus(boolean connected, String terminalId);
|
||||
}
|
||||
|
||||
@@ -47,4 +47,9 @@ public class jt1078ServiceImpl implements Ijt1078Service {
|
||||
public void deleteDeviceByDeviceId(String deviceId) {
|
||||
jtDeviceMapper.deleteDeviceByTerminalId(deviceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDeviceStatus(boolean connected, String terminalId) {
|
||||
jtDeviceMapper.updateDeviceStatus(connected, terminalId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user