diff --git a/src/main/java/com/isu/gaswellwatch/controller/DeviceController.java b/src/main/java/com/isu/gaswellwatch/controller/DeviceController.java index 6221765..79290b6 100644 --- a/src/main/java/com/isu/gaswellwatch/controller/DeviceController.java +++ b/src/main/java/com/isu/gaswellwatch/controller/DeviceController.java @@ -57,7 +57,11 @@ public class DeviceController { @PostMapping(value = "/add") @OperationLog(description = "新增设备", type = LogType.ADD) public Response add(@RequestBody @Valid DeviceCreateDTO deviceCreateDTO) { - deviceService.add(deviceCreateDTO); + try { + deviceService.add(deviceCreateDTO); + }catch (NumberFormatException e){ + throw new BusinessException("设备编号只能为纯数字格式"); + } return Response.succeed(); } @@ -89,15 +93,6 @@ public class DeviceController { return Response.succeed(deviceService.getDeviceControlData(deviceId)); } - /** - * 保存设备控制数据 - */ - @PostMapping("/saveDeviceControlData") - public Response saveDeviceControlData(@RequestBody Map controlData,@RequestParam Long deviceId) { - deviceService.saveDeviceControlData(controlData,deviceId); - return Response.succeed(); - } - /** * 获取设备历史数据 */ @@ -130,17 +125,6 @@ public class DeviceController { } } - /** - * 控制开/关井 - * - * @Param isOpen 1 开井 0 关井 - * @Param deviceId 设备id - */ - @GetMapping("/wellCtl") - public Response wellCtl(@RequestParam Integer isOpen, @RequestParam Long deviceId) { - deviceService.wellCtl(isOpen,deviceId); - return Response.succeed(); - } } diff --git a/src/main/java/com/isu/gaswellwatch/controller/DictionaryController.java b/src/main/java/com/isu/gaswellwatch/controller/DictionaryController.java index 0eef847..4b05191 100644 --- a/src/main/java/com/isu/gaswellwatch/controller/DictionaryController.java +++ b/src/main/java/com/isu/gaswellwatch/controller/DictionaryController.java @@ -1,17 +1,9 @@ package com.isu.gaswellwatch.controller; -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import com.isu.gaswellwatch.annotation.OperationLog; -import com.isu.gaswellwatch.dto.DepartmentDTO; -import com.isu.gaswellwatch.dto.DepartmentEditDTO; -import com.isu.gaswellwatch.entity.Department; import com.isu.gaswellwatch.entity.Dictionary; import com.isu.gaswellwatch.entity.Response; -import com.isu.gaswellwatch.enums.LogType; -import com.isu.gaswellwatch.service.DepartmentService; import com.isu.gaswellwatch.service.DictionaryService; -import com.isu.gaswellwatch.vo.DepartmentVO; import jakarta.annotation.Resource; import jakarta.validation.Valid; import org.springframework.web.bind.annotation.*; diff --git a/src/main/java/com/isu/gaswellwatch/controller/SummaryController.java b/src/main/java/com/isu/gaswellwatch/controller/SummaryController.java new file mode 100644 index 0000000..762c074 --- /dev/null +++ b/src/main/java/com/isu/gaswellwatch/controller/SummaryController.java @@ -0,0 +1,32 @@ +package com.isu.gaswellwatch.controller; + + +import com.isu.gaswellwatch.annotation.OperationLog; +import com.isu.gaswellwatch.dto.LoginDTO; +import com.isu.gaswellwatch.entity.Response; +import com.isu.gaswellwatch.enums.LogType; +import com.isu.gaswellwatch.service.SummaryService; +import com.isu.gaswellwatch.vo.UserLoginInfoVO; +import com.isu.gaswellwatch.vo.summary.PieSummaryVO; +import jakarta.annotation.Resource; +import jakarta.validation.Valid; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + + +@RestController +@RequestMapping("summary") +public class SummaryController { + + + @Resource + private SummaryService summaryService; + + @GetMapping("/getIndexPieSummary") + public Response> getIndexPieSummary(){ + return Response.succeed(summaryService.getIndexPieSummary()); + } + +} + diff --git a/src/main/java/com/isu/gaswellwatch/dao/DeviceDao.java b/src/main/java/com/isu/gaswellwatch/dao/DeviceDao.java index a7195bd..6ad7cac 100644 --- a/src/main/java/com/isu/gaswellwatch/dao/DeviceDao.java +++ b/src/main/java/com/isu/gaswellwatch/dao/DeviceDao.java @@ -11,6 +11,7 @@ import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; import java.util.Date; +import java.util.List; @Mapper @Repository @@ -34,5 +35,7 @@ public interface DeviceDao extends BaseMapper { @Param("startTime")Date startTime, @Param("endTime")Date endTime, @Param("deviceId") Long deviceId); + + List getDeviceVOByIds(@Param("idList")List deviceIdList); } diff --git a/src/main/java/com/isu/gaswellwatch/modbus/impl/CommandServiceImpl.java b/src/main/java/com/isu/gaswellwatch/modbus/impl/CommandServiceImpl.java index 38d496a..047fd1f 100644 --- a/src/main/java/com/isu/gaswellwatch/modbus/impl/CommandServiceImpl.java +++ b/src/main/java/com/isu/gaswellwatch/modbus/impl/CommandServiceImpl.java @@ -4,7 +4,9 @@ import com.isu.gaswellwatch.dao.DeviceDao; import com.isu.gaswellwatch.dto.modbus.ModbusCommandDto; import com.isu.gaswellwatch.entity.Response; import com.isu.gaswellwatch.modbus.CommandService; +import com.isu.gaswellwatch.service.DeviceOptLogService; import com.isu.gaswellwatch.vo.command.Command; +import jakarta.annotation.Resource; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; @@ -28,10 +30,22 @@ public class CommandServiceImpl implements CommandService { private final DeviceDao deviceDao; private final RestTemplate restTemplate = new RestTemplate(); + @Resource + private DeviceOptLogService deviceOptLogService; @Override public Response control(Command command) { - return this.sendCommand("control", command); + Response result = this.sendCommand("control", command); + + //记录用户保存控制指令日志 + Integer flag = null; + if(Command.KNPCV1_TURN_ON_THE_WELL.equals(command.getCode())){ + flag = 1; + }else if(Command.KNPCV1_TURN_OFF_THE_WELL.equals(command.getCode())){ + flag = 0; + }; + deviceOptLogService.saveGasWellOptLog(flag,command.getDeviceId()); + return result; } @Override diff --git a/src/main/java/com/isu/gaswellwatch/service/DeviceService.java b/src/main/java/com/isu/gaswellwatch/service/DeviceService.java index 9b0a250..c834134 100644 --- a/src/main/java/com/isu/gaswellwatch/service/DeviceService.java +++ b/src/main/java/com/isu/gaswellwatch/service/DeviceService.java @@ -10,6 +10,7 @@ import com.isu.gaswellwatch.vo.DeviceHistoryVO; import com.isu.gaswellwatch.vo.DeviceVO; import java.text.ParseException; +import java.util.List; import java.util.Map; public interface DeviceService extends IService { @@ -26,12 +27,10 @@ public interface DeviceService extends IService { Map getDeviceControlData(Long deviceId); - void saveDeviceControlData(Map controlData, Long deviceId); - Page getDeviceHistoryData(Integer currentPage, Integer pageSize, String startTime, String endTime, Long deviceId) throws ParseException; - void wellCtl(Integer isOpen, Long deviceId); - Page getDeviceLogData(Integer currentPage, Integer pageSize, String startTime, String endTime, Long deviceId) throws ParseException; + + List getDeviceVOByIds(List deviceIdList); } diff --git a/src/main/java/com/isu/gaswellwatch/service/SummaryService.java b/src/main/java/com/isu/gaswellwatch/service/SummaryService.java new file mode 100644 index 0000000..6877544 --- /dev/null +++ b/src/main/java/com/isu/gaswellwatch/service/SummaryService.java @@ -0,0 +1,12 @@ +package com.isu.gaswellwatch.service; + + +import com.isu.gaswellwatch.vo.summary.PieSummaryVO; + +import java.util.List; + +public interface SummaryService{ + + List getIndexPieSummary(); +} + diff --git a/src/main/java/com/isu/gaswellwatch/service/impl/DeviceServiceImpl.java b/src/main/java/com/isu/gaswellwatch/service/impl/DeviceServiceImpl.java index b8b239b..cd6bde6 100644 --- a/src/main/java/com/isu/gaswellwatch/service/impl/DeviceServiceImpl.java +++ b/src/main/java/com/isu/gaswellwatch/service/impl/DeviceServiceImpl.java @@ -12,12 +12,14 @@ import com.isu.gaswellwatch.entity.*; import com.isu.gaswellwatch.exception.BusinessException; import com.isu.gaswellwatch.modbus.data.PersistenceHandler; import com.isu.gaswellwatch.modbus.data.Redis2DBPersistenceService; +import com.isu.gaswellwatch.modbus.data.listener.DynamicRabbitListener; import com.isu.gaswellwatch.service.*; import com.isu.gaswellwatch.utils.ConverterUtil; import com.isu.gaswellwatch.vo.DeviceHistoryVO; import com.isu.gaswellwatch.vo.DeviceVO; import jakarta.annotation.Resource; import org.apache.commons.lang3.StringUtils; +import org.springframework.data.redis.RedisConnectionFailureException; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -47,6 +49,8 @@ public class DeviceServiceImpl extends ServiceImpl implements private DictionaryService dictionaryService; @Resource private DeviceOptLogService deviceOptLogService; + @Resource + private DynamicRabbitListener dynamicRabbitListener; @Override public Page page(Integer currentPage, Integer pageSize, String gasWellName, String gasStationName,Long deviceTypeId){ @@ -57,45 +61,50 @@ public class DeviceServiceImpl extends ServiceImpl implements Map runModeMap = dictionaryService.getValueMapByType("runMode"); Map plugStatusMap = dictionaryService.getValueMapByType("plugStatus"); - 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()); + 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 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 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 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 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()); + } - 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()); - } - + }catch (RedisConnectionFailureException e){ + log.error("redis连接失败,请检查redis连接"); } + } return ConverterUtil.convertPage(page, DeviceVO.class); } @Override - public void add(DeviceCreateDTO deviceCreateDTO){ + public void add(DeviceCreateDTO deviceCreateDTO) throws NumberFormatException{ //查重 List list = list(new LambdaQueryWrapper().eq(Device::getId, deviceCreateDTO.getCode())); if(CollectionUtil.isNotEmpty(list)) { @@ -117,6 +126,9 @@ public class DeviceServiceImpl extends ServiceImpl implements //在气井中绑定设备 gasWellService.bindDevice(deviceCreateDTO.getGasWell(),device.getId()); + + //创建该设备在mq中的事件队列 + dynamicRabbitListener.registerDeviceAndListener(device.getId()); } @Override @@ -126,10 +138,11 @@ public class DeviceServiceImpl extends ServiceImpl implements @Override public void delete(Long id){ + Long gasWellId = getDevice(id).getGasWell().getId(); //删除设备 - removeById(id); - //解绑气井 - gasWellService.unbindDevice(id); + removeById(id); + //解绑气井 + gasWellService.unbindDevice(gasWellId); } @Override @@ -157,15 +170,11 @@ public class DeviceServiceImpl extends ServiceImpl implements } @Override - public void saveDeviceControlData(Map controlData, Long deviceId) { - - //TODO 等待封装控制指令 - - - //记录用户保存控制指令日志 - deviceOptLogService.saveGasWellOptLog(null,deviceId); + public List getDeviceVOByIds(List deviceIdList) { + return deviceDao.getDeviceVOByIds(deviceIdList); } + @Override public Page getDeviceHistoryData(Integer currentPage, Integer pageSize, String startTime, String endTime, Long deviceId) throws ParseException { Date start = null; @@ -192,14 +201,6 @@ public class DeviceServiceImpl extends ServiceImpl implements return page; } - @Override - public void wellCtl(Integer isOpen, Long deviceId) { - //TODO 待封装控制指令 - - - //记录用户操作开关井日志 - deviceOptLogService.saveGasWellOptLog(isOpen,deviceId); - } } diff --git a/src/main/java/com/isu/gaswellwatch/service/impl/SummaryServiceImpl.java b/src/main/java/com/isu/gaswellwatch/service/impl/SummaryServiceImpl.java new file mode 100644 index 0000000..03f8c22 --- /dev/null +++ b/src/main/java/com/isu/gaswellwatch/service/impl/SummaryServiceImpl.java @@ -0,0 +1,109 @@ +package com.isu.gaswellwatch.service.impl; + +import com.isu.gaswellwatch.service.DeviceService; +import com.isu.gaswellwatch.service.SummaryService; +import com.isu.gaswellwatch.vo.DeviceVO; +import com.isu.gaswellwatch.vo.summary.PieDataVO; +import com.isu.gaswellwatch.vo.summary.PieSummaryVO; +import jakarta.annotation.Resource; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static com.isu.gaswellwatch.modbus.data.PersistenceHandler.*; + +@Service("summaryService") +@Transactional(rollbackFor = Exception.class) +public class SummaryServiceImpl implements SummaryService { + + @Resource(name = "stringRedisTemplate") + private RedisTemplate redisTemplate; + + @Resource + private DeviceService deviceService; + + + @Override + public List getIndexPieSummary() { +// Map onlineMap = redisTemplate.opsForHash().entries(ONLINE_DEVICE_CACHE); + //TODO 记得改回从缓存里取值 + Map onlineMap = new HashMap<>(); + onlineMap.put("1","true"); + onlineMap.put("2","false"); + onlineMap.put("3","true"); + onlineMap.put("4","true"); + List deviceIdList = onlineMap.keySet().stream().map(Long::parseLong).toList(); + + List deviceVOList = deviceService.getDeviceVOByIds(deviceIdList); + + //将所有设备按不同的设备品牌进行分组 + Map> deviceGroup = new HashMap<>(); + for (DeviceVO deviceVO : deviceVOList) { + if(deviceGroup.containsKey(deviceVO.getProduct().getCode())){ + deviceGroup.get(deviceVO.getProduct().getCode()).add(deviceVO); + }else { + deviceGroup.put(deviceVO.getProduct().getCode(),List.of(deviceVO)); + } + } + + List pieSummaryVOList = new ArrayList<>(); + + addKNPCDeviceSummary(KNPCV1_MODBUS_TYPE,deviceGroup, onlineMap, pieSummaryVOList,"KNPCV1"); + addKNPCDeviceSummary(ETC_MODBUS_TYPE,deviceGroup, onlineMap, pieSummaryVOList,"ETC"); + addKNPCDeviceSummary(SCSS_MODBUS_TYPE,deviceGroup, onlineMap, pieSummaryVOList,"四川双晟"); + + //计算总数 + PieSummaryVO pieSummaryVO = new PieSummaryVO(); + pieSummaryVO.setChartName("total"); + pieSummaryVO.setTitle("设备总数"); + + Integer onLineCount = 0; + Integer offLineCount = 0; + for(DeviceVO deviceVO : deviceVOList){ + if("true".equalsIgnoreCase(onlineMap.get(deviceVO.getId().toString()))){ + onLineCount++; + }else { + offLineCount++; + } + } + PieDataVO onlinePieDataVO = new PieDataVO("在线",onLineCount); + PieDataVO offlinePieDataVO = new PieDataVO("离线",offLineCount); + pieSummaryVO.setData(List.of(onlinePieDataVO,offlinePieDataVO)); + + pieSummaryVOList.add(pieSummaryVO); + + return pieSummaryVOList; + } + + private static void addKNPCDeviceSummary(String productName, Map> deviceGroup, Map onlineMap, List pieSummaryVOList, String title) { + List knpcDeviceList = deviceGroup.get(productName); + PieSummaryVO pieSummaryVO = new PieSummaryVO(); + pieSummaryVO.setChartName(productName); + pieSummaryVO.setTitle(title); + if(knpcDeviceList != null){ + //根据设备列表查找在线map中的状态进行统计在线数量 + Integer onLineCount = 0; + Integer offLineCount = 0; + for(DeviceVO deviceVO : knpcDeviceList){ + if("true".equalsIgnoreCase(onlineMap.get(deviceVO.getId().toString()))){ + onLineCount++; + }else { + offLineCount++; + } + } + PieDataVO onlinePieDataVO = new PieDataVO("在线",onLineCount); + PieDataVO offlinePieDataVO = new PieDataVO("离线",offLineCount); + pieSummaryVO.setData(List.of(onlinePieDataVO,offlinePieDataVO)); + }else{ + //无值时需要构建空值结构 + pieSummaryVO.setData(new ArrayList<>()); + } + pieSummaryVOList.add(pieSummaryVO); + } +} + diff --git a/src/main/java/com/isu/gaswellwatch/vo/summary/PieDataVO.java b/src/main/java/com/isu/gaswellwatch/vo/summary/PieDataVO.java new file mode 100644 index 0000000..2cd87fb --- /dev/null +++ b/src/main/java/com/isu/gaswellwatch/vo/summary/PieDataVO.java @@ -0,0 +1,27 @@ +package com.isu.gaswellwatch.vo.summary; + +import lombok.*; +import lombok.experimental.SuperBuilder; + +import java.io.Serializable; + +@Getter +@Setter +@SuperBuilder +@NoArgsConstructor +@EqualsAndHashCode +@ToString(callSuper = true) +public class PieDataVO implements Serializable { + + private static final long serialVersionUID = 1L; + + private Integer value; + + private String name; + + + public PieDataVO(String name, Integer value) { + this.name = name; + this.value = value; + } +} diff --git a/src/main/java/com/isu/gaswellwatch/vo/summary/PieSummaryVO.java b/src/main/java/com/isu/gaswellwatch/vo/summary/PieSummaryVO.java new file mode 100644 index 0000000..ae3d814 --- /dev/null +++ b/src/main/java/com/isu/gaswellwatch/vo/summary/PieSummaryVO.java @@ -0,0 +1,26 @@ +package com.isu.gaswellwatch.vo.summary; + +import lombok.*; +import lombok.experimental.SuperBuilder; + +import java.io.Serializable; +import java.util.List; + +@Getter +@Setter +@SuperBuilder +@NoArgsConstructor +@EqualsAndHashCode +@ToString(callSuper = true) +public class PieSummaryVO implements Serializable { + + private static final long serialVersionUID = 1L; + + private List data; + + private String chartName; + + private String title; + + +} diff --git a/src/main/resources/mapper/DeviceDao.xml b/src/main/resources/mapper/DeviceDao.xml index 2d11a46..8d97725 100644 --- a/src/main/resources/mapper/DeviceDao.xml +++ b/src/main/resources/mapper/DeviceDao.xml @@ -89,5 +89,15 @@ order by t.create_time desc + +