中央热水项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
4.8 KiB

package com.mh.user.strategy;
import com.mh.user.constants.Constant;
import com.mh.user.entity.DeviceCodeParamEntity;
import com.mh.user.service.*;
import com.mh.user.utils.ExchangeStringUtil;
import com.mh.user.utils.SpringBeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import java.text.DecimalFormat;
/**
* @author LJF
* @version 1.0
* @project CHWS
* @description 压力变送器策略
* @date 2024-03-18 09:51:17
*/
@Slf4j
public class PressureTransStrategy implements DeviceStrategy {
// 调用service
ApplicationContext context = SpringBeanUtil.getApplicationContext();
NowDataService nowDataService = context.getBean(NowDataService.class);
DeviceInstallService deviceInstallService = context.getBean(DeviceInstallService.class);
BuildingService buildingService = context.getBean(BuildingService.class);
private static class SingletonHolder {
private static final PressureTransStrategy INSTANCE = new PressureTransStrategy();
}
private PressureTransStrategy() {
// 防止外部直接实例化
}
public static PressureTransStrategy getInstance() {
return PressureTransStrategy.SingletonHolder.INSTANCE;
}
@Override
public String createOrders(DeviceCodeParamEntity deviceCodeParamEntity) {
String deviceAddr = deviceCodeParamEntity.getDeviceAddr();
String brand = deviceCodeParamEntity.getBrand();
String str = "";
if (deviceAddr != null && deviceAddr.length() > 0) {
try {
str = ExchangeStringUtil.decToHex(deviceAddr); //十进制转为十六进制
str = ExchangeStringUtil.addZeroForNum(str, 2); //以0补位
if (Constant.BRAND_AU_SUN.equals(brand)) {
str = str + "0300040001"; // 新款压力变送器
} else if (Constant.BRAND_RU_YI.equals(brand)) {
str = str + "0300040001";
} else {
str = str + "0300000002"; // 压力变送器(新的协议)
}
String checkWord = ExchangeStringUtil.getStrCRC16(str); //CRC16校验
str = str + checkWord;
} catch (Exception e) {
log.info("生成压变指令出错!" + 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 = "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")) {// 读
double wtHeight = 0.0;
if (brand == null || brand.equals("")) {
float fdata = ExchangeStringUtil.hexToSingle(checkStr.substring(6, 14));//十六进制转浮点型
wtHeight = fdata * 1.02; //通过压力求水高
} else if (Constant.BRAND_AU_SUN.equals(brand) || Constant.BRAND_RU_YI.equals(brand)) {
data = ExchangeStringUtil.hexToDec(checkStr.substring(6, 10));//十六进制转整形
wtHeight = Double.parseDouble(data) / 100 * 0.102; //通过压力求水高
}
Double tankHeight = buildingService.queryTankHeight(buildingId);//水箱高,从数据库获取
if (tankHeight == null) {
tankHeight = 2.0;
}
Double wtLevel = wtHeight / tankHeight * 100; //水箱水位
log.info("------水箱水高:" + wtLevel + "------");
if (wtLevel <= 0) {
wtLevel = 0.0;
} else if (wtLevel >= 100) {
wtLevel = 100.0;
}
DecimalFormat df = new DecimalFormat("0.0");
String strWtLevel = df.format(wtLevel);
// 更新device_install数据
deviceInstallService.updateLastValueByOther(addr, strWtLevel, "压变", buildingId);
nowDataService.saveNowHistoryData2(addr, "压变", strWtLevel, "waterLevel", buildingId);
log.info("压变号:" + addr + ",保存数据库成功!楼栋名称:" + buildingName);
nowDataService.proWaterLevel(dateStr, buildingId, addr); //楼栋水位
log.info("------保存每栋楼小时水位情况" + dateStr + "------");
result = strWtLevel;
}
return result;
}
}