Browse Source

1、添加优化设备台账在线状态;

2、优化mqtt主题订阅数据,改成通过项目进行匹配;
dev
mh 1 month ago
parent
commit
593f1528b8
  1. 6
      mh-admin/src/main/resources/logback.xml
  2. 8
      mh-common/src/main/java/com/mh/common/constant/TopicEnum.java
  3. 19
      mh-framework/src/main/java/com/mh/framework/mqtt/handler/InboundMessageRouter.java
  4. 29
      mh-quartz/src/main/java/com/mh/quartz/task/DealOnOrOffData.java
  5. 71
      mh-system/src/main/java/com/mh/system/mapper/device/CollectionParamsManageMapper.java
  6. 5
      mh-system/src/main/java/com/mh/system/mapper/device/DeviceLedgerMapper.java
  7. 2
      mh-system/src/main/java/com/mh/system/service/device/IDeviceLedgerService.java
  8. 15
      mh-system/src/main/java/com/mh/system/service/device/impl/DeviceLedgerServiceImpl.java

6
mh-admin/src/main/resources/logback.xml

@ -20,7 +20,7 @@
<!-- 日志文件名格式 --> <!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/sys-info.%d{yyyy-MM-dd}.log</fileNamePattern> <fileNamePattern>${log.path}/sys-info.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 --> <!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory> <maxHistory>1</maxHistory>
</rollingPolicy> </rollingPolicy>
<encoder> <encoder>
<pattern>${log.pattern}</pattern> <pattern>${log.pattern}</pattern>
@ -42,7 +42,7 @@
<!-- 日志文件名格式 --> <!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/sys-error.%d{yyyy-MM-dd}.log</fileNamePattern> <fileNamePattern>${log.path}/sys-error.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 --> <!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory> <maxHistory>3</maxHistory>
</rollingPolicy> </rollingPolicy>
<encoder> <encoder>
<pattern>${log.pattern}</pattern> <pattern>${log.pattern}</pattern>
@ -64,7 +64,7 @@
<!-- 按天回滚 daily --> <!-- 按天回滚 daily -->
<fileNamePattern>${log.path}/sys-user.%d{yyyy-MM-dd}.log</fileNamePattern> <fileNamePattern>${log.path}/sys-user.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 --> <!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory> <maxHistory>1</maxHistory>
</rollingPolicy> </rollingPolicy>
<encoder> <encoder>
<pattern>${log.pattern}</pattern> <pattern>${log.pattern}</pattern>

8
mh-common/src/main/java/com/mh/common/constant/TopicEnum.java

@ -1,5 +1,7 @@
package com.mh.common.constant; package com.mh.common.constant;
import com.mh.common.config.MHConfig;
import java.util.Arrays; import java.util.Arrays;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -53,7 +55,9 @@ public enum TopicEnum {
return beanName; return beanName;
} }
public static TopicEnum find(String topic) { public static TopicEnum find(String proName, String topic) {
return Arrays.stream(TopicEnum.values()).filter(topicEnum -> topicEnum.pattern.matcher(topic).matches()).findAny().orElse(UNKNOWN); // 去掉第一个"/"以及之前数据
String finalTopic = topic.replaceFirst("^"+proName, "");;
return Arrays.stream(TopicEnum.values()).filter(topicEnum -> topicEnum.pattern.matcher(finalTopic).matches()).findAny().orElse(UNKNOWN);
} }
} }

19
mh-framework/src/main/java/com/mh/framework/mqtt/handler/InboundMessageRouter.java

