2026-04-17 17:12:41 +08:00
|
|
|
|
package com.maibu.service;
|
2026-04-17 10:42:16 +08:00
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.io.IoUtil;
|
|
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
2026-04-17 17:12:41 +08:00
|
|
|
|
import com.maibu.common.CommandConstant;
|
|
|
|
|
|
import com.maibu.common.NettyCacheKey;
|
2026-04-30 15:25:58 +08:00
|
|
|
|
import com.maibu.core.business.DeviceRunningStatusHistory;
|
|
|
|
|
|
import com.maibu.core.business.DeviceStatusRecordDTO;
|
|
|
|
|
|
import com.maibu.core.enums.CompareEnum;
|
2026-07-09 08:40:32 +08:00
|
|
|
|
import com.maibu.core.enums.ErrorSource;
|
2026-04-30 15:25:58 +08:00
|
|
|
|
import com.maibu.core.enums.RespondCode;
|
2026-04-17 17:12:41 +08:00
|
|
|
|
import com.maibu.core.redis.RedisCache;
|
|
|
|
|
|
import com.maibu.dto.DeviceErrorPushDTO;
|
|
|
|
|
|
import com.maibu.dto.DeviceErrorPushDetail;
|
2026-04-30 15:25:58 +08:00
|
|
|
|
import com.maibu.core.business.device.NettyDevice;
|
2026-07-09 08:40:32 +08:00
|
|
|
|
import com.maibu.core.business.inter.WebsocketMesDispather;
|
2026-04-30 15:25:58 +08:00
|
|
|
|
import com.maibu.core.business.ErrorIdentificationStandard;
|
2026-07-09 08:40:32 +08:00
|
|
|
|
import com.maibu.core.business.alarm_center.AlarmMessage;
|
2026-04-17 17:12:41 +08:00
|
|
|
|
import com.maibu.manager.DeviceSessionManager;
|
|
|
|
|
|
import com.maibu.mapper.ErrorIdentificationStandardMapper;
|
2026-07-09 08:40:32 +08:00
|
|
|
|
import com.maibu.memory.GlobalMemory;
|
2026-04-30 15:25:58 +08:00
|
|
|
|
import com.maibu.memory.SiteMemory;
|
2026-07-09 08:40:32 +08:00
|
|
|
|
import com.maibu.mqtt.MqttTopic;
|
2026-04-17 17:12:41 +08:00
|
|
|
|
import com.maibu.utils.CommandUtils;
|
|
|
|
|
|
import com.maibu.utils.CompareUtils;
|
2026-07-09 08:40:32 +08:00
|
|
|
|
import com.maibu.utils.json.JsonUtils;
|
|
|
|
|
|
|
2026-04-17 10:42:16 +08:00
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2026-07-09 08:40:32 +08:00
|
|
|
|
import org.eclipse.paho.client.mqttv3.MqttException;
|
2026-04-17 10:42:16 +08:00
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
import org.springframework.core.io.Resource;
|
|
|
|
|
|
import org.springframework.core.io.ResourceLoader;
|
|
|
|
|
|
import org.springframework.stereotype.Service;
|
2026-04-21 09:20:55 +08:00
|
|
|
|
import org.springframework.util.CollectionUtils;
|
2026-04-17 10:42:16 +08:00
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
import java.lang.reflect.Field;
|
2026-07-09 08:40:32 +08:00
|
|
|
|
import java.time.LocalDateTime;
|
2026-04-17 10:42:16 +08:00
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
|
import java.util.Comparator;
|
|
|
|
|
|
import java.util.List;
|
2026-07-09 08:40:32 +08:00
|
|
|
|
import java.util.Map;
|
2026-04-17 10:42:16 +08:00
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
|
@Service
|
|
|
|
|
|
public class DeviceThreadService {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private ResourceLoader resourceLoader;
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
2026-04-30 15:25:58 +08:00
|
|
|
|
private SiteMemory siteMemory;
|
2026-04-17 10:42:16 +08:00
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private RedisCache redisCache;
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private DeviceSessionManager sessionManager;
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private ErrorIdentificationStandardMapper standardMapper;
|
|
|
|
|
|
|
2026-07-09 08:40:32 +08:00
|
|
|
|
@Autowired
|
|
|
|
|
|
private WebsocketMesDispather websocketMesDispather;
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private AlertPushService alertPushService;
|
2026-04-17 10:42:16 +08:00
|
|
|
|
|
|
|
|
|
|
public void deviceErrorMonitor() throws InterruptedException, IOException {
|
|
|
|
|
|
initStandard();
|
|
|
|
|
|
|
|
|
|
|
|
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
|
|
|
|
|
executor.scheduleWithFixedDelay(() -> {
|
|
|
|
|
|
try {
|
|
|
|
|
|
String key = NettyCacheKey.deviceRunningStatusKey;
|
|
|
|
|
|
Collection<String> keys = redisCache.getListKeyByPrefix(key);
|
|
|
|
|
|
if (!CollectionUtils.isEmpty(keys)) {
|
2026-07-09 08:40:32 +08:00
|
|
|
|
List<DeviceErrorPushDTO> pushList = new ArrayList<>();
|
2026-04-17 10:42:16 +08:00
|
|
|
|
keys.forEach(k -> {
|
|
|
|
|
|
DeviceStatusRecordDTO recordDTO = redisCache.getCacheObject(k);
|
|
|
|
|
|
if (recordDTO != null && !CollectionUtils.isEmpty(recordDTO.getHistories())) {
|
2026-07-09 08:40:32 +08:00
|
|
|
|
recordDTO.getHistories()
|
|
|
|
|
|
.sort(Comparator.comparing(DeviceRunningStatusHistory::getCreateTime).reversed());
|
2026-04-17 10:42:16 +08:00
|
|
|
|
DeviceRunningStatusHistory history = recordDTO.getHistories().get(0);
|
2026-07-09 08:40:32 +08:00
|
|
|
|
List<AlarmMessage> compared = compareErrorStandard(history,
|
|
|
|
|
|
siteMemory.standards.get(ErrorSource.MOWER));
|
2026-04-17 10:42:16 +08:00
|
|
|
|
|
|
|
|
|
|
String[] parts = k.split(":");
|
|
|
|
|
|
String deviceId = parts[1];
|
|
|
|
|
|
|
|
|
|
|
|
DeviceErrorPushDTO pushDTO = new DeviceErrorPushDTO();
|
|
|
|
|
|
pushDTO.setDeviceId(deviceId);
|
|
|
|
|
|
pushDTO.setTime(System.currentTimeMillis());
|
|
|
|
|
|
pushDTO.setDetails(compared);
|
|
|
|
|
|
pushDTO.setEvent(RespondCode.device_error_push);
|
2026-07-09 08:40:32 +08:00
|
|
|
|
pushList.add(pushDTO);
|
2026-04-17 10:42:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2026-07-09 08:40:32 +08:00
|
|
|
|
// 按实际deviceId分组,推送错误信息
|
|
|
|
|
|
Map<String, List<DeviceErrorPushDTO>> pushMap = pushList.stream()
|
|
|
|
|
|
.collect(java.util.stream.Collectors.groupingBy(DeviceErrorPushDTO::getDeviceId));
|
|
|
|
|
|
pushMap.forEach((deviceId, dtos) -> {
|
|
|
|
|
|
// pushErrorMessage(deviceId, JSON.toJSONString(dtos));
|
|
|
|
|
|
// todo 判断错误级别,是否需要推送邮件 或者钉钉。
|
|
|
|
|
|
// 找到这个设备的场站 添加场站是否开启关闭 钉钉或者邮件推送,推送邮件 添加人员是否订阅邮件,钉钉是加入钉钉群
|
|
|
|
|
|
alertPushService.messagePushHandler(deviceId, dtos);
|
|
|
|
|
|
});
|
2026-04-17 10:42:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
log.error("执行监测错误线程错误:{}", e.getMessage());
|
|
|
|
|
|
}
|
|
|
|
|
|
}, 0, 2, TimeUnit.MINUTES);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void initStandard() throws IOException {
|
2026-07-09 08:40:32 +08:00
|
|
|
|
if (siteMemory.standards == null) {
|
|
|
|
|
|
List<ErrorIdentificationStandard> list = standardMapper.selectList();
|
|
|
|
|
|
if (CollectionUtils.isEmpty(list)) {
|
2026-04-17 10:42:16 +08:00
|
|
|
|
// 读取resource目录下的device-config.json文件
|
|
|
|
|
|
Resource resource = resourceLoader.getResource("classpath:errorStandard.json");
|
|
|
|
|
|
// 使用fastjson2转换为DeviceConfig对象
|
|
|
|
|
|
String jsonContent = IoUtil.readUtf8(resource.getInputStream());
|
2026-07-09 08:40:32 +08:00
|
|
|
|
List<ErrorIdentificationStandard> newStandards = JSON.parseArray(jsonContent,
|
|
|
|
|
|
ErrorIdentificationStandard.class);
|
|
|
|
|
|
if (!CollectionUtils.isEmpty(newStandards)) {
|
|
|
|
|
|
standardMapper.insertBatch(newStandards);
|
|
|
|
|
|
siteMemory.standards = newStandards.stream()
|
|
|
|
|
|
.collect(java.util.stream.Collectors
|
|
|
|
|
|
.groupingBy(ErrorIdentificationStandard::getErrorSource));
|
|
|
|
|
|
}
|
2026-04-17 10:42:16 +08:00
|
|
|
|
} else {
|
2026-07-09 08:40:32 +08:00
|
|
|
|
siteMemory.standards = list.stream()
|
|
|
|
|
|
.collect(java.util.stream.Collectors.groupingBy(ErrorIdentificationStandard::getErrorSource));
|
2026-04-17 10:42:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void pushErrorMessage(String deviceId, String dto) {
|
2026-07-09 08:40:32 +08:00
|
|
|
|
// NettyDevice device = sessionManager.getDevice(deviceId);
|
|
|
|
|
|
// if (device != null) {
|
|
|
|
|
|
// ByteBuf buf = Unpooled.buffer();
|
|
|
|
|
|
// CommandUtils.buildCommand(buf, dto, CommandConstant.interaction);
|
|
|
|
|
|
// if (device.getChannel() != null && device.getChannel().isActive()) {
|
|
|
|
|
|
// device.getChannel().writeAndFlush(buf);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
try {
|
|
|
|
|
|
GlobalMemory.mqttClientUtil.publish(String.format(MqttTopic.DEVICE_ERROR_PUSH_TOPIC, deviceId), dto);
|
|
|
|
|
|
List<NettyDevice> controlMasters = sessionManager.getAllSlaveControl(deviceId);
|
|
|
|
|
|
if (!CollectionUtils.isEmpty(controlMasters)) {
|
|
|
|
|
|
controlMasters.forEach(x -> {
|
|
|
|
|
|
websocketMesDispather.dispather(x.getConnectorId(), dto);
|
|
|
|
|
|
log.info("推送错误信息 device:{},content:{}", deviceId, dto);
|
|
|
|
|
|
});
|
2026-04-17 10:42:16 +08:00
|
|
|
|
}
|
2026-07-09 08:40:32 +08:00
|
|
|
|
} catch (MqttException e) {
|
|
|
|
|
|
log.error("推送错误信息失败 device:{}error:{}", deviceId, e.getMessage());
|
2026-04-17 10:42:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-09 08:40:32 +08:00
|
|
|
|
public List<AlarmMessage> compareErrorStandard(DeviceRunningStatusHistory history,
|
|
|
|
|
|
List<ErrorIdentificationStandard> standards) {
|
|
|
|
|
|
if (history == null || CollectionUtils.isEmpty(standards))
|
|
|
|
|
|
return null;
|
|
|
|
|
|
List<AlarmMessage> list = new ArrayList<>();
|
2026-04-17 10:42:16 +08:00
|
|
|
|
standards.forEach(standard -> {
|
|
|
|
|
|
String fieldName = standard.getField();
|
|
|
|
|
|
String compareValues = standard.getCompareValues();
|
|
|
|
|
|
CompareEnum compareType = standard.getCompareType();
|
|
|
|
|
|
String targetValue = knownClassFieldFinding(fieldName, history);
|
|
|
|
|
|
if (!StringUtils.isEmpty(compareValues) && !StringUtils.isEmpty(targetValue)) {
|
|
|
|
|
|
boolean result = CompareUtils.compare(compareType, compareValues, targetValue);
|
|
|
|
|
|
if (!result) {
|
2026-07-09 08:40:32 +08:00
|
|
|
|
AlarmMessage alarm = new AlarmMessage();
|
|
|
|
|
|
alarm.setContent(standard.getErrorDescription());
|
|
|
|
|
|
alarm.setDeviceSn(history.getDeviceId());
|
|
|
|
|
|
alarm.setDeviceName(history.getDeviceId());
|
|
|
|
|
|
alarm.setLevel(standard.getErrorLevel());
|
|
|
|
|
|
alarm.setLocation(history.getLatitude() + "," + history.getLongitude());
|
|
|
|
|
|
alarm.setName(standard.getErrorName());
|
|
|
|
|
|
alarm.setNo(standard.getErrorCode());
|
|
|
|
|
|
alarm.setSuggestion(standard.getSuggestion());
|
|
|
|
|
|
alarm.setStatus("未处理");
|
|
|
|
|
|
alarm.setTime(LocalDateTime.now());
|
|
|
|
|
|
list.add(alarm);
|
2026-04-17 10:42:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
return list;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static String knownClassFieldFinding(String fieldName, DeviceRunningStatusHistory history) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 1. 获取类的Class对象
|
|
|
|
|
|
Class<?> historyClass = DeviceRunningStatusHistory.class;
|
|
|
|
|
|
Field field = historyClass.getDeclaredField(fieldName);
|
|
|
|
|
|
field.setAccessible(true);
|
|
|
|
|
|
Object value = field.get(history);
|
|
|
|
|
|
return (String) value;
|
|
|
|
|
|
} catch (NoSuchFieldException | IllegalAccessException e) {
|
|
|
|
|
|
System.out.println("获取字段失败: " + e.getMessage());
|
|
|
|
|
|
log.error("获取属性值失败:{}", e.getMessage());
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|