gasWellWatch/src/main/java/com/isu/gaswellwatch/service/impl/DeviceServiceImpl.java

208 lines
8.8 KiB
Java
Raw Normal View History

2024-11-25 01:04:53 +08:00
package com.isu.gaswellwatch.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isu.gaswellwatch.config.SnowflakeConfig;
import com.isu.gaswellwatch.dao.DeviceDao;
import com.isu.gaswellwatch.dto.DeviceCreateDTO;
import com.isu.gaswellwatch.dto.DeviceEditDTO;
import com.isu.gaswellwatch.entity.*;
2024-11-25 01:04:53 +08:00
import com.isu.gaswellwatch.exception.BusinessException;
import com.isu.gaswellwatch.modbus.data.PersistenceHandler;
2024-11-26 18:46:34 +08:00
import com.isu.gaswellwatch.modbus.data.Redis2DBPersistenceService;
2024-11-28 01:30:00 +08:00
import com.isu.gaswellwatch.modbus.data.listener.DynamicRabbitListener;
import com.isu.gaswellwatch.service.*;
2024-11-25 01:04:53 +08:00
import com.isu.gaswellwatch.utils.ConverterUtil;
import com.isu.gaswellwatch.vo.DeviceHistoryVO;
2024-11-25 15:48:00 +08:00
import com.isu.gaswellwatch.vo.DeviceVO;
2024-11-25 01:04:53 +08:00
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
2024-11-28 01:30:00 +08:00
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.data.redis.core.RedisTemplate;
2024-11-25 01:04:53 +08:00
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
2024-11-25 01:04:53 +08:00
import java.util.List;
import java.util.Map;
import java.util.Objects;
2024-11-25 01:04:53 +08:00
@Service("deviceService")
@Transactional(rollbackFor = Exception.class)
public class DeviceServiceImpl extends ServiceImpl<DeviceDao, Device> implements DeviceService {
@Resource
private SnowflakeConfig snowflakeConfig;
@Resource
private DeviceDao deviceDao;
@Resource
private GasWellService gasWellService;
@Resource(name = "stringRedisTemplate")
private RedisTemplate redisTemplate;
@Resource
private DictionaryService dictionaryService;
2024-11-26 18:46:34 +08:00
@Resource
private DeviceOptLogService deviceOptLogService;
2024-11-28 01:30:00 +08:00
@Resource
private DynamicRabbitListener dynamicRabbitListener;
2024-11-25 01:04:53 +08:00
@Override
2024-11-25 15:48:00 +08:00
public Page<DeviceVO> page(Integer currentPage, Integer pageSize, String gasWellName, String gasStationName,Long deviceTypeId){
Page<DeviceVO> page = deviceDao.page(new Page<>(currentPage, pageSize),gasWellName,gasStationName,deviceTypeId);
List<DeviceVO> deviceVOList = page.getRecords();
// 从Redis获取设备运行数据
if(CollectionUtil.isNotEmpty(deviceVOList)) {
Map<String, Dictionary> runModeMap = dictionaryService.getValueMapByType("runMode");
Map<String, Dictionary> plugStatusMap = dictionaryService.getValueMapByType("plugStatus");
2024-11-28 01:30:00 +08:00
try {
for (DeviceVO deviceVO : deviceVOList) {
String deviceKey = PersistenceHandler.DEVICE_DATA_CACHE + deviceVO.getId();
Object casPressure = redisTemplate.opsForHash().get(deviceKey, "casPressure");
deviceVO.setCasPressure(casPressure == null ? "" : casPressure.toString());
Object oilPressure = redisTemplate.opsForHash().get(deviceKey, "oilPressure");
deviceVO.setOilPressure(oilPressure == null ? "" : oilPressure.toString());
Object prePressure = redisTemplate.opsForHash().get(deviceKey, "prePressure");
deviceVO.setPrePressure(prePressure == null ? "" : prePressure.toString());
Object online = redisTemplate.opsForHash().get(deviceKey, "online");
deviceVO.setOnline(online == null ? "" : online.toString());
Object gas_status = redisTemplate.opsForHash().get(deviceKey, "gas_status");
deviceVO.setWellCtl(gas_status == null ? "" : gas_status.toString());
Object runMode = redisTemplate.opsForHash().get(deviceKey, "runMode");
if(runMode==null) {
deviceVO.setRunMode("");
}else {
Dictionary runMode1 = runModeMap.get(runMode.toString());
deviceVO.setRunMode(runMode1==null?"":runMode1.getName());
}
Object plugStatus = redisTemplate.opsForHash().get(deviceKey, "plugStatus");
if(plugStatus==null) {
deviceVO.setPlugStatus("");
}else {
Dictionary plugStatus1 = plugStatusMap.get(plugStatus.toString());
deviceVO.setPlugStatus(plugStatus1==null?"":plugStatus1.getName());
}
2024-11-26 22:51:15 +08:00
}
2024-11-28 01:30:00 +08:00
}catch (RedisConnectionFailureException e){
log.error("redis连接失败请检查redis连接");
}
2024-11-28 01:30:00 +08:00
}
2024-11-25 15:48:00 +08:00
return ConverterUtil.convertPage(page, DeviceVO.class);
2024-11-25 01:04:53 +08:00
}
@Override
2024-11-28 01:30:00 +08:00
public void add(DeviceCreateDTO deviceCreateDTO) throws NumberFormatException{
2024-11-25 01:04:53 +08:00
//查重
List<Device> list = list(new LambdaQueryWrapper<Device>().eq(Device::getId, deviceCreateDTO.getCode()));
if(CollectionUtil.isNotEmpty(list)) {
throw new BusinessException("已有相同设备编码,请重新输入");
}
2024-11-25 16:29:26 +08:00
GasWell gasWell = gasWellService.getOne(new LambdaQueryWrapper<GasWell>().eq(GasWell::getId, deviceCreateDTO.getGasWell()));
if(gasWell==null) {
throw new BusinessException("该气井不存在");
}
if(gasWell.getDeviceId()!=null) {
throw new BusinessException("该气井已存在绑定设备,无法添加新设备");
}
2024-11-25 01:04:53 +08:00
Device device = ConverterUtil.convert(deviceCreateDTO, Device.class);
//code必须为整形用于在缓存中查询设备的上报数据
device.setId(Long.valueOf(deviceCreateDTO.getCode()));
save(device);
//在气井中绑定设备
gasWellService.bindDevice(deviceCreateDTO.getGasWell(),device.getId());
2024-11-28 01:30:00 +08:00
//创建该设备在mq中的事件队列
dynamicRabbitListener.registerDeviceAndListener(device.getId());
2024-11-25 01:04:53 +08:00
}
@Override
public void edit(DeviceEditDTO deviceEditDTO){
updateById(ConverterUtil.convert(deviceEditDTO, Device.class));
}
@Override
public void delete(Long id){
2024-11-28 01:30:00 +08:00
Long gasWellId = getDevice(id).getGasWell().getId();
2024-11-25 01:04:53 +08:00
//删除设备
2024-11-28 01:30:00 +08:00
removeById(id);
//解绑气井
gasWellService.unbindDevice(gasWellId);
2024-11-25 01:04:53 +08:00
}
@Override
2024-11-25 15:48:00 +08:00
public DeviceVO getDevice(Long id) {
return deviceDao.getDeviceById(id);
2024-11-25 01:04:53 +08:00
}
@Override
public Map<String,String> getDeviceControlData(Long deviceId) {
return redisTemplate.opsForHash().entries(PersistenceHandler.DEVICE_DATA_CACHE + deviceId);
2024-11-25 01:04:53 +08:00
}
@Override
2024-11-26 18:46:34 +08:00
public Page<DeviceOptLog> getDeviceLogData(Integer currentPage, Integer pageSize, String startTime, String endTime, Long deviceId) throws ParseException {
Date start = null;
Date end = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if(!StringUtils.isEmpty(startTime) ) {
start = simpleDateFormat.parse(startTime);
}
if(!StringUtils.isEmpty(endTime)) {
end = simpleDateFormat.parse(endTime);
}
return deviceOptLogService.page(new Page<>(currentPage, pageSize),start,end,deviceId);
2024-11-25 01:04:53 +08:00
}
@Override
2024-11-28 01:30:00 +08:00
public List<DeviceVO> getDeviceVOByIds(List<Long> deviceIdList) {
return deviceDao.getDeviceVOByIds(deviceIdList);
}
2024-11-28 01:30:00 +08:00
@Override
public Page<DeviceHistoryVO> getDeviceHistoryData(Integer currentPage, Integer pageSize, String startTime, String endTime, Long deviceId) throws ParseException {
Date start = null;
Date end = null;
2024-11-26 18:46:34 +08:00
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if(!StringUtils.isEmpty(startTime) ) {
start = simpleDateFormat.parse(startTime);
}
if(!StringUtils.isEmpty(endTime)) {
end = simpleDateFormat.parse(endTime);
}
2024-11-26 18:46:34 +08:00
String tableName = Redis2DBPersistenceService.DEFAULT_DATA_TABLE +deviceId;
Page<DeviceHistoryVO> page = deviceDao.historyPage(new Page<>(currentPage, pageSize),start,end,deviceId,tableName);
List<DeviceHistoryVO> deviceHistoryVO = page.getRecords();
if(CollectionUtil.isNotEmpty(deviceHistoryVO)) {
Map<String, Dictionary> runModeMap = dictionaryService.getValueMapByType("runMode");
Map<String, Dictionary> plugStatusMap = dictionaryService.getValueMapByType("plugStatus");
for (DeviceHistoryVO deviceVO : deviceHistoryVO) {
deviceVO.setRunMode(StringUtils.isEmpty(deviceVO.getRunMode()) ?"":runModeMap.get(deviceVO.getRunMode()).getName());
deviceVO.setPlugStatus(StringUtils.isEmpty(deviceVO.getPlugStatus()) ?"":plugStatusMap.get(deviceVO.getPlugStatus()).getName());
}
}
return page;
2024-11-25 01:04:53 +08:00
}
2024-11-26 18:46:34 +08:00
2024-11-25 01:04:53 +08:00
}