@ -1,9 +1,11 @@
package com.mh.framework.mqtt.handler; package com.mh.framework.mqtt.handler;
import com.mh.common.config.MHConfig;
import com.mh.common.constant.ChannelName; import com.mh.common.constant.ChannelName;
import com.mh.common.constant.TopicEnum; import com.mh.common.constant.TopicEnum;
import com.mh.common.utils.spring.SpringUtils; import com.mh.common.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.annotation.Router; import org.springframework.integration.annotation.Router;
import org.springframework.integration.mqtt.support.MqttHeaders; import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.integration.router.AbstractMessageRouter; import org.springframework.integration.router.AbstractMessageRouter;
@ -27,6 +29,10 @@ import java.util.Objects;
@Component @Component
public class InboundMessageRouter extends AbstractMessageRouter { public class InboundMessageRouter extends AbstractMessageRouter {
/** 系统基础配置 */
@Autowired
private MHConfig mHConfig;
/** /**
* 目前只需要这个方式后期在拓展使用IntegrationFlow方式 * 目前只需要这个方式后期在拓展使用IntegrationFlow方式
* @param message * @param message
@ -39,9 +45,18 @@ public class InboundMessageRouter extends AbstractMessageRouter {
String topic = Objects.requireNonNull(headers.get(MqttHeaders.RECEIVED_TOPIC)).toString(); String topic = Objects.requireNonNull(headers.get(MqttHeaders.RECEIVED_TOPIC)).toString();
// byte[] payload = (byte[]) message.getPayload(); // byte[] payload = (byte[]) message.getPayload();
// log.info("从当前主题 topic: {}, 接收到的消息:{}", topic, new String(payload)); // log.info("从当前主题 topic: {}, 接收到的消息:{}", topic, new String(payload));
// 判断当前主题是否是当前项目的,温湿度目前写死的
if (!topic.startsWith(mHConfig.getName()) && !topic.contains("/temp")) {
log.info("当前主题 topic: {} 不是当前项目的,直接丢弃", topic);
return Collections.singleton(SpringUtils.getBean(ChannelName.DEFAULT_BOUND));
}
// 找到对应的主题消息通道 // 找到对应的主题消息通道
TopicEnum topicEnum = TopicEnum.find(topic); if (topic.contains("/temp")) {
MessageChannel bean = (MessageChannel) SpringUtils.getBean(topicEnum.getBeanName()); return Collections.singleton(SpringUtils.getBean(ChannelName.EVENTS_UPLOAD_INBOUND));
} else {
TopicEnum topicEnum = TopicEnum.find(mHConfig.getName() + "/", topic);
MessageChannel bean = SpringUtils.getBean(topicEnum.getBeanName());
return Collections.singleton(bean); return Collections.singleton(bean);
} }
}
} }

29
mh-quartz/src/main/java/com/mh/quartz/task/DealOnOrOffData.java

@ -0,0 +1,29 @@
package com.mh.quartz.task;
import com.mh.system.service.device.IDeviceLedgerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @author LJF
* @version 1.0
* @project EEMCS
* @description 更新在线状态
* @date 2025-04-27 09:32:08
*/
@Slf4j
@Component("dealOnOrOffData")
public class DealOnOrOffData {
private IDeviceLedgerService deviceLedgerService;
public DealOnOrOffData(IDeviceLedgerService deviceLedgerService) {
this.deviceLedgerService = deviceLedgerService;
}
public void dealDeviceLedger() {
log.info("处理设备在线状态数据");
deviceLedgerService.updateDeviceLedgerStatus();
}
}

71
mh-system/src/main/java/com/mh/system/mapper/device/CollectionParamsManageMapper.java

@ -139,7 +139,8 @@ public interface CollectionParamsManageMapper extends BaseMapper<CollectionParam
" and csr.area_id = #{areaId} ") " and csr.area_id = #{areaId} ")
List<CollectionParamsManage> selectByAreaId(@Param("areaId") String areaId); List<CollectionParamsManage> selectByAreaId(@Param("areaId") String areaId);
@Select("select " + @Select("WITH ranked_data AS ( " +
" SELECT " +
" csr.house_id, " + " csr.house_id, " +
" hi.house_name, " + " hi.house_name, " +
" cpm.other_name, " + " cpm.other_name, " +
@ -149,27 +150,46 @@ public interface CollectionParamsManageMapper extends BaseMapper<CollectionParam
" dl.status, " + " dl.status, " +
" hi.order_num, " + " hi.order_num, " +
" cpm.param_type, " + " cpm.param_type, " +
" case " + " CASE " +
" when ar.create_time >= CURRENT_DATE " + " WHEN ar.create_time >= CURRENT_DATE AND ar.create_time < CURRENT_DATE + interval '1 day' " +
" and ar.create_time < CURRENT_DATE + interval '1 day' then 1 " + " THEN 1 " +
" else 0 " + " ELSE 0 " +
" end as alarm_status " + " END as alarm_status, " +
"from " + " ROW_NUMBER() OVER ( " +
" PARTITION BY csr.house_id " +
" ORDER BY " +
" CASE WHEN ar.create_time >= CURRENT_DATE AND ar.create_time < CURRENT_DATE + interval '1 day' THEN 0 ELSE 1 END, " +
" cpm.cur_time DESC " +
" ) as row_num " +
" FROM " +
" cpm_space_relation csr " + " cpm_space_relation csr " +
"join collection_params_manage cpm on " + " JOIN collection_params_manage cpm ON csr.cpm_id = cpm.id " +
" csr.cpm_id = cpm.id " + " RIGHT JOIN house_info hi ON csr.house_id = hi.id " +
"join house_info hi on " + " JOIN device_ledger dl ON dl.id = cpm.device_ledger_id " +
" csr.house_id = hi.id " + " LEFT JOIN alarm_records ar ON ar.cpm_id = cpm.id " +
"join device_ledger dl on " + " WHERE " +
" dl.id = cpm.device_ledger_id " +
"left join alarm_records ar on " +
" ar.cpm_id = cpm.id " +
"where " +
" csr.floor_id = #{floorId} " + " csr.floor_id = #{floorId} " +
" and hi.floor_id = #{floorId} " + " AND hi.floor_id = #{floorId} " +
" and cpm.param_type = #{paramType} " + " AND cpm.param_type = #{paramType} " +
" and cpm.system_type = #{systemType}" + " AND cpm.system_type = #{systemType} " +
" order by hi.order_num ; ") ") " +
"SELECT " +
" house_id, " +
" house_name, " +
" other_name, " +
" cur_value, " +
" cur_time, " +
" cpm_id, " +
" status, " +
" order_num, " +
" param_type, " +
" alarm_status " +
"FROM " +
" ranked_data " +
"WHERE " +
" row_num = 1 " +
"ORDER BY " +
" order_num; ")
List<TempHumidityDTO> selectByParamType(@Param("systemType") String systemType, List<TempHumidityDTO> selectByParamType(@Param("systemType") String systemType,
@Param("floorId") String floorId, @Param("floorId") String floorId,
@Param("paramType") String paramType); @Param("paramType") String paramType);
@ -307,4 +327,15 @@ public interface CollectionParamsManageMapper extends BaseMapper<CollectionParam
@Update("update collection_params_manage set cur_value = #{curValue}, cur_time = #{curTime} where id = #{id} ") @Update("update collection_params_manage set cur_value = #{curValue}, cur_time = #{curTime} where id = #{id} ")
void updateCurValueById(@Param("id") String id, @Param("curValue") BigDecimal curValue, @Param("curTime") Date curTime); void updateCurValueById(@Param("id") String id, @Param("curValue") BigDecimal curValue, @Param("curTime") Date curTime);
@Select("select" +
" device_ledger_id " +
"from" +
" collection_params_manage cpm " +
"where" +
" is_use = 0 " +
" and date(cur_time) != date(current_timestamp) " +
" and device_ledger_id is not null " +
" group by device_ledger_id;")
List<String> OffLine();
} }

