diff --git a/user-service/src/main/java/com/mh/user/constants/Constant.java b/user-service/src/main/java/com/mh/user/constants/Constant.java index 3fa7d80..bd88cd1 100644 --- a/user-service/src/main/java/com/mh/user/constants/Constant.java +++ b/user-service/src/main/java/com/mh/user/constants/Constant.java @@ -13,6 +13,7 @@ public class Constant { public static final CharSequence CUSTOM_NAME_GUANGSHANG = "广州商学院"; public static final CharSequence CUSTOM_NAME_ZBSD = "北京师范大学珠海校区"; public static final CharSequence CUSTOM_NAME_HUARUAN = "广州软件学院"; + public static final CharSequence CUSTOM_NAME_LIGONG = "理工学院"; public static final String WEATHER_DATA = "weather_data"; public static final String COMMUNITY_TYPE_REAL_COM = "realCom"; public static final String COMMUNITY_TYPE_TCP = "tcp"; diff --git a/user-service/src/main/java/com/mh/user/controller/DeviceOperateController.java b/user-service/src/main/java/com/mh/user/controller/DeviceOperateController.java index a5bf6ee..cabba57 100644 --- a/user-service/src/main/java/com/mh/user/controller/DeviceOperateController.java +++ b/user-service/src/main/java/com/mh/user/controller/DeviceOperateController.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson2.JSONObject; import com.mh.common.http.HttpResult; import com.mh.common.utils.StringUtils; import com.mh.user.annotation.SysLogger; +import com.mh.user.config.MHConfig; import com.mh.user.constants.Constant; import com.mh.user.entity.ControlSetEntity; import com.mh.user.entity.DeviceCodeParamEntity; @@ -13,9 +14,11 @@ import com.mh.user.model.DeviceModel; import com.mh.user.model.SerialPortModel; import com.mh.user.serialport.SerialPortSingle2; import com.mh.user.service.*; +import com.mh.user.service.mqtt.service.IMqttGatewayService; import com.mh.user.utils.ExchangeStringUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import java.math.BigDecimal; @@ -42,6 +45,14 @@ public class DeviceOperateController { @Autowired private DeviceControlService deviceControlService; + @Autowired + private MHConfig mhConfig; + + @Value("${control.topic}") + String controlTopic; + + @Autowired + private IMqttGatewayService iMqttGatewayService; //操作设备 @SysLogger(title = "控制管理", optDesc = "设置设备参数值") @@ -49,10 +60,43 @@ public class DeviceOperateController { public HttpResult operateDevice(@RequestBody List params) { try { Constant.WEB_FLAG = true; //单抄,暂时停止采集 - Thread.sleep(1000); - String returnStr = deviceControlService.readOrWriteDevice(params, Constant.WRITE); - if (!StringUtils.isBlank(returnStr) && "fail".equals(returnStr)) { - return HttpResult.error(500, "fail"); + // 判断通讯方式 + if (deviceControlService.isMqttControl(params)) { + for (SerialPortModel serialPortModel : + params) { + String sn = deviceControlService.getSn(serialPortModel); + if (StringUtils.isBlank(sn)) { + return HttpResult.error(500, "mqtt设备未配置SN"); + } + String sendOrder = deviceControlService.operationDevice(serialPortModel); + if (Constant.FAIL.equals(sendOrder)) { + return HttpResult.error(500, "fail"); + } + String name = mhConfig.getName(); + // 获取mqtt操作队列(后期通过mqtt队列配置发送主题) + String sendTopic = name + "/" + controlTopic + "/" + sn; + log.info("发送主题:{},消息:{}", sendTopic, sendOrder); + iMqttGatewayService.publish(sendTopic, sendOrder, 1); + // 判断当前cpmId是否是 11:固定是 启用写入时间戳 + if (serialPortModel.getCpmId().equals("11")) { + // 是的话,重新写入,启用时间写入值变成 0 + serialPortModel.setDataValue("0"); + sendOrder = deviceControlService.operationDevice(serialPortModel); + if (Constant.FAIL.equals(sendOrder)) { + return HttpResult.error(500, "fail"); + } + name = mhConfig.getName(); + // 获取mqtt操作队列(后期通过mqtt队列配置发送主题) + sendTopic = name + "/" + controlTopic + "/" + sn; + log.info("发送主题:{},消息:{}", sendTopic, sendOrder); + iMqttGatewayService.publish(sendTopic, sendOrder, 1); + } + } + } else { + String returnStr = deviceControlService.readOrWriteDevice(params, Constant.WRITE); + if (!StringUtils.isBlank(returnStr) && "fail".equals(returnStr)) { + return HttpResult.error(500, "fail"); + } } Constant.WEB_FLAG = false; //单抄,恢复采集 return HttpResult.ok(); diff --git a/user-service/src/main/java/com/mh/user/dto/HotWaterBackPumpControlVO.java b/user-service/src/main/java/com/mh/user/dto/HotWaterBackPumpControlVO.java index 16769e5..4472602 100644 --- a/user-service/src/main/java/com/mh/user/dto/HotWaterBackPumpControlVO.java +++ b/user-service/src/main/java/com/mh/user/dto/HotWaterBackPumpControlVO.java @@ -40,6 +40,11 @@ public class HotWaterBackPumpControlVO { private int oneMinTimeCloseSetOne; private String oneMinTimeCloseSetOneId; + // 定时_时分开1 + private String oneHourMinTimeOpenSetOneStr; + // 定时_时分关1 + private String oneHourMinTimeCloseSetOneStr; + // 定时_时开2 private int oneHourTimeOpenSetTwo; private String oneHourTimeOpenSetTwoId; @@ -56,6 +61,11 @@ public class HotWaterBackPumpControlVO { private int oneMinTimeCloseSetTwo; private String oneMinTimeCloseSetTwoId; + // 定时_时分开2 + private String oneHourMinTimeOpenSetTwoStr; + // 定时_时分关2 + private String oneHourMinTimeCloseSetTwoStr; + // 定时_时开3 private int oneHourTimeOpenSetThree; private String oneHourTimeOpenSetThreeId; @@ -72,6 +82,11 @@ public class HotWaterBackPumpControlVO { private int oneMinTimeCloseSetThree; private String oneMinTimeCloseSetThreeId; + // 定时_时分开3 + private String oneHourMinTimeOpenSetThreeStr; + // 定时_时分关3 + private String oneHourMinTimeCloseSetThreeStr; + // 开延时 @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "0") private int openDelayTime; @@ -99,6 +114,15 @@ public class HotWaterBackPumpControlVO { private int fault; private String faultId; + // 一键启动 + private int startOneKey; + private String startOneKeyId; + + // 手自动切换 + private int manualAutoSwitch; + private String manualAutoSwitchId; + + @Override public String toString() { return new StringJoiner(", ", HotWaterBackPumpControlVO.class.getSimpleName() + "[", "]") diff --git a/user-service/src/main/java/com/mh/user/dto/HotWaterCircuitPumpControlVO.java b/user-service/src/main/java/com/mh/user/dto/HotWaterCircuitPumpControlVO.java index 82c9a4f..928a4f6 100644 --- a/user-service/src/main/java/com/mh/user/dto/HotWaterCircuitPumpControlVO.java +++ b/user-service/src/main/java/com/mh/user/dto/HotWaterCircuitPumpControlVO.java @@ -32,6 +32,9 @@ public class HotWaterCircuitPumpControlVO { private int oneMinTimeOpenSetOne; private String oneMinTimeOpenSetOneId; + // 定时_时分开1 + private String oneHourMinTimeOpenSetOneStr; + // 定时_时关1 private int oneHourTimeCloseSetOne; private String oneHourTimeCloseSetOneId; @@ -40,6 +43,9 @@ public class HotWaterCircuitPumpControlVO { private int oneMinTimeCloseSetOne; private String oneMinTimeCloseSetOneId; + // 定时_时分关1 + private String oneHourMinTimeCloseSetOneStr; + // 开延时 @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "0") private int openDelayTime; @@ -72,6 +78,14 @@ public class HotWaterCircuitPumpControlVO { private int fault; private String faultId; + // 一键启动 + private int startOneKey; + private String startOneKeyId; + + // 手自动切换 + private int manualAutoSwitch; + private String manualAutoSwitchId; + @Override public String toString() { return new StringJoiner(", ", HotWaterCircuitPumpControlVO.class.getSimpleName() + "[", "]") diff --git a/user-service/src/main/java/com/mh/user/dto/HotWaterDeviceControlVO.java b/user-service/src/main/java/com/mh/user/dto/HotWaterDeviceControlVO.java index c2b615d..d448e5d 100644 --- a/user-service/src/main/java/com/mh/user/dto/HotWaterDeviceControlVO.java +++ b/user-service/src/main/java/com/mh/user/dto/HotWaterDeviceControlVO.java @@ -30,6 +30,11 @@ public class HotWaterDeviceControlVO { private BigDecimal totalReading; private String totalReadingId; + // 当前时间 + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date currentTime; + private String currentTimeId; + @Override public String toString() { return new StringJoiner(", ", HotWaterDeviceControlVO.class.getSimpleName() + "[", "]") @@ -38,6 +43,8 @@ public class HotWaterDeviceControlVO { .add("orderNum=" + orderNum) .add("totalReading=" + totalReading) .add("totalReadingId='" + totalReadingId + "'") + .add("currentTime=" + currentTime) + .add("currentTimeId='" + currentTimeId + "'") .toString(); } } diff --git a/user-service/src/main/java/com/mh/user/dto/HotWaterHotPumpControlVO.java b/user-service/src/main/java/com/mh/user/dto/HotWaterHotPumpControlVO.java index 6ce3b74..08dca63 100644 --- a/user-service/src/main/java/com/mh/user/dto/HotWaterHotPumpControlVO.java +++ b/user-service/src/main/java/com/mh/user/dto/HotWaterHotPumpControlVO.java @@ -32,6 +32,9 @@ public class HotWaterHotPumpControlVO { // 去掉pump后的类名 private int oneMinTimeOpenSetOne; private String oneMinTimeOpenSetOneId; + // 定时_时分开1 + private String oneHourMinTimeOpenSetOneStr; + // 热泵12定时_时关1 private int oneHourTimeCloseSetOne; private String oneHourTimeCloseSetOneId; @@ -40,6 +43,9 @@ public class HotWaterHotPumpControlVO { // 去掉pump后的类名 private int oneMinTimeCloseSetOne; private String oneMinTimeCloseSetOneId; + // 定时_时分关1 + private String oneHourMinTimeCloseSetOneStr; + // 热泵12定时_时开2 private int oneHourTimeOpenSetTwo; private String oneHourTimeOpenSetTwoId; @@ -48,6 +54,9 @@ public class HotWaterHotPumpControlVO { // 去掉pump后的类名 private int oneMinTimeOpenSetTwo; private String oneMinTimeOpenSetTwoId; + // 定时_时分开2 + private String oneHourMinTimeOpenSetTwoStr; + // 热泵12定时_时关2 private int oneHourTimeCloseSetTwo; private String oneHourTimeCloseSetTwoId; @@ -56,6 +65,9 @@ public class HotWaterHotPumpControlVO { // 去掉pump后的类名 private int oneMinTimeCloseSetTwo; private String oneMinTimeCloseSetTwoId; + // 定时_时分关2 + private String oneHourMinTimeCloseSetTwoStr; + // 热泵12定时_时开3 private int oneHourTimeOpenSetThree; private String oneHourTimeOpenSetThreeId; @@ -64,6 +76,9 @@ public class HotWaterHotPumpControlVO { // 去掉pump后的类名 private int oneMinTimeOpenSetThree; private String oneMinTimeOpenSetThreeId; + // 定时_时分开3 + private String oneHourMinTimeOpenSetThreeStr; + // 热泵12定时_时关3 private int oneHourTimeCloseSetThree; private String oneHourTimeCloseSetThreeId; @@ -72,7 +87,10 @@ public class HotWaterHotPumpControlVO { // 去掉pump后的类名 private int oneMinTimeCloseSetThree; private String oneMinTimeCloseSetThreeId; - // 热泵12定时_时开1 + // 定时_时分关3 + private String oneHourMinTimeCloseSetThreeStr; + + // 热泵34定时_时开1 private int twoHourTimeOpenSetOne; private String twoHourTimeOpenSetOneId; @@ -80,6 +98,9 @@ public class HotWaterHotPumpControlVO { // 去掉pump后的类名 private int twoMinTimeOpenSetOne; private String twoMinTimeOpenSetOneId; + // 定时_时分开1 + private String twoHourMinTimeOpenSetOneStr; + // 热泵34定时_时关1 private int twoHourTimeCloseSetOne; private String twoHourTimeCloseSetOneId; @@ -88,6 +109,9 @@ public class HotWaterHotPumpControlVO { // 去掉pump后的类名 private int twoMinTimeCloseSetOne; private String twoMinTimeCloseSetOneId; + // 定时_时分关1 + private String twoHourMinTimeCloseSetOneStr; + // 热泵34定时_时开2 private int twoHourTimeOpenSetTwo; private String twoHourTimeOpenSetTwoId; @@ -96,6 +120,9 @@ public class HotWaterHotPumpControlVO { // 去掉pump后的类名 private int twoMinTimeOpenSetTwo; private String twoMinTimeOpenSetTwoId; + // 定时_时分开2 + private String twoHourMinTimeOpenSetTwoStr; + // 热泵34定时_时关2 private int twoHourTimeCloseSetTwo; private String twoHourTimeCloseSetTwoId; @@ -104,6 +131,9 @@ public class HotWaterHotPumpControlVO { // 去掉pump后的类名 private int twoMinTimeCloseSetTwo; private String twoMinTimeCloseSetTwoId; + // 定时_时分关2 + private String twoHourMinTimeCloseSetTwoStr; + // 热泵34定时_时开3 private int twoHourTimeOpenSetThree; private String twoHourTimeOpenSetThreeId; @@ -112,6 +142,9 @@ public class HotWaterHotPumpControlVO { // 去掉pump后的类名 private int twoMinTimeOpenSetThree; private String twoMinTimeOpenSetThreeId; + // 定时_时分开3 + private String twoHourMinTimeOpenSetThreeStr; + // 热泵34定时_时关3 private int twoHourTimeCloseSetThree; private String twoHourTimeCloseSetThreeId; @@ -120,6 +153,9 @@ public class HotWaterHotPumpControlVO { // 去掉pump后的类名 private int twoMinTimeCloseSetThree; private String twoMinTimeCloseSetThreeId; + // 定时_时分关3 + private String twoHourMinTimeCloseSetThreeStr; + // 热泵_开机 -> 去掉pump前缀 private String start; private String startId; @@ -136,6 +172,14 @@ public class HotWaterHotPumpControlVO { // 去掉pump后的类名 private int startStopControl; private String startStopControlId; + // 热泵_12启停控制 + private int startStopControlOne; + private String startStopControlOneId; + + // 热泵_34启停控制 + private int startStopControlTwo; + private String startStopControlTwoId; + // 热泵_设定温度 -> 去掉pump前缀 @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "0") private BigDecimal setTemp; diff --git a/user-service/src/main/java/com/mh/user/dto/HotWaterNowDataDTO.java b/user-service/src/main/java/com/mh/user/dto/HotWaterNowDataDTO.java index d82e1fc..e33cd37 100644 --- a/user-service/src/main/java/com/mh/user/dto/HotWaterNowDataDTO.java +++ b/user-service/src/main/java/com/mh/user/dto/HotWaterNowDataDTO.java @@ -48,7 +48,15 @@ public class HotWaterNowDataDTO { private String useWaterState; // 补水状态 - private String backWaterState; // 回水状态 + private String backWaterState; // 回水状态1 + + private String backWaterState2; // 回水状态2 + + private String pressure; // 压力 + + private String circulateState; // 循环泵状态1 + + private String circulateState2; // 循环泵状态2 private int orderNum; diff --git a/user-service/src/main/java/com/mh/user/dto/HotWaterSystemControlVO.java b/user-service/src/main/java/com/mh/user/dto/HotWaterSystemControlVO.java index 5aa109c..d72fa0f 100644 --- a/user-service/src/main/java/com/mh/user/dto/HotWaterSystemControlVO.java +++ b/user-service/src/main/java/com/mh/user/dto/HotWaterSystemControlVO.java @@ -50,8 +50,8 @@ public class HotWaterSystemControlVO { // PLC 时间 private String plcTime; // 秒_读 6 - private int sTimeRead; - private String sTimeReadId; + private int scTimeRead; + private String scTimeReadId; // 分_读 8 private int minTimeRead; @@ -74,8 +74,8 @@ public class HotWaterSystemControlVO { private String yearTimeReadId; // 秒_写 6 - private int sTimeSet; - private String sTimeSetId; + private int scTimeSet; + private String scTimeSetId; // 分_写 8 private int minTimeSet; @@ -115,8 +115,8 @@ public class HotWaterSystemControlVO { .add("pressure=" + pressure) .add("pressureId='" + pressureId + "'") .add("plcTime='" + plcTime + "'") - .add("sTimeRead=" + sTimeRead) - .add("sTimeReadId='" + sTimeReadId + "'") + .add("scTimeRead=" + scTimeRead) + .add("scTimeReadId='" + scTimeReadId + "'") .add("minTimeRead=" + minTimeRead) .add("minTimeReadId='" + minTimeReadId + "'") .add("hourTimeRead=" + hourTimeRead) @@ -127,8 +127,8 @@ public class HotWaterSystemControlVO { .add("monthTimeReadId='" + monthTimeReadId + "'") .add("yearTimeRead=" + yearTimeRead) .add("yearTimeReadId='" + yearTimeReadId + "'") - .add("sTimeSet=" + sTimeSet) - .add("sTimeSetId='" + sTimeSetId + "'") + .add("scTimeSet=" + scTimeSet) + .add("scTimeSetId='" + scTimeSetId + "'") .add("minTimeSet=" + minTimeSet) .add("minTimeSetId='" + minTimeSetId + "'") .add("hourTimeSet=" + hourTimeSet) diff --git a/user-service/src/main/java/com/mh/user/entity/NowDataEntity.java b/user-service/src/main/java/com/mh/user/entity/NowDataEntity.java index 9218fd3..291125d 100644 --- a/user-service/src/main/java/com/mh/user/entity/NowDataEntity.java +++ b/user-service/src/main/java/com/mh/user/entity/NowDataEntity.java @@ -23,10 +23,24 @@ public class NowDataEntity { private String tankName; //水箱名称 private String envTemp; //环境温度 - private String upWaterState; // 供水状态 + private String upWaterState; // 供水状态1 - private String useWaterState; // 补水状态 + private String useWaterState; // 补水状态1 - private String backWaterState; // 回水状态 + private String backWaterState; // 回水状态1 + + private String upWaterState2; // 供水状态2 + + private String useWaterState2; // 补水状态2 + + private String backWaterState2; // 回水状态2 + + private String circulateState; // 循环泵状态1 + + private String circulateState2; // 循环泵状态2 + + private String pressure; // 压力 + + private String gwState; // 网关状态 } diff --git a/user-service/src/main/java/com/mh/user/entity/NowPublicDataEntity.java b/user-service/src/main/java/com/mh/user/entity/NowPublicDataEntity.java index cef9f58..aaa299b 100644 --- a/user-service/src/main/java/com/mh/user/entity/NowPublicDataEntity.java +++ b/user-service/src/main/java/com/mh/user/entity/NowPublicDataEntity.java @@ -17,5 +17,6 @@ public class NowPublicDataEntity { private String useWaterTemp; //供水温度 private String backWaterTemp; //回水温度 private String singleTemp; //单箱温度 + private String pressure; //压力 } diff --git a/user-service/src/main/java/com/mh/user/job/CollectionLoopRunner.java b/user-service/src/main/java/com/mh/user/job/CollectionLoopRunner.java index 144f81f..d5d774b 100644 --- a/user-service/src/main/java/com/mh/user/job/CollectionLoopRunner.java +++ b/user-service/src/main/java/com/mh/user/job/CollectionLoopRunner.java @@ -17,6 +17,7 @@ import com.mh.user.utils.ExchangeStringUtil; import com.mh.user.utils.GetReadOrder485; import gnu.io.SerialPort; import gnu.io.SerialPortEvent; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; @@ -48,9 +49,11 @@ public class CollectionLoopRunner implements ApplicationRunner { private GetWeatherInfoJob getWeatherInfoJob; @Resource + @Autowired(required = false) private MqttSubscriptionService iMqttSubscriptionService; @Resource + @Autowired(required = false) private IMqttTopicService iMqttTopicService; @Override diff --git a/user-service/src/main/java/com/mh/user/mapper/CollectionParamsManageMapper.java b/user-service/src/main/java/com/mh/user/mapper/CollectionParamsManageMapper.java index 1f2e049..cbd6c27 100644 --- a/user-service/src/main/java/com/mh/user/mapper/CollectionParamsManageMapper.java +++ b/user-service/src/main/java/com/mh/user/mapper/CollectionParamsManageMapper.java @@ -79,10 +79,10 @@ public interface CollectionParamsManageMapper extends BaseMapper selectHotWaterByBuildingId(String buildingId); + + @Select("SELECT " + + " top 1 gm.community_type " + + "FROM " + + " device_install di " + + " JOIN collection_params_manage cpm ON di.id = cpm.device_install_id " + + " JOIN gateway_manage gm ON di.data_com = gm.data_com " + + " AND cpm.id = #{cpmId} ") + String selectCommunicationType(String cpmId); + + @Select("SELECT " + + " top 1 gm.sn " + + "FROM " + + " device_install di " + + " JOIN collection_params_manage cpm ON di.id = cpm.device_install_id " + + " JOIN gateway_manage gm ON di.data_com = gm.data_com " + + " AND cpm.id = #{cpmId} ") + String selectSn(String cpmId); + + @Select("SELECT " + + " top 1 gm.gateway_name " + + "FROM " + + " device_install di " + + " JOIN collection_params_manage cpm ON di.id = cpm.device_install_id " + + " JOIN gateway_manage gm ON di.data_com = gm.data_com " + + " AND cpm.id = #{cpmId} ") + String selectPlcName(String cpmId); + + @Select("select top 1 * from collection_params_manage where id = #{id}") + @Results({ + @Result(column = "id", property = "id"), + @Result(column = "create_time", property = "createTime"), + @Result(column = "update_time", property = "updateTime"), + @Result(column = "device_install_id", property = "deviceInstallId"), + @Result(column = "register_addr", property = "registerAddr"), + @Result(column = "func_code", property = "funcCode"), + @Result(column = "mt_ratio", property = "mtRatio"), + @Result(column = "mt_init_value", property = "mtInitValue"), + @Result(column = "digits", property = "digits"), + @Result(column = "data_type", property = "dataType"), + @Result(column = "cur_value", property = "curValue"), + @Result(column = "cur_time", property = "curTime"), + @Result(column = "mt_is_sum", property = "mtIsSum"), + @Result(column = "unit", property = "unit"), + @Result(column = "order_num", property = "orderNum"), + @Result(column = "remark", property = "remark"), + @Result(column = "register_size", property = "registerSize"), + @Result(column = "is_use", property = "isUse"), + @Result(column = "other_name", property = "otherName"), + @Result(column = "grade", property = "grade"), + @Result(column = "param_type_id", property = "paramTypeId"), + @Result(column = "collection_type", property = "collectionType"), + @Result(column = "quality", property = "quality") + }) + CollectionParamsManageEntity selectById(String id); + + @Select("select top 1 * from collection_params_manage where device_install_id = #{deviceInstallId} and param_type_id = #{paramTypeId}") + @Results({ + @Result(column = "id", property = "id"), + @Result(column = "create_time", property = "createTime"), + @Result(column = "update_time", property = "updateTime"), + @Result(column = "device_install_id", property = "deviceInstallId"), + @Result(column = "register_addr", property = "registerAddr"), + @Result(column = "func_code", property = "funcCode"), + @Result(column = "mt_ratio", property = "mtRatio"), + @Result(column = "mt_init_value", property = "mtInitValue"), + @Result(column = "digits", property = "digits"), + @Result(column = "data_type", property = "dataType"), + @Result(column = "cur_value", property = "curValue"), + @Result(column = "cur_time", property = "curTime"), + @Result(column = "mt_is_sum", property = "mtIsSum"), + @Result(column = "unit", property = "unit"), + @Result(column = "order_num", property = "orderNum"), + @Result(column = "remark", property = "remark"), + @Result(column = "register_size", property = "registerSize"), + @Result(column = "is_use", property = "isUse"), + @Result(column = "other_name", property = "otherName"), + @Result(column = "grade", property = "grade"), + @Result(column = "param_type_id", property = "paramTypeId"), + @Result(column = "collection_type", property = "collectionType"), + @Result(column = "quality", property = "quality") + }) + CollectionParamsManageEntity selectByDeviceIdAndParamTypeId(Long deviceInstallId, String paramTypeId); } diff --git a/user-service/src/main/java/com/mh/user/mapper/DeviceInstallMapper.java b/user-service/src/main/java/com/mh/user/mapper/DeviceInstallMapper.java index 6fcc077..3138824 100644 --- a/user-service/src/main/java/com/mh/user/mapper/DeviceInstallMapper.java +++ b/user-service/src/main/java/com/mh/user/mapper/DeviceInstallMapper.java @@ -49,7 +49,7 @@ public interface DeviceInstallMapper extends BaseMapper { int updateDevice(DeviceInstallEntity deviceInstallEntity); //按id更新水电表读数和采集日期 - @Update("update device_install set last_value=#{lastValue},last_date=#{lastDate} where id=#{id}") + @Update("update device_install set last_value=#{lastValue},last_date=#{lastDate}, is_online = '在线' where id=#{id}") void updateLastValue(@Param("id") Long id,@Param("lastValue") String lastValue,@Param("lastDate") Date lastDate); /** * 设备管理模块: diff --git a/user-service/src/main/java/com/mh/user/mapper/GatewayManageMapper.java b/user-service/src/main/java/com/mh/user/mapper/GatewayManageMapper.java index 125d129..508f1e2 100644 --- a/user-service/src/main/java/com/mh/user/mapper/GatewayManageMapper.java +++ b/user-service/src/main/java/com/mh/user/mapper/GatewayManageMapper.java @@ -118,4 +118,7 @@ public interface GatewayManageMapper { @Update("update gateway_manage set grade = #{grade}, connect_date = getdate() where sn = #{sn}") void updateGatewayManageOnlineBySn(String sn, int grade); + + @Select("select top 1 building_id from device_install di join gateway_manage gm on di.data_com = gm.data_com and gm.sn = #{sn}") + String queryBuildingIdBySn(String sn); } diff --git a/user-service/src/main/java/com/mh/user/model/SanShiFengDatas.java b/user-service/src/main/java/com/mh/user/model/SanShiFengDatas.java index 28e0772..9099fb5 100644 --- a/user-service/src/main/java/com/mh/user/model/SanShiFengDatas.java +++ b/user-service/src/main/java/com/mh/user/model/SanShiFengDatas.java @@ -6,11 +6,11 @@ import lombok.Data; * @author LJF * @version 1.0 * @project EEMCS - * @description 研华数据体 + * @description 三石峰数据体 * @date 2025-01-22 14:47:25 */ @Data -public class SanShiFengDatas { +public class SanShiFengDatas { /** * 对应研华的标签值 diff --git a/user-service/src/main/java/com/mh/user/model/SerialPortModel.java b/user-service/src/main/java/com/mh/user/model/SerialPortModel.java index 72bd8a3..7d2655d 100644 --- a/user-service/src/main/java/com/mh/user/model/SerialPortModel.java +++ b/user-service/src/main/java/com/mh/user/model/SerialPortModel.java @@ -35,4 +35,9 @@ public class SerialPortModel { */ private String dataValue; + /** + * 对应的采集参数id + */ + private String cpmId; + } diff --git a/user-service/src/main/java/com/mh/user/service/CollectionParamsManageService.java b/user-service/src/main/java/com/mh/user/service/CollectionParamsManageService.java index a9bfbf3..cbc40e9 100644 --- a/user-service/src/main/java/com/mh/user/service/CollectionParamsManageService.java +++ b/user-service/src/main/java/com/mh/user/service/CollectionParamsManageService.java @@ -29,9 +29,9 @@ public interface CollectionParamsManageService { int deleteByIds(String[] msIds); - void updateCPMByOtherName(String name, BigDecimal value, String time); + void updateCPMByOtherName(String name, BigDecimal value, String time, String buildingId); - CollectionParamsManageEntity selectDeviceInstallByOtherName(String name); + CollectionParamsManageEntity selectDeviceInstallByOtherName(String name, String buildingId); List monitorList(String buildingId); diff --git a/user-service/src/main/java/com/mh/user/service/DeviceControlService.java b/user-service/src/main/java/com/mh/user/service/DeviceControlService.java index 4bf4f6e..839ab06 100644 --- a/user-service/src/main/java/com/mh/user/service/DeviceControlService.java +++ b/user-service/src/main/java/com/mh/user/service/DeviceControlService.java @@ -20,4 +20,9 @@ public interface DeviceControlService { */ public String readOrWriteDevice(List params, String type); + boolean isMqttControl(List params); + + String operationDevice(SerialPortModel params); + + String getSn(SerialPortModel serialPortModel); } diff --git a/user-service/src/main/java/com/mh/user/service/GatewayManageService.java b/user-service/src/main/java/com/mh/user/service/GatewayManageService.java index e2924f7..defb992 100644 --- a/user-service/src/main/java/com/mh/user/service/GatewayManageService.java +++ b/user-service/src/main/java/com/mh/user/service/GatewayManageService.java @@ -55,4 +55,6 @@ public interface GatewayManageService { void updateGatewayManageOnlineByImei(String imei, int status); void updateGatewayManageOnlineBySn(String sn, int grade); + + String queryBuildingIdBySn(String sn); } diff --git a/user-service/src/main/java/com/mh/user/service/impl/CollectionParamsManageServiceImpl.java b/user-service/src/main/java/com/mh/user/service/impl/CollectionParamsManageServiceImpl.java index 65ad66e..d162bdd 100644 --- a/user-service/src/main/java/com/mh/user/service/impl/CollectionParamsManageServiceImpl.java +++ b/user-service/src/main/java/com/mh/user/service/impl/CollectionParamsManageServiceImpl.java @@ -10,10 +10,7 @@ import com.mh.user.service.CollectionParamsManageService; import org.springframework.stereotype.Service; import java.math.BigDecimal; -import java.util.Collections; -import java.util.Comparator; -import java.util.Date; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; /** @@ -34,6 +31,7 @@ public class CollectionParamsManageServiceImpl implements CollectionParamsManage @Override public List monitorList(String buildingId) { + // 查询nowData表中数据 return Collections.emptyList(); } @@ -45,483 +43,698 @@ public class CollectionParamsManageServiceImpl implements CollectionParamsManage } return hotWaterControlListVOS.stream() -// .peek(vo -> System.out.println(vo.getHiOrderNum())) .collect(Collectors.groupingBy(HotWaterControlListVO::getDeviceType)) .entrySet().stream() - .map(houseEntry -> { - HotWaterControlDTO dto = new HotWaterControlDTO(); - dto.setId(houseEntry.getKey()); - dto.setName(houseEntry.getValue().get(0).getDeviceType()); - dto.setOrderNum(houseEntry.getValue().get(0).getCtOrderNum()); - - List children = houseEntry.getValue().stream() - .collect(Collectors.groupingBy(HotWaterControlListVO::getDeviceId)) - .entrySet().stream() - .map(dlEntry -> { - List dlItems = dlEntry.getValue(); - switch (dto.getName()) { - case "系统": - HotWaterSystemControlVO vo = new HotWaterSystemControlVO(); - vo.setId(dlEntry.getKey()); - if (StringUtils.isBlank(dlItems.get(0).getDeviceName())) { - // 如果设备名称为空,则使用房间号 - vo.setName(dto.getName()); - } else { - vo.setName(dlItems.get(0).getDeviceName()); - } - vo.setOrderNum(dlItems.get(0).getDlOrderNum()); - // 单独对系统参数 - dlItems.forEach(item -> { - switch (item.getParamTypeId()) { - case "1": - // 启用写入时间 - vo.setTimeSet(item.getCurValue().intValue()); - vo.setTimeSetId(item.getCpmId()); - break; - case "5": - // 压力 - vo.setPressure(item.getCurValue()); - vo.setPressureId(item.getCpmId()); - break; - case "6": - // 回水温度 - vo.setInTemp(item.getCurValue()); - vo.setInTempId(item.getCpmId()); - break; - case "10": - // 水箱温度 - vo.setTankTemp(item.getCurValue()); - vo.setTankTempId(item.getCpmId()); - break; - case "19": - // 太阳能温度 - vo.setSolarTemp(item.getCurValue()); - vo.setSolarTempId(item.getCpmId()); - break; - case "15": - // 时间 - if ("年_读".equals(item.getOtherName())) { - vo.setYearTimeRead(item.getCurValue().intValue()); - vo.setYearTimeReadId(item.getCpmId()); - } else if ("月_读".equals(item.getOtherName())) { - vo.setMonthTimeRead(item.getCurValue().intValue()); - vo.setMonthTimeReadId(item.getCpmId()); - } else if ("日_读".equals(item.getOtherName())) { - vo.setDayTimeRead(item.getCurValue().intValue()); - vo.setDayTimeReadId(item.getCpmId()); - } else if ("时_读".equals(item.getOtherName())) { - vo.setHourTimeRead(item.getCurValue().intValue()); - vo.setHourTimeReadId(item.getCpmId()); - } else if ("分_读".equals(item.getOtherName())) { - vo.setMinTimeRead(item.getCurValue().intValue()); - vo.setMinTimeReadId(item.getCpmId()); - } else if ("秒_读".equals(item.getOtherName())) { - vo.setSTimeSet(item.getCurValue().intValue()); - vo.setSTimeSetId(item.getCpmId()); - } else if ("年_写".equals(item.getOtherName())) { - vo.setYearTimeSet(item.getCurValue().intValue()); - vo.setYearTimeSetId(item.getCpmId()); - } else if ("月_写".equals(item.getOtherName())) { - vo.setMonthTimeSet(item.getCurValue().intValue()); - vo.setMonthTimeSetId(item.getCpmId()); - } else if ("日_写".equals(item.getOtherName())) { - vo.setDayTimeSet(item.getCurValue().intValue()); - vo.setDayTimeSetId(item.getCpmId()); - } else if ("时_写".equals(item.getOtherName())) { - vo.setHourTimeSet(item.getCurValue().intValue()); - vo.setHourTimeSetId(item.getCpmId()); - } else if ("分_写".equals(item.getOtherName())) { - vo.setMinTimeSet(item.getCurValue().intValue()); - vo.setMinTimeSetId(item.getCpmId()); - } else if ("秒_写".equals(item.getOtherName())) { - vo.setSTimeSet(item.getCurValue().intValue()); - vo.setSTimeSetId(item.getCpmId()); - } - break; - default: - break; - } - }); - return vo; - case "热泵": - HotWaterHotPumpControlVO hotPumpVo = new HotWaterHotPumpControlVO(); // 修改类名 - hotPumpVo.setId(dlEntry.getKey()); - if (StringUtils.isBlank(dlItems.get(0).getDeviceName())) { - // 如果设备名称为空,则使用房间号 - hotPumpVo.setName(dto.getName()); - } else { - hotPumpVo.setName(dlItems.get(0).getDeviceName()); - } - hotPumpVo.setOrderNum(dlItems.get(0).getDlOrderNum()); - // 判断只有存在一个故障点,那么当前设备就是故障 - if (dlItems.stream().anyMatch(item -> ("3".equals(item.getParamTypeId()) && "1".equals(item.getCurValue().toString())))) { - hotPumpVo.setFault(1); // 去除pump前缀 - } - // 单独对系统参数 - dlItems.forEach(item -> { - switch (item.getParamTypeId()) { - case "4": - // 定时开关 - if (item.getOtherName().contains("12定时_时开1")) { - hotPumpVo.setOneHourTimeOpenSetOne(item.getCurValue().intValue()); - hotPumpVo.setOneHourTimeOpenSetOneId(item.getCpmId()); - } else if (item.getOtherName().contains("12定时_时关1")) { - hotPumpVo.setOneHourTimeCloseSetOne(item.getCurValue().intValue()); - hotPumpVo.setOneHourTimeCloseSetOneId(item.getCpmId()); - } else if (item.getOtherName().contains("12定时_时开2")) { - hotPumpVo.setOneHourTimeOpenSetTwo(item.getCurValue().intValue()); - hotPumpVo.setOneHourTimeOpenSetTwoId(item.getCpmId()); - } else if (item.getOtherName().contains("12定时_时关2")) { - hotPumpVo.setOneHourTimeCloseSetTwo(item.getCurValue().intValue()); - hotPumpVo.setOneHourTimeCloseSetTwoId(item.getCpmId()); - } else if (item.getOtherName().contains("12定时_时开3")) { - hotPumpVo.setOneHourTimeOpenSetThree(item.getCurValue().intValue()); - hotPumpVo.setOneHourTimeOpenSetThreeId(item.getCpmId()); - } else if (item.getOtherName().contains("12定时_时关3")) { - hotPumpVo.setOneHourTimeCloseSetThree(item.getCurValue().intValue()); - hotPumpVo.setOneHourTimeCloseSetThreeId(item.getCpmId()); - } else if (item.getOtherName().contains("12定时_分开1")) { // 新增分钟设置 - hotPumpVo.setOneMinTimeOpenSetOne(item.getCurValue().intValue()); - hotPumpVo.setOneMinTimeOpenSetOneId(item.getCpmId()); - } else if (item.getOtherName().contains("12定时_分关1")) { // 新增分钟设置 - hotPumpVo.setOneMinTimeCloseSetOne(item.getCurValue().intValue()); - hotPumpVo.setOneMinTimeCloseSetOneId(item.getCpmId()); - } else if (item.getOtherName().contains("12定时_分开2")) { // 新增分钟设置 - hotPumpVo.setOneMinTimeOpenSetTwo(item.getCurValue().intValue()); - hotPumpVo.setOneMinTimeOpenSetTwoId(item.getCpmId()); - } else if (item.getOtherName().contains("12定时_分关2")) { // 新增分钟设置 - hotPumpVo.setOneMinTimeCloseSetTwo(item.getCurValue().intValue()); - hotPumpVo.setOneMinTimeCloseSetTwoId(item.getCpmId()); - } else if (item.getOtherName().contains("12定时_分开3")) { // 新增分钟设置 - hotPumpVo.setOneMinTimeOpenSetThree(item.getCurValue().intValue()); - hotPumpVo.setOneMinTimeOpenSetThreeId(item.getCpmId()); - } else if (item.getOtherName().contains("12定时_分关3")) { // 新增分钟设置 - hotPumpVo.setOneMinTimeCloseSetThree(item.getCurValue().intValue()); - hotPumpVo.setOneMinTimeCloseSetThreeId(item.getCpmId()); - } else if (item.getOtherName().contains("34定时_时开1")) { - hotPumpVo.setTwoHourTimeOpenSetOne(item.getCurValue().intValue()); - hotPumpVo.setTwoHourTimeOpenSetOneId(item.getCpmId()); - } else if (item.getOtherName().contains("34定时_时关1")) { - hotPumpVo.setTwoHourTimeCloseSetOne(item.getCurValue().intValue()); - hotPumpVo.setTwoHourTimeCloseSetOneId(item.getCpmId()); - } else if (item.getOtherName().contains("34定时_时开2")) { - hotPumpVo.setTwoHourTimeOpenSetTwo(item.getCurValue().intValue()); - hotPumpVo.setTwoHourTimeOpenSetTwoId(item.getCpmId()); - } else if (item.getOtherName().contains("34定时_时关2")) { - hotPumpVo.setTwoHourTimeCloseSetTwo(item.getCurValue().intValue()); - hotPumpVo.setTwoHourTimeCloseSetTwoId(item.getCpmId()); - } else if (item.getOtherName().contains("34定时_时开3")) { - hotPumpVo.setTwoHourTimeOpenSetThree(item.getCurValue().intValue()); - hotPumpVo.setTwoHourTimeOpenSetThreeId(item.getCpmId()); - } else if (item.getOtherName().contains("34定时_时关3")) { - hotPumpVo.setTwoHourTimeCloseSetThree(item.getCurValue().intValue()); - hotPumpVo.setTwoHourTimeCloseSetThreeId(item.getCpmId()); - } else if (item.getOtherName().contains("34定时_分开1")) { // 新增分钟设置 - hotPumpVo.setTwoMinTimeOpenSetOne(item.getCurValue().intValue()); - hotPumpVo.setTwoMinTimeOpenSetOneId(item.getCpmId()); - } else if (item.getOtherName().contains("34定时_分关1")) { // 新增分钟设置 - hotPumpVo.setTwoMinTimeCloseSetOne(item.getCurValue().intValue()); - hotPumpVo.setTwoMinTimeCloseSetOneId(item.getCpmId()); - } else if (item.getOtherName().contains("34定时_分开2")) { // 新增分钟设置 - hotPumpVo.setTwoMinTimeOpenSetTwo(item.getCurValue().intValue()); - hotPumpVo.setTwoMinTimeOpenSetTwoId(item.getCpmId()); - } else if (item.getOtherName().contains("34定时_分关2")) { // 新增分钟设置 - hotPumpVo.setTwoMinTimeCloseSetTwo(item.getCurValue().intValue()); - hotPumpVo.setTwoMinTimeCloseSetTwoId(item.getCpmId()); - } else if (item.getOtherName().contains("34定时_分开3")) { // 新增分钟设置 - hotPumpVo.setTwoMinTimeOpenSetThree(item.getCurValue().intValue()); - hotPumpVo.setTwoMinTimeOpenSetThreeId(item.getCpmId()); - } else if (item.getOtherName().contains("34定时_分关3")) { // 新增分钟设置 - hotPumpVo.setTwoMinTimeCloseSetThree(item.getCurValue().intValue()); - hotPumpVo.setTwoMinTimeCloseSetThreeId(item.getCpmId()); - } - break; - case "1": - // 启停控制 - hotPumpVo.setStartStopControl(item.getCurValue().intValue()); // 去除pump前缀 - hotPumpVo.setStartStopControlId(item.getCpmId()); // 去除pump前缀 - break; - case "2": - // 运行状态 - hotPumpVo.setRunState(item.getCurValue().intValue()); // 去除pump前缀 - hotPumpVo.setRunStateId(item.getCpmId()); // 去除pump前缀 - break; - case "10": - // 水箱温度 - hotPumpVo.setTankTemp(item.getCurValue()); // 去除pump前缀 - hotPumpVo.setTankTempId(item.getCpmId()); // 去除pump前缀 - break; - case "11": - // 出水温度 - hotPumpVo.setOutWaterTemp(item.getCurValue()); // 去除pump前缀 - hotPumpVo.setOutWaterTempId(item.getCpmId()); // 去除pump前缀 - break; - case "12": - // 回水温度 - hotPumpVo.setInWaterTemp(item.getCurValue()); // 去除pump前缀 - hotPumpVo.setInWaterTempId(item.getCpmId()); // 去除pump前缀 - break; - case "16": - // 累计运行时间 - hotPumpVo.setRunTime(item.getCurValue()); // 去除pump前缀 - hotPumpVo.setRunTimeId(item.getCpmId()); // 去除pump前缀 - break; - case "17": - // 开机 - hotPumpVo.setStart(item.getCurValue().toString()); // 去除pump前缀 - hotPumpVo.setStartId(item.getCpmId()); // 去除pump前缀 - break; - case "18": - // 关机 - hotPumpVo.setStop(item.getCurValue().toString()); // 去除pump前缀 - hotPumpVo.setStopId(item.getCpmId()); // 去除pump前缀 - break; - default: - break; - } - }); - return hotPumpVo; - case "循环泵": - HotWaterCircuitPumpControlVO circuitPumpVo = new HotWaterCircuitPumpControlVO(); - circuitPumpVo.setId(dlEntry.getKey()); - if (StringUtils.isBlank(dlItems.get(0).getDeviceName())) { - // 如果设备名称为空,则使用房间号 - circuitPumpVo.setName(dto.getName()); - } else { - circuitPumpVo.setName(dlItems.get(0).getDeviceName()); - } - circuitPumpVo.setOrderNum(dlItems.get(0).getDlOrderNum()); - // 单独对系统参数 - dlItems.forEach(item -> { - switch (item.getParamTypeId()) { - case "1": - // 启停 - circuitPumpVo.setStartStopControl(item.getCurValue().intValue()); - circuitPumpVo.setStartStopControlId(item.getCpmId()); - break; - case "2": - // 运行状态 - circuitPumpVo.setRunState(item.getCurValue().intValue()); - circuitPumpVo.setRunStateId(item.getCpmId()); - break; - case "3": - // 故障 - circuitPumpVo.setFault(item.getCurValue().intValue()); - circuitPumpVo.setFaultId(item.getCpmId()); - break; - case "4": - // 时控 - if (item.getOtherName().contains("定时_时开1")) { - circuitPumpVo.setOneHourTimeOpenSetOne(item.getCurValue().intValue()); - circuitPumpVo.setOneHourTimeOpenSetOneId(item.getCpmId()); - } else if (item.getOtherName().contains("定时_时关1")) { - circuitPumpVo.setOneHourTimeCloseSetOne(item.getCurValue().intValue()); - circuitPumpVo.setOneHourTimeCloseSetOneId(item.getCpmId()); - } else if (item.getOtherName().contains("定时_分开1")) { - circuitPumpVo.setOneMinTimeOpenSetOne(item.getCurValue().intValue()); - circuitPumpVo.setOneMinTimeOpenSetOneId(item.getCpmId()); - } else if (item.getOtherName().contains("定时_分关1")) { - circuitPumpVo.setOneMinTimeCloseSetOne(item.getCurValue().intValue()); - circuitPumpVo.setOneMinTimeCloseSetOneId(item.getCpmId()); - } - break; - case "15": - // 开延时 - circuitPumpVo.setOpenDelayTime(item.getCurValue().intValue()); - circuitPumpVo.setOpenDelayTimeId(item.getCpmId()); - break; - case "16": - if (item.getOtherName().contains("小时")) { - // 累计运行时间 - circuitPumpVo.setRunTime(item.getCurValue()); - circuitPumpVo.setRunTimeId(item.getCpmId()); - } - break; - default: - break; - } - }); - return circuitPumpVo; - case "回水泵": - HotWaterBackPumpControlVO backPumpVo = new HotWaterBackPumpControlVO(); - backPumpVo.setId(dlEntry.getKey()); - if (StringUtils.isBlank(dlItems.get(0).getDeviceName())) { - // 如果设备名称为空,则使用房间号 - backPumpVo.setName(dto.getName()); - } else { - backPumpVo.setName(dlItems.get(0).getDeviceName()); - } - backPumpVo.setOrderNum(dlItems.get(0).getDlOrderNum()); - // 单独对系统参数 - dlItems.forEach(item -> { - switch (item.getParamTypeId()) { - case "1": - // 启停 - backPumpVo.setStartStopControl(item.getCurValue().intValue()); - backPumpVo.setStartStopControlId(item.getCpmId()); - break; - case "2": - // 运行状态 - backPumpVo.setRunState(item.getCurValue().intValue()); - backPumpVo.setRunStateId(item.getCpmId()); - break; - case "3": - // 故障 - backPumpVo.setFault(item.getCurValue().intValue()); - backPumpVo.setFaultId(item.getCpmId()); - break; - case "4": - // 时控 - if (item.getOtherName().contains("定时_时开1")) { - backPumpVo.setOneHourTimeOpenSetOne(item.getCurValue().intValue()); - backPumpVo.setOneHourTimeOpenSetOneId(item.getCpmId()); - } else if (item.getOtherName().contains("定时_时关1")) { - backPumpVo.setOneHourTimeCloseSetOne(item.getCurValue().intValue()); - backPumpVo.setOneHourTimeCloseSetOneId(item.getCpmId()); - } else if (item.getOtherName().contains("定时_分开1")) { - backPumpVo.setOneMinTimeOpenSetOne(item.getCurValue().intValue()); - backPumpVo.setOneMinTimeOpenSetOneId(item.getCpmId()); - } else if (item.getOtherName().contains("定时_分关1")) { - backPumpVo.setOneMinTimeCloseSetOne(item.getCurValue().intValue()); - backPumpVo.setOneMinTimeCloseSetOneId(item.getCpmId()); - } else if (item.getOtherName().contains("定时_时开2")) { - backPumpVo.setOneHourTimeOpenSetTwo(item.getCurValue().intValue()); - backPumpVo.setOneHourTimeOpenSetTwoId(item.getCpmId()); - } else if (item.getOtherName().contains("定时_时关2")) { - backPumpVo.setOneHourTimeCloseSetTwo(item.getCurValue().intValue()); - backPumpVo.setOneHourTimeCloseSetTwoId(item.getCpmId()); - } else if (item.getOtherName().contains("定时_分开2")) { - backPumpVo.setOneMinTimeOpenSetTwo(item.getCurValue().intValue()); - backPumpVo.setOneMinTimeOpenSetTwoId(item.getCpmId()); - } else if (item.getOtherName().contains("定时_分关2")) { - backPumpVo.setOneMinTimeCloseSetTwo(item.getCurValue().intValue()); - backPumpVo.setOneMinTimeCloseSetTwoId(item.getCpmId()); - } else if (item.getOtherName().contains("定时_时开3")) { - backPumpVo.setOneHourTimeOpenSetThree(item.getCurValue().intValue()); - backPumpVo.setOneHourTimeOpenSetThreeId(item.getCpmId()); - } else if (item.getOtherName().contains("定时_时关3")) { - backPumpVo.setOneHourTimeCloseSetThree(item.getCurValue().intValue()); - backPumpVo.setOneHourTimeCloseSetThreeId(item.getCpmId()); - } else if (item.getOtherName().contains("定时_分开3")) { - backPumpVo.setOneMinTimeOpenSetThree(item.getCurValue().intValue()); - backPumpVo.setOneMinTimeOpenSetThreeId(item.getCpmId()); - } else if (item.getOtherName().contains("定时_分关3")) { - backPumpVo.setOneMinTimeCloseSetThree(item.getCurValue().intValue()); - backPumpVo.setOneMinTimeCloseSetThreeId(item.getCpmId()); - } - break; - case "7": - // 回水温度设置 - backPumpVo.setTempSet(item.getCurValue()); - backPumpVo.setTempSetId(item.getCpmId()); - break; - case "15": - // 开延时 - backPumpVo.setOpenDelayTime(item.getCurValue().intValue()); - backPumpVo.setOpenDelayTimeId(item.getCpmId()); - break; - case "16": - if (item.getOtherName().contains("小时")) { - // 累计运行时间 - backPumpVo.setRunTime(item.getCurValue()); - backPumpVo.setRunTimeId(item.getCpmId()); - } - break; - default: - break; - } - }); - return backPumpVo; - case "设备校准": - HotWaterDeviceCalibrationControlVO deviceCalibrationVo = new HotWaterDeviceCalibrationControlVO(); - deviceCalibrationVo.setId(dlEntry.getKey()); - if (StringUtils.isBlank(dlItems.get(0).getDeviceName())) { - // 如果设备名称为空,则使用房间号 - deviceCalibrationVo.setName(dto.getName()); - } else { - deviceCalibrationVo.setName(dlItems.get(0).getDeviceName()); - } - deviceCalibrationVo.setOrderNum(dlItems.get(0).getDlOrderNum()); - // 单独对系统参数 - dlItems.forEach(item -> { - switch (item.getParamTypeId()) { - case "0": - if (item.getOtherName().contains("工程量上限")) { - deviceCalibrationVo.setEngineeringMaxValue(item.getCurValue()); - deviceCalibrationVo.setEngineeringMaxValueId(item.getCpmId()); - } else if (item.getOtherName().contains("工程量下限")) { - deviceCalibrationVo.setEngineeringMinValue(item.getCurValue()); - deviceCalibrationVo.setEngineeringMinValueId(item.getCpmId()); - } else if (item.getOtherName().contains("数字量上限")) { - deviceCalibrationVo.setDigitalMaxValue(item.getCurValue()); - deviceCalibrationVo.setDigitalMaxValueId(item.getCpmId()); - } else if (item.getOtherName().contains("数字量下限")) { - deviceCalibrationVo.setDigitalMinValue(item.getCurValue()); - deviceCalibrationVo.setDigitalMinValueId(item.getCpmId()); - } - break; - case "9": - // 校准值 - deviceCalibrationVo.setCalibrationValue(item.getCurValue()); - deviceCalibrationVo.setCalibrationValueId(item.getCpmId()); - break; - default: - break; - } - }); - return deviceCalibrationVo; - case "电表": - case "水表": - HotWaterDeviceControlVO meterVo = new HotWaterDeviceControlVO(); - meterVo.setId(dlEntry.getKey()); - if (StringUtils.isBlank(dlItems.get(0).getDeviceName())) { - meterVo.setName(dto.getName()); - } else { - meterVo.setName(dlItems.get(0).getDeviceName()); - } - meterVo.setOrderNum(dlItems.get(0).getDlOrderNum()); - // 单独对系统参数 - dlItems.forEach(item -> { - switch (item.getParamTypeId()) { - case "13": - meterVo.setTotalReading(item.getCurValue()); - meterVo.setTotalReadingId(item.getCpmId()); - break; - case "14": - meterVo.setTotalReading(item.getCurValue()); - meterVo.setTotalReadingId(item.getCpmId()); - break; - default: - break; - } - }); - return meterVo; - default: - break; - } - return null; - }) - .collect(Collectors.toList()); - dto.setChildren(children); - return dto; - }) - // 添加排序操作 + .map(this::convertToHotWaterControlDTO) .sorted(Comparator.comparingInt(HotWaterControlDTO::getOrderNum)) .collect(Collectors.toList()); } + private HotWaterControlDTO convertToHotWaterControlDTO(Map.Entry> houseEntry) { + HotWaterControlDTO dto = new HotWaterControlDTO(); + dto.setId(houseEntry.getKey()); + dto.setName(houseEntry.getValue().get(0).getDeviceType()); + dto.setOrderNum(houseEntry.getValue().get(0).getCtOrderNum()); + + // 不需要强制转换,直接使用Object类型 + List children = houseEntry.getValue().stream() + .collect(Collectors.groupingBy(HotWaterControlListVO::getDeviceId)) + .entrySet().stream() + .map(dlEntry -> createDeviceVO(dlEntry, dto)) + .sorted((obj1, obj2) -> { + // 根据实际对象类型获取orderNum进行排序 + int order1 = getOrderNumFromObject(obj1); + int order2 = getOrderNumFromObject(obj2); + return Integer.compare(order1, order2); + }) + .collect(Collectors.toList()); + + dto.setChildren(children); + return dto; + } + + // 辅助方法:从不同的VO对象中获取orderNum + private int getOrderNumFromObject(Object obj) { + try { + if (obj != null) { + // 使用反射获取orderNum字段 + java.lang.reflect.Method getOrderNumMethod = obj.getClass().getMethod("getOrderNum"); + return (int) getOrderNumMethod.invoke(obj); + } + } catch (Exception e) { + // 如果发生异常,默认返回0 + } + return 0; + } + + + private Object createDeviceVO(Map.Entry> dlEntry, HotWaterControlDTO parentDto) { + List dlItems = dlEntry.getValue(); + String deviceTypeName = parentDto.getName(); + + switch (deviceTypeName) { + case "系统": + return createSystemControlVO(dlEntry, dlItems, parentDto); + case "热泵": + return createHotPumpControlVO(dlEntry, dlItems, parentDto); + case "循环泵": + return createCircuitPumpControlVO(dlEntry, dlItems, parentDto); + case "回水泵": + return createBackPumpControlVO(dlEntry, dlItems, parentDto); + case "设备校准": + return createDeviceCalibrationControlVO(dlEntry, dlItems, parentDto); + case "电表": + case "水表": + return createDeviceControlVO(dlEntry, dlItems, parentDto); + default: + return null; + } + } + + private HotWaterSystemControlVO createSystemControlVO( + Map.Entry> dlEntry, + List dlItems, + HotWaterControlDTO parentDto) { + + HotWaterSystemControlVO vo = new HotWaterSystemControlVO(); + setupBasicDeviceInfo(vo, dlEntry, dlItems, parentDto); + + dlItems.forEach(item -> { + switch (item.getParamTypeId()) { + case "1": + // 时间写入控制 + vo.setTimeSet(item.getCurValue().intValue()); + vo.setTimeSetId(item.getCpmId()); + break; + case "5": + // 压力 + vo.setPressure(item.getCurValue()); + vo.setPressureId(item.getCpmId()); + break; + case "6": + case "12": + // 回水温度 + vo.setInTemp(item.getCurValue()); + vo.setInTempId(item.getCpmId()); + break; + case "10": + // 水箱温度 + vo.setTankTemp(item.getCurValue()); + vo.setTankTempId(item.getCpmId()); + break; + case "20": + // 太阳能温度 + vo.setSolarTemp(item.getCurValue()); + vo.setSolarTempId(item.getCpmId()); + break; + case "15": + // 时间 + handleSystemTimeParameters(vo, item); + break; + default: + break; + } + }); + // 开始组装时间,写入plcTime + vo.setPlcTime(getPlcTime(vo)); + return vo; + } + + private String getPlcTime(HotWaterSystemControlVO vo) { + return String.format("%04d-%02d-%02d %02d:%02d:%02d", + vo.getYearTimeRead(), vo.getMonthTimeRead(), vo.getDayTimeRead(), + vo.getHourTimeRead(), vo.getMinTimeRead(), vo.getScTimeRead()); + } + + private void handleSystemTimeParameters(HotWaterSystemControlVO vo, HotWaterControlListVO item) { + String otherName = item.getOtherName(); + int value = item.getCurValue().intValue(); + String cpmId = item.getCpmId(); + + switch (otherName) { + case "年_读": + vo.setYearTimeRead(Integer.parseInt("20" + value)); + vo.setYearTimeReadId(cpmId); + break; + case "月_读": + vo.setMonthTimeRead(value); + vo.setMonthTimeReadId(cpmId); + break; + case "日_读": + vo.setDayTimeRead(value); + vo.setDayTimeReadId(cpmId); + break; + case "时_读": + vo.setHourTimeRead(value); + vo.setHourTimeReadId(cpmId); + break; + case "分_读": + vo.setMinTimeRead(value); + vo.setMinTimeReadId(cpmId); + break; + case "秒_读": + vo.setScTimeRead(value); + vo.setScTimeReadId(cpmId); + break; + case "秒_写": + vo.setScTimeSet(value); + vo.setScTimeSetId(cpmId); + break; + case "年_写": + vo.setYearTimeSet(Integer.parseInt("20" + value)); + vo.setYearTimeSetId(cpmId); + break; + case "月_写": + vo.setMonthTimeSet(value); + vo.setMonthTimeSetId(cpmId); + break; + case "日_写": + vo.setDayTimeSet(value); + vo.setDayTimeSetId(cpmId); + break; + case "时_写": + vo.setHourTimeSet(value); + vo.setHourTimeSetId(cpmId); + break; + case "分_写": + vo.setMinTimeSet(value); + vo.setMinTimeSetId(cpmId); + break; + default: + break; + } + } + + private HotWaterHotPumpControlVO createHotPumpControlVO( + Map.Entry> dlEntry, + List dlItems, + HotWaterControlDTO parentDto) { + + HotWaterHotPumpControlVO hotPumpVo = new HotWaterHotPumpControlVO(); + setupBasicDeviceInfo(hotPumpVo, dlEntry, dlItems, parentDto); + + // 判断故障状态 + if (dlItems.stream().anyMatch(item -> + "3".equals(item.getParamTypeId()) && "1".equals(item.getCurValue().toString()))) { + hotPumpVo.setFault(1); + } + + dlItems.forEach(item -> { + switch (item.getParamTypeId()) { + case "4": + // 时控 + handleHotPumpTimeParameters(hotPumpVo, item); + break; + case "1": + // 启停控制 + if (item.getOtherName().equals("12")) { + hotPumpVo.setStartStopControlOne(item.getCurValue().intValue()); + hotPumpVo.setStartStopControlOneId(item.getCpmId()); + } else if (item.getOtherName().equals("34")) { + hotPumpVo.setStartStopControlTwo(item.getCurValue().intValue()); + hotPumpVo.setStartStopControlTwoId(item.getCpmId()); + } else { + hotPumpVo.setStartStopControl(item.getCurValue().intValue()); + hotPumpVo.setStartStopControlId(item.getCpmId()); + } + break; + case "2": + // 运行状态 + hotPumpVo.setRunState(item.getCurValue().intValue()); + hotPumpVo.setRunStateId(item.getCpmId()); + break; + case "10": + // 水箱温度 + hotPumpVo.setTankTemp(item.getCurValue()); + hotPumpVo.setTankTempId(item.getCpmId()); + break; + case "11": + // 出水温度 + hotPumpVo.setOutWaterTemp(item.getCurValue()); + hotPumpVo.setOutWaterTempId(item.getCpmId()); + break; + case "12": + /// 回水温度 + hotPumpVo.setInWaterTemp(item.getCurValue()); + hotPumpVo.setInWaterTempId(item.getCpmId()); + break; + case "16": + // 累积运行时间 + hotPumpVo.setRunTime(item.getCurValue()); + hotPumpVo.setRunTimeId(item.getCpmId()); + break; + case "17": + // 开 + hotPumpVo.setStart(item.getCurValue().toString()); + hotPumpVo.setStartId(item.getCpmId()); + break; + case "18": + // 关 + hotPumpVo.setStop(item.getCurValue().toString()); + hotPumpVo.setStopId(item.getCpmId()); + break; + default: + break; + } + }); + // 开始拼接时间,写入oneHourMinTimeOpenSetOneStr等时间参数 + if (hotPumpVo.getOneHourTimeOpenSetOneId() != null && hotPumpVo.getOneMinTimeCloseSetOneId() != null) { + hotPumpVo.setOneHourMinTimeOpenSetOneStr(String.format("%02d:%02d", hotPumpVo.getOneHourTimeOpenSetOne(), hotPumpVo.getOneMinTimeOpenSetOne())); + hotPumpVo.setOneHourMinTimeCloseSetOneStr(String.format("%02d:%02d", hotPumpVo.getOneHourTimeCloseSetOne(), hotPumpVo.getOneMinTimeCloseSetOne())); + } + if (hotPumpVo.getTwoHourTimeOpenSetOneId() != null && hotPumpVo.getTwoMinTimeCloseSetOneId() != null) { + hotPumpVo.setTwoHourMinTimeOpenSetOneStr(String.format("%02d:%02d", hotPumpVo.getTwoHourTimeOpenSetOne(), hotPumpVo.getTwoMinTimeOpenSetOne())); + hotPumpVo.setTwoHourMinTimeCloseSetOneStr(String.format("%02d:%02d", hotPumpVo.getTwoHourTimeCloseSetOne(), hotPumpVo.getTwoMinTimeCloseSetOne())); + } + if (hotPumpVo.getOneHourTimeOpenSetTwoId() != null && hotPumpVo.getOneMinTimeCloseSetTwoId() != null) { + hotPumpVo.setOneHourMinTimeOpenSetTwoStr(String.format("%02d:%02d", hotPumpVo.getOneHourTimeOpenSetTwo(), hotPumpVo.getOneMinTimeOpenSetTwo())); + hotPumpVo.setOneHourMinTimeCloseSetTwoStr(String.format("%02d:%02d", hotPumpVo.getOneHourTimeCloseSetTwo(), hotPumpVo.getOneMinTimeCloseSetTwo())); + } + if (hotPumpVo.getTwoHourTimeOpenSetTwoId() != null && hotPumpVo.getTwoMinTimeCloseSetTwoId() != null) { + hotPumpVo.setTwoHourMinTimeOpenSetTwoStr(String.format("%02d:%02d", hotPumpVo.getTwoHourTimeOpenSetTwo(), hotPumpVo.getTwoMinTimeOpenSetTwo())); + hotPumpVo.setTwoHourMinTimeCloseSetTwoStr(String.format("%02d:%02d", hotPumpVo.getTwoHourTimeCloseSetTwo(), hotPumpVo.getTwoMinTimeCloseSetTwo())); + } + if (hotPumpVo.getOneHourTimeOpenSetThreeId() != null && hotPumpVo.getOneMinTimeCloseSetThreeId() != null) { + hotPumpVo.setOneHourMinTimeOpenSetThreeStr(String.format("%02d:%02d", hotPumpVo.getOneHourTimeOpenSetThree(), hotPumpVo.getOneMinTimeOpenSetThree())); + hotPumpVo.setOneHourMinTimeCloseSetThreeStr(String.format("%02d:%02d", hotPumpVo.getOneHourTimeCloseSetThree(), hotPumpVo.getOneMinTimeCloseSetThree())); + } + if (hotPumpVo.getTwoHourTimeOpenSetThreeId() != null && hotPumpVo.getTwoMinTimeCloseSetThreeId() != null) { + hotPumpVo.setTwoHourMinTimeOpenSetThreeStr(String.format("%02d:%02d", hotPumpVo.getTwoHourTimeOpenSetThree(), hotPumpVo.getTwoMinTimeOpenSetThree())); + hotPumpVo.setTwoHourMinTimeCloseSetThreeStr(String.format("%02d:%02d", hotPumpVo.getTwoHourTimeCloseSetThree(), hotPumpVo.getTwoMinTimeCloseSetThree())); + } + return hotPumpVo; + } + + private void handleHotPumpTimeParameters(HotWaterHotPumpControlVO hotPumpVo, HotWaterControlListVO item) { + String otherName = item.getOtherName(); + int value = item.getCurValue().intValue(); + String cpmId = item.getCpmId(); + + // 12定时组 + if (otherName.contains("12定时_时开1")) { + hotPumpVo.setOneHourTimeOpenSetOne(value); + hotPumpVo.setOneHourTimeOpenSetOneId(cpmId); + } else if (otherName.contains("12定时_时关1")) { + hotPumpVo.setOneHourTimeCloseSetOne(value); + hotPumpVo.setOneHourTimeCloseSetOneId(cpmId); + } else if (otherName.contains("12定时_时开2")) { + hotPumpVo.setOneHourTimeOpenSetTwo(value); + hotPumpVo.setOneHourTimeOpenSetTwoId(cpmId); + } else if (otherName.contains("12定时_时关2")) { + hotPumpVo.setOneHourTimeCloseSetTwo(value); + hotPumpVo.setOneHourTimeCloseSetTwoId(cpmId); + } else if (otherName.contains("12定时_时开3")) { + hotPumpVo.setOneHourTimeOpenSetThree(value); + hotPumpVo.setOneHourTimeOpenSetThreeId(cpmId); + } else if (otherName.contains("12定时_时关3")) { + hotPumpVo.setOneHourTimeCloseSetThree(value); + hotPumpVo.setOneHourTimeCloseSetThreeId(cpmId); + } else if (otherName.contains("12定时_分开1")) { + hotPumpVo.setOneMinTimeOpenSetOne(value); + hotPumpVo.setOneMinTimeOpenSetOneId(cpmId); + } else if (otherName.contains("12定时_分关1")) { + hotPumpVo.setOneMinTimeCloseSetOne(value); + hotPumpVo.setOneMinTimeCloseSetOneId(cpmId); + } else if (otherName.contains("12定时_分开2")) { + hotPumpVo.setOneMinTimeOpenSetTwo(value); + hotPumpVo.setOneMinTimeOpenSetTwoId(cpmId); + } else if (otherName.contains("12定时_分关2")) { + hotPumpVo.setOneMinTimeCloseSetTwo(value); + hotPumpVo.setOneMinTimeCloseSetTwoId(cpmId); + } else if (otherName.contains("12定时_分开3")) { + hotPumpVo.setOneMinTimeOpenSetThree(value); + hotPumpVo.setOneMinTimeOpenSetThreeId(cpmId); + } else if (otherName.contains("12定时_分关3")) { + hotPumpVo.setOneMinTimeCloseSetThree(value); + hotPumpVo.setOneMinTimeCloseSetThreeId(cpmId); + } + // 34定时组 + else if (otherName.contains("34定时_时开1")) { + hotPumpVo.setTwoHourTimeOpenSetOne(value); + hotPumpVo.setTwoHourTimeOpenSetOneId(cpmId); + } else if (otherName.contains("34定时_时关1")) { + hotPumpVo.setTwoHourTimeCloseSetOne(value); + hotPumpVo.setTwoHourTimeCloseSetOneId(cpmId); + } else if (otherName.contains("34定时_时开2")) { + hotPumpVo.setTwoHourTimeOpenSetTwo(value); + hotPumpVo.setTwoHourTimeOpenSetTwoId(cpmId); + } else if (otherName.contains("34定时_时关2")) { + hotPumpVo.setTwoHourTimeCloseSetTwo(value); + hotPumpVo.setTwoHourTimeCloseSetTwoId(cpmId); + } else if (otherName.contains("34定时_时开3")) { + hotPumpVo.setTwoHourTimeOpenSetThree(value); + hotPumpVo.setTwoHourTimeOpenSetThreeId(cpmId); + } else if (otherName.contains("34定时_时关3")) { + hotPumpVo.setTwoHourTimeCloseSetThree(value); + hotPumpVo.setTwoHourTimeCloseSetThreeId(cpmId); + } else if (otherName.contains("34定时_分开1")) { + hotPumpVo.setTwoMinTimeOpenSetOne(value); + hotPumpVo.setTwoMinTimeOpenSetOneId(cpmId); + } else if (otherName.contains("34定时_分关1")) { + hotPumpVo.setTwoMinTimeCloseSetOne(value); + hotPumpVo.setTwoMinTimeCloseSetOneId(cpmId); + } else if (otherName.contains("34定时_分开2")) { + hotPumpVo.setTwoMinTimeOpenSetTwo(value); + hotPumpVo.setTwoMinTimeOpenSetTwoId(cpmId); + } else if (otherName.contains("34定时_分关2")) { + hotPumpVo.setTwoMinTimeCloseSetTwo(value); + hotPumpVo.setTwoMinTimeCloseSetTwoId(cpmId); + } else if (otherName.contains("34定时_分开3")) { + hotPumpVo.setTwoMinTimeOpenSetThree(value); + hotPumpVo.setTwoMinTimeOpenSetThreeId(cpmId); + } else if (otherName.contains("34定时_分关3")) { + hotPumpVo.setTwoMinTimeCloseSetThree(value); + hotPumpVo.setTwoMinTimeCloseSetThreeId(cpmId); + } + } + + private HotWaterCircuitPumpControlVO createCircuitPumpControlVO( + Map.Entry> dlEntry, + List dlItems, + HotWaterControlDTO parentDto) { + + HotWaterCircuitPumpControlVO circuitPumpVo = new HotWaterCircuitPumpControlVO(); + setupBasicDeviceInfo(circuitPumpVo, dlEntry, dlItems, parentDto); + + dlItems.forEach(item -> { + switch (item.getParamTypeId()) { + case "1": + // 启停控制 + circuitPumpVo.setStartStopControl(item.getCurValue().intValue()); + circuitPumpVo.setStartStopControlId(item.getCpmId()); + break; + case "2": + // 运行状态 + circuitPumpVo.setRunState(item.getCurValue().intValue()); + circuitPumpVo.setRunStateId(item.getCpmId()); + break; + case "3": + // 故障状态 + circuitPumpVo.setFault(item.getCurValue().intValue()); + circuitPumpVo.setFaultId(item.getCpmId()); + break; + case "4": + // 时控 + handleCircuitPumpTimeParameters(circuitPumpVo, item); + break; + case "8": + // 温差 + circuitPumpVo.setTempDiff(item.getCurValue()); + circuitPumpVo.setTempDiffId(item.getCpmId()); + break; + case "9": + // 温差设置 + circuitPumpVo.setTempDiffSet(item.getCurValue()); + circuitPumpVo.setTempDiffSetId(item.getCpmId()); + break; + case "15": + // 开延时时间 + circuitPumpVo.setOpenDelayTime(item.getCurValue().intValue()); + circuitPumpVo.setOpenDelayTimeId(item.getCpmId()); + break; + case "16": + // 累计运行时间 + if (item.getOtherName().contains("小时")) { + circuitPumpVo.setRunTime(item.getCurValue()); + circuitPumpVo.setRunTimeId(item.getCpmId()); + } + break; + case "21": + // 一键启动 + circuitPumpVo.setStartOneKey(item.getCurValue().intValue()); + circuitPumpVo.setStartOneKeyId(item.getCpmId()); + break; + case "22": + // 手动自动切换 + circuitPumpVo.setManualAutoSwitch(item.getCurValue().intValue()); + circuitPumpVo.setManualAutoSwitchId(item.getCpmId()); + break; + default: + break; + } + }); + if (circuitPumpVo.getOneHourTimeOpenSetOneId() != null + && circuitPumpVo.getOneHourTimeCloseSetOneId() != null + && circuitPumpVo.getOneMinTimeOpenSetOneId() != null + && circuitPumpVo.getOneMinTimeCloseSetOneId() != null) { + // 设置时分开写入oneHourMinTimeOpenSetOneStr,oneHourMinTimeCloseSetOneStr + circuitPumpVo.setOneHourMinTimeOpenSetOneStr(String.format("%02d:%02d", circuitPumpVo.getOneHourTimeOpenSetOne(), circuitPumpVo.getOneMinTimeOpenSetOne())); + circuitPumpVo.setOneHourMinTimeCloseSetOneStr(String.format("%02d:%02d", circuitPumpVo.getOneHourTimeCloseSetOne(), circuitPumpVo.getOneMinTimeCloseSetOne())); + } + return circuitPumpVo; + } + + private void handleCircuitPumpTimeParameters(HotWaterCircuitPumpControlVO circuitPumpVo, HotWaterControlListVO item) { + String otherName = item.getOtherName(); + int value = item.getCurValue().intValue(); + String cpmId = item.getCpmId(); + + if (otherName.contains("定时_时开1")) { + circuitPumpVo.setOneHourTimeOpenSetOne(value); + circuitPumpVo.setOneHourTimeOpenSetOneId(cpmId); + } else if (otherName.contains("定时_时关1")) { + circuitPumpVo.setOneHourTimeCloseSetOne(value); + circuitPumpVo.setOneHourTimeCloseSetOneId(cpmId); + } else if (otherName.contains("定时_分开1")) { + circuitPumpVo.setOneMinTimeOpenSetOne(value); + circuitPumpVo.setOneMinTimeOpenSetOneId(cpmId); + } else if (otherName.contains("定时_分关1")) { + circuitPumpVo.setOneMinTimeCloseSetOne(value); + circuitPumpVo.setOneMinTimeCloseSetOneId(cpmId); + } + } + + private HotWaterBackPumpControlVO createBackPumpControlVO( + Map.Entry> dlEntry, + List dlItems, + HotWaterControlDTO parentDto) { + + HotWaterBackPumpControlVO backPumpVo = new HotWaterBackPumpControlVO(); + setupBasicDeviceInfo(backPumpVo, dlEntry, dlItems, parentDto); + + dlItems.forEach(item -> { + switch (item.getParamTypeId()) { + case "1": + // 启停控制 + backPumpVo.setStartStopControl(item.getCurValue().intValue()); + backPumpVo.setStartStopControlId(item.getCpmId()); + break; + case "2": + // 运行状态 + backPumpVo.setRunState(item.getCurValue().intValue()); + backPumpVo.setRunStateId(item.getCpmId()); + break; + case "3": + // 故障状态 + backPumpVo.setFault(item.getCurValue().intValue()); + backPumpVo.setFaultId(item.getCpmId()); + break; + case "4": + // 时控 + handleBackPumpTimeParameters(backPumpVo, item); + break; + case "7": + // 温度设置 + backPumpVo.setTempSet(item.getCurValue()); + backPumpVo.setTempSetId(item.getCpmId()); + break; + case "15": + // 时间 + backPumpVo.setOpenDelayTime(item.getCurValue().intValue()); + backPumpVo.setOpenDelayTimeId(item.getCpmId()); + break; + case "16": + // 累积运行时间 + if (item.getOtherName().contains("小时")) { + backPumpVo.setRunTime(item.getCurValue()); + backPumpVo.setRunTimeId(item.getCpmId()); + } + break; + case "21": + // 一键启动 + backPumpVo.setStartOneKey(item.getCurValue().intValue()); + backPumpVo.setStartOneKeyId(item.getCpmId()); + break; + case "22": + // 手动自动切换 + backPumpVo.setManualAutoSwitch(item.getCurValue().intValue()); + backPumpVo.setManualAutoSwitchId(item.getCpmId()); + break; + default: + break; + } + }); + if (backPumpVo.getOneHourTimeOpenSetOneId() != null + && backPumpVo.getOneHourTimeCloseSetOneId() != null + && backPumpVo.getOneMinTimeOpenSetOneId() != null + && backPumpVo.getOneMinTimeCloseSetOneId() != null) { + // 设置时分开写入oneHourMinTimeOpenSetOneStr,oneHourMinTimeCloseSetOneStr + backPumpVo.setOneHourMinTimeOpenSetOneStr(String.format("%02d:%02d", backPumpVo.getOneHourTimeOpenSetOne(), backPumpVo.getOneMinTimeOpenSetOne())); + backPumpVo.setOneHourMinTimeCloseSetOneStr(String.format("%02d:%02d", backPumpVo.getOneHourTimeCloseSetOne(), backPumpVo.getOneMinTimeCloseSetOne())); + } + if (backPumpVo.getOneHourTimeOpenSetTwoId() != null + && backPumpVo.getOneHourTimeCloseSetTwoId() != null + && backPumpVo.getOneMinTimeOpenSetTwoId() != null + && backPumpVo.getOneMinTimeCloseSetTwoId() != null) { + // 获取时分开写入oneHourMinTimeOpenSetTwoStr,oneHourMinTimeCloseSetTwoStr + backPumpVo.setOneHourMinTimeOpenSetTwoStr(String.format("%02d:%02d", backPumpVo.getOneHourTimeOpenSetTwo(), backPumpVo.getOneMinTimeOpenSetTwo())); + backPumpVo.setOneHourMinTimeCloseSetTwoStr(String.format("%02d:%02d", backPumpVo.getOneHourTimeCloseSetTwo(), backPumpVo.getOneMinTimeCloseSetTwo())); + } + if (backPumpVo.getOneHourTimeOpenSetThreeId() != null + && backPumpVo.getOneHourTimeCloseSetThreeId() != null + && backPumpVo.getOneMinTimeOpenSetThreeId() != null + && backPumpVo.getOneMinTimeCloseSetThreeId() != null) { + // 获取时分开写入oneHourMinTimeOpenSetThreeStr,oneHourMinTimeCloseSetThreeStr + backPumpVo.setOneHourMinTimeOpenSetThreeStr(String.format("%02d:%02d", backPumpVo.getOneHourTimeOpenSetThree(), backPumpVo.getOneMinTimeOpenSetThree())); + backPumpVo.setOneHourMinTimeCloseSetThreeStr(String.format("%02d:%02d", backPumpVo.getOneHourTimeCloseSetThree(), backPumpVo.getOneMinTimeCloseSetThree())); + } + return backPumpVo; + } + + private void handleBackPumpTimeParameters(HotWaterBackPumpControlVO backPumpVo, HotWaterControlListVO item) { + String otherName = item.getOtherName(); + int value = item.getCurValue().intValue(); + String cpmId = item.getCpmId(); + + if (otherName.contains("定时_时开1")) { + backPumpVo.setOneHourTimeOpenSetOne(value); + backPumpVo.setOneHourTimeOpenSetOneId(cpmId); + } else if (otherName.contains("定时_时关1")) { + backPumpVo.setOneHourTimeCloseSetOne(value); + backPumpVo.setOneHourTimeCloseSetOneId(cpmId); + } else if (otherName.contains("定时_分开1")) { + backPumpVo.setOneMinTimeOpenSetOne(value); + backPumpVo.setOneMinTimeOpenSetOneId(cpmId); + } else if (otherName.contains("定时_分关1")) { + backPumpVo.setOneMinTimeCloseSetOne(value); + backPumpVo.setOneMinTimeCloseSetOneId(cpmId); + } else if (otherName.contains("定时_时开2")) { + backPumpVo.setOneHourTimeOpenSetTwo(value); + backPumpVo.setOneHourTimeOpenSetTwoId(cpmId); + } else if (otherName.contains("定时_时关2")) { + backPumpVo.setOneHourTimeCloseSetTwo(value); + backPumpVo.setOneHourTimeCloseSetTwoId(cpmId); + } else if (otherName.contains("定时_分开2")) { + backPumpVo.setOneMinTimeOpenSetTwo(value); + backPumpVo.setOneMinTimeOpenSetTwoId(cpmId); + } else if (otherName.contains("定时_分关2")) { + backPumpVo.setOneMinTimeCloseSetTwo(value); + backPumpVo.setOneMinTimeCloseSetTwoId(cpmId); + } else if (otherName.contains("定时_时开3")) { + backPumpVo.setOneHourTimeOpenSetThree(value); + backPumpVo.setOneHourTimeOpenSetThreeId(cpmId); + } else if (otherName.contains("定时_时关3")) { + backPumpVo.setOneHourTimeCloseSetThree(value); + backPumpVo.setOneHourTimeCloseSetThreeId(cpmId); + } else if (otherName.contains("定时_分开3")) { + backPumpVo.setOneMinTimeOpenSetThree(value); + backPumpVo.setOneMinTimeOpenSetThreeId(cpmId); + } else if (otherName.contains("定时_分关3")) { + backPumpVo.setOneMinTimeCloseSetThree(value); + backPumpVo.setOneMinTimeCloseSetThreeId(cpmId); + } + } + + private HotWaterDeviceCalibrationControlVO createDeviceCalibrationControlVO( + Map.Entry> dlEntry, + List dlItems, + HotWaterControlDTO parentDto) { + + HotWaterDeviceCalibrationControlVO deviceCalibrationVo = new HotWaterDeviceCalibrationControlVO(); + setupBasicDeviceInfo(deviceCalibrationVo, dlEntry, dlItems, parentDto); + + dlItems.forEach(item -> { + switch (item.getParamTypeId()) { + case "0": + // 上下限 + handleCalibrationParameters(deviceCalibrationVo, item); + break; + case "19": + // 校准值 + deviceCalibrationVo.setCalibrationValue(item.getCurValue()); + deviceCalibrationVo.setCalibrationValueId(item.getCpmId()); + break; + default: + break; + } + }); + + return deviceCalibrationVo; + } + + private void handleCalibrationParameters(HotWaterDeviceCalibrationControlVO deviceCalibrationVo, HotWaterControlListVO item) { + String otherName = item.getOtherName(); + BigDecimal value = item.getCurValue(); + String cpmId = item.getCpmId(); + + if (otherName.contains("工程量上限")) { + deviceCalibrationVo.setEngineeringMaxValue(value); + deviceCalibrationVo.setEngineeringMaxValueId(cpmId); + } else if (otherName.contains("工程量下限")) { + deviceCalibrationVo.setEngineeringMinValue(value); + deviceCalibrationVo.setEngineeringMinValueId(cpmId); + } else if (otherName.contains("数字量上限")) { + deviceCalibrationVo.setDigitalMaxValue(value); + deviceCalibrationVo.setDigitalMaxValueId(cpmId); + } else if (otherName.contains("数字量下限")) { + deviceCalibrationVo.setDigitalMinValue(value); + deviceCalibrationVo.setDigitalMinValueId(cpmId); + } + } + + private HotWaterDeviceControlVO createDeviceControlVO( + Map.Entry> dlEntry, + List dlItems, + HotWaterControlDTO parentDto) { + + HotWaterDeviceControlVO meterVo = new HotWaterDeviceControlVO(); + setupBasicDeviceInfo(meterVo, dlEntry, dlItems, parentDto); + + dlItems.forEach(item -> { + switch (item.getParamTypeId()) { + case "13": + case "14": + // 水电表读数 + meterVo.setTotalReading(item.getCurValue()); + meterVo.setTotalReadingId(item.getCpmId()); + meterVo.setCurrentTime(item.getCurTime()); + meterVo.setCurrentTimeId(item.getCpmId()); + break; + default: + break; + } + }); + + return meterVo; + } + + private void setupBasicDeviceInfo(Object vo, Map.Entry> dlEntry, + List dlItems, HotWaterControlDTO parentDto) { + // 使用反射设置基本属性 + try { + vo.getClass().getMethod("setId", String.class).invoke(vo, dlEntry.getKey()); + vo.getClass().getMethod("setOrderNum", int.class).invoke(vo, dlItems.get(0).getDlOrderNum()); + + String deviceName = StringUtils.isBlank(dlItems.get(0).getDeviceName()) ? + parentDto.getName() : dlItems.get(0).getDeviceName(); + vo.getClass().getMethod("setName", String.class).invoke(vo, deviceName); + } catch (Exception e) { + // 忽略反射异常 + } + } + + @Override - public CollectionParamsManageEntity selectDeviceInstallByOtherName(String name) { - return collectionParamsManageMapper.selectDeviceInstallByOtherName(name); + public CollectionParamsManageEntity selectDeviceInstallByOtherName(String name, String buildingId) { + return collectionParamsManageMapper.selectDeviceInstallByOtherName(name, buildingId); } @Override - public void updateCPMByOtherName(String name, BigDecimal value, String time) { + public void updateCPMByOtherName(String name, BigDecimal value, String time, String buildingId) { try { - collectionParamsManageMapper.updateCPMByOtherName(name, value, time, "0"); + collectionParamsManageMapper.updateCPMByOtherName(name, value, time, "0", buildingId); } catch (Exception e) { - collectionParamsManageMapper.updateCPMByOtherName(name, new BigDecimal(0), time, "-1"); - ; + collectionParamsManageMapper.updateCPMByOtherName(name, new BigDecimal(0), time, "-1", buildingId); } } diff --git a/user-service/src/main/java/com/mh/user/service/impl/DataResultServiceImpl.java b/user-service/src/main/java/com/mh/user/service/impl/DataResultServiceImpl.java index 085b387..81bbf19 100644 --- a/user-service/src/main/java/com/mh/user/service/impl/DataResultServiceImpl.java +++ b/user-service/src/main/java/com/mh/user/service/impl/DataResultServiceImpl.java @@ -13,9 +13,12 @@ import org.springframework.stereotype.Service; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Calendar; import java.util.Date; import java.util.List; +import static java.util.Calendar.*; + @Service public class DataResultServiceImpl implements DataResultService { @@ -28,6 +31,8 @@ public class DataResultServiceImpl implements DataResultService { @Autowired DeviceInstallService deviceInstallService; + private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("0.000"); + @Override public void saveDataResult(DataResultEntity dataResultEntity) { SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @@ -43,6 +48,10 @@ public class DataResultServiceImpl implements DataResultService { if (dataResultEntity.getCurDate()==null){ dataResultEntity.setCurDate(date); } + // 格式化时间,按五分钟保存一次 + Date formattedDate = formatToFiveMinuteInterval(dataResultEntity.getCurDate()); + dataResultEntity.setCurDate(formattedDate); + //从安装表获取设备信息 DeviceInstallEntity deviceInstallEntity=deviceInstallMapper.selectDevice(dataResultEntity.getDeviceAddr(),dataResultEntity.getDeviceType(),dataResultEntity.getBuildingId()); double ratio=deviceInstallEntity.getRatio(); //倍率 @@ -60,10 +69,15 @@ public class DataResultServiceImpl implements DataResultService { } dayValue=100; } - //判断是否有记录 int r = dataResultMapper.selectDataResultCount(sdf1.format(dataResultEntity.getCurDate()), dataResultEntity.getDeviceAddr(),dataResultEntity.getDeviceType()); if (r==0){//插入记录 + // 获取上一个抄表记录curValue,curDate + // dataResultEntity.getCurDate()减去5分钟, + Calendar calendar = Calendar.getInstance(); + calendar.setTime(dataResultEntity.getCurDate()); + calendar.add(Calendar.MINUTE, -5); + DataResultEntity lastData = dataResultMapper.selectDataResult(sdf1.format(calendar.getTime()),dataResultEntity.getDeviceAddr(),dataResultEntity.getDeviceType()); DataResultEntity data=new DataResultEntity(); data.setDeviceAddr(dataResultEntity.getDeviceAddr()); //通讯地址 data.setDeviceType(dataResultEntity.getDeviceType()); //设备类型 @@ -73,22 +87,30 @@ public class DataResultServiceImpl implements DataResultService { curValue=dataResultEntity.getCurValue(); data.setBuildingId(deviceInstallEntity.getBuildingId()); //楼栋编号 data.setRatio(ratio); //倍率 - lastValue=deviceInstallEntity.getLastValue(); //上次读数 + if (lastData!=null){ + data.setLastDate(lastData.getCurDate()); //上次抄表日期 + data.setLastValue(lastData.getCurValue()); //上次读数 + } else { + lastValue = deviceInstallEntity.getLastValue(); //上次读数 + if (deviceInstallEntity.getLastDate()!=null){ //上次抄表日期 + String lastDate=sdf1.format(deviceInstallEntity.getLastDate()); + data.setLastDate(sdf1.parse(lastDate)); + }else{ + String lastDate=sdf1.format(date); + data.setLastDate(sdf1.parse(lastDate)); + } + data.setLastValue(lastValue); + } calcValue=(dataResultEntity.getCurValue()-lastValue)*ratio; //计算用量 - DecimalFormat df = new DecimalFormat("0.000"); - calcValue=Double.parseDouble(df.format(calcValue)); + // 使用时 + calcValue = Double.parseDouble(DECIMAL_FORMAT.format(calcValue)); data.setCalcValue(calcValue); //用量 - if (deviceInstallEntity.getLastDate()!=null){ //上次抄表日期 - String lastDate=sdf1.format(deviceInstallEntity.getLastDate()); - data.setLastDate(sdf1.parse(lastDate)); - }else{ - String lastDate=sdf1.format(date); - data.setLastDate(sdf1.parse(lastDate)); - } - data.setLastValue(lastValue); //判断读数,并保存数据 + if (deviceInstallEntity.getLastDate() == null) { + deviceInstallEntity.setLastDate(date); + } days=(int)ExchangeStringUtil.daysBetween(dataResultEntity.getCurDate(),deviceInstallEntity.getLastDate()); //计算相差天数 if (calcValue>=0 && calcValue<=dayValue){ dataResultMapper.saveDataResult(data); //插入新的记录 @@ -141,6 +163,42 @@ public class DataResultServiceImpl implements DataResultService { } } + /** + * 将时间格式化为五分钟间隔 + * 例如:09:01:02 -> 09:05:00, 09:06:30 -> 09:10:00 + * @param originalDate 原始时间 + * @return 格式化后的时间 + */ + private Date formatToFiveMinuteInterval(Date originalDate) { + if (originalDate == null) { + return new Date(); + } + + Calendar calendar = getInstance(); + calendar.setTime(originalDate); + + int minutes = calendar.get(MINUTE); + int seconds = calendar.get(SECOND); + int milliseconds = calendar.get(MILLISECOND); + + // 计算下一个五分钟节点 + int nextFiveMinute = ((minutes / 5) + 1) * 5; + + // 如果超过55分钟,则进位到下一小时 + if (nextFiveMinute >= 60) { + calendar.add(HOUR_OF_DAY, 1); + calendar.set(MINUTE, 0); + } else { + calendar.set(MINUTE, nextFiveMinute); + } + + // 秒和毫秒设为0 + calendar.set(SECOND, 0); + calendar.set(MILLISECOND, 0); + + return calendar.getTime(); + } + @Override public void updateDataResult(DataResultEntity dataResultEntity) { dataResultMapper.updateDataResult(dataResultEntity); diff --git a/user-service/src/main/java/com/mh/user/service/impl/DeviceControlServiceImpl.java b/user-service/src/main/java/com/mh/user/service/impl/DeviceControlServiceImpl.java index f3d9812..c0954bf 100644 --- a/user-service/src/main/java/com/mh/user/service/impl/DeviceControlServiceImpl.java +++ b/user-service/src/main/java/com/mh/user/service/impl/DeviceControlServiceImpl.java @@ -1,22 +1,26 @@ package com.mh.user.service.impl; +import com.alibaba.fastjson2.JSONObject; import com.mh.common.http.HttpResult; import com.mh.common.utils.StringUtils; import com.mh.user.constants.Constant; -import com.mh.user.entity.ControlSetEntity; -import com.mh.user.entity.DeviceCodeParamEntity; -import com.mh.user.entity.DeviceInstallEntity; -import com.mh.user.entity.PumpSetEntity; +import com.mh.user.entity.*; +import com.mh.user.mapper.CollectionParamsManageMapper; import com.mh.user.model.DeviceModel; +import com.mh.user.model.SanShiFengDatas; +import com.mh.user.model.SanShiFengReceiver; import com.mh.user.model.SerialPortModel; import com.mh.user.serialport.SerialPortSingle2; import com.mh.user.service.*; +import com.mh.user.utils.DateUtil; import com.mh.user.utils.ExchangeStringUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.stream.Collectors; @@ -43,6 +47,70 @@ public class DeviceControlServiceImpl implements DeviceControlService { @Resource private ControlSetService controlSetService; + @Resource + private CollectionParamsManageMapper collectionParamsManageMapper; + + @Override + public String getSn(SerialPortModel serialPortModel) { + return collectionParamsManageMapper.selectSn(serialPortModel.getCpmId()); + } + + @Override + public boolean isMqttControl(List params) { + if (!params.isEmpty()) { + for (SerialPortModel serialPortModel : params) { + // 判断是否是mqtt通讯 + String communicationType = collectionParamsManageMapper.selectCommunicationType(serialPortModel.getCpmId()); + if ("mqtt".equals(communicationType)) { + return true; + } + } + } + return false; + } + + @Override + public String operationDevice(SerialPortModel params) { + // 拼接发送的报文 + SanShiFengReceiver sendData = new SanShiFengReceiver<>(); + try { + List advantechDatas = getAdvantechDatas(params); + if (null == advantechDatas) { + return Constant.FAIL; + } + sendData.setDatas(advantechDatas); + sendData.setTime(DateUtil.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss")); + // 获取plcName + String plcName = collectionParamsManageMapper.selectPlcName(params.getCpmId()); + sendData.setPlcName(plcName); + } catch (Exception e) { + log.error("发送mqtt控制设备指令失败", e); + return Constant.FAIL; + } + return JSONObject.toJSONString(sendData); + } + + public List getAdvantechDatas(SerialPortModel params) { + List advantechDatas = new ArrayList<>(); + String dataValue = params.getDataValue(); + // 获取报文类型 + // 获取报文其他信息 + String otherName = ""; + // 从数据库id获取设备名称 + CollectionParamsManageEntity collectionParamsManage = collectionParamsManageMapper.selectById(params.getCpmId()); + if (null != collectionParamsManage) { + otherName = collectionParamsManage.getOtherName(); + } else { + return null; + } + // 发送报文 + SanShiFengDatas data = new SanShiFengDatas(); + data.setName(otherName); + data.setValue(dataValue); + advantechDatas.add(data); + return advantechDatas; + } + @Override public String readOrWriteDevice(List params, String type) { try { @@ -159,7 +227,7 @@ public class DeviceControlServiceImpl implements DeviceControlService { deviceCodeParam.setFunCode("10"); deviceCodeParam.setRegisterAddr("010C"); rtData = serialPortSingle.serialPortSend(deviceCodeParam); - } else if (!StringUtils.isBlank(brand) && brand.equals("蒙浩") && !rtData.equals(Constant.FAIL) && deviceCodeParam.getRegisterAddr().equals("0000")) { + } else if (!StringUtils.isBlank(brand) && brand.equals("蒙浩") && !rtData.equals(Constant.FAIL) && deviceCodeParam.getRegisterAddr().equals("0000")) { log.error("进入蒙浩时控控制指令发送"); // 需要继续发送指令 deviceCodeParam.setFunCode("10"); @@ -402,8 +470,8 @@ public class DeviceControlServiceImpl implements DeviceControlService { deviceCodeParam.setFunCode("06"); String time1 = split[1].substring(0, 2); String time2 = split[1].substring(3, 5); - deviceCodeParam.setDataValue(String.valueOf(Integer.parseInt(time1)*60 + Integer.parseInt(time2))); - serialPortModel.setDataValue(String.valueOf(Integer.parseInt(time1)*60 + Integer.parseInt(time2))); + deviceCodeParam.setDataValue(String.valueOf(Integer.parseInt(time1) * 60 + Integer.parseInt(time2))); + serialPortModel.setDataValue(String.valueOf(Integer.parseInt(time1) * 60 + Integer.parseInt(time2))); } break; case "timeSetClose1": @@ -414,8 +482,8 @@ public class DeviceControlServiceImpl implements DeviceControlService { deviceCodeParam.setFunCode("06"); String time1 = split[1].substring(0, 2); String time2 = split[1].substring(3, 5); - deviceCodeParam.setDataValue(String.valueOf(Integer.parseInt(time1)*60 + Integer.parseInt(time2))); - serialPortModel.setDataValue(String.valueOf(Integer.parseInt(time1)*60 + Integer.parseInt(time2))); + deviceCodeParam.setDataValue(String.valueOf(Integer.parseInt(time1) * 60 + Integer.parseInt(time2))); + serialPortModel.setDataValue(String.valueOf(Integer.parseInt(time1) * 60 + Integer.parseInt(time2))); } break; case "timeSetOpen2": @@ -426,8 +494,8 @@ public class DeviceControlServiceImpl implements DeviceControlService { deviceCodeParam.setFunCode("06"); String time1 = split[1].substring(0, 2); String time2 = split[1].substring(3, 5); - deviceCodeParam.setDataValue(String.valueOf(Integer.parseInt(time1)*60 + Integer.parseInt(time2))); - serialPortModel.setDataValue(String.valueOf(Integer.parseInt(time1)*60 + Integer.parseInt(time2))); + deviceCodeParam.setDataValue(String.valueOf(Integer.parseInt(time1) * 60 + Integer.parseInt(time2))); + serialPortModel.setDataValue(String.valueOf(Integer.parseInt(time1) * 60 + Integer.parseInt(time2))); } break; case "timeSetClose2": @@ -438,8 +506,8 @@ public class DeviceControlServiceImpl implements DeviceControlService { deviceCodeParam.setFunCode("06"); String time1 = split[1].substring(0, 2); String time2 = split[1].substring(3, 5); - deviceCodeParam.setDataValue(String.valueOf(Integer.parseInt(time1)*60 + Integer.parseInt(time2))); - serialPortModel.setDataValue(String.valueOf(Integer.parseInt(time1)*60 + Integer.parseInt(time2))); + deviceCodeParam.setDataValue(String.valueOf(Integer.parseInt(time1) * 60 + Integer.parseInt(time2))); + serialPortModel.setDataValue(String.valueOf(Integer.parseInt(time1) * 60 + Integer.parseInt(time2))); } break; case "timeSetOpen3": @@ -450,8 +518,8 @@ public class DeviceControlServiceImpl implements DeviceControlService { deviceCodeParam.setFunCode("06"); String time1 = split[1].substring(0, 2); String time2 = split[1].substring(3, 5); - deviceCodeParam.setDataValue(String.valueOf(Integer.parseInt(time1)*60 + Integer.parseInt(time2))); - serialPortModel.setDataValue(String.valueOf(Integer.parseInt(time1)*60 + Integer.parseInt(time2))); + deviceCodeParam.setDataValue(String.valueOf(Integer.parseInt(time1) * 60 + Integer.parseInt(time2))); + serialPortModel.setDataValue(String.valueOf(Integer.parseInt(time1) * 60 + Integer.parseInt(time2))); } break; case "timeSetClose3": @@ -462,8 +530,8 @@ public class DeviceControlServiceImpl implements DeviceControlService { deviceCodeParam.setFunCode("06"); String time1 = split[1].substring(0, 2); String time2 = split[1].substring(3, 5); - deviceCodeParam.setDataValue(String.valueOf(Integer.parseInt(time1)*60 + Integer.parseInt(time2))); - serialPortModel.setDataValue(String.valueOf(Integer.parseInt(time1)*60 + Integer.parseInt(time2))); + deviceCodeParam.setDataValue(String.valueOf(Integer.parseInt(time1) * 60 + Integer.parseInt(time2))); + serialPortModel.setDataValue(String.valueOf(Integer.parseInt(time1) * 60 + Integer.parseInt(time2))); } break; case "timeSetOpen4": @@ -474,8 +542,8 @@ public class DeviceControlServiceImpl implements DeviceControlService { deviceCodeParam.setFunCode("06"); String time1 = split[1].substring(0, 2); String time2 = split[1].substring(3, 5); - deviceCodeParam.setDataValue(String.valueOf(Integer.parseInt(time1)*60 + Integer.parseInt(time2))); - serialPortModel.setDataValue(String.valueOf(Integer.parseInt(time1)*60 + Integer.parseInt(time2))); + deviceCodeParam.setDataValue(String.valueOf(Integer.parseInt(time1) * 60 + Integer.parseInt(time2))); + serialPortModel.setDataValue(String.valueOf(Integer.parseInt(time1) * 60 + Integer.parseInt(time2))); } break; case "timeSetClose4": @@ -486,8 +554,8 @@ public class DeviceControlServiceImpl implements DeviceControlService { deviceCodeParam.setFunCode("06"); String time1 = split[1].substring(0, 2); String time2 = split[1].substring(3, 5); - deviceCodeParam.setDataValue(String.valueOf(Integer.parseInt(time1)*60 + Integer.parseInt(time2))); - serialPortModel.setDataValue(String.valueOf(Integer.parseInt(time1)*60 + Integer.parseInt(time2))); + deviceCodeParam.setDataValue(String.valueOf(Integer.parseInt(time1) * 60 + Integer.parseInt(time2))); + serialPortModel.setDataValue(String.valueOf(Integer.parseInt(time1) * 60 + Integer.parseInt(time2))); } break; case "channelSet": @@ -500,7 +568,7 @@ public class DeviceControlServiceImpl implements DeviceControlService { break; case "weekSet": // 星期设置 - registerStr = ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.decToHex(String.valueOf(24 + (scene-1) * 18)), 4); + registerStr = ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.decToHex(String.valueOf(24 + (scene - 1) * 18)), 4); if (Constant.WRITE.equals(type)) { String dataValue = ExchangeStringUtil.addZeroForNum(deviceCodeParam.getDataValue(), 8); dataValue = ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.parseByte2HexStr(dataValue), 4); diff --git a/user-service/src/main/java/com/mh/user/service/impl/GatewayManageServiceImpl.java b/user-service/src/main/java/com/mh/user/service/impl/GatewayManageServiceImpl.java index fefffc1..4bf108e 100644 --- a/user-service/src/main/java/com/mh/user/service/impl/GatewayManageServiceImpl.java +++ b/user-service/src/main/java/com/mh/user/service/impl/GatewayManageServiceImpl.java @@ -113,4 +113,9 @@ public class GatewayManageServiceImpl implements GatewayManageService { public void updateGatewayManageOnlineBySn(String sn, int grade) { gatewayManageMapper.updateGatewayManageOnlineBySn(sn, grade); } + + @Override + public String queryBuildingIdBySn(String sn) { + return gatewayManageMapper.queryBuildingIdBySn(sn); + } } diff --git a/user-service/src/main/java/com/mh/user/service/impl/NowDataServiceImpl.java b/user-service/src/main/java/com/mh/user/service/impl/NowDataServiceImpl.java index 610686b..6f6e644 100644 --- a/user-service/src/main/java/com/mh/user/service/impl/NowDataServiceImpl.java +++ b/user-service/src/main/java/com/mh/user/service/impl/NowDataServiceImpl.java @@ -19,6 +19,7 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import java.math.BigDecimal; +import java.math.RoundingMode; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.locks.ReentrantLock; @@ -56,6 +57,8 @@ public class NowDataServiceImpl implements NowDataService { @Qualifier("gatewayManageMapper") @Autowired private GatewayManageMapper gatewayManageMapper; + @Autowired + private CollectionParamsManageMapper collectionParamsManageMapper; @Override public void updateMultiNowData(NowDataEntity nowDataEntity) { @@ -110,8 +113,8 @@ public class NowDataServiceImpl implements NowDataService { @Override public NowDataEntity queryNowDataByPumpName(String buildingId, String buildingName, String deviceAddr, String deviceName) { - int count = nowDataMapper.selectNowDataByPumName(buildingId,deviceAddr,deviceName); - if (count==0){ + int count = nowDataMapper.selectNowDataByPumName(buildingId, deviceAddr, deviceName); + if (count == 0) { NowDataEntity nowDataEntity = new NowDataEntity(); nowDataEntity.setPumpId(deviceAddr); nowDataEntity.setPumpName(deviceName); @@ -120,7 +123,7 @@ public class NowDataServiceImpl implements NowDataService { nowDataMapper.saveNowData(nowDataEntity); return nowDataEntity; } - return nowDataMapper.queryNowDataByPumpName(buildingId,deviceAddr,deviceName); + return nowDataMapper.queryNowDataByPumpName(buildingId, deviceAddr, deviceName); } @Override @@ -130,94 +133,94 @@ public class NowDataServiceImpl implements NowDataService { //保存热泵采集数据 @Override - public void saveNowHistoryData(String pumpId, String deviceType, String strData,String dataType,String buildingId) { + public void saveNowHistoryData(String pumpId, String deviceType, String strData, String dataType, String buildingId) { SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - Date date=new Date(); - String strDate=sdf1.format(date); - strDate=strDate.substring(0,13)+":00:00"; + Date date = new Date(); + String strDate = sdf1.format(date); + strDate = strDate.substring(0, 13) + ":00:00"; - NowDataEntity data=new NowDataEntity(); - try{ - if (dataType.equals("tempSet")){ //温度设定 + NowDataEntity data = new NowDataEntity(); + try { + if (dataType.equals("tempSet")) { //温度设定 data.setTempSet(strData); - }else if (dataType.equals("waterTemp")){ //水箱水温 + } else if (dataType.equals("waterTemp")) { //水箱水温 data.setWaterTemp(strData); - }else if (dataType.equals("runState")){ //运行状态 + } else if (dataType.equals("runState")) { //运行状态 data.setRunState(strData); - }else if (dataType.equals("isFault")){ //是否故障 + } else if (dataType.equals("isFault")) { //是否故障 data.setIsFault(strData); } //从安装表获取设备信息 - DeviceInstallEntity deviceInstallEntity=deviceInstallMapper.selectDevice(pumpId,deviceType,buildingId); + DeviceInstallEntity deviceInstallEntity = deviceInstallMapper.selectDevice(pumpId, deviceType, buildingId); //获取对应水箱信息 - DeviceModel tank=deviceFloorMapper.selectTankName(buildingId,"水箱",pumpId); - if (tank!=null){ + DeviceModel tank = deviceFloorMapper.selectTankName(buildingId, "水箱", pumpId); + if (tank != null) { data.setTankId(String.valueOf(tank.getId())); //对应水箱编号 data.setTankName(tank.getDeviceName()); //对应水箱名称 } - String buildingName=buildingService.queryBuildingName(buildingId); + String buildingName = buildingService.queryBuildingName(buildingId); //判断实时表是否有记录 - int r1 = nowDataMapper.selectNowDataCount(deviceInstallEntity.getBuildingId(),pumpId);//判断now_data表是否存在记录 - if(r1==0){ + int r1 = nowDataMapper.selectNowDataCount(deviceInstallEntity.getBuildingId(), pumpId);//判断now_data表是否存在记录 + if (r1 == 0) { data.setPumpId(pumpId); //通讯地址 data.setPumpName(deviceInstallEntity.getDeviceName()); //通讯地址 data.setBuildingId(deviceInstallEntity.getBuildingId()); data.setBuildingName(buildingName); nowDataMapper.saveNowData(data); } else { - NowDataEntity data1=nowDataMapper.selectNowData(deviceInstallEntity.getBuildingId(),pumpId); - if (data1!=null){ - if (dataType.equals("tempSet")){ //温度设定 + NowDataEntity data1 = nowDataMapper.selectNowData(deviceInstallEntity.getBuildingId(), pumpId); + if (data1 != null) { + if (dataType.equals("tempSet")) { //温度设定 data1.setTempSet(strData); - }else if (dataType.equals("waterTemp")){ //水箱水温 + } else if (dataType.equals("waterTemp")) { //水箱水温 data1.setWaterTemp(strData); - }else if (dataType.equals("runState")){ //运行状态 + } else if (dataType.equals("runState")) { //运行状态 data1.setRunState(strData); - }else if (dataType.equals("isFault")){ //是否故障 + } else if (dataType.equals("isFault")) { //是否故障 data1.setIsFault(strData); } nowDataMapper.updateNowData(data1); } } //判断历史表是否有记录 - int r2 = nowDataMapper.selectHistoryDataCount(strDate,deviceInstallEntity.getBuildingId(),pumpId);//判断now_data表是否存在记录 - if (r2==0){ + int r2 = nowDataMapper.selectHistoryDataCount(strDate, deviceInstallEntity.getBuildingId(), pumpId);//判断now_data表是否存在记录 + if (r2 == 0) { data.setPumpId(pumpId); //通讯地址 data.setPumpName(deviceInstallEntity.getDeviceName()); //通讯地址 data.setBuildingId(deviceInstallEntity.getBuildingId()); data.setBuildingName(buildingName); data.setCurDate(strDate); nowDataMapper.saveHistoryData(data); - }else { - NowDataEntity data2=nowDataMapper.selectHistoryData(strDate,deviceInstallEntity.getBuildingId(),pumpId); - if (data2!=null){ - if (dataType.equals("tempSet")){ //温度设定 + } else { + NowDataEntity data2 = nowDataMapper.selectHistoryData(strDate, deviceInstallEntity.getBuildingId(), pumpId); + if (data2 != null) { + if (dataType.equals("tempSet")) { //温度设定 data2.setTempSet(strData); - }else if (dataType.equals("waterTemp")){ //水箱水温 + } else if (dataType.equals("waterTemp")) { //水箱水温 data2.setWaterTemp(strData); - }else if (dataType.equals("runState")){ //运行状态 + } else if (dataType.equals("runState")) { //运行状态 data2.setRunState(strData); - }else if (dataType.equals("isFault")){ //是否故障 + } else if (dataType.equals("isFault")) { //是否故障 data2.setIsFault(strData); } nowDataMapper.updateHistoryData(data2); } } - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); } } //非热泵温控、状态检测数据 @Override - public void saveNowHistoryData2(String deviceAddr, String deviceType, String strData, String dataType,String buildingId) { - NowDataEntity data=new NowDataEntity(); + public void saveNowHistoryData2(String deviceAddr, String deviceType, String strData, String dataType, String buildingId) { + NowDataEntity data = new NowDataEntity(); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - Date date=new Date(); - String strDate=sdf1.format(date); - strDate=strDate.substring(0,13)+":00:00"; + Date date = new Date(); + String strDate = sdf1.format(date); + strDate = strDate.substring(0, 13) + ":00:00"; - try{ + try { switch (dataType) { case "tempSet": //温度设定 data.setTempSet(strData); @@ -239,28 +242,28 @@ public class NowDataServiceImpl implements NowDataService { break; } //从安装表获取设备信息 - DeviceInstallEntity deviceInstallEntity=deviceInstallMapper.selectDevice(deviceAddr,deviceType,buildingId); - String buildingName=buildingService.queryBuildingName(buildingId); + DeviceInstallEntity deviceInstallEntity = deviceInstallMapper.selectDevice(deviceAddr, deviceType, buildingId); + String buildingName = buildingService.queryBuildingName(buildingId); data.setBuildingName(buildingName); // 获取项目名称 String name = dealDataService.customName(); if (deviceType.equals("温控") && !name.contains(Constant.CUSTOM_NAME_HUAXIA) - && !name.contains(Constant.CUSTOM_NAME_HUARUAN) ) { + && !name.contains(Constant.CUSTOM_NAME_HUARUAN)) { log.error("进入到热泵1------------------------"); //为了兼容旧系统,旧系统热泵信息保存在楼面表,从楼面表获取热泵资料 - PumpModel pump=deviceFloorMapper.selectDeviceId2("热泵",buildingId); - String pumpId="1"; - String pumpName="热泵1"; - if (pump!=null){ - pumpId=pump.getPumpId(); - pumpName=pump.getPumpName(); + PumpModel pump = deviceFloorMapper.selectDeviceId2("热泵", buildingId); + String pumpId = "1"; + String pumpName = "热泵1"; + if (pump != null) { + pumpId = pump.getPumpId(); + pumpName = pump.getPumpName(); //获取对应水箱信息 - DeviceModel tank=deviceFloorMapper.selectTankName(buildingId,"水箱",pumpId); - if (tank!=null){ + DeviceModel tank = deviceFloorMapper.selectTankName(buildingId, "水箱", pumpId); + if (tank != null) { data.setTankId(String.valueOf(tank.getId())); //对应水箱编号 data.setTankName(tank.getDeviceName()); //对应水箱名称 - }else{ + } else { data.setTankId("1"); //对应水箱编号 data.setTankName("水箱1"); //对应水箱名称 } @@ -272,68 +275,68 @@ public class NowDataServiceImpl implements NowDataService { data.setCurDate(strDate); //判断实时表是否有记录 int r1 = nowDataMapper.selectNowDataCount2(buildingId);//判断now_data表是否存在记录 - if (r1==0){ + if (r1 == 0) { nowDataMapper.saveNowData(data); - }else { + } else { if (deviceType.equals("压变")) { - String seat=deviceInstallService.selectSeat(deviceType,deviceAddr,buildingId); - log.info("-------楼栋:"+buildingName+",类型:"+deviceType+",地址:"+deviceAddr+",区位:"+seat+",数值:"+strData+"------"); - if (!StringUtils.isBlank(seat)){ - nowDataMapper.nowDataWaterLevel(strData,seat,buildingId); - }else{ + String seat = deviceInstallService.selectSeat(deviceType, deviceAddr, buildingId); + log.info("-------楼栋:" + buildingName + ",类型:" + deviceType + ",地址:" + deviceAddr + ",区位:" + seat + ",数值:" + strData + "------"); + if (!StringUtils.isBlank(seat)) { + nowDataMapper.nowDataWaterLevel(strData, seat, buildingId); + } else { nowDataMapper.updateNowData2(data); } - }else if (deviceType.equals("水位开关")){ - String seat=deviceInstallService.selectSeat(deviceType,deviceAddr,buildingId); - log.info("-------楼栋:"+buildingName+",类型:"+deviceType+",地址:"+deviceAddr+",区位:"+seat+",数值:"+strData+"------"); - if (seat!=null){ - nowDataMapper.nowDataLevelSet(strData,seat,buildingId); - }else{ + } else if (deviceType.equals("水位开关")) { + String seat = deviceInstallService.selectSeat(deviceType, deviceAddr, buildingId); + log.info("-------楼栋:" + buildingName + ",类型:" + deviceType + ",地址:" + deviceAddr + ",区位:" + seat + ",数值:" + strData + "------"); + if (seat != null) { + nowDataMapper.nowDataLevelSet(strData, seat, buildingId); + } else { nowDataMapper.updateNowData2(data); } - }else{ + } else { nowDataMapper.updateNowData2(data); } } //判断历史表是否有记录 - int r2 = nowDataMapper.selectHistoryDataCount2(strDate,buildingId);//判断history_Data表是否存在记录 - if (r2==0){ + int r2 = nowDataMapper.selectHistoryDataCount2(strDate, buildingId);//判断history_Data表是否存在记录 + if (r2 == 0) { nowDataMapper.saveHistoryData(data); - }else { + } else { if (deviceType.equals("压变")) { - String seat=deviceInstallService.selectSeat(deviceType,deviceAddr,buildingId); - if (!StringUtils.isBlank(seat)){ - nowDataMapper.historyDataWaterLevel(strData,seat,buildingId,strDate); - log.info("------楼栋:"+buildingName+",历史记录压变区位:"+seat+"------"); - }else{ + String seat = deviceInstallService.selectSeat(deviceType, deviceAddr, buildingId); + if (!StringUtils.isBlank(seat)) { + nowDataMapper.historyDataWaterLevel(strData, seat, buildingId, strDate); + log.info("------楼栋:" + buildingName + ",历史记录压变区位:" + seat + "------"); + } else { nowDataMapper.updateHistoryData2(data); } - }else if (deviceType.equals("水位开关")){ - String seat=deviceInstallService.selectSeat(deviceType,deviceAddr,buildingId); - if (!StringUtils.isBlank(seat)){ - nowDataMapper.historyDataLevelSet(strData,seat,buildingId,strDate); - log.info("------楼栋:"+buildingName+",历史记录水位开关区位:"+seat+"------"); - }else{ + } else if (deviceType.equals("水位开关")) { + String seat = deviceInstallService.selectSeat(deviceType, deviceAddr, buildingId); + if (!StringUtils.isBlank(seat)) { + nowDataMapper.historyDataLevelSet(strData, seat, buildingId, strDate); + log.info("------楼栋:" + buildingName + ",历史记录水位开关区位:" + seat + "------"); + } else { nowDataMapper.updateHistoryData2(data); } - }else{ + } else { nowDataMapper.updateHistoryData2(data); } } - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); } } //非热泵温度变送器、状态检测相关数据 @Override - public void saveNowHistoryData3(String pumpId, String deviceType, String strData,String dataType,String buildingId) { + public void saveNowHistoryData3(String pumpId, String deviceType, String strData, String dataType, String buildingId) { SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - Date date=new Date(); - String strDate=sdf1.format(date); - strDate=strDate.substring(0,13)+":00:00"; - NowDataEntity data=new NowDataEntity(); - try{ + Date date = new Date(); + String strDate = sdf1.format(date); + strDate = strDate.substring(0, 13) + ":00:00"; + NowDataEntity data = new NowDataEntity(); + try { switch (dataType) { case "tempSet": //温度设定 data.setTempSet(strData); @@ -348,48 +351,48 @@ public class NowDataServiceImpl implements NowDataService { data.setIsFault(strData); break; } - String buildingName=buildingService.queryBuildingName(buildingId); + String buildingName = buildingService.queryBuildingName(buildingId); //判断实时表是否有记录 - int r1 = nowDataMapper.selectNowDataCount(buildingId,pumpId);//判断now_data表是否存在记录 - if(r1==0){ + int r1 = nowDataMapper.selectNowDataCount(buildingId, pumpId);//判断now_data表是否存在记录 + if (r1 == 0) { data.setPumpId(pumpId); //热泵编号 - String pumpName="热泵"+pumpId; + String pumpName = "热泵" + pumpId; data.setPumpName(pumpName); //热泵名称 data.setBuildingId(buildingId); data.setBuildingName(buildingName); nowDataMapper.saveNowData(data); } else { - NowDataEntity data1=nowDataMapper.selectNowData(buildingId,pumpId); - if (data1!=null){ - if (dataType.equals("tempSet")){ //温度设定 + NowDataEntity data1 = nowDataMapper.selectNowData(buildingId, pumpId); + if (data1 != null) { + if (dataType.equals("tempSet")) { //温度设定 data1.setTempSet(strData); - } else if (dataType.equals("waterTemp")){ //水箱水温 + } else if (dataType.equals("waterTemp")) { //水箱水温 BigDecimal bigDecimal = new BigDecimal(strData); if (bigDecimal.compareTo(new BigDecimal(0)) >= 0 && bigDecimal.compareTo(new BigDecimal(100)) <= 0) { data1.setWaterTemp(strData); } - }else if (dataType.equals("runState")){ //运行状态 + } else if (dataType.equals("runState")) { //运行状态 data1.setRunState(strData); - }else if (dataType.equals("isFault")){ //是否故障 + } else if (dataType.equals("isFault")) { //是否故障 data1.setIsFault(strData); } nowDataMapper.updateNowData(data1); } } //判断历史表是否有记录 - int r2 = nowDataMapper.selectHistoryDataCount(strDate,buildingId,pumpId);//判断now_data表是否存在记录 - if (r2==0){ + int r2 = nowDataMapper.selectHistoryDataCount(strDate, buildingId, pumpId);//判断now_data表是否存在记录 + if (r2 == 0) { data.setPumpId(pumpId); //热泵编号 - String pumpName="热泵"+pumpId; + String pumpName = "热泵" + pumpId; data.setPumpName(pumpName); //热泵名称 data.setBuildingId(buildingId); data.setBuildingName(buildingName); data.setCurDate(strDate); nowDataMapper.saveHistoryData(data); - }else { - NowDataEntity data2=nowDataMapper.selectHistoryData(strDate,buildingId,pumpId); - if (data2!=null){ + } else { + NowDataEntity data2 = nowDataMapper.selectHistoryData(strDate, buildingId, pumpId); + if (data2 != null) { switch (dataType) { case "tempSet": //温度设定 data2.setTempSet(strData); @@ -407,7 +410,7 @@ public class NowDataServiceImpl implements NowDataService { nowDataMapper.updateHistoryData(data2); } } - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); } } @@ -422,22 +425,138 @@ public class NowDataServiceImpl implements NowDataService { nowDataMapper.updateTempSet(buildingId); nowDataMapper.updateLevelSet(buildingId); List nowDataEntities = nowDataMapper.queryNowData(buildingId); + + // 处理压变设备离线状态 + handlePressureTransmitterOfflineStatus(buildingId, nowDataEntities); + + // 处理循环泵状态 + handlePumpStatus(buildingId, nowDataEntities, "循环泵", + new HashMap() {{ + put("循环泵1", "setCirculateState"); + put("循环泵2", "setCirculateState2"); + }}); + + // 处理回水泵状态 + handlePumpStatus(buildingId, nowDataEntities, "回水泵", + new HashMap() {{ + put("回水泵1", "setBackWaterState"); + put("回水泵2", "setBackWaterState2"); + }}); + + // 处理压力值和网关 + handlePressureAndGatewayStatus(buildingId, nowDataEntities, "系统", "5"); + + return nowDataEntities; + } + + private void handlePressureAndGatewayStatus(String buildingId, List nowDataEntities, String deviceType, String paramTypeId) { + List pumpList = deviceInstallMapper.selectOfflineDevice(buildingId, deviceType); + for (DeviceInstallEntity pump : pumpList) { + CollectionParamsManageEntity pressure = collectionParamsManageMapper.selectByDeviceIdAndParamTypeId(pump.getId(), paramTypeId); + if (pressure != null) { + String pressureValue = pressure.getCurValue().setScale(1, RoundingMode.HALF_UP).toString(); + if (!StringUtils.isBlank(pressureValue)) { + nowDataEntities.stream() + .filter(nowDataEntity -> buildingId.equals(nowDataEntity.getBuildingId())) + .forEach(nowDataEntity -> nowDataEntity.setPressure(pressureValue)); + String dataCom = pump.getDataCom(); + if (!StringUtils.isBlank(dataCom)) { + int gwState = gatewayManageMapper.queryGatewayByDataCom(dataCom); + nowDataEntities.stream() + .filter(nowDataEntity -> buildingId.equals(nowDataEntity.getBuildingId())) + .forEach(nowDataEntity -> nowDataEntity.setGwState(gwState == 0 ? "在线" : "离线")); + } + break; + } + } + } + } + + /** + * 处理压变设备离线状态 + */ + private void handlePressureTransmitterOfflineStatus(String buildingId, List nowDataEntities) { List list = deviceInstallMapper.selectOfflineDevice(buildingId, "压变"); - if (list != null && list.size() > 0) { + if (list != null && !list.isEmpty()) { for (DeviceInstallEntity deviceInstallEntity : list) { if (!DateUtil.isToday(deviceInstallEntity.getLastDate())) { - // 使用stream流nowDataEntities,批量更新waterLevel="离线" nowDataEntities.stream() .filter(nowDataEntity -> buildingId.equals(nowDataEntity.getBuildingId())) .forEach(nowDataEntity -> nowDataEntity.setWaterLevel("离线")); + break; + } + } + } + } + + /** + * 处理泵设备状态 + */ + private void handlePumpStatus(String buildingId, List nowDataEntities, + String deviceType, Map pumpMethodMapping) { + List pumpList = deviceInstallMapper.selectOfflineDevice(buildingId, deviceType); + + for (DeviceInstallEntity pump : pumpList) { + for (Map.Entry entry : pumpMethodMapping.entrySet()) { + String pumpIdentifier = entry.getKey(); + String methodName = entry.getValue(); + + if (pump.getDeviceName().contains(pumpIdentifier)) { + updatePumpStatus(nowDataEntities, buildingId, pump, methodName); + break; } } } - return nowDataEntities; } + /** + * 更新单个泵的状态 + */ + private void updatePumpStatus(List nowDataEntities, String buildingId, + DeviceInstallEntity pump, String setterMethodName) { + try { + // 查询对应collectionParamManage参数 + CollectionParamsManageEntity runState = collectionParamsManageMapper + .selectByDeviceIdAndParamTypeId(pump.getId(), "2"); + + String status; + if (DateUtil.isToday(runState.getCurTime())) { + status = runState.getCurValue().intValue() == 1 ? "运行" : "不运行"; + } else { + status = "离线"; + } + + // 使用反射调用对应setter方法 + nowDataEntities.stream() + .filter(nowDataEntity -> buildingId.equals(nowDataEntity.getBuildingId())) + .forEach(nowDataEntity -> { + try { + nowDataEntity.getClass() + .getMethod(setterMethodName, String.class) + .invoke(nowDataEntity, status); + } catch (Exception e) { + // 记录日志或适当处理异常 + } + }); + } catch (Exception e) { + // 处理查询异常,设置为离线状态 + nowDataEntities.stream() + .filter(nowDataEntity -> buildingId.equals(nowDataEntity.getBuildingId())) + .forEach(nowDataEntity -> { + try { + nowDataEntity.getClass() + .getMethod(setterMethodName, String.class) + .invoke(nowDataEntity, "离线"); + } catch (Exception ex) { + // 记录日志或适当处理异常 + } + }); + } + } + + @Override - public NowDataEntity queryNowDataByPump(String buildingId,String pumpId) { + public NowDataEntity queryNowDataByPump(String buildingId, String pumpId) { // 查询当前楼栋离线的设备 List list = deviceInstallMapper.selectOfflineDevice(buildingId, "压变"); NowDataEntity nowDataEntity = nowDataMapper.queryNowDataByPump(buildingId, pumpId); @@ -462,28 +581,28 @@ public class NowDataServiceImpl implements NowDataService { } @Override - public List queryHistoryData(String curDate, String buildingId,String pumpId,String tankId, int page, int limit) { - return nowDataMapper.queryHistoryData(curDate,buildingId,pumpId,tankId,page,limit); + public List queryHistoryData(String curDate, String buildingId, String pumpId, String tankId, int page, int limit) { + return nowDataMapper.queryHistoryData(curDate, buildingId, pumpId, tankId, page, limit); } @Override - public int getHistoryDataCount(String curDate, String buildingId,String pumpId,String tankId, int page, int limit) { - return nowDataMapper.getHistoryDataCount(curDate,buildingId,pumpId,tankId,page,limit); + public int getHistoryDataCount(String curDate, String buildingId, String pumpId, String tankId, int page, int limit) { + return nowDataMapper.getHistoryDataCount(curDate, buildingId, pumpId, tankId, page, limit); } @Override public List queryWaterLevel(String curDate, String buildingID, int page, int limit) { - return nowDataMapper.queryWaterLevel(curDate,buildingID,page,limit); + return nowDataMapper.queryWaterLevel(curDate, buildingID, page, limit); } @Override public int getWaterLevelCount(String curDate, String buildingID) { - return nowDataMapper.getWaterLevelCount(curDate,buildingID); + return nowDataMapper.getWaterLevelCount(curDate, buildingID); } @Override - public List queryWaterLevelByTime(String curDate,int page,int limit) { - return nowDataMapper.queryWaterLevelByTime(curDate,page,limit); + public List queryWaterLevelByTime(String curDate, int page, int limit) { + return nowDataMapper.queryWaterLevelByTime(curDate, page, limit); } @Override @@ -508,36 +627,36 @@ public class NowDataServiceImpl implements NowDataService { } @Override - public String selectSingleTemp(String pumpId,String buildingId) { - return nowDataMapper.selectSingleTemp(pumpId,buildingId); + public String selectSingleTemp(String pumpId, String buildingId) { + return nowDataMapper.selectSingleTemp(pumpId, buildingId); } //从监视表查询热泵运行状态 @Override public String selectStateCount(String buildingId) { - int count=nowDataMapper.selectStateCount(buildingId); - String state=""; - if (count==0){ - state="不运行"; - }else{ - state="运行"; + int count = nowDataMapper.selectStateCount(buildingId); + String state = ""; + if (count == 0) { + state = "不运行"; + } else { + state = "运行"; } return state; } @Override public String selectState(String buildingId, String pumpId) { - return nowDataMapper.selectState(buildingId,pumpId); + return nowDataMapper.selectState(buildingId, pumpId); } @Override public void upLevelSet(String buildingId, String levelSet) { - nowDataMapper.upLevelSet(buildingId,levelSet); + nowDataMapper.upLevelSet(buildingId, levelSet); } @Override public void upTempSet(String buildingId, String tempSet) { - nowDataMapper.upTempSet(buildingId,tempSet); + nowDataMapper.upTempSet(buildingId, tempSet); } @Override @@ -546,18 +665,18 @@ public class NowDataServiceImpl implements NowDataService { } @Override - public List queryWaterTemp(String buildingID, String curDate,int page,int limit) { - return nowDataMapper.queryWaterTemp(buildingID,curDate,page,limit); + public List queryWaterTemp(String buildingID, String curDate, int page, int limit) { + return nowDataMapper.queryWaterTemp(buildingID, curDate, page, limit); } @Override public int queryWaterTempCount(String buildingID, String curDate) { - return nowDataMapper.queryWaterTempCount(buildingID,curDate); + return nowDataMapper.queryWaterTempCount(buildingID, curDate); } //查询所有楼栋每栋楼的平均温度 @Override - public List queryWaterTemp2(String curDate,int page,int limit, int level, String buildingID) { + public List queryWaterTemp2(String curDate, int page, int limit, int level, String buildingID) { List waterTempEntityList = nowDataMapper.queryWaterTemp2(curDate, page, limit); // 根据level层级查询 if (level == 1) { @@ -568,6 +687,7 @@ public class NowDataServiceImpl implements NowDataService { } return waterTempEntityList; } + //查询所有楼栋每栋楼的平均温度记录数 @Override public int queryWaterTempCount2(String curDate, int level, String buildingID) { @@ -581,7 +701,7 @@ public class NowDataServiceImpl implements NowDataService { try { lockB.lock(); try { - nowDataMapper.proWaterTemp(curDate,buildingID,pumpID); + nowDataMapper.proWaterTemp(curDate, buildingID, pumpID); } finally { lockB.unlock(); } @@ -613,11 +733,11 @@ public class NowDataServiceImpl implements NowDataService { } @Override - public void updateRunState(String buildingId, String pumpId,String strState, String buildingName) { - DeviceInstallEntity deviceInstallEntity=deviceInstallMapper.selectDevice(pumpId,"热泵",buildingId); - NowDataEntity data=new NowDataEntity(); - int r1 = nowDataMapper.selectNowDataCount(buildingId,pumpId);//判断now_data表是否存在记录 - if(r1==0){ + public void updateRunState(String buildingId, String pumpId, String strState, String buildingName) { + DeviceInstallEntity deviceInstallEntity = deviceInstallMapper.selectDevice(pumpId, "热泵", buildingId); + NowDataEntity data = new NowDataEntity(); + int r1 = nowDataMapper.selectNowDataCount(buildingId, pumpId);//判断now_data表是否存在记录 + if (r1 == 0) { data.setPumpId(pumpId); //通讯地址 data.setPumpName(deviceInstallEntity.getDeviceName()); //通讯地址 data.setBuildingId(buildingId); @@ -625,22 +745,22 @@ public class NowDataServiceImpl implements NowDataService { data.setRunState(strState); nowDataMapper.saveNowData(data); } - nowDataMapper.updateRunState(buildingId,pumpId,strState); + nowDataMapper.updateRunState(buildingId, pumpId, strState); } @Override public void proPumpMinutes(String buildingId, String pumpId, String runState) { - nowDataMapper.proPumpMinutes(buildingId,pumpId,runState); + nowDataMapper.proPumpMinutes(buildingId, pumpId, runState); } @Override - public List pumpMinutes(String startDate,String endDate, String buildingId, String pumpId, int page, int limit) { - return nowDataMapper.pumpMinutes(startDate,endDate,buildingId,pumpId,page,limit); + public List pumpMinutes(String startDate, String endDate, String buildingId, String pumpId, int page, int limit) { + return nowDataMapper.pumpMinutes(startDate, endDate, buildingId, pumpId, page, limit); } @Override - public int pumpMinutesCount(String startDate,String endDate, String buildingId, String pumpId) { - return nowDataMapper.pumpMinutesCount(startDate,endDate,buildingId,pumpId); + public int pumpMinutesCount(String startDate, String endDate, String buildingId, String pumpId) { + return nowDataMapper.pumpMinutesCount(startDate, endDate, buildingId, pumpId); } @Override diff --git a/user-service/src/main/java/com/mh/user/service/impl/NowPublicDataServiceImpl.java b/user-service/src/main/java/com/mh/user/service/impl/NowPublicDataServiceImpl.java index 832257c..5ffcfd9 100644 --- a/user-service/src/main/java/com/mh/user/service/impl/NowPublicDataServiceImpl.java +++ b/user-service/src/main/java/com/mh/user/service/impl/NowPublicDataServiceImpl.java @@ -1,11 +1,11 @@ package com.mh.user.service.impl; +import com.mh.common.utils.StringUtils; import com.mh.user.constants.Constant; +import com.mh.user.entity.CollectionParamsManageEntity; +import com.mh.user.entity.DeviceInstallEntity; import com.mh.user.entity.NowPublicDataEntity; -import com.mh.user.mapper.BuildingMapper; -import com.mh.user.mapper.DeviceInstallMapper; -import com.mh.user.mapper.NowDataMapper; -import com.mh.user.mapper.NowPublicDataMapper; +import com.mh.user.mapper.*; import com.mh.user.model.TempModel; import com.mh.user.service.DealDataService; import com.mh.user.service.NowPublicDataService; @@ -14,6 +14,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.math.BigDecimal; +import java.math.RoundingMode; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; @@ -39,6 +41,8 @@ public class NowPublicDataServiceImpl implements NowPublicDataService { @Autowired private DealDataService dealDataService; + @Autowired + private CollectionParamsManageMapper collectionParamsManageMapper; @Override public void saveNowPublicData(NowPublicDataEntity nowPublicDataEntity) { nowPublicDataMapper.saveNowPublicData(nowPublicDataEntity); @@ -54,6 +58,18 @@ public class NowPublicDataServiceImpl implements NowPublicDataService { NowPublicDataEntity nowPublicDataEntity = nowPublicDataMapper.queryNowPublicData(buildingId); String maxTemp = nowDataMapper.selectMaxTemp(buildingId, null); nowPublicDataEntity.setUseWaterTemp(maxTemp); + // 查询当前压力 + List pumpList = deviceInstallMapper.selectOfflineDevice(buildingId, "系统"); + for (DeviceInstallEntity pump : pumpList) { + CollectionParamsManageEntity pressure = collectionParamsManageMapper.selectByDeviceIdAndParamTypeId(pump.getId(), "5"); + if (pressure != null) { + String pressureValue = pressure.getCurValue().setScale(1, RoundingMode.HALF_UP).toString(); + if (!StringUtils.isBlank(pressureValue)) { + nowPublicDataEntity.setPressure(pressureValue); + break; + } + } + } return nowPublicDataEntity; } diff --git a/user-service/src/main/java/com/mh/user/service/mqtt/config/MqttConfig.java b/user-service/src/main/java/com/mh/user/service/mqtt/config/MqttConfig.java index 9062975..071b90f 100644 --- a/user-service/src/main/java/com/mh/user/service/mqtt/config/MqttConfig.java +++ b/user-service/src/main/java/com/mh/user/service/mqtt/config/MqttConfig.java @@ -5,6 +5,7 @@ import com.mh.user.constants.MqttProtocolEnum; import com.mh.user.constants.MqttUseEnum; import lombok.Data; import org.eclipse.paho.client.mqttv3.MqttConnectOptions; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -28,10 +29,21 @@ public class MqttConfig { private static Map mqttSpring; + // 添加MQTT启用开关 + private boolean mqttEnabled = false; + public void setMqttSpring(Map mqtt) { MqttConfig.mqttSpring = mqtt; } + public boolean isMqttEnabled() { + return mqttEnabled; + } + + public void setMqttEnabled(boolean mqttEnabled) { + this.mqttEnabled = mqttEnabled; + } + /** * 获取mqtt基本配置 * @return @@ -70,8 +82,8 @@ public class MqttConfig { * @return */ @Bean + @ConditionalOnProperty(name = "mqtt.enabled", havingValue = "true", matchIfMissing = false) public MqttConnectOptions mqttConnectionOptions() { - MqttConnectOptions mqttConnectOptions = new MqttConnectOptions(); MqttClientOptions customizeOptions = getBasicMqttClientOptions(); @@ -81,19 +93,19 @@ public class MqttConfig { customizeOptions.getUsername() : ""); mqttConnectOptions.setPassword(StringUtils.hasText(customizeOptions.getPassword()) ? customizeOptions.getPassword().toCharArray() : new char[0]); - // 直接进行自动连接 + // 设置为手动连接 mqttConnectOptions.setAutomaticReconnect(true); - // 时间间隔时间10s - mqttConnectOptions.setKeepAliveInterval(1000); + mqttConnectOptions.setKeepAliveInterval(30); // 30秒保活 return mqttConnectOptions; } @Bean + @ConditionalOnProperty(name = "mqtt.enabled", havingValue = "true", matchIfMissing = false) public MqttPahoClientFactory mqttClientFactory() { DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory(); factory.setConnectionOptions(mqttConnectionOptions()); return factory; } - } + diff --git a/user-service/src/main/java/com/mh/user/service/mqtt/config/MqttInboundConfig.java b/user-service/src/main/java/com/mh/user/service/mqtt/config/MqttInboundConfig.java index 509f764..f10b92e 100644 --- a/user-service/src/main/java/com/mh/user/service/mqtt/config/MqttInboundConfig.java +++ b/user-service/src/main/java/com/mh/user/service/mqtt/config/MqttInboundConfig.java @@ -4,6 +4,7 @@ import com.mh.user.constants.ChannelName; import com.mh.user.constants.MqttClientOptions; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.integration.annotation.IntegrationComponentScan; @@ -28,6 +29,7 @@ import javax.annotation.Resource; @Slf4j @Configuration @IntegrationComponentScan +@ConditionalOnProperty(name = "mqtt.enabled", havingValue = "true", matchIfMissing = false) public class MqttInboundConfig { @Autowired @@ -38,6 +40,8 @@ public class MqttInboundConfig { private String clientId; + private MqttPahoMessageDrivenChannelAdapter adapter; + /** * 入站适配器 * @return @@ -45,29 +49,48 @@ public class MqttInboundConfig { @Bean(name = "adapter") public MessageProducerSupport mqttInbound() { MqttClientOptions options = MqttConfig.getBasicMqttClientOptions(); - // 此处初始化的时候,默认订阅了配置文件中已经写好的topic - // 如果需要订阅多个,可以自己手动订阅,会写一个addTopic()进行添加订阅 clientId = options.getClientId() + "_consumer_" + System.currentTimeMillis(); - MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter( + adapter = new MqttPahoMessageDrivenChannelAdapter( clientId, mqttClientFactory, options.getInboundTopic().split(",")); - // System.out.println("每一次都会入站适配器吗?"+clientId); + DefaultPahoMessageConverter converter = new DefaultPahoMessageConverter(); - // 统一是字节处理 converter.setPayloadAsBytes(true); - // 设置消息转换器 adapter.setConverter(converter); - // 设置qos(quality of service) - // 0:最多一次传输(消息会丢失), - // 1:至少一次传输(消息会重复), - // 2:只有当消息发送成功时才确认(消息不回丢,但延迟高)。 adapter.setQos(0); - // 设置在接收已经订阅的主题信息后,发送给哪个通道,具体的发送方法需要翻上层的抽象类 adapter.setOutputChannel(inboundChannel); return adapter; } + /** + * 手动启动MQTT连接 + */ + public void startMqtt() { + if (adapter != null && !adapter.isRunning()) { + adapter.start(); + log.info("MQTT入站适配器已启动"); + } + } + + /** + * 手动停止MQTT连接 + */ + public void stopMqtt() { + if (adapter != null && adapter.isRunning()) { + adapter.stop(); + log.info("MQTT入站适配器已停止"); + } + } + + /** + * 检查MQTT连接状态 + * @return + */ + public boolean isMqttRunning() { + return adapter != null && adapter.isRunning(); + } + /** * 默认声明一个消息处理器,用于处理无效的消息 * @return @@ -76,16 +99,16 @@ public class MqttInboundConfig { @ServiceActivator(inputChannel = ChannelName.DEFAULT_BOUND) public MessageHandler handler() { return message -> { - log.info("The default channel does not handle messages." + - "\nTopic: {}" + - "\nPayload: {}", - message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC), - message.getPayload()); +// log.info("The default channel does not handle messages." + +// "\nTopic: {}" + +// "\nPayload: {}", +// message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC), +// message.getPayload()); }; } public String getClientId() { return clientId; } - } + diff --git a/user-service/src/main/java/com/mh/user/service/mqtt/config/MqttOutboundConfig.java b/user-service/src/main/java/com/mh/user/service/mqtt/config/MqttOutboundConfig.java index dc4f34d..8cbb472 100644 --- a/user-service/src/main/java/com/mh/user/service/mqtt/config/MqttOutboundConfig.java +++ b/user-service/src/main/java/com/mh/user/service/mqtt/config/MqttOutboundConfig.java @@ -3,6 +3,7 @@ package com.mh.user.service.mqtt.config; import com.mh.user.constants.ChannelName; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.integration.annotation.IntegrationComponentScan; @@ -22,11 +23,14 @@ import org.springframework.messaging.MessageHandler; @Slf4j @Configuration @IntegrationComponentScan +@ConditionalOnProperty(name = "mqtt.enabled", havingValue = "true", matchIfMissing = false) public class MqttOutboundConfig { @Autowired private MqttPahoClientFactory mqttClientFactory; + private MqttPahoMessageHandler messageHandler; + /** * 默认声明一个出站处理器,用于处理无效的消息 * @return @@ -34,11 +38,10 @@ public class MqttOutboundConfig { @Bean @ServiceActivator(inputChannel = ChannelName.OUTBOUND) public MessageHandler mqttOutbound() { - MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler( + messageHandler = new MqttPahoMessageHandler( MqttConfig.getBasicMqttClientOptions().getClientId() + "_producer_" + System.currentTimeMillis(), mqttClientFactory); DefaultPahoMessageConverter converter = new DefaultPahoMessageConverter(); - // use byte types uniformly converter.setPayloadAsBytes(true); messageHandler.setAsync(true); @@ -47,5 +50,24 @@ public class MqttOutboundConfig { return messageHandler; } + /** + * 手动启动MQTT出站连接 + */ + public void startMqtt() { + if (messageHandler != null) { + // 出站处理器通常不需要显式启动 + log.info("MQTT出站处理器已准备就绪"); + } + } + /** + * 手动停止MQTT出站连接 + */ + public void stopMqtt() { + if (messageHandler != null) { + // 出站处理器通常不需要显式停止 + log.info("MQTT出站处理器已停止"); + } + } } + diff --git a/user-service/src/main/java/com/mh/user/service/mqtt/service/IMqttManagerService.java b/user-service/src/main/java/com/mh/user/service/mqtt/service/IMqttManagerService.java new file mode 100644 index 0000000..873ac44 --- /dev/null +++ b/user-service/src/main/java/com/mh/user/service/mqtt/service/IMqttManagerService.java @@ -0,0 +1,17 @@ +package com.mh.user.service.mqtt.service; + +/** + * @author LJF + * @version 1.0 + * @project CHWS + * @description mqtt启动停止管理接口 + * @date 2025-12-19 13:59:13 + */ +public interface IMqttManagerService { + + void startMqtt(); + + void stopMqtt(); + + boolean isMqttRunning(); +} diff --git a/user-service/src/main/java/com/mh/user/service/mqtt/service/impl/EventsServiceImpl.java b/user-service/src/main/java/com/mh/user/service/mqtt/service/impl/EventsServiceImpl.java index 1f5e7ce..d58327c 100644 --- a/user-service/src/main/java/com/mh/user/service/mqtt/service/impl/EventsServiceImpl.java +++ b/user-service/src/main/java/com/mh/user/service/mqtt/service/impl/EventsServiceImpl.java @@ -2,6 +2,7 @@ package com.mh.user.service.mqtt.service.impl; import com.alibaba.fastjson2.JSONObject; import com.fasterxml.jackson.databind.ObjectMapper; +import com.mh.common.utils.StringUtils; import com.mh.user.constants.ChannelName; import com.mh.user.constants.Constant; import com.mh.user.entity.CollectionParamsManageEntity; @@ -95,6 +96,12 @@ public class EventsServiceImpl implements IEventsService { String time = datas.getTime(); // 更新网关设备在线状态 gatewayManageService.updateGatewayManageOnlineBySn(sn, 0); + // 获取网关对应的buildingId + String buildingId = gatewayManageService.queryBuildingIdBySn(sn); + if (StringUtils.isBlank(buildingId)) { + log.error("未找到对应的buildingId"); + return; + } // 修复类型转换问题 List rawDataList = datas.getDatas(); for (Object rawData : rawDataList) { @@ -110,7 +117,7 @@ public class EventsServiceImpl implements IEventsService { } catch (Exception e) { log.error("mqtt数据上报异常", e); data.setName(jsonObject.getString("name")); - data.setValue(0); + data.setValue("-1"); } } // 不使用消息队列,查询属于哪种设备类型,然后通过策略进行数据解析 @@ -125,9 +132,9 @@ public class EventsServiceImpl implements IEventsService { value = BigDecimal.ZERO; } // 直接更新collectionParamManage参数值 - collectionParamManageService.updateCPMByOtherName(name, value, time); + collectionParamManageService.updateCPMByOtherName(name, value, time, buildingId); // 查询device_install表,走之前的逻辑 - CollectionParamsManageEntity collectionParamsManageEntity = collectionParamManageService.selectDeviceInstallByOtherName(name); + CollectionParamsManageEntity collectionParamsManageEntity = collectionParamManageService.selectDeviceInstallByOtherName(name, buildingId); if (null != collectionParamsManageEntity && collectionParamsManageEntity.getDeviceInstallId() != null) { DeviceInstallEntity deviceInstallEntity = deviceInstallService.selectDeviceById(collectionParamsManageEntity.getDeviceInstallId()); if (deviceInstallEntity != null) { diff --git a/user-service/src/main/java/com/mh/user/service/mqtt/service/impl/MqttManagerServiceImpl.java b/user-service/src/main/java/com/mh/user/service/mqtt/service/impl/MqttManagerServiceImpl.java new file mode 100644 index 0000000..94425d8 --- /dev/null +++ b/user-service/src/main/java/com/mh/user/service/mqtt/service/impl/MqttManagerServiceImpl.java @@ -0,0 +1,64 @@ +package com.mh.user.service.mqtt.service.impl; + +import com.mh.user.service.mqtt.config.MqttInboundConfig; +import com.mh.user.service.mqtt.config.MqttOutboundConfig; +import com.mh.user.service.mqtt.service.IMqttManagerService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.stereotype.Service; + +/** + * @author LJF + * @version 1.0 + * @project CHWS + * @description 管理mqtt启动和关闭 + * @date 2025-12-19 13:57:41 + */ +@Slf4j +@Service +@ConditionalOnProperty(name = "mqtt.enabled", havingValue = "true", matchIfMissing = false) +public class MqttManagerServiceImpl implements IMqttManagerService { + + @Autowired + private MqttInboundConfig mqttInboundConfig; + + @Autowired + private MqttOutboundConfig mqttOutboundConfig; + + /** + * 启动MQTT连接 + */ + @Override + public void startMqtt() { + try { + mqttInboundConfig.startMqtt(); + mqttOutboundConfig.startMqtt(); + } catch (Exception e) { + log.error("启动MQTT连接失败", e); + } + } + + /** + * 停止MQTT连接 + */ + @Override + public void stopMqtt() { + try { + mqttInboundConfig.stopMqtt(); + mqttOutboundConfig.stopMqtt(); + } catch (Exception e) { + log.error("停止MQTT连接失败", e); + } + } + + /** + * 检查MQTT连接状态 + * @return + */ + @Override + public boolean isMqttRunning() { + return mqttInboundConfig.isMqttRunning(); + } +} + diff --git a/user-service/src/main/java/com/mh/user/service/mqtt/service/impl/MqttTopicServiceImpl.java b/user-service/src/main/java/com/mh/user/service/mqtt/service/impl/MqttTopicServiceImpl.java index f66f7b5..29ce785 100644 --- a/user-service/src/main/java/com/mh/user/service/mqtt/service/impl/MqttTopicServiceImpl.java +++ b/user-service/src/main/java/com/mh/user/service/mqtt/service/impl/MqttTopicServiceImpl.java @@ -1,6 +1,7 @@ package com.mh.user.service.mqtt.service.impl; import com.mh.user.service.mqtt.service.IMqttTopicService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter; import org.springframework.stereotype.Service; @@ -17,25 +18,35 @@ import javax.annotation.Resource; public class MqttTopicServiceImpl implements IMqttTopicService { @Resource + @Autowired(required = false) private MqttPahoMessageDrivenChannelAdapter adapter; @Override public void subscribe(String topic) { - adapter.addTopic(topic); + if (adapter != null) { + adapter.addTopic(topic); + } } @Override public void subscribe(String topic, int qos) { - adapter.addTopic(topic, qos); + if (adapter != null) { + adapter.addTopic(topic, qos); + } } @Override public void unsubscribe(String topic) { - adapter.removeTopic(topic); + if (adapter != null) { + adapter.removeTopic(topic); + } } @Override public String[] getSubscribedTopics() { - return adapter.getTopic(); + if (adapter != null) { + return adapter.getTopic(); + } + return new String[0]; } } diff --git a/user-service/src/main/java/com/mh/user/strategy/WaterLevelSwitchStrategy.java b/user-service/src/main/java/com/mh/user/strategy/WaterLevelSwitchStrategy.java index a240211..92ffdd1 100644 --- a/user-service/src/main/java/com/mh/user/strategy/WaterLevelSwitchStrategy.java +++ b/user-service/src/main/java/com/mh/user/strategy/WaterLevelSwitchStrategy.java @@ -204,6 +204,7 @@ public class WaterLevelSwitchStrategy implements DeviceStrategy { if ((customName.contains(Constant.CUSTOM_NAME_HUAXIA) || customName.contains(Constant.CUSTOM_NAME_GUANGSHANG) || customName.contains(Constant.CUSTOM_NAME_HUARUAN) + || customName.contains(Constant.CUSTOM_NAME_LIGONG) ) && !Constant.WEB_FLAG ) { analysisPumpStateOrder(dateStr, dataStr, registerAddr, brand, buildingId, buildingName); return analysisRelayOrder485(dateStr, dataStr,registerAddr,brand,buildingId, buildingName, customName); @@ -529,6 +530,26 @@ public class WaterLevelSwitchStrategy implements DeviceStrategy { publicData.setUpWater("不运行"); } log.info("-----------------补水状态:" + l1 + "-----------------"); + } else if (customName.contains(Constant.CUSTOM_NAME_LIGONG)) { + if (l1.equals("0001")) { //1路,0000表示有输入,0001表示无输入 + publicData.setUseWater("运行"); //供水 + } else { + publicData.setUseWater("不运行"); + } + log.info("-----------------供水状态:" + l1 + "-----------------"); + // 回水 + if (l3.equals("0001")) { + publicData.setBackWater("运行");//回水 + } else { + publicData.setBackWater("不运行"); + } + log.info("-----------------回水状态:" + l3 + "-----------------"); + if (l2.equals("0001")) { //2路 //补水 + publicData.setUpWater("运行"); + } else { + publicData.setUpWater("不运行"); + } + log.info("-----------------补水状态:" + l2 + "-----------------"); } else { if (l2.equals("0001") || l3.equals("0001")) { //2,3路,0000表示有输入,0001表示无输入 publicData.setUseWater("运行"); //供水 diff --git a/user-service/src/main/resources/application-dev.yml b/user-service/src/main/resources/application-dev.yml index ec239fd..bef93a8 100644 --- a/user-service/src/main/resources/application-dev.yml +++ b/user-service/src/main/resources/application-dev.yml @@ -1,7 +1,7 @@ # 项目相关配置 mh: # 名称 - name: nfxy + name: nfxy_dev # 版本 version: 1.0.0 # 版权年份 @@ -110,6 +110,9 @@ logging: amap: key: 984603bf28ef94ac78765a3ea27a6c26 +mqtt: + enabled: true + # mqtt服务器地址 mqttSpring: # BASIC parameters are required. BASIC: @@ -131,7 +134,7 @@ mqttSpring: port: 8083 path: /mqtt control: - topic: chws_nfxy_mqtt_dev/control/events_upload/devices + topic: mh_control/events_control/devices diff --git a/user-service/src/main/resources/application-prod.yml b/user-service/src/main/resources/application-prod.yml index eb33eba..b1fc53a 100644 --- a/user-service/src/main/resources/application-prod.yml +++ b/user-service/src/main/resources/application-prod.yml @@ -14,10 +14,10 @@ spring: # password: mh@803 ## 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;encrypt=false -# driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver -# username: test -# password: minghan123456@ + url: jdbc:sqlserver://111.230.50.186:32012;DatabaseName=CHWS;allowMultiQueries=true;encrypt=false + driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver + username: test + password: minghan123456@ # #华厦云服务器 # url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=CHWS;allowMultiQueries=true # driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver @@ -29,10 +29,10 @@ spring: # username: chws_gsh # password: Mhtech@803 #广商服务器 - url: jdbc:sqlserver://175.178.153.91:8033;DatabaseName=chws_gsh;allowMultiQueries=true;encrypt=false - driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver - username: chws_gsh - password: Mhtech@803gsh +# url: jdbc:sqlserver://175.178.153.91:8033;DatabaseName=chws_gsh;allowMultiQueries=true;encrypt=false +# driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver +# username: chws_gsh +# password: Mhtech@803gsh #本机 # url: jdbc:sqlserver://127.0.0.1:9956;DatabaseName=CHWS;allowMultiQueries=true # driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver @@ -143,11 +143,14 @@ logging: amap: key: 984603bf28ef94ac78765a3ea27a6c26 +mqtt: + enabled: true + mqttSpring: # BASIC parameters are required. BASIC: protocol: MQTT - host: 192.168.1.79 + host: 192.168.1.179 port: 1883 username: test password: test123456 @@ -160,7 +163,7 @@ mqttSpring: # 无人机远程控制模式(drone remote control) DRC: protocol: WS - host: 192.168.1.79 + host: 192.168.1.179 port: 8083 path: /mqtt control: