|
|
|
|
@ -16,11 +16,15 @@ |
|
|
|
|
*/ |
|
|
|
|
package org.springblade.modules.monitor.service.impl; |
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
|
import com.alibaba.fastjson.JSONArray; |
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
|
|
import com.google.gson.JsonArray; |
|
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import org.apache.commons.lang.StringUtils; |
|
|
|
|
@ -135,108 +139,179 @@ public class ConfigMonitorServiceImpl extends ServiceImpl<ConfigMonitorMapper, C |
|
|
|
|
|
|
|
|
|
//如果所有设备都坏了,直接排查父级问题;否则修改设备状态为异常
|
|
|
|
|
//modbus设备排查服务、网关;mqtt设备排查服务、mqtt服务、dtu;bacnet设备是通过广播直连,如果全部设备都有问题,直接排查网络问题;
|
|
|
|
|
if(breakList.size() == monitorList.size()){ |
|
|
|
|
|
|
|
|
|
boolean flag = true; |
|
|
|
|
for(Long id : parentIdList){ |
|
|
|
|
//通过标识位来判断当前是否应该静默
|
|
|
|
|
if(flag){ |
|
|
|
|
LambdaQueryWrapper<ConfigMonitor> wrapper = new LambdaQueryWrapper<>(); |
|
|
|
|
wrapper.eq(ConfigMonitor::getId,id); |
|
|
|
|
wrapper.eq(BaseEntity::getIsDeleted,0); |
|
|
|
|
ConfigMonitor configMonitor = baseMapper.selectOne(wrapper); |
|
|
|
|
//服务排查
|
|
|
|
|
if(configMonitor.getType().equals(MonitorConstant.SERVICE) && configMonitor.getResult().equals(MonitorConstant.RESULT)){ |
|
|
|
|
flag = false; |
|
|
|
|
configMonitor.setStatus(MonitorConstant.STATUS_2); |
|
|
|
|
configMonitor.setUpdateTime(nowDate); |
|
|
|
|
baseMapper.updateById(configMonitor); |
|
|
|
|
parentId = id; |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
//mqtt服务排查
|
|
|
|
|
if(configMonitor.getType().equals(MonitorConstant.MIDDLEWARE) && configMonitor.getMonitorType().equals(MonitorConstant.MQTT)){ |
|
|
|
|
//通过查询所有mqtt相关设备来判断mqtt服务是否正常
|
|
|
|
|
int count = baseMapper.findMqttEquipCount(DateFormatUtils.format(time,"yyyy-MM-dd HH:mm:ss")); |
|
|
|
|
//如果count=0说明所有mqtt相关设备都没取到数,可证明mqtt出现问题
|
|
|
|
|
if(count == 0){ |
|
|
|
|
flag = false; |
|
|
|
|
parentId = id; |
|
|
|
|
configMonitor.setStatus(MonitorConstant.STATUS_2); |
|
|
|
|
configMonitor.setUpdateTime(nowDate); |
|
|
|
|
baseMapper.updateById(configMonitor); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//网关排查 --适用于modbus 空调面板 lora网关;mqtt dtu;http 空开照明 通讯模块;
|
|
|
|
|
if(configMonitor.getType().equals(MonitorConstant.MIDDLEWARE) && configMonitor.getMonitorType().equals(MonitorConstant.GATEWAY) && StringUtils.isNotEmpty(configMonitor.getIp())){ |
|
|
|
|
//ping网关ip,如果能ping通,则证明是网关本身有问题,如果ping不通,说明通信有问题
|
|
|
|
|
boolean isNormal = IpUtil.isIPReachable(configMonitor.getIp()); |
|
|
|
|
if(isNormal){ |
|
|
|
|
configMonitor.setStatus(MonitorConstant.STATUS_2); |
|
|
|
|
}else{ |
|
|
|
|
configMonitor.setStatus(MonitorConstant.STATUS_3); |
|
|
|
|
} |
|
|
|
|
configMonitor.setUpdateTime(nowDate); |
|
|
|
|
baseMapper.updateById(configMonitor); |
|
|
|
|
} |
|
|
|
|
}else{ |
|
|
|
|
//如果是静默,根据parentId将所有子节点状态改为静默
|
|
|
|
|
LambdaUpdateWrapper<ConfigMonitor> updateWrapper = new LambdaUpdateWrapper<>(); |
|
|
|
|
updateWrapper.eq(ConfigMonitor::getParentId,parentId); |
|
|
|
|
ConfigMonitor configMonitor = new ConfigMonitor(); |
|
|
|
|
configMonitor.setStatus(MonitorConstant.STATUS_4); |
|
|
|
|
//configMonitor.setUpdateTime(nowDate);
|
|
|
|
|
baseMapper.update(configMonitor,updateWrapper); |
|
|
|
|
parentId = id; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//将所有设备状态改为静默
|
|
|
|
|
breakList.forEach(item ->{ |
|
|
|
|
item.setStatus(MonitorConstant.STATUS_4); |
|
|
|
|
//item.setUpdateTime(nowDate);
|
|
|
|
|
baseMapper.updateById(item); |
|
|
|
|
}); |
|
|
|
|
}else{ |
|
|
|
|
for(ConfigMonitor configMonitor : breakList){ |
|
|
|
|
configMonitor.setStatus(MonitorConstant.STATUS_2); |
|
|
|
|
baseMapper.updateById(configMonitor); |
|
|
|
|
} |
|
|
|
|
//将父级id设备状态更新为正常
|
|
|
|
|
LambdaQueryWrapper<ConfigMonitor> wrapper = new LambdaQueryWrapper<>(); |
|
|
|
|
wrapper.eq(ConfigMonitor::getId,parentId); |
|
|
|
|
wrapper.eq(BaseEntity::getIsDeleted,0); |
|
|
|
|
ConfigMonitor configMonitor = baseMapper.selectOne(wrapper); |
|
|
|
|
configMonitor.setStatus(MonitorConstant.STATUS_1); |
|
|
|
|
baseMapper.updateById(configMonitor); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (breakList.size() == monitorList.size()) { |
|
|
|
|
|
|
|
|
|
boolean flag = true; |
|
|
|
|
for (Long id : parentIdList) { |
|
|
|
|
//通过标识位来判断当前是否应该静默
|
|
|
|
|
if (flag) { |
|
|
|
|
LambdaQueryWrapper<ConfigMonitor> wrapper = new LambdaQueryWrapper<>(); |
|
|
|
|
wrapper.eq(ConfigMonitor::getId, id); |
|
|
|
|
wrapper.eq(BaseEntity::getIsDeleted, 0); |
|
|
|
|
ConfigMonitor configMonitor = baseMapper.selectOne(wrapper); |
|
|
|
|
//服务排查
|
|
|
|
|
if (configMonitor.getType().equals(MonitorConstant.SERVICE) && configMonitor.getResult().equals(MonitorConstant.RESULT)) { |
|
|
|
|
flag = false; |
|
|
|
|
configMonitor.setStatus(MonitorConstant.STATUS_2); |
|
|
|
|
configMonitor.setUpdateTime(nowDate); |
|
|
|
|
baseMapper.updateById(configMonitor); |
|
|
|
|
parentId = id; |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
//mqtt服务排查
|
|
|
|
|
if (configMonitor.getType().equals(MonitorConstant.MIDDLEWARE) && configMonitor.getMonitorType().equals(MonitorConstant.MQTT)) { |
|
|
|
|
//通过查询所有mqtt相关设备来判断mqtt服务是否正常
|
|
|
|
|
int count = baseMapper.findMqttEquipCount(DateFormatUtils.format(time, "yyyy-MM-dd HH:mm:ss")); |
|
|
|
|
//如果count=0说明所有mqtt相关设备都没取到数,可证明mqtt出现问题
|
|
|
|
|
if (count == 0) { |
|
|
|
|
flag = false; |
|
|
|
|
parentId = id; |
|
|
|
|
configMonitor.setStatus(MonitorConstant.STATUS_2); |
|
|
|
|
configMonitor.setUpdateTime(nowDate); |
|
|
|
|
baseMapper.updateById(configMonitor); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//网关排查 --适用于modbus 空调面板 lora网关;mqtt dtu;http 空开照明 通讯模块;
|
|
|
|
|
if (configMonitor.getType().equals(MonitorConstant.MIDDLEWARE) && configMonitor.getMonitorType().equals(MonitorConstant.GATEWAY) && StringUtils.isNotEmpty(configMonitor.getIp())) { |
|
|
|
|
//ping网关ip,如果能ping通,则证明是网关本身有问题,如果ping不通,说明通信有问题
|
|
|
|
|
boolean isNormal = IpUtil.isIPReachable(configMonitor.getIp()); |
|
|
|
|
if (isNormal) { |
|
|
|
|
configMonitor.setStatus(MonitorConstant.STATUS_2); |
|
|
|
|
} else { |
|
|
|
|
configMonitor.setStatus(MonitorConstant.STATUS_3); |
|
|
|
|
} |
|
|
|
|
configMonitor.setUpdateTime(nowDate); |
|
|
|
|
baseMapper.updateById(configMonitor); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
//如果是静默,根据parentId将所有子节点状态改为静默
|
|
|
|
|
LambdaUpdateWrapper<ConfigMonitor> updateWrapper = new LambdaUpdateWrapper<>(); |
|
|
|
|
updateWrapper.eq(ConfigMonitor::getParentId, parentId); |
|
|
|
|
ConfigMonitor configMonitor = new ConfigMonitor(); |
|
|
|
|
configMonitor.setStatus(MonitorConstant.STATUS_4); |
|
|
|
|
//configMonitor.setUpdateTime(nowDate);
|
|
|
|
|
baseMapper.update(configMonitor, updateWrapper); |
|
|
|
|
parentId = id; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//将所有设备状态改为静默
|
|
|
|
|
breakList.forEach(item -> { |
|
|
|
|
item.setStatus(MonitorConstant.STATUS_4); |
|
|
|
|
//item.setUpdateTime(nowDate);
|
|
|
|
|
baseMapper.updateById(item); |
|
|
|
|
}); |
|
|
|
|
} else { |
|
|
|
|
for (ConfigMonitor configMonitor : breakList) { |
|
|
|
|
configMonitor.setStatus(MonitorConstant.STATUS_2); |
|
|
|
|
baseMapper.updateById(configMonitor); |
|
|
|
|
} |
|
|
|
|
//将父级id设备状态更新为正常
|
|
|
|
|
LambdaQueryWrapper<ConfigMonitor> wrapper = new LambdaQueryWrapper<>(); |
|
|
|
|
wrapper.eq(ConfigMonitor::getId, parentId); |
|
|
|
|
wrapper.eq(BaseEntity::getIsDeleted, 0); |
|
|
|
|
ConfigMonitor configMonitor = baseMapper.selectOne(wrapper); |
|
|
|
|
configMonitor.setStatus(MonitorConstant.STATUS_1); |
|
|
|
|
baseMapper.updateById(configMonitor); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public List<ConfigMonitorVO> tree() { |
|
|
|
|
return ForestNodeMerger.merge(baseMapper.tree()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public List<Long> getParentIdList(List<Long> parentIdList,Long parentId){ |
|
|
|
|
parentIdList.add(parentId); |
|
|
|
|
if(parentId != 0){ |
|
|
|
|
LambdaQueryWrapper<ConfigMonitor> queryWrapper = new LambdaQueryWrapper<>(); |
|
|
|
|
queryWrapper.eq(ConfigMonitor::getId,parentId); |
|
|
|
|
queryWrapper.eq(BaseEntity::getIsDeleted,0); |
|
|
|
|
ConfigMonitor configMonitor = baseMapper.selectOne(queryWrapper); |
|
|
|
|
parentId = configMonitor.getParentId(); |
|
|
|
|
//服务器不需要排查,所以只需要到服务层面就可以
|
|
|
|
|
if(configMonitor.getType().equals(MonitorConstant.SERVICE)){ |
|
|
|
|
return parentIdList; |
|
|
|
|
} |
|
|
|
|
getParentIdList(parentIdList,parentId); |
|
|
|
|
} |
|
|
|
|
return parentIdList; |
|
|
|
|
} |
|
|
|
|
@Override |
|
|
|
|
public List<ConfigMonitorVO> tree() { |
|
|
|
|
return ForestNodeMerger.merge(baseMapper.tree()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Object getWarnLinePoint(String parentId) { |
|
|
|
|
// 先查询该微服务
|
|
|
|
|
List resultList = new ArrayList(); |
|
|
|
|
ConfigMonitor monitor = baseMapper.selectOne(Wrappers.<ConfigMonitor>lambdaQuery().eq(ConfigMonitor::getId, parentId)); |
|
|
|
|
|
|
|
|
|
if (monitor == null) { |
|
|
|
|
return resultList; |
|
|
|
|
} |
|
|
|
|
JSONObject monitorJson = new JSONObject(); |
|
|
|
|
monitorJson.put("parent", null); |
|
|
|
|
monitorJson.put("id", monitor.getId()); |
|
|
|
|
monitorJson.put("name", monitor.getMonitorType()); |
|
|
|
|
monitorJson.put("type", monitor.getType()); |
|
|
|
|
monitorJson.put("class_code", monitor.getClassCode()); |
|
|
|
|
monitorJson.put("status", monitor.getStatus()); |
|
|
|
|
monitorJson.put("icon", monitor.getIcon()); |
|
|
|
|
|
|
|
|
|
// 递归查询该微服务下的所有设备
|
|
|
|
|
JSONArray allEquipArray = getChildList(monitorJson); |
|
|
|
|
|
|
|
|
|
// 遍历设备组织返回数据
|
|
|
|
|
JSONObject resultObj = new JSONObject(); |
|
|
|
|
for (Object o : allEquipArray) { |
|
|
|
|
JSONObject item = JSONObject.parseObject(JSON.toJSONString(o)); |
|
|
|
|
resultObj.put("classCode", item.getString("class_code")); |
|
|
|
|
resultObj.put("lineData", getLineData(item)); |
|
|
|
|
resultList.add(resultObj); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return resultList.stream().distinct().collect(Collectors.toList()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private JSONArray getChildList(JSONObject parent) { |
|
|
|
|
JSONArray resultArray = new JSONArray(); |
|
|
|
|
// 查询子等级的设备
|
|
|
|
|
List<ConfigMonitor> monitor1List = baseMapper.selectList(Wrappers.<ConfigMonitor>lambdaQuery().eq(ConfigMonitor::getParentId, parent.getLong("id"))); |
|
|
|
|
if (monitor1List == null || monitor1List.size() == 0) { |
|
|
|
|
resultArray.add(parent); |
|
|
|
|
return resultArray; |
|
|
|
|
} else { |
|
|
|
|
for (ConfigMonitor monitor : monitor1List) { |
|
|
|
|
JSONObject monitorJson = new JSONObject(); |
|
|
|
|
monitorJson.put("parent", parent); |
|
|
|
|
monitorJson.put("id", monitor.getId()); |
|
|
|
|
monitorJson.put("name", monitor.getMonitorType()); |
|
|
|
|
monitorJson.put("type", monitor.getType()); |
|
|
|
|
monitorJson.put("class_code", monitor.getClassCode()); |
|
|
|
|
monitorJson.put("status", monitor.getStatus()); |
|
|
|
|
monitorJson.put("icon", monitor.getIcon()); |
|
|
|
|
resultArray.addAll(getChildList(monitorJson)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return resultArray; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private JSONArray getLineData(JSONObject item) { |
|
|
|
|
JSONArray resultArray = new JSONArray(); |
|
|
|
|
JSONObject parent = item.getJSONObject("parent"); |
|
|
|
|
if (parent == null) { |
|
|
|
|
item.put("parent", "0"); |
|
|
|
|
resultArray.add(item); |
|
|
|
|
} else { |
|
|
|
|
item.put("parent", parent.getString("id")); |
|
|
|
|
resultArray.add(item); |
|
|
|
|
for (Object lineDatum : getLineData(parent)) { |
|
|
|
|
resultArray.add(lineDatum); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return resultArray; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private List<Long> getParentIdList(List<Long> parentIdList, Long parentId) { |
|
|
|
|
parentIdList.add(parentId); |
|
|
|
|
if (parentId != 0) { |
|
|
|
|
LambdaQueryWrapper<ConfigMonitor> queryWrapper = new LambdaQueryWrapper<>(); |
|
|
|
|
queryWrapper.eq(ConfigMonitor::getId, parentId); |
|
|
|
|
queryWrapper.eq(BaseEntity::getIsDeleted, 0); |
|
|
|
|
ConfigMonitor configMonitor = baseMapper.selectOne(queryWrapper); |
|
|
|
|
parentId = configMonitor.getParentId(); |
|
|
|
|
//服务器不需要排查,所以只需要到服务层面就可以
|
|
|
|
|
if (configMonitor.getType().equals(MonitorConstant.SERVICE)) { |
|
|
|
|
return parentIdList; |
|
|
|
|
} |
|
|
|
|
getParentIdList(parentIdList, parentId); |
|
|
|
|
} |
|
|
|
|
return parentIdList; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|