package com.mh.user.strategy; import com.alibaba.fastjson2.JSON; import com.mh.common.utils.StringUtils; import com.mh.user.constants.Constant; import com.mh.user.entity.DeviceCodeParamEntity; 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; import java.util.HashMap; import java.util.Map; /** * @author LJF * @version 1.0 * @project CHWS * @description 热泵状态 * @date 2024-03-18 09:51:17 */ @Slf4j public class HeatPumpStatusStrategy 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 HeatPumpStatusStrategy INSTANCE = new HeatPumpStatusStrategy(); } private HeatPumpStatusStrategy() { // 防止外部直接实例化 } public static HeatPumpStatusStrategy getInstance() { return HeatPumpStatusStrategy.SingletonHolder.INSTANCE; } @Override public String createOrders(DeviceCodeParamEntity deviceCodeParamEntity) { String deviceAddr = deviceCodeParamEntity.getDeviceAddr(); String brand = deviceCodeParamEntity.getBrand(); String funCode = deviceCodeParamEntity.getFunCode(); String registerAddr = deviceCodeParamEntity.getRegisterAddr(); String str = ""; if (deviceAddr != null && deviceAddr.length() > 0) { try { if (StringUtils.isBlank(brand) || Constant.BRAND_ZHONG_KAI.equals(brand)) { str = ExchangeStringUtil.decToHex(deviceAddr); str = ExchangeStringUtil.addZeroForNum(str, 2); str = "33" + str + "1700000000"; //旧版继电器 // 检验和 String checkSum = ExchangeStringUtil.makeChecksum(str); str = str + checkSum; } else if (Constant.BRAND_YUAN_XIANG.equals(brand)) { str = ExchangeStringUtil.decToHex(deviceAddr); str = ExchangeStringUtil.addZeroForNum(str, 2); str = str + "0300100008"; String checkWord = ExchangeStringUtil.getStrCRC16(str); //CRC16校验 str = str + checkWord; } else if (Constant.BRAND_DING_WEI.equals(brand)) { str = String.format("%012d", Long.parseLong(deviceAddr)); //转换位置 str = ExchangeStringUtil.changePosition(str); //表号 String strData = ""; str = "68" + str + "680102"; strData = "25C0"; //标志 // } strData = ExchangeStringUtil.addThree(strData); //加33 str = str + strData; //检验和 String checkSum = ExchangeStringUtil.makeChecksum(str);//校验 str = "FEFEFE" + str + checkSum + "16"; } } catch (Exception e) { log.error("生成热泵状态指令出错!" + str, e); } } log.info("生成热泵状态采集指令>>>>" + str); return str.toUpperCase(); } @Override public String analysisReceiveData(String dateStr, String deviceType, String registerAddr, String brand, String buildingId, String buildingName, String receiveData) { String result = Constant.FAIL; String l1 = ""; String l2 = ""; String l3 = ""; String l4 = ""; String l5 = ""; String l6 = ""; String l7 = ""; String l8 = ""; String runState = ""; String addr = ""; Map map = new HashMap(); String checkStr = receiveData.substring(0, receiveData.length() - 4); //检验报文减,去校验码 if (StringUtils.isBlank(brand) || brand.equals("顶威")) { String checkNum = ExchangeStringUtil.makeChecksum(checkStr); //生成校验码 if (checkNum.equalsIgnoreCase(receiveData.substring(receiveData.length() - 4, receiveData.length() - 2))) { //表号,12位 addr = checkStr.substring(12, 14) + checkStr.substring(10, 12) + checkStr.substring(8, 10) + checkStr.substring(6, 8) + checkStr.substring(4, 6) + checkStr.substring(2, 4); addr = addr.replaceAll("^0*", ""); String data = checkStr.substring(24, 26); //截取数值 data = ExchangeStringUtil.cutThree(data); String strStatus = ExchangeStringUtil.hexString2binaryString(data); strStatus = String.format("%08d", Long.parseLong(strStatus)); if (strStatus.charAt(7) == '1') { l1 = "1"; } else { l1 = "0"; } if (strStatus.charAt(6) == '1') { l2 = "1"; } else { l2 = "0"; } if (strStatus.charAt(5) == '1') { l3 = "1"; } else { l3 = "0"; } if (strStatus.charAt(4) == '1') { l4 = "1"; } else { l4 = "0"; } if (strStatus.charAt(3) == '1') { l5 = "1"; } else { l5 = "0"; } if (strStatus.charAt(2) == '1') { l6 = "1"; } else { l6 = "0"; } if (strStatus.charAt(1) == '1') { l7 = "1"; } else { l7 = "0"; } if (strStatus.charAt(0) == '1') { l8 = "1"; } else { l8 = "0"; } } } else if (brand.equals("中凯")) { addr = ExchangeStringUtil.hexToDec(checkStr.substring(2, 4)); l1 = checkStr.substring(4, 6); l2 = checkStr.substring(20, 22); l3 = checkStr.substring(36, 38); l4 = checkStr.substring(52, 54); l5 = checkStr.substring(68, 70); l6 = checkStr.substring(84, 86); l7 = checkStr.substring(100, 102); l8 = checkStr.substring(116, 118); } else if (brand.equals("远向")) { String checkWord = ExchangeStringUtil.getStrCRC16(checkStr); if (checkWord.equalsIgnoreCase(receiveData.substring(receiveData.length() - 4))) { addr = ExchangeStringUtil.hexToDec(checkStr.substring(0, 2));//地址 l1 = checkStr.substring(6, 10); l2 = checkStr.substring(10, 14); l3 = checkStr.substring(14, 18); l4 = checkStr.substring(18, 22); l5 = checkStr.substring(22, 26); l6 = checkStr.substring(26, 30); l7 = checkStr.substring(30, 34); l8 = checkStr.substring(34, 38); } else { log.info("热泵状态报文检验失败: " + receiveData); } } if (l1.equals("12") || l1.equals("0001")) { //正常是0001 map.put("1", "运行"); } else { map.put("1", "不运行"); } if (l2.equals("12") || l2.equals("0001")) { map.put("2", "运行"); } else { map.put("2", "不运行"); } if (l3.equals("12") || l3.equals("0001")) { map.put("3", "运行"); } else { map.put("3", "不运行"); } if (l4.equals("12") || l4.equals("0001")) { map.put("4", "运行"); } else { map.put("4", "不运行"); } if (l5.equals("12") || l5.equals("0001")) { map.put("5", "运行"); } else { map.put("5", "不运行"); } if (l6.equals("12") || l6.equals("0001")) { map.put("6", "运行"); } else { map.put("6", "不运行"); } if (l7.equals("12") || l7.equals("0001")) { map.put("7", "运行"); } else { map.put("7", "不运行"); } if (l8.equals("12") || l8.equals("0001")) { map.put("8", "运行"); } else { map.put("8", "不运行"); } int count = buildingService.selectPumpCount(buildingId); //楼栋热泵数 if (count > 0) { for (Map.Entry entry : map.entrySet()) { addr = entry.getKey(); //获取Key值作为热泵ID runState = entry.getValue(); if (addr != null && Integer.parseInt(addr) <= count) { nowDataService.saveNowHistoryData3(addr, "热泵状态", runState, "runState", buildingId); nowDataService.proPumpMinutes(buildingId, addr, runState); //计算热泵运行时长 nowDataService.proWaterTemp(dateStr, buildingId, addr); //保存时间点温度 log.info("计算热泵运行时长,楼栋名称:" + buildingName + ",热泵编号:" + addr + ",状态:" + runState); } } log.info("热泵状态号:" + addr + ",保存数据库成功!楼栋名称:" + buildingName); result = JSON.toJSONString(map); //map转json字符串 } return result; } }