8 changed files with 207 additions and 47 deletions
-
24sql/demo.sql
-
4src/main/java/com/qyft/gd/controller/SysSettingsController.java
-
14src/main/java/com/qyft/gd/model/vo/SysSettingVO.java
-
138src/main/java/com/qyft/gd/service/BaseDataService.java
-
23src/main/java/com/qyft/gd/service/CMDService.java
-
40src/main/java/com/qyft/gd/service/StatusService.java
-
1src/main/java/com/qyft/gd/service/impl/SysSettingsServiceImpl.java
-
10src/main/resources/mapper/SysSettingsMapper.xml
@ -1,57 +1,157 @@ |
|||
package com.qyft.gd.service; |
|||
|
|||
import com.qyft.gd.mapper.SysSettingsMapper; |
|||
import com.qyft.gd.model.dto.SysSettingsDTO; |
|||
import com.qyft.gd.model.vo.BaseDataVO; |
|||
import com.qyft.gd.model.vo.ContainerListVO; |
|||
import com.qyft.gd.model.vo.SysSettingVO; |
|||
import jakarta.annotation.PostConstruct; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
@Slf4j |
|||
@Service |
|||
public class BaseDataService { |
|||
private final SysSettingsService sysSettingsService; |
|||
private final ContainerService containerService; |
|||
private final SysSettingsMapper sysSettingsMapper; |
|||
private final BaseDataVO map = new BaseDataVO(); |
|||
|
|||
public BaseDataService(SysSettingsService sysSettingsService, ContainerService containerService) { |
|||
public BaseDataService(SysSettingsService sysSettingsService, ContainerService containerService, SysSettingsMapper sysSettingsMapper) { |
|||
this.sysSettingsService = sysSettingsService; |
|||
this.containerService = containerService; |
|||
this.sysSettingsMapper = sysSettingsMapper; |
|||
} |
|||
|
|||
// @PostConstruct |
|||
public void init() { |
|||
// 获取试管架某个试管坐标 |
|||
public String getTubePositionByHeatArea(int areaId, int tubeNumber) { |
|||
SysSettingVO sysSettingVO = findSysSettingById(map.getHeat_area(), areaId); |
|||
if (sysSettingVO != null) { |
|||
return sysSettingVO.getTubePositionList().get(tubeNumber - 1); |
|||
} |
|||
return null; |
|||
} |
|||
// 获取加液区某个试管坐标 |
|||
public String getTubePositionBySolutionArea(int tubeNumber) { |
|||
List<SysSettingVO> list = map.getSolution_area(); |
|||
return list.get(0).getTubePositionList().get(tubeNumber - 1); |
|||
} |
|||
|
|||
// 获取所有加热区 |
|||
public List<SysSettingVO> getAllHeatArea() { |
|||
return map.getHeat_area(); |
|||
} |
|||
|
|||
// 根据id获取加热区坐标 |
|||
public String getHeatAreaPositionById(int id) { |
|||
for (SysSettingVO sysSettingVO : map.getHeat_area()) { |
|||
if (sysSettingVO.getId() == id) { |
|||
return sysSettingVO.getValue(); |
|||
} |
|||
} |
|||
return null; // 如果没有找到,返回 null |
|||
} |
|||
|
|||
// 获取加液区坐标 |
|||
public String getSolutionAreaPosition() { |
|||
List<SysSettingVO> list = map.getSolution_area(); |
|||
if (!list.isEmpty()) { |
|||
return list.get(0).getValue(); |
|||
} |
|||
return null; // 如果没有找到,返回 null |
|||
} |
|||
|
|||
// 根据id获取加热区拍子的坐标 |
|||
public String getHeatAreaLidPositionById(int id) { |
|||
for (SysSettingVO sysSettingVO : map.getHeat_area()) { |
|||
if (sysSettingVO.getId() == id) { |
|||
return sysSettingVO.getLidPosition(); |
|||
} |
|||
} |
|||
return null; // 如果没有找到,返回 null |
|||
} |
|||
|
|||
// 获取所有基础数据 |
|||
public BaseDataVO getBaseData() { |
|||
return map; |
|||
} |
|||
|
|||
// 更新配置 |
|||
public Boolean updateConfig(List<SysSettingsDTO> dto) { |
|||
for (SysSettingsDTO sysSettingsDTO : dto) { |
|||
sysSettingsMapper.updateById(sysSettingsDTO); |
|||
} |
|||
init(); |
|||
return true; |
|||
} |
|||
|
|||
@PostConstruct |
|||
private void init() { |
|||
List<SysSettingVO> heatAreaList = sysSettingsService.getConfig("heat_area"); |
|||
List<SysSettingVO> solutionAreaList = sysSettingsService.getConfig("solution_area"); |
|||
List<SysSettingVO> lidAreaList = sysSettingsService.getConfig("lid_area"); |
|||
List<SysSettingVO> sysSettingList = sysSettingsService.getConfig("sys_setting"); |
|||
List<ContainerListVO> containerList = containerService.getAllContainer(); |
|||
for (SysSettingVO sysSettingVO : sysSettingList) { |
|||
sysSettingVO.setTemperature(0); |
|||
sysSettingVO.setCraftId(null); |
|||
} |
|||
map.setHeat_area(heatAreaList); |
|||
map.setSolution_area(solutionAreaList); |
|||
map.setLid_area(lidAreaList); |
|||
map.setSys_setting(sysSettingList); |
|||
map.setContainers(containerList); |
|||
calculateTubeCoordinates(); |
|||
log.info("初始化数据成功:{}", map); |
|||
} |
|||
public BaseDataVO getBaseData() { |
|||
return map; |
|||
|
|||
// 计算试管坐标 |
|||
private void calculateTubeCoordinates() { |
|||
List<SysSettingVO> sysSettingVOList = sysSettingsService.getConfig("sys_offset"); |
|||
Map<String, Object> offsetMap = new HashMap<>(); |
|||
for (SysSettingVO sysSettingVO : sysSettingVOList) { |
|||
offsetMap.put(sysSettingVO.getCode(), sysSettingVO.getValue()); |
|||
} |
|||
log.info("calculateTubeCoordinates offsetMap:{}", offsetMap); |
|||
calculateTubePositions(map.getHeat_area(), offsetMap, 0); |
|||
calculateTubePositions(map.getSolution_area(), offsetMap, 1); |
|||
} |
|||
public void updateBaseData(SysSettingsDTO dto) { |
|||
init(); |
|||
List<SysSettingVO> heatAreaList = map.getHeat_area(); |
|||
for (SysSettingVO sysSettingVO : heatAreaList) { |
|||
if (sysSettingVO.getId().equals(dto.getId())) { |
|||
if(dto.getTemperature() != null ) { |
|||
sysSettingVO.setTemperature(Math.toIntExact(dto.getTemperature())); |
|||
} |
|||
if (dto.getCraftId() != null) { |
|||
sysSettingVO.setCraftId(dto.getCraftId()); |
|||
// 封装计算试管位置的公共方法 |
|||
private void calculateTubePositions(List<SysSettingVO> areaList, Map<String, Object> offsetMap, int type) { |
|||
areaList.forEach(sysSettingVO -> { |
|||
List<String> tubePositionList = new ArrayList<>(); |
|||
// 试管架中心点 |
|||
String centerPosition = sysSettingVO.getValue(); |
|||
String[] split = centerPosition.split(","); |
|||
// 试管半径 |
|||
int radius = Integer.parseInt((String) offsetMap.get("sys_offset_tube_radius")); |
|||
// 圆心间距 |
|||
int distance = Integer.parseInt((String) offsetMap.get("sys_offset_tube_distance")); |
|||
// 圆心之间的水平和垂直偏移量 |
|||
int firstX = (int) (Integer.parseInt(split[0]) - 1.5 * distance); |
|||
int firstY = (int) (Integer.parseInt(split[1]) + 1.5 * distance); |
|||
// 循环计算每个圆的坐标 |
|||
for (int row = 0; row < 4 ; row++) { |
|||
for (int col = 0; col < 4; col++) { |
|||
int x = firstX + col * distance; |
|||
int y = firstY - row * distance; |
|||
tubePositionList.add(x + "," + y + ","+ offsetMap.get("sys_offset_tube_height")); |
|||
} |
|||
} |
|||
sysSettingVO.setTubePositionList(tubePositionList); |
|||
if(type == 0) { |
|||
sysSettingVO.setLidPosition(split[0] + "," + split[1] + "," + offsetMap.get("sys_offset_lid_height")); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
private SysSettingVO findSysSettingById(List<SysSettingVO> list, int id) { |
|||
for (SysSettingVO item : list) { |
|||
if (item.getId() == id) { |
|||
return item; |
|||
} |
|||
} |
|||
return null; // 如果没有找到,返回 null |
|||
} |
|||
} |
@ -0,0 +1,40 @@ |
|||
package com.qyft.gd.service; |
|||
|
|||
import com.qyft.gd.device.model.bo.DeviceStatus; |
|||
import com.qyft.gd.device.service.DeviceStateService; |
|||
import jakarta.annotation.PostConstruct; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import java.util.Timer; |
|||
import java.util.TimerTask; |
|||
|
|||
@Slf4j |
|||
@Service |
|||
public class StatusService { |
|||
|
|||
private final WebSocketService webSocketService; |
|||
private final DeviceStateService deviceStateService; |
|||
private Timer timer; |
|||
|
|||
@Autowired |
|||
public StatusService(WebSocketService webSocketService, DeviceStateService deviceStateService) { |
|||
this.webSocketService = webSocketService; |
|||
this.deviceStateService = deviceStateService; |
|||
} |
|||
|
|||
@PostConstruct |
|||
public void init() { |
|||
timer = new Timer(); |
|||
TimerTask task = new TimerTask() { |
|||
@Override |
|||
public void run() { |
|||
DeviceStatus deviceStatus = deviceStateService.getDeviceStatus(); |
|||
webSocketService.pushMsg("status", deviceStatus); |
|||
} |
|||
}; |
|||
|
|||
timer.schedule(task, 10, 100); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue