新增JT1078 Template支持

This commit is contained in:
QingObject
2023-04-28 10:10:06 +08:00
parent 813fd772d1
commit 97b673d6ad
29 changed files with 1868 additions and 0 deletions

View File

@@ -0,0 +1,114 @@
package com.genersoft.iot.vmp.jt1078.session;
import com.genersoft.iot.vmp.jt1078.proc.Header;
import io.netty.channel.Channel;
import io.netty.util.AttributeKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author QingtaiJiang
* @date 2023/4/27 18:54
* @email qingtaij@163.com
*/
public class Session {
private final static Logger log = LoggerFactory.getLogger(Session.class);
public static final AttributeKey<Session> KEY = AttributeKey.newInstance(Session.class.getName());
// Netty的channel
protected final Channel channel;
// 原子类的自增ID
private final AtomicInteger serialNo = new AtomicInteger(0);
// 是否注册成功
private boolean registered = false;
// 设备ID
private String devId;
// 创建时间
private final long creationTime;
// 协议版本号
private Integer protocolVersion;
private Header header;
protected Session(Channel channel) {
this.channel = channel;
this.creationTime = System.currentTimeMillis();
}
public void writeObject(Object message) {
log.info("<<<<<<<<<< cmd{},{}", this, message);
channel.writeAndFlush(message);
}
/**
* 获得下一个流水号
*
* @return 流水号
*/
public int nextSerialNo() {
int current;
int next;
do {
current = serialNo.get();
next = current > 0xffff ? 0 : current;
} while (!serialNo.compareAndSet(current, next + 1));
return next;
}
/**
* 注册session
*
* @param devId 设备ID
*/
public void register(String devId, Integer version, Header header) {
this.devId = devId;
this.registered = true;
this.protocolVersion = version;
this.header = header;
SessionManager.INSTANCE.put(devId, this);
}
/**
* 获取设备号
*
* @return 设备号
*/
public String getDevId() {
return devId;
}
public boolean isRegistered() {
return registered;
}
public long getCreationTime() {
return creationTime;
}
public Integer getProtocolVersion() {
return protocolVersion;
}
public Header getHeader() {
return header;
}
@Override
public String toString() {
return "[" +
"devId=" + devId +
", reg=" + registered +
", version=" + protocolVersion +
",ip=" + channel.remoteAddress() +
']';
}
}

View File

@@ -0,0 +1,127 @@
package com.genersoft.iot.vmp.jt1078.session;
import com.genersoft.iot.vmp.jt1078.proc.entity.Cmd;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
/**
* @author QingtaiJiang
* @date 2023/4/27 19:54
* @email qingtaij@163.com
*/
public enum SessionManager {
INSTANCE;
private final static Logger log = LoggerFactory.getLogger(SessionManager.class);
// 用与消息的缓存
private final Map<String, SynchronousQueue<String>> topicSubscribers = new ConcurrentHashMap<>();
// session的缓存
private final Map<Object, Session> sessionMap;
SessionManager() {
this.sessionMap = new ConcurrentHashMap<>();
}
/**
* 创建新的Session
*
* @param channel netty通道
* @return 创建的session对象
*/
public Session newSession(Channel channel) {
return new Session(channel);
}
/**
* 获取指定设备的Session
*
* @param clientId 设备Id
* @return Session
*/
public Session get(Object clientId) {
return sessionMap.get(clientId);
}
/**
* 放入新设备连接的session
*
* @param clientId 设备ID
* @param newSession session
*/
protected void put(Object clientId, Session newSession) {
sessionMap.put(clientId, newSession);
}
/**
* 发送同步消息,接收响应
* 默认超时时间6秒
*/
public String request(Cmd cmd) {
// 默认6秒
int timeOut = 6000;
return request(cmd, timeOut);
}
public String request(Cmd cmd, Integer timeOut) {
Session session = this.get(cmd.getDevId());
if (session == null) {
log.error("DevId: {} not online!", cmd.getDevId());
return "-1";
}
String requestKey = requestKey(cmd.getDevId(), cmd.getRespId(), cmd.getPackageNo());
SynchronousQueue<String> subscribe = subscribe(requestKey);
if (subscribe == null) {
log.error("DevId: {} key:{} send repaid", cmd.getDevId(), requestKey);
return "-1";
}
session.writeObject(cmd);
try {
return subscribe.poll(timeOut, TimeUnit.SECONDS);
} catch (InterruptedException e) {
log.warn("<<<<<<<<<< timeout" + session, e);
} finally {
this.unsubscribe(requestKey);
}
return null;
}
public Boolean response(String devId, String respId, Long responseNo, String data) {
String requestKey = requestKey(devId, respId, responseNo);
SynchronousQueue<String> queue = topicSubscribers.get(requestKey);
if (queue != null) {
try {
return queue.offer(data, 2, TimeUnit.SECONDS);
} catch (InterruptedException e) {
log.error("{}", e.getMessage(), e);
}
}
log.warn("未找到对应回复指令,key:{} 消息:{} ", requestKey, data);
return false;
}
private void unsubscribe(String key) {
topicSubscribers.remove(key);
}
private SynchronousQueue<String> subscribe(String key) {
SynchronousQueue<String> queue = null;
if (!topicSubscribers.containsKey(key))
topicSubscribers.put(key, queue = new SynchronousQueue<String>());
return queue;
}
private String requestKey(String devId, String respId, Long requestNo) {
return String.join("_", devId.replaceFirst("^0*", ""), respId, requestNo.toString());
}
}