opt:对NDC S消息进行消息校验和拆包处理
This commit is contained in:
@@ -27,6 +27,7 @@ import java.io.DataInputStream;
|
|||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -103,120 +104,149 @@ public class OneNDCSocketConnectionAutoRun extends AbstractAutoRunnable {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
int[] arr = new int[count];
|
int[] packetData = new int[count];
|
||||||
StringBuffer bs = new StringBuffer();
|
StringBuffer bs = new StringBuffer();
|
||||||
|
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
int temp = b[i];
|
int temp = b[i];
|
||||||
if (temp < 0)
|
if (temp < 0)
|
||||||
temp += 256;
|
temp += 256;
|
||||||
arr[i] = temp;
|
packetData[i] = temp;
|
||||||
StringBuffer bs1 = new StringBuffer("0");
|
StringBuffer bs1 = new StringBuffer("0");
|
||||||
bs.append(temp < 16 ? bs1.append(Integer.toHexString(temp)) : Integer.toHexString(temp));
|
bs.append(temp < 16 ? bs1.append(Integer.toHexString(temp)) : Integer.toHexString(temp));
|
||||||
}
|
}
|
||||||
|
// 从字节流的开头开始读取
|
||||||
//System.out.println("收到请求参数:" + bs);
|
int offset = 0;
|
||||||
boolean flag = false;
|
// 循环处理字节流,逐个拆包 按照S消息解析
|
||||||
if (arr[8] * 256 + arr[9] == 0x73) {
|
while (offset < packetData.length) {
|
||||||
byte[] data = null;
|
if (offset + 6 > packetData.length) {
|
||||||
System.out.println("接收agv上报信息:" + bs);
|
System.out.println("错误:没有足够的字节来读取长度字段。");
|
||||||
//执行阶段
|
break;
|
||||||
int phase = arr[16] * 256 + arr[17];
|
|
||||||
// agv任务号
|
|
||||||
int index = arr[12] * 256 + arr[13];
|
|
||||||
//任务号
|
|
||||||
int ikey = arr[26] * 256 + arr[27];
|
|
||||||
//站点号
|
|
||||||
int agvaddr = arr[18] * 256 + arr[19];
|
|
||||||
//车号
|
|
||||||
int carno = arr[20];
|
|
||||||
//充电桩站点号
|
|
||||||
int station = arr[25];
|
|
||||||
Instruction link_inst = null;
|
|
||||||
Instruction inst = null;
|
|
||||||
List<Instruction> insts = null;
|
|
||||||
boolean link_flag = false;
|
|
||||||
Device agv_device = null;
|
|
||||||
if (carno != 0) {
|
|
||||||
agv_device = deviceAppService.findDeviceByCode(String.valueOf(carno));
|
|
||||||
}
|
}
|
||||||
if (ikey != 0) {
|
int messageHeader = packetData[offset] * 256 + packetData[offset+1];
|
||||||
inst = instructionService.findByCodeFromCache(String.valueOf(ikey));
|
//检测消息头是否有效
|
||||||
if (ObjectUtil.isEmpty(inst)) {
|
if (messageHeader == 0x87CD){
|
||||||
inst = instructionService.findByCode(String.valueOf(ikey));
|
//消息头长度
|
||||||
|
int messageHeaderLength = packetData[offset+2] * 256 + packetData[offset+3];
|
||||||
|
//消息数据长度
|
||||||
|
int messageDataLength = packetData[offset+4] * 256 + packetData[offset+5];
|
||||||
|
//消息总长度
|
||||||
|
int messageLength = messageHeaderLength + messageDataLength;
|
||||||
|
// 检查剩余字节是否足够处理完整的数据包
|
||||||
|
if (offset + messageLength > packetData.length) {
|
||||||
|
System.out.println("错误:检测到不完整的数据包。");
|
||||||
|
// 数据包不完整,停止处理
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
int[] arr = Arrays.copyOfRange(packetData, offset, offset+messageLength);
|
||||||
if (!ObjectUtil.isEmpty(link_inst)) {
|
boolean flag = false;
|
||||||
link_flag = true;
|
if (arr[8] * 256 + arr[9] == 0x73) {
|
||||||
}
|
byte[] data = null;
|
||||||
log.info("接收agv上报信息:" + bs);
|
System.out.println("接收agv上报信息:" + bs);
|
||||||
log.info("接收agv上报信息:" + "phase--" + phase + " index--" + index + " ikey--" + ikey + " agvaddr--" + agvaddr + " Car--" + carno);
|
//执行阶段
|
||||||
Device device = null;
|
int phase = arr[16] * 256 + arr[17];
|
||||||
String device_code = null;
|
// agv任务号
|
||||||
|
int index = arr[12] * 256 + arr[13];
|
||||||
|
//任务号
|
||||||
//
|
int ikey = arr[26] * 256 + arr[27];
|
||||||
AgvNdcOneDeviceDriver agvNdcOneDeviceDriver;
|
//站点号
|
||||||
|
int agvaddr = arr[18] * 256 + arr[19];
|
||||||
//开始任务/上报订单号
|
//车号
|
||||||
if (phase == 0x01) {
|
int carno = arr[20];
|
||||||
if (!ObjectUtil.isEmpty(inst)) {
|
//充电桩站点号
|
||||||
inst.setInstruction_status("1");
|
int station = arr[25];
|
||||||
inst.setAgv_jobno(String.valueOf(index));
|
Instruction link_inst = null;
|
||||||
inst.setSend_status("1");
|
Instruction inst = null;
|
||||||
instructionService.update(inst);
|
List<Instruction> insts = null;
|
||||||
data = NDCAgvService.sendAgvOneModeInst(phase, index, 0, 0, 0, 0, 0);
|
boolean link_flag = false;
|
||||||
}
|
Device agv_device = null;
|
||||||
}
|
if (carno != 0) {
|
||||||
//任务完毕
|
agv_device = deviceAppService.findDeviceByCode(String.valueOf(carno));
|
||||||
//(无车id及状态)
|
|
||||||
else if (phase == 0x0A) {
|
|
||||||
if (!ObjectUtil.isEmpty(inst)) {
|
|
||||||
instructionService.finish(inst.getInstruction_id());
|
|
||||||
} else {
|
|
||||||
log.warn("指令号:{},未反馈wms任务完成,因为agv上报指令号查询指令为空!");
|
|
||||||
}
|
|
||||||
data = NDCAgvService.sendAgvOneModeInst(phase, index, 0, 0, 0, 0, 0);
|
|
||||||
log.info("指令号:{},已反馈agv任务完成!");
|
|
||||||
}
|
|
||||||
//todo 检查协议里删除任务流程有很多个步骤,在哪体现的
|
|
||||||
//(需要WCS反馈)
|
|
||||||
else if (phase == 0x30) {
|
|
||||||
data = NDCAgvService.sendAgvOneModeInst(0x8F, index, 0, 0, 0, 0, 0);
|
|
||||||
}
|
|
||||||
//任务删除确认
|
|
||||||
//(需要WCS反馈)
|
|
||||||
else if (phase == 0xFF) {
|
|
||||||
|
|
||||||
if (!ObjectUtil.isEmpty(inst)) {
|
|
||||||
if (!ObjectUtil.isEmpty(inst)) {
|
|
||||||
instructionService.cancelNOSendAgv(inst.getInstruction_id());
|
|
||||||
}
|
}
|
||||||
}
|
if (ikey != 0) {
|
||||||
data = NDCAgvService.sendAgvOneModeInst(phase, index, 0, 0, 0, 0, 0);
|
inst = instructionService.findByCodeFromCache(String.valueOf(ikey));
|
||||||
} else {
|
if (ObjectUtil.isEmpty(inst)) {
|
||||||
//上报异常信息
|
inst = instructionService.findByCode(String.valueOf(ikey));
|
||||||
//(不需要WCS反馈)
|
}
|
||||||
if (phase == 0x70 || phase == 0x71 || phase == 0x72 || phase == 0x73 || phase == 0x74 || phase == 0x75) {
|
|
||||||
device = deviceAppService.findDeviceByCode(String.valueOf(agvaddr));
|
|
||||||
} else {
|
|
||||||
//todo NDC根据车辆编号123456进行查找特定车辆
|
|
||||||
device = deviceAppService.findDeviceByCode(Integer.toString(arr[20]));
|
|
||||||
}
|
|
||||||
if (ObjectUtil.isNotEmpty(device)) {
|
|
||||||
if (device.getDeviceDriver() instanceof AgvNdcOneDeviceDriver) {
|
|
||||||
agvNdcOneDeviceDriver = (AgvNdcOneDeviceDriver) device.getDeviceDriver();
|
|
||||||
agvNdcOneDeviceDriver.processSocket(arr);
|
|
||||||
}
|
}
|
||||||
|
if (!ObjectUtil.isEmpty(link_inst)) {
|
||||||
|
link_flag = true;
|
||||||
|
}
|
||||||
|
log.info("接收agv上报信息:" + bs);
|
||||||
|
log.info("接收agv上报信息:" + "phase--" + phase + " index--" + index + " ikey--" + ikey + " agvaddr--" + agvaddr + " Car--" + carno);
|
||||||
|
Device device = null;
|
||||||
|
String device_code = null;
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
AgvNdcOneDeviceDriver agvNdcOneDeviceDriver;
|
||||||
|
|
||||||
|
//开始任务/上报订单号
|
||||||
|
if (phase == 0x01) {
|
||||||
|
if (!ObjectUtil.isEmpty(inst)) {
|
||||||
|
inst.setInstruction_status("1");
|
||||||
|
inst.setAgv_jobno(String.valueOf(index));
|
||||||
|
inst.setSend_status("1");
|
||||||
|
instructionService.update(inst);
|
||||||
|
data = NDCAgvService.sendAgvOneModeInst(phase, index, 0, 0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//任务完毕
|
||||||
|
//(无车id及状态)
|
||||||
|
else if (phase == 0x0A) {
|
||||||
|
if (!ObjectUtil.isEmpty(inst)) {
|
||||||
|
instructionService.finish(inst.getInstruction_id());
|
||||||
|
} else {
|
||||||
|
log.warn("指令号:{},未反馈wms任务完成,因为agv上报指令号查询指令为空!");
|
||||||
|
}
|
||||||
|
data = NDCAgvService.sendAgvOneModeInst(phase, index, 0, 0, 0, 0, 0);
|
||||||
|
log.info("指令号:{},已反馈agv任务完成!");
|
||||||
|
}
|
||||||
|
//todo 检查协议里删除任务流程有很多个步骤,在哪体现的
|
||||||
|
//(需要WCS反馈)
|
||||||
|
else if (phase == 0x30) {
|
||||||
|
data = NDCAgvService.sendAgvOneModeInst(0x8F, index, 0, 0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
//任务删除确认
|
||||||
|
//(需要WCS反馈)
|
||||||
|
else if (phase == 0xFF) {
|
||||||
|
|
||||||
|
if (!ObjectUtil.isEmpty(inst)) {
|
||||||
|
if (!ObjectUtil.isEmpty(inst)) {
|
||||||
|
instructionService.cancelNOSendAgv(inst.getInstruction_id());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
data = NDCAgvService.sendAgvOneModeInst(phase, index, 0, 0, 0, 0, 0);
|
||||||
|
} else {
|
||||||
|
//上报异常信息
|
||||||
|
//(不需要WCS反馈)
|
||||||
|
if (phase == 0x70 || phase == 0x71 || phase == 0x72 || phase == 0x73 || phase == 0x74 || phase == 0x75) {
|
||||||
|
device = deviceAppService.findDeviceByCode(String.valueOf(agvaddr));
|
||||||
|
} else {
|
||||||
|
//todo NDC根据车辆编号123456进行查找特定车辆
|
||||||
|
device = deviceAppService.findDeviceByCode(Integer.toString(arr[20]));
|
||||||
|
}
|
||||||
|
if (ObjectUtil.isNotEmpty(device)) {
|
||||||
|
if (device.getDeviceDriver() instanceof AgvNdcOneDeviceDriver) {
|
||||||
|
agvNdcOneDeviceDriver = (AgvNdcOneDeviceDriver) device.getDeviceDriver();
|
||||||
|
agvNdcOneDeviceDriver.processSocket(arr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if (!ObjectUtil.isEmpty(data)) {
|
||||||
|
write(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
System.out.println("agv上报不是0073类型动作,不处理");
|
||||||
}
|
}
|
||||||
|
// 更新偏移量,继续处理下一个数据包
|
||||||
|
offset+=messageLength;
|
||||||
|
}else {
|
||||||
|
System.out.println("消息头无效,不处理。");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if (!ObjectUtil.isEmpty(data)) {
|
|
||||||
write(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
System.out.println("agv上报不是0073类型动作,不处理");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user