diff --git a/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/RemoteTdEngineService.java b/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/RemoteTdEngineService.java index 0c3e55d..f123653 100644 --- a/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/RemoteTdEngineService.java +++ b/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/RemoteTdEngineService.java @@ -60,4 +60,7 @@ public interface RemoteTdEngineService { @PostMapping("/tdengine/getLastOnlineDevices") R getLastOnlineDevices(@Validated @RequestBody DeviceStatus queryDeviceStatus, @RequestHeader(SecurityConstants.FROM_SOURCE) String source); + + @PostMapping("/tdengine/getDeviceStatusList") + R>> getDeviceStatusList(@Validated @RequestBody DeviceStatus queryDeviceStatus, @RequestHeader(SecurityConstants.FROM_SOURCE) String source); } diff --git a/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/domain/DeviceStatus.java b/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/domain/DeviceStatus.java index 0fca5cb..fc3153e 100644 --- a/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/domain/DeviceStatus.java +++ b/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/domain/DeviceStatus.java @@ -19,9 +19,9 @@ public class DeviceStatus extends BaseEntity { private String deviceCode; - private int deviceType; + private Integer deviceType; - private int onlineStatus; + private Integer onlineStatus; private Long sceneId; @@ -54,19 +54,19 @@ public class DeviceStatus extends BaseEntity { this.deviceCode = deviceCode; } - public int getDeviceType() { + public Integer getDeviceType() { return deviceType; } - public void setDeviceType(int deviceType) { + public void setDeviceType(Integer deviceType) { this.deviceType = deviceType; } - public int getOnlineStatus() { + public Integer getOnlineStatus() { return onlineStatus; } - public void setOnlineStatus(int onlineStatus) { + public void setOnlineStatus(Integer onlineStatus) { this.onlineStatus = onlineStatus; } diff --git a/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/factory/RemoteTdEngineFallbackFactory.java b/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/factory/RemoteTdEngineFallbackFactory.java index f18c87f..7e737b2 100644 --- a/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/factory/RemoteTdEngineFallbackFactory.java +++ b/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/factory/RemoteTdEngineFallbackFactory.java @@ -97,6 +97,11 @@ public class RemoteTdEngineFallbackFactory implements FallbackFactory>> getDeviceStatusList(DeviceStatus queryDeviceStatus, String source) { + return R.fail("获取设备状态信息失败:" + throwable.getMessage()); + } + }; } } diff --git a/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/domain/HwAlarmRuleLink.java b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/domain/HwAlarmRuleLink.java index 9f08cfa..a0aefba 100644 --- a/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/domain/HwAlarmRuleLink.java +++ b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/domain/HwAlarmRuleLink.java @@ -18,6 +18,8 @@ public class HwAlarmRuleLink extends BaseEntity /** 报警规则关联ID,主键 */ private Long ruleLinkId; + private String alarmRuleType; + /** 报警规则ID,关联hw_alarm_rule的alarm_rule_id */ @Excel(name = "报警规则ID,关联hw_alarm_rule的alarm_rule_id") private Long alarmRuleId; @@ -42,6 +44,7 @@ public class HwAlarmRuleLink extends BaseEntity @Excel(name = "联动设备服务的参数值,json格式保存") private String linkDeviceFunctionData; + private String linkDeviceCode; private String linkDeviceName; private String linkDeviceModeFunctionName; @@ -56,7 +59,16 @@ public class HwAlarmRuleLink extends BaseEntity { return ruleLinkId; } - public void setAlarmRuleId(Long alarmRuleId) + + public String getAlarmRuleType() { + return alarmRuleType; + } + + public void setAlarmRuleType(String alarmRuleType) { + this.alarmRuleType = alarmRuleType; + } + + public void setAlarmRuleId(Long alarmRuleId) { this.alarmRuleId = alarmRuleId; } @@ -111,6 +123,14 @@ public class HwAlarmRuleLink extends BaseEntity return linkDeviceFunctionData; } + public String getLinkDeviceCode() { + return linkDeviceCode; + } + + public void setLinkDeviceCode(String linkDeviceCode) { + this.linkDeviceCode = linkDeviceCode; + } + public String getLinkDeviceName() { return linkDeviceName; } diff --git a/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/domain/HwOfflineRule.java b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/domain/HwOfflineRule.java new file mode 100644 index 0000000..b010a47 --- /dev/null +++ b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/domain/HwOfflineRule.java @@ -0,0 +1,338 @@ +package com.ruoyi.dataprocess.domain; + +import com.ruoyi.common.core.annotation.Excel; +import com.ruoyi.common.core.web.domain.BaseEntity; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + +import java.math.BigDecimal; +import java.util.List; + +/** + * 离线报警规则对象 hw_offline_rule + * + * @author xins + * @date 2023-11-13 + */ +public class HwOfflineRule extends BaseEntity +{ + private static final long serialVersionUID = 1L; + + /** 离线报警规则ID */ + @Excel(name = "离线报警规则ID") + private Long offlineRuleId; + + /** 离线报警规则名称 */ + @Excel(name = "离线报警规则名称") + private String offlineRuleName; + + /** 租户ID,关联hw_tenant的tenant_id */ + @Excel(name = "租户ID") + private Long tenantId; + + /** 所属场景,关联hw_scene表的scene_id字段 */ + @Excel(name = "所属场景ID") + private Long sceneId; + + /** 报警级别,关联表hw_alarm_level字段alarm_level_id */ + @Excel(name = "报警级别ID") + private Long alarmLevelId; + + /** 规则类型(1设备,2监控单元,3场景) */ + @Excel(name = "规则类型", readConverterExp = "1=设备,2=监控单元,3=场景") + private String ruleType; + + /** 离线时间(单位:秒,大于此时间的) */ + @Excel(name = "离线时间(单位:秒)") + private Long offlineTime; + + /** 几分钟内掉线次数的分钟 */ + @Excel(name = "几分钟内掉线次数的分钟") + private Long offlineNumberTime; + + /** 几分钟内掉线次数的次数 */ + @Excel(name = "几分钟内掉线次数的次数") + private Long offlineNumber; + + /** 无线信号强度(小于等于,1=极弱,2=弱,3=中,4=强,5=极强) */ + private String triggerTimeFrame; + + /** 当月消耗流量(大于) */ + private Long consumeTraffic; + + /** 电池电量(小于),范围是0.01-1 */ + private BigDecimal batteryLevel; + + /** 设备掉电报警(1=是,0=否) */ + private String powerDownFlag; + + /** 联动标识(1是,0否) */ + @Excel(name = "联动标识", readConverterExp = "1=是,0=否") + private String linkFlag; + + /** 报警启用状态(1是,0否) */ + @Excel(name = "状态(1=启用,0=禁用)") + private String alarmRuleStatus; + + /** 报警推送标识(1是,0否) */ + private String alarmPushFlag; + + /** 报警推送内容 */ + private String alarmPushContent; + + /** 恢复正常推送内容 */ + private String alarmRecoverContent; + + /** 预留字段 */ + private String offlineRuleField; + + /** 报警规则联动信息信息 */ + private List hwAlarmRuleLinkList; + + @Excel(name = "租户名称") + private String tenantName; + + @Excel(name = "场景名称") + private String sceneName; + + @Excel(name = "报警级别名称") + private String alarmLevelName; + + + public void setOfflineRuleId(Long offlineRuleId) + { + this.offlineRuleId = offlineRuleId; + } + + public Long getOfflineRuleId() + { + return offlineRuleId; + } + public void setOfflineRuleName(String offlineRuleName) + { + this.offlineRuleName = offlineRuleName; + } + + public String getOfflineRuleName() + { + return offlineRuleName; + } + public void setTenantId(Long tenantId) + { + this.tenantId = tenantId; + } + + public Long getTenantId() + { + return tenantId; + } + public void setSceneId(Long sceneId) + { + this.sceneId = sceneId; + } + + public Long getSceneId() + { + return sceneId; + } + public void setAlarmLevelId(Long alarmLevelId) + { + this.alarmLevelId = alarmLevelId; + } + + public Long getAlarmLevelId() + { + return alarmLevelId; + } + public void setRuleType(String ruleType) + { + this.ruleType = ruleType; + } + + public String getRuleType() + { + return ruleType; + } + public void setOfflineTime(Long offlineTime) + { + this.offlineTime = offlineTime; + } + + public Long getOfflineTime() + { + return offlineTime; + } + public void setOfflineNumberTime(Long offlineNumberTime) + { + this.offlineNumberTime = offlineNumberTime; + } + + public Long getOfflineNumberTime() + { + return offlineNumberTime; + } + public void setOfflineNumber(Long offlineNumber) + { + this.offlineNumber = offlineNumber; + } + + public Long getOfflineNumber() + { + return offlineNumber; + } + public void setTriggerTimeFrame(String triggerTimeFrame) + { + this.triggerTimeFrame = triggerTimeFrame; + } + + public String getTriggerTimeFrame() + { + return triggerTimeFrame; + } + public void setConsumeTraffic(Long consumeTraffic) + { + this.consumeTraffic = consumeTraffic; + } + + public Long getConsumeTraffic() + { + return consumeTraffic; + } + public void setBatteryLevel(BigDecimal batteryLevel) + { + this.batteryLevel = batteryLevel; + } + + public BigDecimal getBatteryLevel() + { + return batteryLevel; + } + public void setPowerDownFlag(String powerDownFlag) + { + this.powerDownFlag = powerDownFlag; + } + + public String getPowerDownFlag() + { + return powerDownFlag; + } + public void setLinkFlag(String linkFlag) + { + this.linkFlag = linkFlag; + } + + public String getLinkFlag() + { + return linkFlag; + } + public void setAlarmRuleStatus(String alarmRuleStatus) + { + this.alarmRuleStatus = alarmRuleStatus; + } + + public String getAlarmRuleStatus() + { + return alarmRuleStatus; + } + public void setAlarmPushFlag(String alarmPushFlag) + { + this.alarmPushFlag = alarmPushFlag; + } + + public String getAlarmPushFlag() + { + return alarmPushFlag; + } + public void setAlarmPushContent(String alarmPushContent) + { + this.alarmPushContent = alarmPushContent; + } + + public String getAlarmPushContent() + { + return alarmPushContent; + } + public void setAlarmRecoverContent(String alarmRecoverContent) + { + this.alarmRecoverContent = alarmRecoverContent; + } + + public String getAlarmRecoverContent() + { + return alarmRecoverContent; + } + public void setOfflineRuleField(String offlineRuleField) + { + this.offlineRuleField = offlineRuleField; + } + + public String getOfflineRuleField() + { + return offlineRuleField; + } + + public List getHwAlarmRuleLinkList() + { + return hwAlarmRuleLinkList; + } + + public void setHwAlarmRuleLinkList(List hwAlarmRuleLinkList) + { + this.hwAlarmRuleLinkList = hwAlarmRuleLinkList; + } + + public String getTenantName() { + return tenantName; + } + + public void setTenantName(String tenantName) { + this.tenantName = tenantName; + } + + public String getSceneName() { + return sceneName; + } + + public void setSceneName(String sceneName) { + this.sceneName = sceneName; + } + + public String getAlarmLevelName() { + return alarmLevelName; + } + + public void setAlarmLevelName(String alarmLevelName) { + this.alarmLevelName = alarmLevelName; + } + + @Override + public String toString() { + return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) + .append("offlineRuleId", getOfflineRuleId()) + .append("offlineRuleName", getOfflineRuleName()) + .append("tenantId", getTenantId()) + .append("sceneId", getSceneId()) + .append("alarmLevelId", getAlarmLevelId()) + .append("ruleType", getRuleType()) + .append("offlineTime", getOfflineTime()) + .append("offlineNumberTime", getOfflineNumberTime()) + .append("offlineNumber", getOfflineNumber()) + .append("triggerTimeFrame", getTriggerTimeFrame()) + .append("consumeTraffic", getConsumeTraffic()) + .append("batteryLevel", getBatteryLevel()) + .append("powerDownFlag", getPowerDownFlag()) + .append("linkFlag", getLinkFlag()) + .append("alarmRuleStatus", getAlarmRuleStatus()) + .append("alarmPushFlag", getAlarmPushFlag()) + .append("alarmPushContent", getAlarmPushContent()) + .append("alarmRecoverContent", getAlarmRecoverContent()) + .append("remark", getRemark()) + .append("createBy", getCreateBy()) + .append("createTime", getCreateTime()) + .append("updateBy", getUpdateBy()) + .append("updateTime", getUpdateTime()) + .append("offlineRuleField", getOfflineRuleField()) + .append("hwAlarmRuleLinkList", getHwAlarmRuleLinkList()) + .toString(); + } +} diff --git a/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/mapper/HwOfflineRuleMapper.java b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/mapper/HwOfflineRuleMapper.java new file mode 100644 index 0000000..60f55b5 --- /dev/null +++ b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/mapper/HwOfflineRuleMapper.java @@ -0,0 +1,34 @@ +package com.ruoyi.dataprocess.mapper; + + + +import com.ruoyi.dataprocess.domain.HwAlarmRuleLink; +import com.ruoyi.dataprocess.domain.HwOfflineRule; + +import java.util.List; + +/** + * 离线报警规则Mapper接口 + * + * @author xins + * @date 2023-11-13 + */ +public interface HwOfflineRuleMapper +{ + /** + * 查询离线报警规则 + * + * @param deviceId 设备Id + * @return 离线报警规则 + */ + public HwOfflineRule selectHwOfflineRuleByDeviceId(Long deviceId); + + /** + * 查询离线报警规则 + * + * @param sceneId 场景Id + * @return 离线报警规则 + */ + public HwOfflineRule selectHwOfflineRuleBySceneId(Long sceneId); + +} diff --git a/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/CommanHandleService.java b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/CommanHandleService.java new file mode 100644 index 0000000..d528c4a --- /dev/null +++ b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/CommanHandleService.java @@ -0,0 +1,38 @@ +package com.ruoyi.dataprocess.service; + +import com.alibaba.fastjson.JSONObject; +import com.ruoyi.common.core.constant.HwDictConstants; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Service; + +@Service +public class CommanHandleService { + @Autowired + private StringRedisTemplate redisTemplate; + + /** + * @param: controlCommandTopic 下发数据的主题 + * @param: subDeviceCode 子设备或直连设备设备编号 + * @param: serviceFunctionIdentifier + * @description 联动设备发送指令 + * @author xins + * @date 2023-11-07 16:28 + */ + public void publishControlCommand(String controlCommandTopic, String subDeviceCode, String serviceFunctionIdentifier) { + JSONObject controlCommandJson = new JSONObject(); + JSONObject payloadJson = new JSONObject(); + JSONObject paramJson = new JSONObject(); + paramJson.put(HwDictConstants.CONTROL_COMMAND_PAYLOAD_UID_KEY, subDeviceCode);//子设备或直连设备设备编号 + + payloadJson.put(HwDictConstants.CONTROL_COMMAND_PAYLOAD_MID_KEY, ""); + payloadJson.put(HwDictConstants.CONTROL_COMMAND_PAYLOAD_TIMESTAMP_KEY, System.currentTimeMillis() / 1000);//单位:s + payloadJson.put(HwDictConstants.CONTROL_COMMAND_PAYLOAD_TYPE_KEY, serviceFunctionIdentifier); + payloadJson.put(HwDictConstants.CONTROL_COMMAND_PAYLOAD_PARAM_KEY, paramJson); + + controlCommandJson.put(HwDictConstants.CONTROL_COMMAND_TOPIC_KEY, controlCommandTopic); + controlCommandJson.put(HwDictConstants.CONTROL_COMMAND_PAYLOAD_KEY, payloadJson.toString()); + + redisTemplate.convertAndSend(HwDictConstants.CONTROL_COMMAND_REDIS_KEY, controlCommandJson.toString()); + } +} diff --git a/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/impl/DataProcessServiceImpl.java b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/impl/DataProcessServiceImpl.java index 21d3667..f4bd3fd 100644 --- a/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/impl/DataProcessServiceImpl.java +++ b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/impl/DataProcessServiceImpl.java @@ -17,6 +17,7 @@ import com.ruoyi.dataprocess.amap.PositionVo; import com.ruoyi.dataprocess.common.ImageUtils; import com.ruoyi.dataprocess.domain.*; import com.ruoyi.dataprocess.mapper.*; +import com.ruoyi.dataprocess.service.CommanHandleService; import com.ruoyi.dataprocess.service.IDataProcessService; import com.ruoyi.tdengine.api.RemoteTdEngineService; import com.ruoyi.tdengine.api.domain.TdField; @@ -50,7 +51,7 @@ import java.util.concurrent.atomic.AtomicBoolean; * @Version :1.0 */ @Service -public class DataProcessServiceImpl implements IDataProcessService { +public class DataProcessServiceImpl extends CommanHandleService implements IDataProcessService { private static final Logger logger = LoggerFactory.getLogger(DataProcessServiceImpl.class); @Resource @@ -71,9 +72,6 @@ public class DataProcessServiceImpl implements IDataProcessService { @Autowired private HwAlarmRuleMapper hwAlarmRuleMapper; - @Autowired - private StringRedisTemplate redisTemplate; - /** * @param: jsonData * @param: imagePath ruoyifile的上传地址 @@ -409,6 +407,7 @@ public class DataProcessServiceImpl implements IDataProcessService { hwAlarmInfoMapper.insertHwAlarmInfo(alarmInfo); this.insertHwAlarmDetail(alarmInfo); this.handleAlarmLink(alarmRule, topic, subDeviceCode); + break; } } } @@ -444,40 +443,15 @@ public class DataProcessServiceImpl implements IDataProcessService { * @date 2023-11-07 16:39 */ private void handleAlarmLink(HwAlarmRule alarmRule, String topic, String subDeviceCode) { + String controlCommandTopic = topic.replace(HwDictConstants.TOPIC_TYPE_DATA_POSTFIX, HwDictConstants.TOPIC_TYPE_COMMAND_POSTFIX); if (alarmRule.getLinkFlag().equals(HwDictConstants.ALARM_RULE_LINK_FLAG_YES)) { List alarmRuleLinks = alarmRule.getHwAlarmRuleLinkList(); alarmRuleLinks.forEach(alarmRuleLink -> { - this.publishControlCommand(topic, subDeviceCode, alarmRuleLink.getLinkDeviceFunctionIdentifier()); + this.publishControlCommand(controlCommandTopic, subDeviceCode, alarmRuleLink.getLinkDeviceFunctionIdentifier()); }); } } - /** - * @param: topic 上报数据的主题 - * @param: subDeviceCode 子设备或直连设备设备编号 - * @param: serviceFunctionIdentifier - * @description 联动设备发送指令 - * @author xins - * @date 2023-11-07 16:28 - */ - private void publishControlCommand(String topic, String subDeviceCode, String serviceFunctionIdentifier) { - JSONObject controlCommandJson = new JSONObject(); - String controlCommandTopic = topic.replace(HwDictConstants.TOPIC_TYPE_DATA_POSTFIX, HwDictConstants.TOPIC_TYPE_COMMAND_POSTFIX); - JSONObject payloadJson = new JSONObject(); - JSONObject paramJson = new JSONObject(); - paramJson.put(HwDictConstants.CONTROL_COMMAND_PAYLOAD_UID_KEY, subDeviceCode);//子设备或直连设备设备编号 - - payloadJson.put(HwDictConstants.CONTROL_COMMAND_PAYLOAD_MID_KEY, ""); - payloadJson.put(HwDictConstants.CONTROL_COMMAND_PAYLOAD_TIMESTAMP_KEY, System.currentTimeMillis() / 1000);//单位:s - payloadJson.put(HwDictConstants.CONTROL_COMMAND_PAYLOAD_TYPE_KEY, serviceFunctionIdentifier); - payloadJson.put(HwDictConstants.CONTROL_COMMAND_PAYLOAD_PARAM_KEY, paramJson); - - controlCommandJson.put(HwDictConstants.CONTROL_COMMAND_TOPIC_KEY, controlCommandTopic); - controlCommandJson.put(HwDictConstants.CONTROL_COMMAND_PAYLOAD_KEY, payloadJson.toString()); - - redisTemplate.convertAndSend(HwDictConstants.CONTROL_COMMAND_REDIS_KEY, controlCommandJson.toString()); - } - @Override public void testBase64(String jsonData, String imagePath, String imagePatterns, String imageDomain, String imagePrefix) { JSONObject json = JSON.parseObject(jsonData); diff --git a/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/impl/DeviceStatusServiceImpl.java b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/impl/DeviceStatusServiceImpl.java index fdfdfb6..35b3970 100644 --- a/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/impl/DeviceStatusServiceImpl.java +++ b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/impl/DeviceStatusServiceImpl.java @@ -5,10 +5,16 @@ import com.alibaba.fastjson.JSONObject; import com.ruoyi.common.core.constant.HwDictConstants; import com.ruoyi.common.core.constant.SecurityConstants; import com.ruoyi.common.core.constant.TdEngineConstants; -import com.ruoyi.dataprocess.domain.HwDevice; +import com.ruoyi.common.core.domain.R; +import com.ruoyi.common.core.utils.StringUtils; +import com.ruoyi.dataprocess.domain.*; +import com.ruoyi.dataprocess.mapper.HwAlarmInfoMapper; import com.ruoyi.dataprocess.mapper.HwDeviceMapper; +import com.ruoyi.dataprocess.mapper.HwOfflineRuleMapper; +import com.ruoyi.dataprocess.service.CommanHandleService; import com.ruoyi.dataprocess.service.IDeviceStatusService; import com.ruoyi.tdengine.api.RemoteTdEngineService; +import com.ruoyi.tdengine.api.domain.DeviceStatus; import com.ruoyi.tdengine.api.domain.TdField; import com.ruoyi.tdengine.api.domain.TdTableVo; import org.springframework.beans.factory.annotation.Autowired; @@ -18,6 +24,7 @@ import javax.annotation.Resource; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.Map; /** * @Description: 设备状态处理服务 @@ -27,11 +34,14 @@ import java.util.List; * @Version :1.0 */ @Service -public class DeviceStatusServiceImpl implements IDeviceStatusService { +public class DeviceStatusServiceImpl extends CommanHandleService implements IDeviceStatusService { @Autowired private HwDeviceMapper hwDeviceMapper; - + @Autowired + private HwOfflineRuleMapper hwOfflineRuleMapper; + @Autowired + private HwAlarmInfoMapper hwAlarmInfoMapper; @Resource private RemoteTdEngineService remoteTdEngineService; @@ -81,6 +91,88 @@ public class DeviceStatusServiceImpl implements IDeviceStatusService { } hwDeviceMapper.updateHwDevice(hwDevice); + + if (connectStatus.equals(HwDictConstants.DEVICE_ONLINE_STATUS_OFFLINE)) { + this.checkOfflineAlarm(hwDevice); + } + + } + } + + + /** + * @param: device + * @description 离线报警处理 + * @author xins + * @date 2023-11-14 16:01 + */ + private void checkOfflineAlarm(HwDevice device) { + Long deviceId = device.getDeviceId(); + Long sceneId = device.getSceneId(); + + HwOfflineRule offlineRule = hwOfflineRuleMapper.selectHwOfflineRuleByDeviceId(deviceId); + if (offlineRule == null) { + offlineRule = hwOfflineRuleMapper.selectHwOfflineRuleBySceneId(sceneId); + } + + if (offlineRule != null) { + Long offlineNumberTime = offlineRule.getOfflineNumberTime();//分钟 + Long offlineNumber = offlineRule.getOfflineNumber(); + + if (offlineNumber > 1) { + Long currentTime = System.currentTimeMillis(); + Long startTime = currentTime - offlineNumberTime * 60 * 1000; + DeviceStatus queryDeviceStatus = new DeviceStatus(); + queryDeviceStatus.setDeviceId(deviceId); + queryDeviceStatus.setStartTime(startTime); + queryDeviceStatus.setEndTime(currentTime); + queryDeviceStatus.setOnlineStatus(0); + + R>> deviceStatusListR = this.remoteTdEngineService.getDeviceStatusList(queryDeviceStatus, SecurityConstants.INNER); + List> deviceStatusList = deviceStatusListR.getData(); + //在几分钟时间内离线几次的报警信息保存 + if (deviceStatusList != null && deviceStatusList.size() > offlineNumber) { + insertHwAlarmInfo(device, offlineRule, deviceId); + } + } else {//只要离线就报警 + insertHwAlarmInfo(device, offlineRule, deviceId); + } + } + } + + private void insertHwAlarmInfo(HwDevice device, HwOfflineRule offlineRule, Long deviceId) { + Date currentDate = new Date(); + + HwAlarmInfo alarmInfo = new HwAlarmInfo(); + alarmInfo.setAlarmInfoType(HwDictConstants.ALARM_INFO_TYPE_OFFLINE); + alarmInfo.setAlarmReleatedId(offlineRule.getOfflineRuleId()); + alarmInfo.setDeviceId(deviceId); + alarmInfo.setTenantId(device.getTenantId()); + alarmInfo.setSceneId(device.getSceneId()); + alarmInfo.setMonitorUnitId(device.getMonitorUnitId()); + alarmInfo.setAlarmLevelId(offlineRule.getAlarmLevelId()); + alarmInfo.setHandleStatus(HwDictConstants.ALARM_HANDLE_STATUS_NO); + alarmInfo.setAlarmTime(currentDate); + alarmInfo.setCreateTime(currentDate); + hwAlarmInfoMapper.insertHwAlarmInfo(alarmInfo); + this.handleAlarmLink(offlineRule, device.getDeviceCode()); + } + + /** + * @param: offlineRule + * @param: deviceCode + * @description 处理报警规则联动设备 + * @author xins + * @date 2023-11-07 16:39 + */ + private void handleAlarmLink(HwOfflineRule offlineRule, String deviceCode) { + String controlCommandTopic = StringUtils + .format(HwDictConstants.CONTROL_COMMAND_TOPIC_VALUE, deviceCode); + if (offlineRule.getLinkFlag().equals(HwDictConstants.ALARM_RULE_LINK_FLAG_YES)) { + List offlineRuleLinks = offlineRule.getHwAlarmRuleLinkList(); + offlineRuleLinks.forEach(offlineRuleLink -> { + this.publishControlCommand(controlCommandTopic, deviceCode, offlineRuleLink.getLinkDeviceFunctionIdentifier()); + }); } } } diff --git a/ruoyi-modules/hw-data-process/src/main/resources/mapper/dataprocess/HwAlarmRuleMapper.xml b/ruoyi-modules/hw-data-process/src/main/resources/mapper/dataprocess/HwAlarmRuleMapper.xml index 10ed624..11b306f 100644 --- a/ruoyi-modules/hw-data-process/src/main/resources/mapper/dataprocess/HwAlarmRuleMapper.xml +++ b/ruoyi-modules/hw-data-process/src/main/resources/mapper/dataprocess/HwAlarmRuleMapper.xml @@ -59,7 +59,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" select a.alarm_rule_id, a.alarm_rule_name, a.tenant_id, a.scene_id, a.language_code, a.alarm_level_id, a.alarm_type_id, a.rule_type, a.rule_device_id, a.rule_function_total, a.trigger_expression, a.link_flag, a.alarm_rule_status, a.alarm_push_flag, a.alarm_push_content, a.alarm_recover_content, a.remark, a.create_by, a.create_time, a.update_by, a.update_time, a.alarm_rule_field, b.rule_link_id as sub_rule_link_id, b.alarm_rule_id as sub_alarm_rule_id, b.link_type as sub_link_type, b.link_device_id as sub_link_device_id, b.link_device_function_id as sub_link_device_function_id, b.link_device_function_identifier as sub_link_device_function_identifier, b.link_device_function_data as sub_link_device_function_data from hw_alarm_rule a - left join hw_alarm_rule_link b on b.alarm_rule_id = a.alarm_rule_id + left join hw_alarm_rule_link b on b.alarm_rule_id = a.alarm_rule_id and b.alarm_rule_type=1 and a.alarm_rule_name like concat('%', #{alarmRuleName}, '%') and a.tenant_id = #{tenantId} diff --git a/ruoyi-modules/hw-data-process/src/main/resources/mapper/dataprocess/HwOfflineRuleMapper.xml b/ruoyi-modules/hw-data-process/src/main/resources/mapper/dataprocess/HwOfflineRuleMapper.xml new file mode 100644 index 0000000..b58fc8c --- /dev/null +++ b/ruoyi-modules/hw-data-process/src/main/resources/mapper/dataprocess/HwOfflineRuleMapper.xml @@ -0,0 +1,118 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/controller/TdEngineController.java b/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/controller/TdEngineController.java index 87b93cd..fd5af20 100644 --- a/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/controller/TdEngineController.java +++ b/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/controller/TdEngineController.java @@ -398,7 +398,7 @@ public class TdEngineController { /** * @return R - * @param: tdSelectDto + * @param: queryDeviceStatus * @description 获取每天在线设备信息 * @author xins * @date 2023-08-29 11:26 @@ -428,7 +428,7 @@ public class TdEngineController { /** * @return R - * @param: tdSelectDto + * @param: queryDeviceStatus * @description 获取最近一次在线设备信息 * @author xins * @date 2023-08-29 11:26 @@ -453,4 +453,32 @@ public class TdEngineController { } } + + /** + * @return R + * @param: tdSelectDto + * @description 获取最近一次在线设备信息 + * @author xins + * @date 2023-08-29 11:26 + */ + @InnerAuth + @PostMapping("/getDeviceStatusList") + public R>> getDeviceStatusList(@RequestBody DeviceStatus queryDeviceStatus) { + try { + List> deviceStatusMapList = this.deviceStatusService.getDeviceStatusList(queryDeviceStatus); + return R.ok(deviceStatusMapList); + } catch (UncategorizedSQLException e) { + String message = e.getCause().getMessage(); + try { + message = message.substring(message.lastIndexOf("invalid operation")); + } catch (Exception ex) { + } + log.error(message); + return R.fail(message); + } catch (Exception e) { + log.error(e.getMessage()); + return R.fail(e.getMessage()); + } + } + } diff --git a/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/mapper/DeviceStatusMapper.java b/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/mapper/DeviceStatusMapper.java index 877ed15..e274c40 100644 --- a/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/mapper/DeviceStatusMapper.java +++ b/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/mapper/DeviceStatusMapper.java @@ -32,4 +32,13 @@ public interface DeviceStatusMapper { */ List> getLastOnlineDevices(DeviceStatus deviceStatus); + /** + * @param: deviceStatus + * @description 获取设备状态信息列表 + * @author xins + * @date 2023-09-05 11:43 + * @return List + */ + List> getDeviceStatusList(DeviceStatus deviceStatus); + } diff --git a/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/service/IDeviceStatusService.java b/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/service/IDeviceStatusService.java index d45fcb4..a61fb0e 100644 --- a/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/service/IDeviceStatusService.java +++ b/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/service/IDeviceStatusService.java @@ -24,4 +24,13 @@ public interface IDeviceStatusService { * @date 2023-09-05 13:23 */ public List> getLastOnlineDevices(DeviceStatus queryDeviceStatus); + + /** + * @return List> + * @param: DeviceStatus + * @description 获取设备状态信息列表 + * @author xins + * @date 2023-09-05 13:23 + */ + public List> getDeviceStatusList(DeviceStatus queryDeviceStatus); } diff --git a/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/service/impl/DeviceStatusServiceImpl.java b/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/service/impl/DeviceStatusServiceImpl.java index 6205fa5..e3251b0 100644 --- a/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/service/impl/DeviceStatusServiceImpl.java +++ b/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/service/impl/DeviceStatusServiceImpl.java @@ -66,12 +66,8 @@ public class DeviceStatusServiceImpl implements IDeviceStatusService { List> deviceStatusMap = deviceStatusMapper.getLastOnlineDevices(queryDeviceStatus); List> filterMaps = new ArrayList<>(); + //tdengine,在select时row会带着括号,需要将括号过滤掉 for (Map latestMap : deviceStatusMap) { - Set kk = latestMap.keySet(); - for(String k : kk){ - System.out.println(k+"==="+latestMap.get(k)); - } - Map filterMap = latestMap.entrySet() .stream() .filter(entry -> entry.getValue() != null) @@ -84,4 +80,21 @@ public class DeviceStatusServiceImpl implements IDeviceStatusService { return filterMaps; } + + /** + * @return List> + * @param: DeviceStatus + * @description 获取设备状态信息列表 + * @author xins + * @date 2023-09-05 13:23 + */ + @Override + public List> getDeviceStatusList(DeviceStatus queryDeviceStatus) { + Map params = queryDeviceStatus.getParams(); + params.put("databaseName", TdEngineConstants.PLATFORM_DB_NAME); + params.put("tableName", TdEngineConstants.getDeviceStatusTableName(queryDeviceStatus.getDeviceId())); + List> deviceStatusMap = deviceStatusMapper.getDeviceStatusList(queryDeviceStatus); + return deviceStatusMap; + } + } diff --git a/ruoyi-modules/hw-tdengine/src/main/resources/mapper/tdengine/DeviceStatusMapper.xml b/ruoyi-modules/hw-tdengine/src/main/resources/mapper/tdengine/DeviceStatusMapper.xml index d1d6605..26b986a 100644 --- a/ruoyi-modules/hw-tdengine/src/main/resources/mapper/tdengine/DeviceStatusMapper.xml +++ b/ruoyi-modules/hw-tdengine/src/main/resources/mapper/tdengine/DeviceStatusMapper.xml @@ -26,5 +26,20 @@ + +