Browse Source

1、添加温控读取控制接口;

dev
mh 5 months ago
parent
commit
75c621581a
  1. 38
      user-service/src/main/java/com/mh/user/job/DealDataJob.java
  2. 55
      user-service/src/main/java/com/mh/user/service/impl/DeviceControlServiceImpl.java
  3. 22
      user-service/src/main/java/com/mh/user/strategy/TempControlStrategy.java
  4. 2
      user-service/src/main/resources/application-dev.yml
  5. 17
      user-service/src/main/resources/application-prod.yml
  6. 2
      user-service/src/main/resources/application.yml
  7. 41
      user-service/src/test/java/com/mh/user/DealDataTest.java

38
user-service/src/main/java/com/mh/user/job/DealDataJob.java

@ -180,25 +180,25 @@ public class DealDataJob {
} }
} }
@Scheduled(cron = "0 0 0/1 * * ?") // @Scheduled(cron = "0 0 0/1 * * ?")
public void preUseData() { // public void preUseData() {
// 每小时预测一次数据 // // 每小时预测一次数据
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); // SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(); // Date date = new Date();
String curDate = sdf1.format(date); // String curDate = sdf1.format(date);
List<BuildingModel> buildingModels = buildingService.selectBuildingName(); // List<BuildingModel> buildingModels = buildingService.selectBuildingName();
for (BuildingModel buildingModel : buildingModels) { // for (BuildingModel buildingModel : buildingModels) {
String buildingId = String.valueOf(buildingModel.getBuildingId()); // String buildingId = String.valueOf(buildingModel.getBuildingId());
try { // try {
// 训练数据 // // 训练数据
historyDataPreService.startTrainData(buildingId); // historyDataPreService.startTrainData(buildingId);
// 预测数据 // // 预测数据
historyDataPreService.startPredictData(buildingId, curDate); // historyDataPreService.startPredictData(buildingId, curDate);
} catch (Exception e) { // } catch (Exception e) {
log.error("定时处理数据以及预测数据异常==>", e); // log.error("定时处理数据以及预测数据异常==>", e);
} // }
} // }
} // }
/** /**
* 定时删除历史流水记录删除前三个月的记录 * 定时删除历史流水记录删除前三个月的记录

55
user-service/src/main/java/com/mh/user/service/impl/DeviceControlServiceImpl.java

@ -92,6 +92,10 @@ public class DeviceControlServiceImpl implements DeviceControlService {
rtData = handleTimeControl(serialPortModel, deviceCodeParam, controlData, rtData, type, serialPortSingle); rtData = handleTimeControl(serialPortModel, deviceCodeParam, controlData, rtData, type, serialPortSingle);
log.info("设备类型为时控==>{}", rtData); log.info("设备类型为时控==>{}", rtData);
break; break;
case "温控":
rtData = handleTempControl(serialPortModel, deviceCodeParam, controlData, rtData, type, serialPortSingle);
log.info("设备类型为温控==>{}", rtData);
break;
case "水位开关": case "水位开关":
rtData = handleWaterLevelSwitch(serialPortModel, deviceCodeParam, controlData, rtData, type, serialPortSingle); rtData = handleWaterLevelSwitch(serialPortModel, deviceCodeParam, controlData, rtData, type, serialPortSingle);
log.info("设备类型为水位开关==>{}", rtData); log.info("设备类型为水位开关==>{}", rtData);
@ -139,6 +143,57 @@ public class DeviceControlServiceImpl implements DeviceControlService {
} }
} }
private String handleTempControl(SerialPortModel serialPortModel, DeviceCodeParamEntity deviceCodeParam, ControlSetEntity controlData, String rtData, String type, SerialPortSingle2 serialPortSingle) {
switch (deviceCodeParam.getParam()) {
case "upperLimit":
// 上限
deviceCodeParam.setRegisterAddr("0100");
break;
case "lowerLimit":
// 下限
deviceCodeParam.setRegisterAddr("0108");
break;
case "upperUpperLimit":
// 上上限
deviceCodeParam.setRegisterAddr("0110");
break;
case "lowerLowerLimit":
// 下下限
deviceCodeParam.setRegisterAddr("0118");
break;
case "curTemp":
// 当前温度
deviceCodeParam.setRegisterAddr("0000");
break;
case "upperLimitDiff":
// 上限回差
deviceCodeParam.setRegisterAddr("0104");
break;
case "lowerLimitDiff":
// 下限回差
deviceCodeParam.setRegisterAddr("010C");
break;
case "upperUpperLimitDiff":
// 上上限回差
deviceCodeParam.setRegisterAddr("0114");
break;
case "lowerLowerLimitDiff":
// 下下限回差
deviceCodeParam.setRegisterAddr("011C");
break;
}
if (Constant.READ.equals(type)) {
deviceCodeParam.setFunCode("03"); //功能码读数据
rtData = serialPortSingle.serialPortSend(deviceCodeParam);//生成并发送指令
} else {
// 传入值,小数点保留一位,需要乘以10倍
int setValue = new BigDecimal(serialPortModel.getDataValue()).multiply(new BigDecimal(10)).intValue();
deviceCodeParam.setDataValue(String.valueOf(setValue));
deviceCodeParam.setFunCode("10"); //功能码写数据
}
return rtData;
}
private String handleElectricMeter(SerialPortModel serialPortModel, DeviceCodeParamEntity deviceCodeParam, ControlSetEntity controlData, String rtData, String type, SerialPortSingle2 serialPortSingle) { private String handleElectricMeter(SerialPortModel serialPortModel, DeviceCodeParamEntity deviceCodeParam, ControlSetEntity controlData, String rtData, String type, SerialPortSingle2 serialPortSingle) {
rtData = serialPortSingle.serialPortSend(deviceCodeParam); rtData = serialPortSingle.serialPortSend(deviceCodeParam);
return rtData; return rtData;

22
user-service/src/main/java/com/mh/user/strategy/TempControlStrategy.java

@ -64,7 +64,15 @@ public class TempControlStrategy implements DeviceStrategy {
// }else if(registerAddr.equals("")){ // }else if(registerAddr.equals("")){
// str=str+"0300000002"; // str=str+"0300000002";
// } // }
str = str + "0300000002"; String funCode = ExchangeStringUtil.addZeroForNum(deviceCodeParamEntity.getFunCode(), 2);
if ("03".equals(funCode)) {
registerAddr = ExchangeStringUtil.addZeroForNum(registerAddr, 4); //寄存器地址
str = str + funCode + registerAddr + "0002";
} else if ("10".equals(funCode)) {
str = str + funCode + registerAddr + "0002" + "04"
+ ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.decToHex(deviceCodeParamEntity.getDataValue()), 4)
+ "0001";
}
String checkWord = ExchangeStringUtil.getStrCRC16(str); //CRC16校验 String checkWord = ExchangeStringUtil.getStrCRC16(str); //CRC16校验
str = str + checkWord; str = str + checkWord;
} catch (Exception e) { } catch (Exception e) {
@ -88,11 +96,11 @@ public class TempControlStrategy implements DeviceStrategy {
if (checkStr.substring(2, 4).equalsIgnoreCase("03")) {// 读 if (checkStr.substring(2, 4).equalsIgnoreCase("03")) {// 读
data = ExchangeStringUtil.hexToDec(checkStr.substring(6, 10)); data = ExchangeStringUtil.hexToDec(checkStr.substring(6, 10));
Double fdata = Double.parseDouble(data) / 10; Double fdata = Double.parseDouble(data) / 10;
if (fdata <= 25) { // if (fdata <= 25) {
fdata = 25.0; // fdata = 25.0;
} else if (fdata >= 65) { // } else if (fdata >= 65) {
fdata = 65.0; // fdata = 65.0;
} // }
nowDataService.saveNowHistoryData2(addr, "温控", String.valueOf(fdata), "waterTemp", buildingId); nowDataService.saveNowHistoryData2(addr, "温控", String.valueOf(fdata), "waterTemp", buildingId);
nowDataService.proWaterTemp(dateStr, buildingId, "");//保存时间点温度 nowDataService.proWaterTemp(dateStr, buildingId, "");//保存时间点温度
String avgTemp = nowDataService.selectAve(buildingId); String avgTemp = nowDataService.selectAve(buildingId);
@ -104,6 +112,8 @@ public class TempControlStrategy implements DeviceStrategy {
nowPublicDataService.saveNowHistoryPublicData(publicData); nowPublicDataService.saveNowHistoryPublicData(publicData);
log.info("温控号:" + addr + ",温度值:" + fdata + ",保存数据库成功!楼栋名称:" + buildingName); log.info("温控号:" + addr + ",温度值:" + fdata + ",保存数据库成功!楼栋名称:" + buildingName);
return String.valueOf(fdata); return String.valueOf(fdata);
} else if (checkStr.substring(2, 4).equalsIgnoreCase("10")) {// 写
result = Constant.SUCCESS;
} }
return result; return result;
} }

2
user-service/src/main/resources/application-dev.yml

@ -8,7 +8,7 @@ spring:
type: com.alibaba.druid.pool.DruidDataSource type: com.alibaba.druid.pool.DruidDataSource
druid: druid:
#添加allowMultiQueries=true 在批量更新时才不会出错 #添加allowMultiQueries=true 在批量更新时才不会出错
url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=chws_gsh;allowMultiQueries=true url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=chws_hx;allowMultiQueries=true
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
username: sa username: sa
password: mh@803 password: mh@803

17
user-service/src/main/resources/application-prod.yml

@ -1,5 +1,5 @@
server: server:
port: 8763 #8761创新、8762广商、8763华厦、广州理工 port: 8762 #8761创新、8762广商、8763华厦、广州理工,华粤8762
spring: spring:
application: application:
name: user-service name: user-service
@ -14,11 +14,11 @@ spring:
# password: mh@803 # password: mh@803
## url: jdbc:sqlserver://120.25.220.177:32012;DatabaseName=M_CHWS;allowMultiQueries=true ## url: jdbc:sqlserver://120.25.220.177:32012;DatabaseName=M_CHWS;allowMultiQueries=true
#阿里云服务器-广州理工 #阿里云服务器-广州理工
url: jdbc:sqlserver://111.230.50.186:32012;DatabaseName=CHWS;allowMultiQueries=true # url: jdbc:sqlserver://111.230.50.186:32012;DatabaseName=CHWS;allowMultiQueries=true
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver # driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
username: test # username: test
password: minghan123456@ # password: minghan123456@
#华厦云服务器 # #华厦云服务器
# url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=CHWS;allowMultiQueries=true # url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=CHWS;allowMultiQueries=true
# driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver # driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
# username: chws # username: chws
@ -43,6 +43,11 @@ spring:
# driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver # driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
# username: sa # username: sa
# password: mh@803 # password: mh@803
#华粤服务器
url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=chws_hy;allowMultiQueries=true
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
username: chws_hy
password: huayue@803
filters: stat,wall,config filters: stat,wall,config
max-active: 100 max-active: 100

2
user-service/src/main/resources/application.yml

@ -1,3 +1,3 @@
spring: spring:
profiles: profiles:
active: dev active: prod

41
user-service/src/test/java/com/mh/user/DealDataTest.java

@ -3,8 +3,11 @@ package com.mh.user;
import com.mh.user.entity.EnergyEntity; import com.mh.user.entity.EnergyEntity;
import com.mh.user.mapper.EnergyMapper; import com.mh.user.mapper.EnergyMapper;
import com.mh.user.mapper.DealDataMapper; import com.mh.user.mapper.DealDataMapper;
import com.mh.user.model.BuildingModel;
import com.mh.user.service.BuildingService;
import com.mh.user.service.HistoryDataPreService; import com.mh.user.service.HistoryDataPreService;
import com.mh.user.service.NowDataService; import com.mh.user.service.NowDataService;
import com.mh.user.utils.ExchangeStringUtil;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -46,16 +49,38 @@ public class DealDataTest extends UserServiceApplicationTests {
@Autowired @Autowired
private HistoryDataPreService historyDataPreService; private HistoryDataPreService historyDataPreService;
@Autowired
private BuildingService buildingService;
@Test @Test
public void testPre() { public void testPre() {
try { // try {
// 训练数据 // // 训练数据
historyDataPreService.startTrainData("21"); // historyDataPreService.startTrainData("21");
// 预测数据 // // 预测数据
historyDataPreService.startPredictData("21", "2024-11-19"); // historyDataPreService.startPredictData("21", "2024-11-19");
} catch (Exception e) { // } catch (Exception e) {
e.printStackTrace(); // e.printStackTrace();
System.out.println(e.getMessage()); // System.out.println(e.getMessage());
// }
List<BuildingModel> buildingModels = buildingService.selectBuildingName();
for (BuildingModel buildingModel : buildingModels) {
String buildingId = String.valueOf(buildingModel.getBuildingId());
if ("21".equals(buildingId) || "24".equals(buildingId) || "25".equals(buildingId)) {
continue;
}
for (int i = 1; i < 20; i++) {
String curDate = "2024-11-" + ExchangeStringUtil.addZeroForNum(String.valueOf(i), 2);
try {
// 训练数据
historyDataPreService.startTrainData(buildingId);
// 预测数据
historyDataPreService.startPredictData(buildingId, curDate);
} catch (Exception e) {
e.printStackTrace();
}
}
} }
} }
} }

Loading…
Cancel
Save