|
|
|
@ -4,11 +4,10 @@ import com.mh.user.constants.Constant; |
|
|
|
import com.mh.user.entity.CollectionParamsManageEntity; |
|
|
|
import com.mh.user.entity.CollectionParamsManageEntity; |
|
|
|
import com.mh.user.entity.DeviceInstallEntity; |
|
|
|
import com.mh.user.entity.DeviceInstallEntity; |
|
|
|
import com.mh.user.entity.GatewayManageEntity; |
|
|
|
import com.mh.user.entity.GatewayManageEntity; |
|
|
|
import com.mh.user.mapper.CollectionParamsManageMapper; |
|
|
|
import com.mh.user.mapper.*; |
|
|
|
import com.mh.user.mapper.DeviceInstallMapper; |
|
|
|
import com.mh.user.model.MultiControlModel; |
|
|
|
import com.mh.user.mapper.GatewayManageMapper; |
|
|
|
|
|
|
|
import com.mh.user.mapper.NowDataMapper; |
|
|
|
|
|
|
|
import com.mh.user.s7.S7ConnectorUtil; |
|
|
|
import com.mh.user.s7.S7ConnectorUtil; |
|
|
|
|
|
|
|
import com.mh.user.service.NowDataService; |
|
|
|
import com.mh.user.utils.DateUtil; |
|
|
|
import com.mh.user.utils.DateUtil; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.scheduling.annotation.Scheduled; |
|
|
|
import org.springframework.scheduling.annotation.Scheduled; |
|
|
|
@ -25,7 +24,7 @@ import java.util.stream.Collectors; |
|
|
|
/** |
|
|
|
/** |
|
|
|
* S7 PLC定时采集任务 |
|
|
|
* S7 PLC定时采集任务 |
|
|
|
* 支持M、VB、VW、VD等地址类型的读写操作 |
|
|
|
* 支持M、VB、VW、VD等地址类型的读写操作 |
|
|
|
* |
|
|
|
* |
|
|
|
* @author System |
|
|
|
* @author System |
|
|
|
* @date 2026-06-23 |
|
|
|
* @date 2026-06-23 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@ -40,13 +39,17 @@ public class S7PlcCollectionJob { |
|
|
|
private static final Map<String, S7ConnectorUtil> connectorCache = new ConcurrentHashMap<>(); |
|
|
|
private static final Map<String, S7ConnectorUtil> connectorCache = new ConcurrentHashMap<>(); |
|
|
|
private final DeviceInstallMapper deviceInstallMapper; |
|
|
|
private final DeviceInstallMapper deviceInstallMapper; |
|
|
|
private final NowDataMapper nowDataMapper; |
|
|
|
private final NowDataMapper nowDataMapper; |
|
|
|
|
|
|
|
private final NowDataService nowDataService; |
|
|
|
|
|
|
|
private final NowPublicDataMapper nowPublicDataMapper; |
|
|
|
|
|
|
|
|
|
|
|
public S7PlcCollectionJob(GatewayManageMapper gatewayManageMapper, |
|
|
|
public S7PlcCollectionJob(GatewayManageMapper gatewayManageMapper, |
|
|
|
CollectionParamsManageMapper collectionParamsManageMapper, DeviceInstallMapper deviceInstallMapper, NowDataMapper nowDataMapper, NowDataMapper nowDataMapper1) { |
|
|
|
CollectionParamsManageMapper collectionParamsManageMapper, DeviceInstallMapper deviceInstallMapper, NowDataMapper nowDataMapper, NowDataMapper nowDataMapper1, NowDataService nowDataService, NowPublicDataMapper nowPublicDataMapper) { |
|
|
|
this.gatewayManageMapper = gatewayManageMapper; |
|
|
|
this.gatewayManageMapper = gatewayManageMapper; |
|
|
|
this.collectionParamsManageMapper = collectionParamsManageMapper; |
|
|
|
this.collectionParamsManageMapper = collectionParamsManageMapper; |
|
|
|
this.deviceInstallMapper = deviceInstallMapper; |
|
|
|
this.deviceInstallMapper = deviceInstallMapper; |
|
|
|
this.nowDataMapper = nowDataMapper1; |
|
|
|
this.nowDataMapper = nowDataMapper1; |
|
|
|
|
|
|
|
this.nowDataService = nowDataService; |
|
|
|
|
|
|
|
this.nowPublicDataMapper = nowPublicDataMapper; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -57,7 +60,7 @@ public class S7PlcCollectionJob { |
|
|
|
@Scheduled(cron = "0 0/5 * * * ?") |
|
|
|
@Scheduled(cron = "0 0/5 * * * ?") |
|
|
|
public void collectS7Data() { |
|
|
|
public void collectS7Data() { |
|
|
|
log.info("------S7 PLC定时采集开始>>>>Constant.FLAG=={}------", Constant.PLC_FLAG); |
|
|
|
log.info("------S7 PLC定时采集开始>>>>Constant.FLAG=={}------", Constant.PLC_FLAG); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
// 检查是否有手动操作正在进行
|
|
|
|
// 检查是否有手动操作正在进行
|
|
|
|
if (Constant.PLC_FLAG || Constant.WEB_PLC_FLAG) { |
|
|
|
if (Constant.PLC_FLAG || Constant.WEB_PLC_FLAG) { |
|
|
|
@ -66,7 +69,7 @@ public class S7PlcCollectionJob { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Constant.PLC_FLAG = true; |
|
|
|
Constant.PLC_FLAG = true; |
|
|
|
|
|
|
|
|
|
|
|
// 查询所有在线的S7网关
|
|
|
|
// 查询所有在线的S7网关
|
|
|
|
List<GatewayManageEntity> s7Gateways = gatewayManageMapper.queryS7Gateways(); |
|
|
|
List<GatewayManageEntity> s7Gateways = gatewayManageMapper.queryS7Gateways(); |
|
|
|
if (s7Gateways == null || s7Gateways.isEmpty()) { |
|
|
|
if (s7Gateways == null || s7Gateways.isEmpty()) { |
|
|
|
@ -81,7 +84,7 @@ public class S7PlcCollectionJob { |
|
|
|
try { |
|
|
|
try { |
|
|
|
processGateway(gateway); |
|
|
|
processGateway(gateway); |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("处理S7网关异常: gatewayName={}, dataCom={}", |
|
|
|
log.error("处理S7网关异常: gatewayName={}, dataCom={}", |
|
|
|
gateway.getGatewayName(), gateway.getDataCom(), e); |
|
|
|
gateway.getGatewayName(), gateway.getDataCom(), e); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -136,7 +139,7 @@ public class S7PlcCollectionJob { |
|
|
|
readAndSaveData(connector, param, dateStr); |
|
|
|
readAndSaveData(connector, param, dateStr); |
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("采集点位异常: registerAddr={}, otherName={}", |
|
|
|
log.error("采集点位异常: registerAddr={}, otherName={}", |
|
|
|
param.getRegisterAddr(), param.getOtherName(), e); |
|
|
|
param.getRegisterAddr(), param.getOtherName(), e); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -148,10 +151,12 @@ public class S7PlcCollectionJob { |
|
|
|
// map值有cur_value,cur_time,通过stream判断cur_time是否是当前时间,然后cur_value如果存在一天记录等于1的,back_water_state=运行,否则back_water_state=不运行
|
|
|
|
// map值有cur_value,cur_time,通过stream判断cur_time是否是当前时间,然后cur_value如果存在一天记录等于1的,back_water_state=运行,否则back_water_state=不运行
|
|
|
|
backWaterStates.forEach(backWaterState -> { |
|
|
|
backWaterStates.forEach(backWaterState -> { |
|
|
|
if (backWaterState.get("cur_time").toString().substring(0, 10).equals(dateStr.substring(0, 10))) { |
|
|
|
if (backWaterState.get("cur_time").toString().substring(0, 10).equals(dateStr.substring(0, 10))) { |
|
|
|
if (backWaterState.get("cur_value").equals(1)) { |
|
|
|
if (new BigDecimal(backWaterState.get("cur_value").toString()).intValue() > 0) { |
|
|
|
nowDataMapper.updateBackWaterState(buildingId, "运行"); |
|
|
|
nowDataMapper.updateBackWaterState(buildingId, "1"); |
|
|
|
|
|
|
|
nowPublicDataMapper.updateBackWaterState(buildingId, "运行"); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
nowDataMapper.updateBackWaterState(buildingId, "不运行"); |
|
|
|
nowDataMapper.updateBackWaterState(buildingId, "0"); |
|
|
|
|
|
|
|
nowPublicDataMapper.updateBackWaterState(buildingId, "不运行"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
@ -161,8 +166,8 @@ public class S7PlcCollectionJob { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* 读取并保存数据 |
|
|
|
* 读取并保存数据 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void readAndSaveData(S7ConnectorUtil connector, |
|
|
|
private void readAndSaveData(S7ConnectorUtil connector, |
|
|
|
CollectionParamsManageEntity param, |
|
|
|
CollectionParamsManageEntity param, |
|
|
|
String dateStr) { |
|
|
|
String dateStr) { |
|
|
|
String registerAddr = param.getRegisterAddr(); |
|
|
|
String registerAddr = param.getRegisterAddr(); |
|
|
|
if (registerAddr == null || registerAddr.isEmpty()) { |
|
|
|
if (registerAddr == null || registerAddr.isEmpty()) { |
|
|
|
@ -190,10 +195,12 @@ public class S7PlcCollectionJob { |
|
|
|
nowDataMapper.updateUpWaterState(deviceInstallEntity.getBuildingId(), |
|
|
|
nowDataMapper.updateUpWaterState(deviceInstallEntity.getBuildingId(), |
|
|
|
deviceInstallEntity.getDeviceAddr(), |
|
|
|
deviceInstallEntity.getDeviceAddr(), |
|
|
|
"2", deviceInstallEntity.getDeviceName()); |
|
|
|
"2", deviceInstallEntity.getDeviceName()); |
|
|
|
|
|
|
|
nowPublicDataMapper.updateUpWaterState(Long.valueOf(deviceInstallEntity.getBuildingId()), "异常"); |
|
|
|
} else if (deviceInstallEntity.getDeviceType().equals("补水电磁阀")) { |
|
|
|
} else if (deviceInstallEntity.getDeviceType().equals("补水电磁阀")) { |
|
|
|
nowDataMapper.updateUseWaterState(deviceInstallEntity.getBuildingId(), |
|
|
|
nowDataMapper.updateUseWaterState(deviceInstallEntity.getBuildingId(), |
|
|
|
deviceInstallEntity.getDeviceAddr(), |
|
|
|
deviceInstallEntity.getDeviceAddr(), |
|
|
|
"2", deviceInstallEntity.getDeviceName()); |
|
|
|
"2", deviceInstallEntity.getDeviceName()); |
|
|
|
|
|
|
|
nowPublicDataMapper.updateUseWaterState(Long.valueOf(deviceInstallEntity.getBuildingId()), "异常"); |
|
|
|
} |
|
|
|
} |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -236,8 +243,8 @@ public class S7PlcCollectionJob { |
|
|
|
if (deviceInstallEntity != null) { |
|
|
|
if (deviceInstallEntity != null) { |
|
|
|
// 更新设备安装表中的now_date字段,根据param.getParamTypeId()的值进行判断 // 在对now_date进行更新
|
|
|
|
// 更新设备安装表中的now_date字段,根据param.getParamTypeId()的值进行判断 // 在对now_date进行更新
|
|
|
|
// 查询当前点位是否是运行状态、压力、液位、液位设置、回水温度、故障状态
|
|
|
|
// 查询当前点位是否是运行状态、压力、液位、液位设置、回水温度、故障状态
|
|
|
|
log.error("进入nowData设置==>{}", param.toString()); |
|
|
|
// log.error("进入nowData设置==>{}", param.toString());
|
|
|
|
switch(param.getParamTypeId()) { |
|
|
|
switch (param.getParamTypeId()) { |
|
|
|
case 2: // 运行状态
|
|
|
|
case 2: // 运行状态
|
|
|
|
nowDataMapper.updateRunState(deviceInstallEntity.getBuildingId(), |
|
|
|
nowDataMapper.updateRunState(deviceInstallEntity.getBuildingId(), |
|
|
|
deviceInstallEntity.getDeviceAddr(), |
|
|
|
deviceInstallEntity.getDeviceAddr(), |
|
|
|
@ -249,10 +256,12 @@ public class S7PlcCollectionJob { |
|
|
|
nowDataMapper.updateUpWaterState(deviceInstallEntity.getBuildingId(), |
|
|
|
nowDataMapper.updateUpWaterState(deviceInstallEntity.getBuildingId(), |
|
|
|
deviceInstallEntity.getDeviceAddr(), |
|
|
|
deviceInstallEntity.getDeviceAddr(), |
|
|
|
String.valueOf(curValue.intValue()), deviceInstallEntity.getDeviceName()); |
|
|
|
String.valueOf(curValue.intValue()), deviceInstallEntity.getDeviceName()); |
|
|
|
|
|
|
|
nowPublicDataMapper.updateUpWaterState(Long.valueOf(deviceInstallEntity.getBuildingId()), curValue.intValue() == 1 ? "运行" : "不运行"); |
|
|
|
} else if (deviceInstallEntity.getDeviceType().equals("补水电磁阀")) { |
|
|
|
} else if (deviceInstallEntity.getDeviceType().equals("补水电磁阀")) { |
|
|
|
nowDataMapper.updateUseWaterState(deviceInstallEntity.getBuildingId(), |
|
|
|
nowDataMapper.updateUseWaterState(deviceInstallEntity.getBuildingId(), |
|
|
|
deviceInstallEntity.getDeviceAddr(), |
|
|
|
deviceInstallEntity.getDeviceAddr(), |
|
|
|
String.valueOf(curValue.intValue()), deviceInstallEntity.getDeviceName()); |
|
|
|
String.valueOf(curValue.intValue()), deviceInstallEntity.getDeviceName()); |
|
|
|
|
|
|
|
nowPublicDataMapper.updateUseWaterState(Long.valueOf(deviceInstallEntity.getBuildingId()), curValue.intValue() == 1 ? "运行" : "不运行"); |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 5: // 压力
|
|
|
|
case 5: // 压力
|
|
|
|
@ -263,12 +272,17 @@ public class S7PlcCollectionJob { |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 31: // 液位
|
|
|
|
case 31: // 液位
|
|
|
|
if (param.getOtherName().contains("单箱") && deviceInstallEntity.getIsSingleBox() == 1) { |
|
|
|
if (param.getOtherName().contains("单箱")) { |
|
|
|
|
|
|
|
// 查询热泵类型的device_install以及是单箱的
|
|
|
|
|
|
|
|
deviceInstallEntity = deviceInstallMapper.selectSingleBoxDeviceInstall(deviceInstallEntity.getBuildingId(), "热泵", 1); |
|
|
|
|
|
|
|
if (deviceInstallEntity == null) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
// 更新单箱液位
|
|
|
|
// 更新单箱液位
|
|
|
|
nowDataMapper.updateBoxLevel(deviceInstallEntity.getBuildingId(), |
|
|
|
nowDataMapper.updateBoxLevel(deviceInstallEntity.getBuildingId(), |
|
|
|
null, |
|
|
|
deviceInstallEntity.getDeviceAddr(), |
|
|
|
curValue.setScale(1, RoundingMode.HALF_UP).toString(), |
|
|
|
curValue.setScale(1, RoundingMode.HALF_UP).toString(), |
|
|
|
null, 1); |
|
|
|
deviceInstallEntity.getDeviceName(), 1); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// 获取多箱的液位
|
|
|
|
// 获取多箱的液位
|
|
|
|
nowDataMapper.updateBoxLevel(deviceInstallEntity.getBuildingId(), |
|
|
|
nowDataMapper.updateBoxLevel(deviceInstallEntity.getBuildingId(), |
|
|
|
@ -278,13 +292,24 @@ public class S7PlcCollectionJob { |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 26: // 液位设置
|
|
|
|
case 26: // 液位设置
|
|
|
|
if (param.getOtherName().contains("单箱液位") && param.getOtherName().contains("上限") && deviceInstallEntity.getIsSingleBox() == 1) { |
|
|
|
if (param.getOtherName().contains("单箱液位") && param.getOtherName().contains("上限")) { |
|
|
|
|
|
|
|
// 查询热泵类型的device_install以及是单箱的
|
|
|
|
|
|
|
|
deviceInstallEntity = deviceInstallMapper.selectSingleBoxDeviceInstall(deviceInstallEntity.getBuildingId(), "热泵", 1); |
|
|
|
|
|
|
|
if (deviceInstallEntity == null) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
// 更新单箱液位
|
|
|
|
// 更新单箱液位
|
|
|
|
nowDataMapper.updateBoxLevelSet(deviceInstallEntity.getBuildingId(), |
|
|
|
nowDataMapper.updateBoxLevelSet(deviceInstallEntity.getBuildingId(), |
|
|
|
null, |
|
|
|
deviceInstallEntity.getDeviceAddr(), |
|
|
|
curValue.setScale(1, RoundingMode.HALF_UP).toString(), |
|
|
|
curValue.setScale(1, RoundingMode.HALF_UP).toString(), |
|
|
|
null, 1); |
|
|
|
deviceInstallEntity.getDeviceName(), 1); |
|
|
|
} if (param.getOtherName().contains("多箱液位") && param.getOtherName().contains("上限") && deviceInstallEntity.getIsSingleBox() == 0) { |
|
|
|
} |
|
|
|
|
|
|
|
if (param.getOtherName().contains("多箱液位") && param.getOtherName().contains("上限")) { |
|
|
|
|
|
|
|
// 查询热泵类型的device_install以及是单箱的
|
|
|
|
|
|
|
|
deviceInstallEntity = deviceInstallMapper.selectSingleBoxDeviceInstall(deviceInstallEntity.getBuildingId(), "热泵", 0); |
|
|
|
|
|
|
|
if (deviceInstallEntity == null) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
// 获取多箱的液位
|
|
|
|
// 获取多箱的液位
|
|
|
|
nowDataMapper.updateBoxLevelSet(deviceInstallEntity.getBuildingId(), |
|
|
|
nowDataMapper.updateBoxLevelSet(deviceInstallEntity.getBuildingId(), |
|
|
|
null, |
|
|
|
null, |
|
|
|
@ -301,6 +326,7 @@ public class S7PlcCollectionJob { |
|
|
|
nowDataMapper.updateBackWaterTemp(deviceInstallEntity.getBuildingId(), |
|
|
|
nowDataMapper.updateBackWaterTemp(deviceInstallEntity.getBuildingId(), |
|
|
|
null, |
|
|
|
null, |
|
|
|
curValue.setScale(1, RoundingMode.HALF_UP).toString(), null); |
|
|
|
curValue.setScale(1, RoundingMode.HALF_UP).toString(), null); |
|
|
|
|
|
|
|
nowPublicDataMapper.updateBackWaterTemp(Long.valueOf(deviceInstallEntity.getBuildingId()), curValue.setScale(1, RoundingMode.HALF_UP).toString()); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 3: // 故障状态
|
|
|
|
case 3: // 故障状态
|
|
|
|
nowDataMapper.updatePressureSet(deviceInstallEntity.getBuildingId(), |
|
|
|
nowDataMapper.updatePressureSet(deviceInstallEntity.getBuildingId(), |
|
|
|
@ -317,7 +343,7 @@ public class S7PlcCollectionJob { |
|
|
|
// 根据查询出来的deviceInstall表数据,根据device_addr和device_name值进行更新
|
|
|
|
// 根据查询出来的deviceInstall表数据,根据device_addr和device_name值进行更新
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
log.debug("采集成功: registerAddr={}, value={}, otherName={}", |
|
|
|
log.debug("采集成功: registerAddr={}, value={}, otherName={}", |
|
|
|
registerAddr, curValue, param.getOtherName()); |
|
|
|
registerAddr, curValue, param.getOtherName()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -326,7 +352,7 @@ public class S7PlcCollectionJob { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private S7ConnectorUtil getOrCreateConnector(GatewayManageEntity gateway) { |
|
|
|
private S7ConnectorUtil getOrCreateConnector(GatewayManageEntity gateway) { |
|
|
|
String cacheKey = gateway.getDataCom(); |
|
|
|
String cacheKey = gateway.getDataCom(); |
|
|
|
|
|
|
|
|
|
|
|
// 从缓存中获取
|
|
|
|
// 从缓存中获取
|
|
|
|
S7ConnectorUtil connector = connectorCache.get(cacheKey); |
|
|
|
S7ConnectorUtil connector = connectorCache.get(cacheKey); |
|
|
|
if (connector != null) { |
|
|
|
if (connector != null) { |
|
|
|
@ -369,7 +395,7 @@ public class S7PlcCollectionJob { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 手动写入数据到PLC(供Controller调用) |
|
|
|
* 手动写入数据到PLC(供Controller调用) |
|
|
|
* |
|
|
|
* |
|
|
|
* @param cpmId 采集参数ID |
|
|
|
* @param cpmId 采集参数ID |
|
|
|
* @param value 要写入的值 |
|
|
|
* @param value 要写入的值 |
|
|
|
* @return 是否成功 |
|
|
|
* @return 是否成功 |
|
|
|
@ -414,7 +440,7 @@ public class S7PlcCollectionJob { |
|
|
|
// } else {
|
|
|
|
// } else {
|
|
|
|
// curValue = new BigDecimal(value.toString());
|
|
|
|
// curValue = new BigDecimal(value.toString());
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
collectionParamsManageMapper.updateCollectionParamsManageById( |
|
|
|
collectionParamsManageMapper.updateCollectionParamsManageById( |
|
|
|
cpmId, |
|
|
|
cpmId, |
|
|
|
value.toString(), |
|
|
|
value.toString(), |
|
|
|
|