9 changed files with 503 additions and 96 deletions
			
			
		@ -0,0 +1,43 @@ | 
				
			|||||||
 | 
					package com.mh.user.factory; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.mh.user.entity.DeviceCodeParamEntity; | 
				
			||||||
 | 
					import com.mh.user.strategy.DeviceStrategy; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/** | 
				
			||||||
 | 
					 * @author LJF | 
				
			||||||
 | 
					 * @version 1.0 | 
				
			||||||
 | 
					 * @project CHWS | 
				
			||||||
 | 
					 * @description 回水温度控制器 | 
				
			||||||
 | 
					 * @date 2024-03-18 16:53:35 | 
				
			||||||
 | 
					 */ | 
				
			||||||
 | 
					public class BackTempControl implements Device { | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private DeviceStrategy strategy; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private static class SingletonHolder { | 
				
			||||||
 | 
					        private static final BackTempControl INSTANCE = new BackTempControl(); | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private BackTempControl() { | 
				
			||||||
 | 
					        // 防止外部直接实例化
 | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public static BackTempControl getInstance() { | 
				
			||||||
 | 
					        return BackTempControl.SingletonHolder.INSTANCE; | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override | 
				
			||||||
 | 
					    public void setStrategy(DeviceStrategy strategy) { | 
				
			||||||
 | 
					        this.strategy = strategy; | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override | 
				
			||||||
 | 
					    public String createOrders(DeviceCodeParamEntity deviceCodeParamEntity) { | 
				
			||||||
 | 
					        return strategy.createOrders(deviceCodeParamEntity); | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override | 
				
			||||||
 | 
					    public String analysisReceiveData(String dateStr, String deviceType, String registerAddr, String brand, String buildingId, String buildingName, String dataStr) { | 
				
			||||||
 | 
					        return strategy.analysisReceiveData(dateStr, deviceType, registerAddr, brand, buildingId, buildingName, dataStr); | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					} | 
				
			||||||
@ -0,0 +1,103 @@ | 
				
			|||||||
 | 
					package com.mh.user.strategy; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.mh.user.constants.Constant; | 
				
			||||||
 | 
					import com.mh.user.entity.DeviceCodeParamEntity; | 
				
			||||||
 | 
					import com.mh.user.entity.NowPublicDataEntity; | 
				
			||||||
 | 
					import com.mh.user.service.BuildingService; | 
				
			||||||
 | 
					import com.mh.user.service.NowDataService; | 
				
			||||||
 | 
					import com.mh.user.service.NowPublicDataService; | 
				
			||||||
 | 
					import com.mh.user.utils.ExchangeStringUtil; | 
				
			||||||
 | 
					import com.mh.user.utils.SpringBeanUtil; | 
				
			||||||
 | 
					import lombok.extern.slf4j.Slf4j; | 
				
			||||||
 | 
					import org.springframework.context.ApplicationContext; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/** | 
				
			||||||
 | 
					 * @author LJF | 
				
			||||||
 | 
					 * @version 1.0 | 
				
			||||||
 | 
					 * @project CHWS | 
				
			||||||
 | 
					 * @description 回水温度控制器策略 | 
				
			||||||
 | 
					 * @date 2024-03-18 09:51:17 | 
				
			||||||
 | 
					 */ | 
				
			||||||
 | 
					@Slf4j | 
				
			||||||
 | 
					public class BackTempControlStrategy implements DeviceStrategy { | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // 调用service
 | 
				
			||||||
 | 
					    ApplicationContext context = SpringBeanUtil.getApplicationContext(); | 
				
			||||||
 | 
					    NowDataService nowDataService = context.getBean(NowDataService.class); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    NowPublicDataService nowPublicDataService = context.getBean(NowPublicDataService.class); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    BuildingService buildingService = context.getBean(BuildingService.class); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private static class SingletonHolder { | 
				
			||||||
 | 
					        private static final BackTempControlStrategy INSTANCE = new BackTempControlStrategy(); | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private BackTempControlStrategy() { | 
				
			||||||
 | 
					        // 防止外部直接实例化
 | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public static BackTempControlStrategy getInstance() { | 
				
			||||||
 | 
					        return BackTempControlStrategy.SingletonHolder.INSTANCE; | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override | 
				
			||||||
 | 
					    public String createOrders(DeviceCodeParamEntity deviceCodeParamEntity) { | 
				
			||||||
 | 
					        String deviceAddr = deviceCodeParamEntity.getDeviceAddr(); | 
				
			||||||
 | 
					        String registerAddr = deviceCodeParamEntity.getRegisterAddr(); | 
				
			||||||
 | 
					        String str = ""; | 
				
			||||||
 | 
					        if (deviceAddr != null && deviceAddr.length() > 0) { | 
				
			||||||
 | 
					            try { | 
				
			||||||
 | 
					                str = ExchangeStringUtil.decToHex(deviceAddr); | 
				
			||||||
 | 
					                str = ExchangeStringUtil.addZeroForNum(str, 2);  //以0补位
 | 
				
			||||||
 | 
					                String funCode = ExchangeStringUtil.addZeroForNum(deviceCodeParamEntity.getFunCode(), 2); | 
				
			||||||
 | 
					                if ("03".equals(funCode)) { | 
				
			||||||
 | 
					                    registerAddr = ExchangeStringUtil.addZeroForNum(registerAddr, 4); //寄存器地址
 | 
				
			||||||
 | 
					                    str = str + funCode + registerAddr + "0001"; | 
				
			||||||
 | 
					                } else if ("06".equals(funCode)) { | 
				
			||||||
 | 
					                    str = str + funCode + registerAddr | 
				
			||||||
 | 
					                            + ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.decToHex(deviceCodeParamEntity.getDataValue()), 4); | 
				
			||||||
 | 
					                } | 
				
			||||||
 | 
					                String checkWord = ExchangeStringUtil.getStrCRC16(str); //CRC16校验
 | 
				
			||||||
 | 
					                str = str + checkWord; | 
				
			||||||
 | 
					            } catch (Exception e) { | 
				
			||||||
 | 
					                log.error("生成回水温控指令出错!" + str, e); | 
				
			||||||
 | 
					            } | 
				
			||||||
 | 
					        } | 
				
			||||||
 | 
					        return str.toUpperCase(); | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override | 
				
			||||||
 | 
					    public String analysisReceiveData(String dateStr, String deviceType, String registerAddr, String brand, String buildingId, String buildingName, String dataStr) { | 
				
			||||||
 | 
					        String result = Constant.FAIL; | 
				
			||||||
 | 
					        String checkStr = dataStr.substring(0, dataStr.length() - 4); | 
				
			||||||
 | 
					        String checkWord = ExchangeStringUtil.getStrCRC16(checkStr);//生成校验码
 | 
				
			||||||
 | 
					        if (!checkWord.equalsIgnoreCase(dataStr.substring(dataStr.length() - 4))) { | 
				
			||||||
 | 
					            log.info("回水温控报文检验失败: " + dataStr); | 
				
			||||||
 | 
					            return result; | 
				
			||||||
 | 
					        } | 
				
			||||||
 | 
					        String addr = ExchangeStringUtil.hexToDec(checkStr.substring(0, 2));//地址
 | 
				
			||||||
 | 
					        String data = ""; | 
				
			||||||
 | 
					        if (checkStr.substring(2, 4).equalsIgnoreCase("03")) {// 读
 | 
				
			||||||
 | 
					            data = ExchangeStringUtil.hexToDec(checkStr.substring(6, 10)); | 
				
			||||||
 | 
					            Double fdata = Double.parseDouble(data); | 
				
			||||||
 | 
					            nowDataService.saveNowHistoryData2(addr, "回水温控", String.valueOf(fdata), "waterTemp", buildingId); | 
				
			||||||
 | 
					            nowDataService.proWaterTemp(dateStr, buildingId, "");//保存时间点温度
 | 
				
			||||||
 | 
					            String avgTemp = nowDataService.selectAve(buildingId); | 
				
			||||||
 | 
					            NowPublicDataEntity publicData = new NowPublicDataEntity(); | 
				
			||||||
 | 
					            publicData.setBuildingId(buildingId); | 
				
			||||||
 | 
					            publicData.setUseWaterTemp(avgTemp); | 
				
			||||||
 | 
					            publicData.setBackWaterTemp(avgTemp); | 
				
			||||||
 | 
					            publicData.setSingleTemp(String.valueOf(fdata));//单箱温度
 | 
				
			||||||
 | 
					            nowPublicDataService.saveNowHistoryPublicData(publicData); | 
				
			||||||
 | 
					            log.info("回水温控号:" + addr + ",温度值:" + fdata + ",保存数据库成功!楼栋名称:" + buildingName); | 
				
			||||||
 | 
					            return String.valueOf(fdata); | 
				
			||||||
 | 
					        } else if (checkStr.substring(2, 4).equalsIgnoreCase("06")) {// 写
 | 
				
			||||||
 | 
					            result = Constant.SUCCESS; | 
				
			||||||
 | 
					        } | 
				
			||||||
 | 
					        return result; | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					} | 
				
			||||||
					Loading…
					
					
				
		Reference in new issue