5
mh-system/src/main/java/com/mh/system/mapper/device/DeviceLedgerMapper.java

@ -16,6 +16,9 @@ import org.apache.ibatis.annotations.Update;
@Mapper @Mapper
public interface DeviceLedgerMapper extends BaseMapper<DeviceLedger> { public interface DeviceLedgerMapper extends BaseMapper<DeviceLedger> {
@Update("update device_ledger set status = 0 where id = #{id}") @Update("update device_ledger set status = 0, update_time = current_timestamp where id = #{id}")
void updateOnlineStatus(@Param("id") String deviceLedgerId); void updateOnlineStatus(@Param("id") String deviceLedgerId);
@Update("update device_ledger set status = #{status}, update_time = current_timestamp where id = #{id}")
void updateOnlineOrOfflineStatus(@Param("id") String deviceLedgerId, @Param("status") int integer);
} }

2
mh-system/src/main/java/com/mh/system/service/device/IDeviceLedgerService.java

@ -21,4 +21,6 @@ public interface IDeviceLedgerService {
int updateDeviceLedger(DeviceLedger gatewayManage); int updateDeviceLedger(DeviceLedger gatewayManage);
int deleteDeviceLedgerByIds(String[] ledgerIds); int deleteDeviceLedgerByIds(String[] ledgerIds);
void updateDeviceLedgerStatus();
} }

15
mh-system/src/main/java/com/mh/system/service/device/impl/DeviceLedgerServiceImpl.java

@ -1,8 +1,10 @@
package com.mh.system.service.device.impl; package com.mh.system.service.device.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mh.common.core.domain.entity.CollectionParamsManage;
import com.mh.common.core.domain.entity.DeviceLedger; import com.mh.common.core.domain.entity.DeviceLedger;
import com.mh.common.utils.StringUtils; import com.mh.common.utils.StringUtils;
import com.mh.system.mapper.device.CollectionParamsManageMapper;
import com.mh.system.mapper.device.DeviceLedgerMapper; import com.mh.system.mapper.device.DeviceLedgerMapper;
import com.mh.system.service.device.IDeviceLedgerService; import com.mh.system.service.device.IDeviceLedgerService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
@ -25,6 +27,19 @@ public class DeviceLedgerServiceImpl implements IDeviceLedgerService {
@Resource @Resource
private DeviceLedgerMapper deviceLedgerMapper; private DeviceLedgerMapper deviceLedgerMapper;
@Resource
private CollectionParamsManageMapper collectionParamsManageMapper;
@Override
public void updateDeviceLedgerStatus() {
// 先从采集点为中查询,看看当天是否有数据上来
List<String> deviceLedgerList = collectionParamsManageMapper.OffLine();
// 开始根据id更新设备台账状态
for (String deviceLedgerId : deviceLedgerList) {
deviceLedgerMapper.updateOnlineOrOfflineStatus(deviceLedgerId, 1);
}
}
@Override @Override
public List<DeviceLedger> selectDeviceLedgerList(DeviceLedger ledgerInfo) { public List<DeviceLedger> selectDeviceLedgerList(DeviceLedger ledgerInfo) {
if (ledgerInfo == null) { if (ledgerInfo == null) {

Loading…
Cancel
Save