|
|
|
@ -12,10 +12,9 @@ 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.service.EquipmentFolderLocationService; |
|
|
|
import com.xujie.sys.modules.reader.service.EquipmentFolderSortService; |
|
|
|
import com.xujie.sys.modules.reader.service.GetInformationForExcelService; |
|
|
|
import com.xujie.sys.modules.reader.service.ModbusCommunicateService; |
|
|
|
import com.xujie.sys.modules.reader.entity.EquipmentFolderSortEntity; |
|
|
|
import com.xujie.sys.modules.reader.service.*; |
|
|
|
import com.xujie.sys.modules.reader.util.CollectUtil; |
|
|
|
import com.xujie.sys.modules.report.dao.ProcedureDao; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.collections.CollectionUtils; |
|
|
|
@ -24,11 +23,14 @@ import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
|
|
|
|
import javax.sound.sampled.Port; |
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.net.InetAddress; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Objects; |
|
|
|
import java.util.concurrent.ExecutorService; |
|
|
|
import java.util.concurrent.Executors; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
@ -42,6 +44,8 @@ public class ModbusCommunicateServiceImpl implements ModbusCommunicateService { |
|
|
|
@Autowired |
|
|
|
private EquipmentFolderLocationService equipmentFolderLocationService; |
|
|
|
@Autowired |
|
|
|
private EquipmentDataDetailService equipmentDataDetailService; |
|
|
|
@Autowired |
|
|
|
private GetInformationForExcelService getInformationForExcelService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
@ -55,31 +59,33 @@ public class ModbusCommunicateServiceImpl implements ModbusCommunicateService { |
|
|
|
public void getInfoByModbus() { |
|
|
|
log.info("modbus请求开始解析"); |
|
|
|
long startTime = System.nanoTime(); |
|
|
|
EquipmentFolderLocation folderLocation = equipmentFolderLocationService.lambdaQuery().eq(EquipmentFolderLocation::getSuffix, "PLC").one(); |
|
|
|
if (folderLocation == null) { |
|
|
|
List<EquipmentFolderLocation> list = equipmentFolderLocationService.lambdaQuery().eq(EquipmentFolderLocation::getSuffix, "PLC").list(); |
|
|
|
if (list.isEmpty()){ |
|
|
|
log.error("设备消息未维护"); |
|
|
|
return; |
|
|
|
} |
|
|
|
final String MODBUS_IP = folderLocation.getIp(); |
|
|
|
if (StringUtils.isEmpty(MODBUS_IP) || "127.0.0.1".equals(MODBUS_IP)) { |
|
|
|
return; |
|
|
|
} |
|
|
|
final int MODBUS_PORT = folderLocation.getPort(); |
|
|
|
int registerAddress = folderLocation.getRegisterAddress(); |
|
|
|
int registerNum = folderLocation.getRegisterNum(); |
|
|
|
int unitId = folderLocation.getUnitId(); |
|
|
|
int registerAddressInspection = folderLocation.getRegisterAddressInspection(); |
|
|
|
int registerNumInspection = folderLocation.getRegisterNumInspection(); |
|
|
|
//获取单号 |
|
|
|
String inspectionOrderNumber = getInspectionOrderNumber(MODBUS_IP, MODBUS_PORT, registerAddressInspection, registerNumInspection, unitId).trim(); |
|
|
|
if (StringUtils.isEmpty(inspectionOrderNumber)) {//判断是空或者是寄存器是零的话就返回 |
|
|
|
return; |
|
|
|
} |
|
|
|
for (EquipmentFolderLocation folderLocation : list) { |
|
|
|
//EquipmentFolderLocation folderLocation = equipmentFolderLocationService.lambdaQuery().eq(EquipmentFolderLocation::getSuffix, "PLC").one(); |
|
|
|
final String MODBUS_IP = folderLocation.getIp(); |
|
|
|
if (StringUtils.isEmpty(MODBUS_IP) || "127.0.0.1".equals(MODBUS_IP)) { |
|
|
|
return; |
|
|
|
} |
|
|
|
final int MODBUS_PORT = folderLocation.getPort(); |
|
|
|
int registerAddress = folderLocation.getRegisterAddress(); |
|
|
|
int registerNum = folderLocation.getRegisterNum(); |
|
|
|
int unitId = folderLocation.getUnitId(); |
|
|
|
int registerAddressInspection = folderLocation.getRegisterAddressInspection(); |
|
|
|
int registerNumInspection = folderLocation.getRegisterNumInspection(); |
|
|
|
//获取单号 |
|
|
|
String inspectionOrderNumber = getInspectionOrderNumber(MODBUS_IP, MODBUS_PORT, registerAddressInspection, registerNumInspection, unitId).trim(); |
|
|
|
if (StringUtils.isEmpty(inspectionOrderNumber)) {//判断是空或者是寄存器是零的话就返回 |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
//获取对应检验单号的值 |
|
|
|
List<List<Float>> floatValues = getValue(MODBUS_IP, MODBUS_PORT, registerAddress, registerNum, unitId); |
|
|
|
//获取对应检验单号的值 |
|
|
|
List<List<Float>> floatValues = getValue(MODBUS_IP, MODBUS_PORT, registerAddress, registerNum, unitId); |
|
|
|
|
|
|
|
//获取对应的batchNo |
|
|
|
//获取对应的batchNo |
|
|
|
// LambdaQueryWrapper<EquipmentFolderSortEntity> wrapper = new LambdaQueryWrapper<>(); |
|
|
|
// wrapper.eq(EquipmentFolderSortEntity::getSite,folderLocation.getSite()); |
|
|
|
// wrapper.eq(EquipmentFolderSortEntity::getBuNo,folderLocation.getBuNo()); |
|
|
|
@ -118,29 +124,107 @@ public class ModbusCommunicateServiceImpl implements ModbusCommunicateService { |
|
|
|
// } |
|
|
|
// } |
|
|
|
|
|
|
|
int num = 1; |
|
|
|
List<EquipmentDataDetail> detailList = new ArrayList<>(); |
|
|
|
for (List<Float> aFloat : floatValues) { |
|
|
|
for (Float aFloat1 : aFloat) { |
|
|
|
EquipmentDataDetail equipmentDataDetail = new EquipmentDataDetail(); |
|
|
|
equipmentDataDetail.setInspectionNo(inspectionOrderNumber); |
|
|
|
equipmentDataDetail.setValue0(aFloat1.toString()); |
|
|
|
equipmentDataDetail.setCreateDate(new Date()); |
|
|
|
equipmentDataDetail.setSite(folderLocation.getSite()); |
|
|
|
equipmentDataDetail.setEquipmentNo(folderLocation.getEquipmentNo()); |
|
|
|
equipmentDataDetail.setBuNo(folderLocation.getBuNo()); |
|
|
|
equipmentDataDetail.setBatchNo(1); |
|
|
|
equipmentDataDetail.setNum(num); |
|
|
|
detailList.add(equipmentDataDetail); |
|
|
|
num++; |
|
|
|
int num = 1; |
|
|
|
List<EquipmentDataDetail> detailList = new ArrayList<>(); |
|
|
|
for (List<Float> aFloat : floatValues) { |
|
|
|
for (Float aFloat1 : aFloat) { |
|
|
|
EquipmentDataDetail equipmentDataDetail = new EquipmentDataDetail(); |
|
|
|
equipmentDataDetail.setInspectionNo(inspectionOrderNumber); |
|
|
|
equipmentDataDetail.setValue0(aFloat1.toString()); |
|
|
|
equipmentDataDetail.setCreateDate(new Date()); |
|
|
|
equipmentDataDetail.setSite(folderLocation.getSite()); |
|
|
|
equipmentDataDetail.setEquipmentNo(folderLocation.getEquipmentNo()); |
|
|
|
equipmentDataDetail.setBuNo(folderLocation.getBuNo()); |
|
|
|
equipmentDataDetail.setBatchNo(1); |
|
|
|
equipmentDataDetail.setNum(num); |
|
|
|
detailList.add(equipmentDataDetail); |
|
|
|
num++; |
|
|
|
} |
|
|
|
} |
|
|
|
long endTime = System.nanoTime(); |
|
|
|
long elapsedTimeInNanos = endTime - startTime; |
|
|
|
double elapsedTimeInSeconds = (double) elapsedTimeInNanos / 1_000_000_000.0; |
|
|
|
log.info("总时间: {}", elapsedTimeInSeconds); |
|
|
|
log.info("num: {}", num); |
|
|
|
saveInformation(detailList); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional |
|
|
|
public void readPerSecond() { |
|
|
|
List<EquipmentFolderLocation> list = equipmentFolderLocationService.lambdaQuery().eq(EquipmentFolderLocation::getSuffix, "RS232/RS485").list(); |
|
|
|
if (list.isEmpty()){ |
|
|
|
log.info("没有维护RS232/RS485的设备信息"); |
|
|
|
return; |
|
|
|
} |
|
|
|
for (EquipmentFolderLocation device : list) { |
|
|
|
String IP = device.getIp();//IP |
|
|
|
int PORT = device.getPort();//端口 |
|
|
|
int unitId = device.getUnitId();// 从站ID |
|
|
|
int ref = device.getRegisterAddress();// 开始地址 |
|
|
|
int count = device.getRegisterNum();//数量 |
|
|
|
int size = device.getRegisterAddressInspection();// 记录条数所在寄存器地址 |
|
|
|
String type = device.getRegisterNumColumntype();// 类型 |
|
|
|
|
|
|
|
EquipmentFolderSortEntity sort = equipmentFolderSortService.lambdaQuery() |
|
|
|
.eq(EquipmentFolderSortEntity::getSite, device.getSite()) |
|
|
|
.eq(EquipmentFolderSortEntity::getBuNo, device.getBuNo()) |
|
|
|
.eq(EquipmentFolderSortEntity::getFileNo, device.getFileNo()) |
|
|
|
.one(); |
|
|
|
log.info("IP:{},Port:{}",IP, PORT); |
|
|
|
Integer value = CollectUtil.getValue(IP, PORT, unitId, size-1); |
|
|
|
if (Objects.isNull(sort) || value == null) { |
|
|
|
continue; |
|
|
|
}else if (Integer.valueOf(sort.getBatchNo()).equals(value)){ |
|
|
|
continue; |
|
|
|
}else if (value.equals(0)){ |
|
|
|
equipmentFolderSortService.lambdaUpdate() |
|
|
|
.eq(EquipmentFolderSortEntity::getSite, device.getSite()) |
|
|
|
.eq(EquipmentFolderSortEntity::getBuNo, device.getBuNo()) |
|
|
|
.eq(EquipmentFolderSortEntity::getFileNo, device.getFileNo()) |
|
|
|
.set(EquipmentFolderSortEntity::getBatchNo,value) |
|
|
|
.update(); |
|
|
|
continue; |
|
|
|
} |
|
|
|
List<Integer> values = CollectUtil.getValues(IP, PORT, unitId, ref-1, count); |
|
|
|
List<EquipmentDataDetail> details = new ArrayList<>(); |
|
|
|
Date createDate = new Date(); |
|
|
|
if ("float".equals(type) && count % 2 == 0){ |
|
|
|
for (int i = 0; i < values.size(); i+=2) { |
|
|
|
EquipmentDataDetail detail = new EquipmentDataDetail(); |
|
|
|
detail.setSite(device.getSite()); |
|
|
|
detail.setBuNo(device.getBuNo()); |
|
|
|
detail.setFileNo(device.getFileNo()); |
|
|
|
detail.setValue0(values.get(i)+"."+values.get(i+1)); |
|
|
|
detail.setNum(value); |
|
|
|
detail.setCreateDate(createDate); |
|
|
|
detail.setEquipmentNo(device.getEquipmentNo()); |
|
|
|
details.add(detail); |
|
|
|
} |
|
|
|
}else if ("int".equals(type)){ |
|
|
|
for (int i = 0; i < values.size(); i++) { |
|
|
|
EquipmentDataDetail detail = new EquipmentDataDetail(); |
|
|
|
detail.setSite(device.getSite()); |
|
|
|
detail.setBuNo(device.getBuNo()); |
|
|
|
detail.setFileNo(device.getFileNo()); |
|
|
|
detail.setValue0(values.get(i).toString()); |
|
|
|
detail.setNum(value); |
|
|
|
detail.setCreateDate(createDate); |
|
|
|
detail.setEquipmentNo(device.getEquipmentNo()); |
|
|
|
details.add(detail); |
|
|
|
} |
|
|
|
} |
|
|
|
for (EquipmentDataDetail detail : details) { |
|
|
|
equipmentDataDetailService.save(detail); |
|
|
|
} |
|
|
|
equipmentFolderSortService.lambdaUpdate() |
|
|
|
.eq(EquipmentFolderSortEntity::getSite, device.getSite()) |
|
|
|
.eq(EquipmentFolderSortEntity::getBuNo, device.getBuNo()) |
|
|
|
.eq(EquipmentFolderSortEntity::getFileNo, device.getFileNo()) |
|
|
|
.set(EquipmentFolderSortEntity::getBatchNo,value) |
|
|
|
.update(); |
|
|
|
} |
|
|
|
long endTime = System.nanoTime(); |
|
|
|
long elapsedTimeInNanos = endTime - startTime; |
|
|
|
double elapsedTimeInSeconds = (double) elapsedTimeInNanos / 1_000_000_000.0; |
|
|
|
log.info("总时间: {}", elapsedTimeInSeconds); |
|
|
|
log.info("num: {}", num); |
|
|
|
saveInformation(detailList); |
|
|
|
} |
|
|
|
|
|
|
|
//应为modbus 寄存器每次最大请求一百多个 这里一次连接多次分多次请求 |
|
|
|
|