|
|
|
@ -12,7 +12,6 @@ import com.ghgande.j2mod.modbus.procimg.SimpleRegister; |
|
|
|
import com.xujie.sys.modules.reader.dao.GetInformationForExcelMapper; |
|
|
|
import com.xujie.sys.modules.reader.entity.EquipmentDataDetail; |
|
|
|
import com.xujie.sys.modules.reader.entity.EquipmentFolderLocation; |
|
|
|
import com.xujie.sys.modules.reader.entity.EquipmentFolderSortEntity; |
|
|
|
import com.xujie.sys.modules.reader.service.EquipmentFolderLocationService; |
|
|
|
import com.xujie.sys.modules.reader.service.EquipmentFolderSortService; |
|
|
|
import com.xujie.sys.modules.reader.service.GetInformationForExcelService; |
|
|
|
@ -82,47 +81,44 @@ public class ModbusCommunicateServiceImpl implements ModbusCommunicateService { |
|
|
|
//获取对应检验单号的值 |
|
|
|
List<List<Float>> floatValues = getValue(modbusIp, modbusPort, registerAddress, registerNum, unitId); |
|
|
|
|
|
|
|
|
|
|
|
//获取对应的batchNo |
|
|
|
LambdaQueryWrapper<EquipmentFolderSortEntity> wrapper = new LambdaQueryWrapper<>(); |
|
|
|
wrapper.eq(EquipmentFolderSortEntity::getSite,folderLocation.getSite()); |
|
|
|
wrapper.eq(EquipmentFolderSortEntity::getBuNo,folderLocation.getBuNo()); |
|
|
|
wrapper.eq(EquipmentFolderSortEntity::getInspectionNo,inspectionOrderNumber); |
|
|
|
EquipmentFolderSortEntity one = equipmentFolderSortService.getOne(wrapper); |
|
|
|
|
|
|
|
|
|
|
|
Integer batchNo ; |
|
|
|
//在所有的数据新增玩之后 , 对应的获取批次号进行新增或者修改(数据第一次就新增) |
|
|
|
if (one==null) { |
|
|
|
EquipmentFolderSortEntity equipmentFolderSortEntity = new EquipmentFolderSortEntity(); |
|
|
|
equipmentFolderSortEntity.setSite(folderLocation.getSite()); |
|
|
|
equipmentFolderSortEntity.setBuNo(folderLocation.getBuNo()); |
|
|
|
equipmentFolderSortEntity.setInspectionNo(inspectionOrderNumber); |
|
|
|
equipmentFolderSortEntity.setBatchNo(2); |
|
|
|
equipmentFolderSortEntity.setFileNo(folderLocation.getFileNo()); |
|
|
|
equipmentFolderSortEntity.setCreateDate(new Date()); |
|
|
|
equipmentFolderSortService.save(equipmentFolderSortEntity); |
|
|
|
//对应的批次号为1 |
|
|
|
batchNo = 1; |
|
|
|
} else { |
|
|
|
Date createDate = one.getCreateDate();//获取最近新增一条信息的时间 |
|
|
|
Date date = new Date(); |
|
|
|
if (StringUtils.isEmpty(folderLocation.getBatchDate())) { |
|
|
|
folderLocation.setBatchDate("1"); |
|
|
|
} |
|
|
|
int time = Integer.parseInt(folderLocation.getBatchDate()); |
|
|
|
Long finalTime = date.getTime() - createDate.getTime(); |
|
|
|
if (finalTime / 1000 > time) { |
|
|
|
batchNo = one.getBatchNo(); |
|
|
|
//对应的不是第一次新增的情况下如果批次号不为空的情况下那么草果规定时间 批次号就加一,下次获取的时候就可以直接引用 |
|
|
|
one.setBatchNo(batchNo+1); |
|
|
|
one.setFileNo(folderLocation.getFileNo()); |
|
|
|
one.setCreateDate(new Date()); |
|
|
|
equipmentFolderSortService.updateById(one); |
|
|
|
} else { |
|
|
|
batchNo = one.getBatchNo(); |
|
|
|
} |
|
|
|
} |
|
|
|
// LambdaQueryWrapper<EquipmentFolderSortEntity> wrapper = new LambdaQueryWrapper<>(); |
|
|
|
// wrapper.eq(EquipmentFolderSortEntity::getSite,folderLocation.getSite()); |
|
|
|
// wrapper.eq(EquipmentFolderSortEntity::getBuNo,folderLocation.getBuNo()); |
|
|
|
// wrapper.eq(EquipmentFolderSortEntity::getInspectionNo,inspectionOrderNumber); |
|
|
|
// EquipmentFolderSortEntity one = equipmentFolderSortService.getOne(wrapper); |
|
|
|
// Integer batchNo ; |
|
|
|
// //在所有的数据新增玩之后 , 对应的获取批次号进行新增或者修改(数据第一次就新增) |
|
|
|
// if (one==null) { |
|
|
|
// EquipmentFolderSortEntity equipmentFolderSortEntity = new EquipmentFolderSortEntity(); |
|
|
|
// equipmentFolderSortEntity.setSite(folderLocation.getSite()); |
|
|
|
// equipmentFolderSortEntity.setBuNo(folderLocation.getBuNo()); |
|
|
|
// equipmentFolderSortEntity.setInspectionNo(inspectionOrderNumber); |
|
|
|
// equipmentFolderSortEntity.setBatchNo(2); |
|
|
|
// equipmentFolderSortEntity.setFileNo(folderLocation.getFileNo()); |
|
|
|
// equipmentFolderSortEntity.setCreateDate(new Date()); |
|
|
|
// equipmentFolderSortService.save(equipmentFolderSortEntity); |
|
|
|
// //对应的批次号为1 |
|
|
|
// batchNo = 1; |
|
|
|
// } else { |
|
|
|
// Date createDate = one.getCreateDate();//获取最近新增一条信息的时间 |
|
|
|
// Date date = new Date(); |
|
|
|
// if (StringUtils.isEmpty(folderLocation.getBatchDate())) { |
|
|
|
// folderLocation.setBatchDate("1"); |
|
|
|
// } |
|
|
|
// int time = Integer.parseInt(folderLocation.getBatchDate()); |
|
|
|
// Long finalTime = date.getTime() - createDate.getTime(); |
|
|
|
// if (finalTime / 1000 > time) { |
|
|
|
// batchNo = one.getBatchNo(); |
|
|
|
// //对应的不是第一次新增的情况下如果批次号不为空的情况下那么草果规定时间 批次号就加一,下次获取的时候就可以直接引用 |
|
|
|
// one.setBatchNo(batchNo+1); |
|
|
|
// one.setFileNo(folderLocation.getFileNo()); |
|
|
|
// one.setCreateDate(new Date()); |
|
|
|
// equipmentFolderSortService.updateById(one); |
|
|
|
// } else { |
|
|
|
// batchNo = one.getBatchNo(); |
|
|
|
// } |
|
|
|
// } |
|
|
|
|
|
|
|
int num = 1; |
|
|
|
List<EquipmentDataDetail> detailList = new ArrayList<>(); |
|
|
|
@ -135,7 +131,6 @@ public class ModbusCommunicateServiceImpl implements ModbusCommunicateService { |
|
|
|
equipmentDataDetail.setSite(folderLocation.getSite()); |
|
|
|
equipmentDataDetail.setEquipmentNo(folderLocation.getEquipmentNo()); |
|
|
|
equipmentDataDetail.setBuNo(folderLocation.getBuNo()); |
|
|
|
equipmentDataDetail.setBatchNo(batchNo); |
|
|
|
equipmentDataDetail.setNum(num); |
|
|
|
detailList.add(equipmentDataDetail); |
|
|
|
num++; |
|
|
|
@ -206,51 +201,6 @@ public class ModbusCommunicateServiceImpl implements ModbusCommunicateService { |
|
|
|
return floatValues; |
|
|
|
} |
|
|
|
|
|
|
|
//获取值(请求modbus获取) |
|
|
|
public List<Float> getResponseMessage(int registerAddress, int registerNum, int unitId, TCPMasterConnection connection) { |
|
|
|
ArrayList<Float> floats = new ArrayList<Float>(); |
|
|
|
try { |
|
|
|
// 创建一个Modbus TCP请求 |
|
|
|
ReadMultipleRegistersRequest request = new ReadMultipleRegistersRequest(registerAddress, registerNum); |
|
|
|
request.setUnitID(unitId); // 设置Modbus设备的Unit ID |
|
|
|
|
|
|
|
|
|
|
|
// 创建一个Modbus TCP事务 |
|
|
|
ModbusTCPTransaction transaction = new ModbusTCPTransaction(connection); |
|
|
|
transaction.setRequest(request); |
|
|
|
|
|
|
|
// 执行Modbus TCP事务 |
|
|
|
transaction.execute(); |
|
|
|
log.info("开始执行modbus tcp事物"); |
|
|
|
|
|
|
|
// 获取响应 |
|
|
|
ReadMultipleRegistersResponse response = (ReadMultipleRegistersResponse) transaction.getResponse(); |
|
|
|
log.info("打印响应的数据: {}", response.getMessage()); |
|
|
|
if (response != null) { |
|
|
|
// 处理响应数据 |
|
|
|
ArrayList<Integer> hex = new ArrayList<>(); |
|
|
|
InputRegister[] registers = response.getRegisters(); |
|
|
|
for (InputRegister register : registers) { |
|
|
|
short value = register.toShort(); |
|
|
|
int hexValue = value & 0xFFFF; |
|
|
|
log.info("获取的值是: {},value: {}", hexValue, value); |
|
|
|
hex.add(hexValue); |
|
|
|
} |
|
|
|
for (int i = 0; i < hex.size(); i += 2) { |
|
|
|
int hexI = (hex.get(i) << 16) | (hex.get(i + 1) & 0xFFFF); |
|
|
|
float floatValue = Float.intBitsToFloat(hexI); |
|
|
|
floats.add(floatValue); |
|
|
|
log.info("最终值是: {}", floatValue); |
|
|
|
} |
|
|
|
} else { |
|
|
|
log.info("处理数据出现异常"); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("连接Modbus设备时出现异常: {}", e.getMessage()); |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
return floats; |
|
|
|
} |
|
|
|
|
|
|
|
//获取检验单号 |
|
|
|
@Transactional |
|
|
|
@ -372,6 +322,52 @@ public class ModbusCommunicateServiceImpl implements ModbusCommunicateService { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//获取值(请求modbus获取) |
|
|
|
public List<Float> getResponseMessage(int registerAddress, int registerNum, int unitId, TCPMasterConnection connection) { |
|
|
|
ArrayList<Float> floats = new ArrayList<Float>(); |
|
|
|
try { |
|
|
|
// 创建一个Modbus TCP请求 |
|
|
|
ReadMultipleRegistersRequest request = new ReadMultipleRegistersRequest(registerAddress, registerNum); |
|
|
|
request.setUnitID(unitId); // 设置Modbus设备的Unit ID |
|
|
|
|
|
|
|
|
|
|
|
// 创建一个Modbus TCP事务 |
|
|
|
ModbusTCPTransaction transaction = new ModbusTCPTransaction(connection); |
|
|
|
transaction.setRequest(request); |
|
|
|
|
|
|
|
// 执行Modbus TCP事务 |
|
|
|
transaction.execute(); |
|
|
|
log.info("开始执行modbus tcp事物"); |
|
|
|
|
|
|
|
// 获取响应 |
|
|
|
ReadMultipleRegistersResponse response = (ReadMultipleRegistersResponse) transaction.getResponse(); |
|
|
|
log.info("打印响应的数据: {}", response.getMessage()); |
|
|
|
if (response != null) { |
|
|
|
// 处理响应数据 |
|
|
|
ArrayList<Integer> hex = new ArrayList<>(); |
|
|
|
InputRegister[] registers = response.getRegisters(); |
|
|
|
for (InputRegister register : registers) { |
|
|
|
short value = register.toShort(); |
|
|
|
int hexValue = value & 0xFFFF; |
|
|
|
log.info("获取的值是: {},value: {}", hexValue, value); |
|
|
|
hex.add(hexValue); |
|
|
|
} |
|
|
|
for (int i = 0; i < hex.size(); i += 2) { |
|
|
|
int hexI = (hex.get(i) << 16) | (hex.get(i + 1) & 0xFFFF); |
|
|
|
float floatValue = Float.intBitsToFloat(hexI); |
|
|
|
floats.add(floatValue); |
|
|
|
log.info("最终值是: {}", floatValue); |
|
|
|
} |
|
|
|
} else { |
|
|
|
log.info("处理数据出现异常"); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("连接Modbus设备时出现异常: {}", e.getMessage()); |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
return floats; |
|
|
|
} |
|
|
|
//数据清零 |
|
|
|
public void deleteForModbus(int registerAddress, int registerNum, int unitId, TCPMasterConnection connection) { |
|
|
|
try { |
|
|
|
|