1078-添加设备属性查询接口

This commit is contained in:
648540858 2024-04-30 12:13:49 +08:00
parent 5be7c7636e
commit 7bdde6907a
2 changed files with 53 additions and 69 deletions

View File

@ -16,6 +16,7 @@ import com.genersoft.iot.vmp.jt1078.session.Session;
import com.genersoft.iot.vmp.jt1078.session.SessionManager; import com.genersoft.iot.vmp.jt1078.session.SessionManager;
import com.genersoft.iot.vmp.jt1078.util.BCDUtil; import com.genersoft.iot.vmp.jt1078.util.BCDUtil;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -44,60 +45,37 @@ public class J0107 extends Re {
@Override @Override
protected Rs decode0(ByteBuf buf, Header header, Session session) { protected Rs decode0(ByteBuf buf, Header header, Session session) {
respNo = buf.readUnsignedShort(); // respNo = buf.readUnsignedShort();
paramLength = (int) buf.readUnsignedByte(); // paramLength = (int) buf.readUnsignedByte();
if (paramLength <= 0) { // if (paramLength <= 0) {
return null; // return null;
} // }
JTDeviceAttribute deviceAttribute = new JTDeviceAttribute(); JTDeviceAttribute deviceAttribute = new JTDeviceAttribute();
deviceAttribute.setType(JTDeviceType.getInstance(buf.getUnsignedShort(0))); deviceAttribute.setType(JTDeviceType.getInstance(buf.readUnsignedShort()));
byte[] bytes5 = new byte[5]; deviceAttribute.setMakerId(buf.readCharSequence(5, Charset.forName("GBK")).toString().trim());
buf.getBytes(2, bytes5);
deviceAttribute.setMakerId(new String(bytes5).trim());
byte[] bytes20 = new byte[20]; deviceAttribute.setDeviceModel(buf.readCharSequence(20, Charset.forName("GBK")).toString().trim());
buf.getBytes(7, bytes20); buf.readCharSequence(10, Charset.forName("GBK"));
deviceAttribute.setDeviceModel(new String(bytes20).trim());
byte[] bytes7 = new byte[7]; deviceAttribute.setTerminalId(buf.readCharSequence(7, Charset.forName("GBK")).toString().trim());
buf.getBytes(37, bytes7); buf.readCharSequence(23, Charset.forName("GBK"));
deviceAttribute.setTerminalId(new String(bytes7).trim());
byte[] iccIdBytes = new byte[10]; byte[] bytes = new byte[10];
buf.getBytes(67, iccIdBytes); buf.readBytes(bytes);
deviceAttribute.setIccId(BCDUtil.transform(iccIdBytes)); deviceAttribute.setIccId(BCDUtil.transform(bytes));
int n = buf.getUnsignedByte(77); int hardwareVersionLength = buf.readUnsignedByte();
byte[] hardwareVersionBytes = new byte[n]; deviceAttribute.setHardwareVersion(buf.readCharSequence(hardwareVersionLength, Charset.forName("GBK")).toString().trim());
buf.getBytes(78, hardwareVersionBytes);
try {
deviceAttribute.setHardwareVersion(new String(hardwareVersionBytes, "GBK").trim());
} catch (UnsupportedEncodingException e) {
logger.error("[查询终端属性应答] 读取硬件版本失败" , e);
}
int m = buf.getUnsignedByte(78 + n); int firmwareVersionLength = buf.readUnsignedByte();
byte[] firmwareVersionBytes = new byte[m]; deviceAttribute.setFirmwareVersion(buf.readCharSequence(firmwareVersionLength, Charset.forName("GBK")).toString().trim());
buf.getBytes(79 + n, firmwareVersionBytes);
try {
deviceAttribute.setFirmwareVersion(new String(firmwareVersionBytes, "GBK").trim());
} catch (UnsupportedEncodingException e) {
logger.error("[查询终端属性应答] 读取固件版本失败" , e);
}
deviceAttribute.setGnssAttribute(JGnssAttribute.getInstance(buf.getUnsignedByte(79 + n + m))); deviceAttribute.setGnssAttribute(JGnssAttribute.getInstance(buf.readUnsignedByte()));
deviceAttribute.setCommunicationModuleAttribute(JCommunicationModuleAttribute.getInstance(buf.getUnsignedByte(80 + n + m))); deviceAttribute.setCommunicationModuleAttribute(JCommunicationModuleAttribute.getInstance(buf.readUnsignedByte()));
System.out.println(deviceAttribute);
List<String> allRequestKey = SessionManager.INSTANCE.getAllRequestKey();
String prefix = String.join("_", header.getTerminalId().replaceFirst("^0*", ""), "0107");
for (String key : allRequestKey) {
if (key.startsWith(prefix)) {
SessionManager.INSTANCE.response(key, deviceAttribute);
}
}
SessionManager.INSTANCE.response(header.getTerminalId(), "0107", null, deviceAttribute);
return null; return null;
} }

View File

@ -99,33 +99,38 @@ public enum SessionManager {
public Boolean response(String devId, String respId, Long responseNo, Object data) { public Boolean response(String devId, String respId, Long responseNo, Object data) {
String requestKey = requestKey(devId, respId, responseNo); String requestKey = requestKey(devId, respId, responseNo);
SynchronousQueue<Object> queue = topicSubscribers.get(requestKey);
if (queue != null) { boolean result = false;
try { if (responseNo == null) {
return queue.offer(data, 2, TimeUnit.SECONDS); for (String key : topicSubscribers.keySet()) {
} catch (InterruptedException e) { if (key.startsWith(requestKey)) {
log.error("{}", e.getMessage(), e); System.out.println(key);
SynchronousQueue<Object> queue = topicSubscribers.get(key);
if (queue != null) {
result = true;
try {
queue.offer(data, 2, TimeUnit.SECONDS);
} catch (InterruptedException e) {
log.error("{}", e.getMessage(), e);
}
}
}
}
}else {
SynchronousQueue<Object> queue = topicSubscribers.get(requestKey);
if (queue != null) {
result = true;
try {
queue.offer(data, 2, TimeUnit.SECONDS);
} catch (InterruptedException e) {
log.error("{}", e.getMessage(), e);
}
} }
} }
log.warn("Not find response,key:{} data:{} ", requestKey, data); if (!result) {
return false; log.warn("Not find response,key:{} data:{} ", requestKey, data);
}
public Boolean response(String key, Object data) {
SynchronousQueue<Object> queue = topicSubscribers.get(key);
if (queue != null) {
try {
return queue.offer(data, 2, TimeUnit.SECONDS);
} catch (InterruptedException e) {
log.error("{}", e.getMessage(), e);
}
} }
log.warn("Not find response,key:{} data:{} ", key, data); return result;
return false;
}
public List<String> getAllRequestKey() {
return new ArrayList<>(topicSubscribers.keySet());
} }
private void unsubscribe(String key) { private void unsubscribe(String key) {
@ -140,7 +145,8 @@ public enum SessionManager {
} }
private String requestKey(String devId, String respId, Long requestNo) { private String requestKey(String devId, String respId, Long requestNo) {
return String.join("_", devId.replaceFirst("^0*", ""), respId, requestNo.toString()); return String.join("_", devId.replaceFirst("^0*", ""), respId, requestNo == null?"":requestNo.toString());
} }
public void remove(String devId) { public void remove(String devId) {