修改代码,改为rfid全假,增加rfid的tid查询

镇海石化
LAPTOP-S9HJSOEB\昊天 7 months ago
parent 3b81cc67f8
commit 5230090073

@ -5,6 +5,7 @@ import com.zhehekeji.web.entity.RFID;
import com.zhehekeji.web.entity.Street;
import com.zhehekeji.web.service.RFID.RFIDMap;
import com.zhehekeji.web.service.RFID.RFIDSocket;
import com.zhehekeji.web.service.RFID.RFIDTCPClient;
import com.zhehekeji.web.service.RFIDService;
import com.zhehekeji.web.service.StreetService;
import com.zhehekeji.web.service.TestService;
@ -100,6 +101,23 @@ public class TestController {
return Result.success(new HashSet<>());
}
@ApiOperation("rfidNewTid")
@GetMapping("/rfidNewTid")
public Result<Set<String >> rfidNewTid(@RequestParam String streetId) {
Street street = streetService.getStreetByPlcId(streetId);
if (street != null) {
//rfid不分左右rfid使用同一ip和端口依靠传递字符调整方向
RFID rfid = rfidService.getRFIDByPlc(street.getId(), null);
if (rfid != null) {
Set<String> tags = RFIDStartTid(rfid.getIp(), rfid.getPort(), street.getId(), 1);
return Result.success(tags);
}
}
return Result.success(new HashSet<>());
}
public void RFIDStart(String ip, Integer port, Integer streetId, Integer direction) {
RFIDSocket rfidSocket = new RFIDSocket(ip, port);
log.info("rfid调用ip" + ip + ";port:" + port);
@ -107,6 +125,12 @@ public class TestController {
RFIDMap.put(streetId, rfidSocket);
}
public Set<String> RFIDStartTid(String ip, Integer port, Integer streetId, Integer direction) {
Set<String> tags = RFIDTCPClient.checkTid(ip, port);
return tags;
}
public Set<String> RFIDStop(Street street) {
RFIDSocket rfidSocket = RFIDMap.get(street.getId());
Set<String> tags = null;

@ -12,7 +12,7 @@ public class StockExportExcel {
private String streetName;
@ExcelProperty(index = 8, value = "盘点批次号")
private String lotnum;
private String checkNum;
@ExcelProperty(index = 5, value = "条码")
private String code;
@ -21,10 +21,10 @@ public class StockExportExcel {
private String wmsCode;
@ExcelProperty(index = 1, value = "左右,1:左侧,2:右侧")
private Integer direction;
private String direction;
@ExcelProperty(index = 2, value = "深浅,1:浅货位,2:深货位")
private Integer side;
private String side;
// @ExcelProperty(index = 1, value = "货架号")
// private String shelveId;

@ -3,6 +3,7 @@ package com.zhehekeji.web.service;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
@Data
@AllArgsConstructor
@ -97,8 +98,8 @@ public class PlcCmdInfo {
this.leftRight2 = leftRight2;
this.column2 = column2;
this.row2 = row2;
this.plcId = plcId;
this.orderNum = plcId + "_" + taskId;
this.plcId = StringUtils.leftPad(plcId, 3, '0');;
this.orderNum = this.plcId + "_" + taskId;
this.separation1 = separation1;
this.separation2 = separation2;
this.lotnum = lotnum;

@ -14,6 +14,7 @@ import com.zhehekeji.web.pojo.OrderVO;
import com.zhehekeji.web.pojo.websocket.WebSocketVo;
import com.zhehekeji.web.service.RFID.RFIDMap;
import com.zhehekeji.web.service.RFID.RFIDSocket;
import com.zhehekeji.web.service.RFID.RFIDTCPClient;
import com.zhehekeji.web.service.hikLightSource.HikControlSocket;
import com.zhehekeji.web.service.interfaces.RfidLiveService;
import com.zhehekeji.web.service.ksec.KsecNettyClient;
@ -644,7 +645,7 @@ public class PlcService {
log.info("camera ptz" + c);
gyrateCameraByCode(cameraId, c);
} else {
String c = cmdCode + "1";
String c = cmdCode + "1"+ "-" + plcCmdInfo.getLeftRightStr(1) ;
gyrateCameraByCode(cameraId, c);
try {
Thread.sleep(configProperties.getCameraConfig().getDelayCaptureTime());
@ -722,25 +723,30 @@ public class PlcService {
long startRfidTime = System.currentTimeMillis();
String code = "";
Set<String> tags = new HashSet<>();
try {
RFIDCheck(plcCmdInfo, true);
Thread.sleep(1000 * configProperties.getRfid().getScanTime());
} catch (Exception e) {
e.printStackTrace();
} finally {
tags = RFIDStop(plcCmdInfo);
log.info("盘点rfid扫描结果" + tags);
}
//镇海深巷道,进行假盘点
if(plcCmdInfo.getSeparation1() == 1){
if (tags.contains(wmsTrayCode)){
code = "";
}else code = "rfid识别失败";
}else {
code="";
}
if (street != null) {
//rfid不分左右rfid使用同一ip和端口依靠传递字符调整方向
RFID rfid = rfidService.getRFIDByPlc(street.getId(), null);
if (rfid != null) {
}
if (rfid != null) {
tags = RFIDTCPClient.checkTid(rfid.getIp(), rfid.getPort());
}
}
//镇海项目,进行假盘点
code = "";
// if(plcCmdInfo.getSeparation1() == 1){
// if (tags.contains(wmsTrayCode)){
// code = "";
// }else code = "rfid识别失败";
// }else {
// code="";
// }
if (tags != null && tags.size() > 0) {
// if ( tags.contains(wmsTrayCode)) {
// log.info("扫描成功");

@ -0,0 +1,19 @@
package com.zhehekeji.web.service.RFID;
import lombok.Data;
@Data
public class RFIDData {
private String pc;
private String epc;
private String crc;
private String tid;
public RFIDData(String pc, String epc, String crc, String tid) {
this.pc = pc;
this.epc = epc;
this.crc = crc;
this.tid = tid;
}
}

@ -4,9 +4,11 @@ import com.payne.connect.net.NetworkHandle;
import com.payne.reader.Reader;
import com.payne.reader.base.Consumer;
import com.payne.reader.bean.config.AntennaCount;
import com.payne.reader.bean.config.MemBank;
import com.payne.reader.bean.receive.*;
import com.payne.reader.bean.send.FastSwitchFourAntennaInventory;
import com.payne.reader.bean.send.InventoryConfig;
import com.payne.reader.bean.send.ReadConfig;
import com.payne.reader.process.ReaderImpl;
import com.payne.reader.util.ArrayUtils;
@ -26,15 +28,14 @@ public class RFIDSocket {
private Map<String, Integer> tagsCount = new ConcurrentHashMap<>();
public static void main(String[] args) {
String tagStr = "363000145224505289907900";
if(tagStr.startsWith("363000") &&tagStr.endsWith("00")){
tagStr = tagStr.substring(0, tagStr.length() - 2);
}
System.out.println(tagStr);
RFIDSocket rfidSocket = new RFIDSocket("192.168.2.66", 2001);
rfidSocket.readTid();
}
public Set<String> getTags() {
return tags;
}
public RFIDSocket(String ip, Integer port) {
this.ip = ip;
this.port = port;
@ -42,11 +43,11 @@ public class RFIDSocket {
NetworkHandle handle = new NetworkHandle(ip, (port));
mReader = ReaderImpl.create(AntennaCount.FOUR_CHANNELS);
boolean linkSuccess =false;
boolean linkSuccess = false;
try {
linkSuccess = mReader.connect(handle);
}catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
if (linkSuccess) {
@ -57,40 +58,71 @@ public class RFIDSocket {
}
public void startCheck(Integer direction,boolean foreach) {
public Set<String> readTid() {
ReadConfig config = new ReadConfig.Builder().setMemBank(MemBank.TID).setWordLength((byte) 6).build();
Set<String> tags = new HashSet<>();
byte[] bytes = new byte[1];
bytes[0] = 0x01;
int count = 0;
while (count<100) {
try {
bytes[0] = (byte) (bytes[0] == 0x01 ? 0x00 : 0x01);
Thread.sleep(200);
mReader.setWorkAntenna(bytes[0], (v) -> {
System.out.println("reader1 setWorkAntenna success");
}, (v) -> {
System.out.println("reader1 setWorkAntenna fail");
});
mReader.readTag(config
, (Consumer<OperationTag>) readTag -> {
System.out.println("readTag: " + readTag.toString());
tags.add(readTag.getStrData());
}
, e -> System.out.println("readTag err: " + e.toString()));
count++;
} catch (Exception e) {
e.printStackTrace();
stopCheck();
}
}
return tags;
}
public void startCheck(Integer direction, boolean foreach) {
// Reader mReader = ReaderImpl.create(AntennaCount.SIXTEEN_CHANNELS);
// Reader mReader = ReaderImpl.create(AntennaCount.EIGHT_CHANNELS);
running = true;
Thread thread = new Thread(new Runnable() {
public void run() {
mReader.setOriginalDataCallback(
new Consumer<byte[]>() {
@Override
public void accept(byte[] onSend) throws Exception {
//System.out.println("---reader 1 send :" + ArrayUtils.bytesToHexString(onSend, 0, onSend.length));
System.out.println("---reader 1 send :" + ArrayUtils.bytesToHexString(onSend, 0, onSend.length));
}
},
new Consumer<byte[]>() {
@Override
public void accept(byte[] onReceive) throws Exception {
System.out.println("===reader 1 recv:" + ArrayUtils.bytesToHexString(onReceive, 0, onReceive.length));
}
});
FastSwitchFourAntennaInventory inventory = new FastSwitchFourAntennaInventory.Builder().build();
InventoryConfig config = new InventoryConfig.Builder()
.setInventory(inventory)
.setOnInventoryTagSuccess(new Consumer<InventoryTag>() {
@Override
public void accept(InventoryTag tag) throws Exception {
System.out.println("reader1 inventory tag :" + tag.getEpc());
String tagStr = tag.getEpc().replace(" ","");
System.out.println("reader1 inventory tag :" + tag.toString());
String tagStr = tag.getEpc().replace(" ", "");
System.out.println("tag: " + tag.getEpc());
// 去除头为0
tagStr = tagStr.replace("0000000000","");
tagStr = tagStr.replace("0000000000", "");
//去除尾巴为0
if(tagStr.startsWith("363000") &&tagStr.endsWith("00")){
if (tagStr.startsWith("363000") && tagStr.endsWith("00")) {
tagStr = tagStr.substring(0, tagStr.length() - 2);
}
tags.add(tagStr);
@ -105,6 +137,7 @@ public class RFIDSocket {
@Override
public void accept(InventoryTagEnd arg0) throws Exception {
// System.out.println("reader1 InventoryTagEnd");
}
})
@ -138,11 +171,11 @@ public class RFIDSocket {
while (running) {
try {
bytes[0] = (byte) (bytes[0] == 0x01 ? 0x00 : 0x01);
Thread.sleep(1000);
if(foreach){
mReader.setWorkAntenna(bytes[0],(v)->{
Thread.sleep(200);
if (foreach) {
mReader.setWorkAntenna(bytes[0], (v) -> {
System.out.println("reader1 setWorkAntenna success");
},(v)->{
}, (v) -> {
System.out.println("reader1 setWorkAntenna fail");
});
}

@ -0,0 +1,231 @@
package com.zhehekeji.web.service.RFID;
import lombok.extern.slf4j.Slf4j;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
@Slf4j
public class RFIDTCPClient {
private Socket socket;
private OutputStream outputStream;
private InputStream inputStream;
private String host;
private int port;
public RFIDTCPClient(String host, int port) {
this.host = host;
this.port = port;
}
public boolean connect() {
try {
socket = new Socket(host, port);
socket.setSoTimeout(200);
outputStream = socket.getOutputStream();
inputStream = socket.getInputStream();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public void disconnect() {
try {
if (outputStream != null) {
outputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendHexCommand(String hexCommand) throws IOException {
byte[] bytes = hexStringToByteArray(hexCommand);
outputStream.write(bytes);
outputStream.flush();
}
public Set<RFIDData> receiveRFIDData() throws IOException {
Set<RFIDData> dataSet = new HashSet<>();
// 创建一个缓冲区来存储接收到的数据
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
// 设置一个超时机制,避免无限等待
// 先读取一批数据到缓冲区
byte[] tempBuffer = new byte[1024];
try {
int bytesRead;
while ((bytesRead = inputStream.read(tempBuffer)) != -1) {
buffer.write(tempBuffer, 0, bytesRead); // 记录读取的数据到日志
log.info("读取到 {} 字节数据: {}", bytesRead, bytesToHex(Arrays.copyOfRange(tempBuffer, 0, bytesRead)));
// 如果读取到足够数据就停止
if (buffer.size() > 4096) {
break;
}
}
log.info("总共读取到 {} 字节数据", buffer.size());
} catch (SocketTimeoutException e) {
// 超时正常,继续处理已接收的数据
log.info("读取数据超时,继续处理已接收的数据");
}
// 处理缓冲区中的数据
byte[] receivedData = buffer.toByteArray();
int position = 0;
// 循环查找并解析所有以A0开头的数据包
while (position < receivedData.length - 2) {
// 查找A0起始字节
if (receivedData[position] == (byte) 0xA0) {
// 确保有足够的数据读取长度字节
if (position + 1 >= receivedData.length) {
break;
}
// 读取长度
int length = receivedData[position + 1] & 0xFF;
// 确保有足够的数据
if (position + 2 + length > receivedData.length) {
break;
}
// 提取数据部分
byte[] data = Arrays.copyOfRange(receivedData, position + 2, position + 2 + length);
// 解析数据
RFIDData rfidData = parseRFIDData(data);
if (rfidData != null) {
dataSet.add(rfidData);
}
// 移动到下一个数据包
position += 2 + length;
} else {
// 移动到下一个字节
position++;
}
}
return dataSet;
}
private RFIDData parseRFIDData(byte[] data) {
try {
// 检查数据长度是否足够
if (data.length < 21) { // 至少需要21个字节
return null;
}
// 检查固定值是否正确
if((data[1] & 0xFF) != 0X81){
return null;
}
// 0x01 是固定值 (data[0])
// 81 00 0C 1C 是其他数据 (data[1]-data[4])
// 接下来2个字节是PC值 (data[5], data[6])
String pc = bytesToHex(Arrays.copyOfRange(data, 5, 7));
// 接下来12个字节是EPC值 (data[7] 到 data[18])
String epc = bytesToHex(Arrays.copyOfRange(data, 7, 19));
// 接下来2个字节是CRC值 (data[19], data[20])
String crc = bytesToHex(Arrays.copyOfRange(data, 19, 21));
// 剩余字节是TID值 (data[21] 到 data[33]共12个字节)
// 确保不会越界
int tidStart = 21;
int tidEnd = tidStart + 12; // TID固定为12个字节
if (tidEnd <= data.length) {
String tid = bytesToHex(Arrays.copyOfRange(data, tidStart, tidEnd));
RFIDData rfidData = new RFIDData(pc, epc, crc, tid);
log.info("rfidData:{}", rfidData);
return rfidData;
}
// 如果没有TID数据创建一个空的TID
RFIDData rfidData = new RFIDData(pc, epc, crc, "");
log.info("rfidData:{}", rfidData);
return rfidData;
} catch (Exception e) {
log.error("解析RFID数据时出错: ", e);
return null;
}
}
private byte[] hexStringToByteArray(String hex) {
hex = hex.replaceAll(" ", ""); // 移除空格
int len = hex.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
+ Character.digit(hex.charAt(i+1), 16));
}
return data;
}
private String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02X", b));
}
return sb.toString();
}
public static Set<String> checkTid(String ip ,int port){
RFIDTCPClient client = new RFIDTCPClient(ip, port);
Set<RFIDData> data = new HashSet<>();
if (client.connect()) {
try {
for (int i = 0; i < 8; i++){
if (i%2==0)
client.sendHexCommand("A0 04 01 74 01 E6");
else
client.sendHexCommand("A0 04 01 74 00 E7");
client.sendHexCommand("A0 0A FF 81 02 00 06 00 00 00 00 CE");
data.addAll(client.receiveRFIDData());
}
if (data != null) {
log.info("接收到RFID数据: " + data.toString());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
client.disconnect();
}
} else {
System.out.println("连接失败");
}
Set<String> setData = data.stream().map(RFIDData::getTid).collect(Collectors.toSet());
log.info("接收到的RFID数据: " + setData);
return setData;
}
public static void main(String[] args) {
checkTid("127.0.0.1", 4002);
}
}

@ -270,15 +270,15 @@ public class StockService {
stocks.forEach(stock -> {
StockExportExcel stockExportExcel = new StockExportExcel();
stockExportExcel.setCode(stock.getTrayCode());
stockExportExcel.setLotnum(stock.getLotnum());
stockExportExcel.setCheckNum(stock.getCheckNum());
stockExportExcel.setColumn(stock.getColumn());
stockExportExcel.setWmsCode(stock.getWmsTrayCode());
//stockExportExcel.setCount(stock.getCount());
stockExportExcel.setRow(stock.getRow());
stockExportExcel.setStreetName(street.getName());
stockExportExcel.setExportTime(stock.getExportTime());
stockExportExcel.setDirection(stock.getDirection());
stockExportExcel.setSide(stock.getSide());
stockExportExcel.setDirection(stock.getDirection()==1?"左侧":"右侧");
stockExportExcel.setSide(stock.getSide() == 1?"浅货位":"深货位");
//stockExportExcel.setShelveId(stock.getShelveId());
stockExportExcel.setStatus(stock.getStatus());
stockExportExcels.add(stockExportExcel);
@ -299,14 +299,15 @@ public class StockService {
stocks.forEach(stock -> {
StockExportExcel stockExportExcel = new StockExportExcel();
stockExportExcel.setCode(stock.getTrayCode());
stockExportExcel.setLotnum(stock.getLotnum());
stockExportExcel.setCheckNum(stock.getCheckNum());
stockExportExcel.setColumn(stock.getColumn());
stockExportExcel.setWmsCode(stock.getWmsTrayCode());
//stockExportExcel.setCount(stock.getCount());
stockExportExcel.setRow(stock.getRow());
stockExportExcel.setStreetName(streetMap.get(stock.getStreetId()));
stockExportExcel.setExportTime(stock.getExportTime());
stockExportExcel.setDirection(stock.getDirection());
stockExportExcel.setSide(stock.getSide());
stockExportExcel.setDirection(stock.getDirection()==1?"左侧":"右侧");
stockExportExcel.setSide(stock.getSide() == 1?"浅货位":"深货位");
stockExportExcel.setStatus(stock.getStatus());
stockExportExcels.add(stockExportExcel);
});

@ -4,18 +4,24 @@ import com.alibaba.fastjson.JSONObject;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.charset.StandardCharsets;
/**
* JSON < >
*/
@Slf4j
public class KescEncoder extends MessageToByteEncoder<KsecInfo> {
private static final Logger tcpLogger = LoggerFactory.getLogger("tcp");
@Override
protected void encode(ChannelHandlerContext channelHandlerContext, KsecInfo ksecInfo, ByteBuf byteBuf) throws Exception {
String body = "<" + JSONObject.toJSONString(ksecInfo) + ">";
tcpLogger.info("send ksecInfo:{}",body);
byteBuf.writeBytes(body.getBytes(StandardCharsets.UTF_8));
}
}

@ -128,13 +128,10 @@ public class KsecDecoder extends DelimiterBasedFrameDecoder {
if (Cmd.B1.name().equals(cmdName)) {
//昆船盘点模式下也会发B1 ,但是不会发送B2
//这里判断下,是否存在盘点批次号 若存在既是盘点的B1,无需处理若不存在lotnum则是随行的B1
if(StringUtils.isEmpty(dataInfo.getLotnum())){
//任务开始 旋转到原点位
plcService.gyrateCamera(plcCmdInfo,Cmd.C5.name());
plcService.orderStart(plcCmdInfo);
}else {
log.info("check move");
}
//任务开始 旋转到原点位
plcService.gyrateCamera(plcCmdInfo,Cmd.C5.name());
plcService.orderStart(plcCmdInfo);
} else if (Cmd.B2.name().equals(cmdName)) {
//B2 C4 一起发的需要停止等B2
@ -201,7 +198,7 @@ public class KsecDecoder extends DelimiterBasedFrameDecoder {
//rfid的逻辑
String code = dataInfo.getCmdName();
log.info("盘点指令:{}", ksecInfo);
plcService.check(plcCmdInfo,ksecInfo.getType(),dataInfo.getCode(),dataInfo.getTrayCode());
plcService.check(plcCmdInfo,ksecInfo.getType(),dataInfo.getLotnum(),dataInfo.getLotnum());
ksecInfo.getData().setAckStatus(1);
ctx.channel().writeAndFlush(ksecInfo);

Loading…
Cancel
Save