This commit is contained in:
2026-11-19 09:18:17 +08:00
parent 08888ab612
commit 21b3dbaddb
24 changed files with 449 additions and 220 deletions

View File

@@ -6,6 +6,7 @@ import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@@ -22,6 +23,7 @@ import org.springframework.util.CollectionUtils;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.maibu.constant.Constant;
import com.maibu.constant.NettyCacheKey;
import com.maibu.core.business.Device;
@@ -36,6 +38,8 @@ import com.maibu.core.business.dto.DeviceTaskCommandDTO;
import com.maibu.core.business.dto.WebMapMessageDTO;
import com.maibu.core.business.dto.WebOlineStatusMessageDTO;
import com.maibu.core.business.inter.WebsocketMesDispather;
import com.maibu.core.domain.map.RestPoint;
import com.maibu.core.domain.map.SiteMapInfo;
import com.maibu.core.domain.rule.ActionConfig;
import com.maibu.core.domain.rule.IotAutoRule;
import com.maibu.core.domain.rule.TriggerConditions;
@@ -63,6 +67,7 @@ import com.maibu.utils.json.JsonUtils;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import lombok.extern.slf4j.Slf4j;
import com.fasterxml.jackson.core.type.TypeReference;
@Slf4j
@Service
@@ -104,9 +109,11 @@ public class DeviceThreadService {
@Autowired
private DeviceTaskService deviceTaskService;
@JsonIgnore
@Autowired
private RegionConsecutiveTaskRecordMapper regionConsecutiveTaskRecordMapper;
private static final ObjectMapper objectMapper = new ObjectMapper();
public void deviceErrorMonitor() throws InterruptedException, IOException {
initStandard();
@@ -155,7 +162,6 @@ public class DeviceThreadService {
ErrorIdentificationStandard::getErrorSource,
ConcurrentHashMap::new,
Collectors.toList()));
}
} catch (IORuntimeException e) {
// TODO Auto-generated catch block
@@ -295,40 +301,73 @@ public class DeviceThreadService {
if (nowTime - lastActiveTime >= Constant.hostOfflineInterval) {
// 超时了判断离线
if (nettyDevice != null && nettyDevice.getOnlineStatus() != null
&& nettyDevice.getOnlineStatus() == 1) {
// if (nettyDevice != null && nettyDevice.getOnlineStatus() != null
// && nettyDevice.getOnlineStatus() == 1) {
try {
// 标记离线
nettyDevice.status(DeviceStatus.offline.getType());
updatelatestLocation(deviceId);
if (device != null) {
device.setOnlineStatus(DeviceStatus.offline.getType());
device.setUpdateTime(LocalDateTime.now());
deviceService.updateDeviceBySelf(device);
}
// try {
// // 标记离线
// nettyDevice.status(DeviceStatus.offline.getType());
// updatelatestLocation(deviceId);
// if (device != null) {
// device.setOnlineStatus(DeviceStatus.offline.getType());
// device.setUpdateTime(LocalDateTime.now());
// deviceService.updateDeviceBySelf(device);
// }
if (siteMemory != null) {
updateTotalArea(nettyDevice, siteMemory, deviceId);
}
// TODO 统计总里程、总作业面积
statusChangePush(deviceId, siteId, 0);
sessionManager.removeDevice(deviceId);
// if (siteMemory != null) {
// updateTotalArea(nettyDevice, siteMemory, deviceId);
// }
// // TODO 统计总里程、总作业面积
// statusChangePush(deviceId, siteId, 0);
// sessionManager.removeDevice(deviceId);
} catch (Exception e) {
log.error("设备离线处理异常,deviceId={}", deviceId, e);
// } catch (Exception e) {
// log.error("设备离线处理异常,deviceId={}", deviceId, e);
// } finally {
// // 必须执行:清理实时缓存
// try {
// if (siteMemory != null) {
// siteMemory.removeMapPushDTO(deviceId);
// siteMemory.removeVehicleMessage(deviceId);
// }
// } catch (Exception e) {
// log.error("设备离线缓存清理失败,deviceId={}", deviceId, e);
// }
// }
// }
try {
if (nettyDevice != null
&& Objects.equals(nettyDevice.getOnlineStatus(), 1)) {
} finally {
// 必须执行:清理实时缓存
try {
if (siteMemory != null) {
siteMemory.removeMapPushDTO(deviceId);
siteMemory.removeVehicleMessage(deviceId);
// 标记离线
nettyDevice.status(DeviceStatus.offline.getType());
updatelatestLocation(deviceId);
if (device != null) {
device.setOnlineStatus(DeviceStatus.offline.getType());
device.setUpdateTime(LocalDateTime.now());
deviceService.updateDeviceBySelf(device);
}
if (siteMemory != null) {
updateTotalArea(nettyDevice, siteMemory, deviceId);
}
statusChangePush(deviceId, siteId, 0);
sessionManager.removeDevice(deviceId);
} catch (Exception e) {
log.error("设备离线缓存清理失败,deviceId={}", deviceId, e);
log.error("设备离线处理异常,deviceId={}", deviceId, e);
}
}
} finally {
// 外层兜底清理
if (siteMemory != null) {
siteMemory.removeMapPushDTO(deviceId);
siteMemory.removeVehicleMessage(deviceId);
}
}
} else {
@@ -506,6 +545,53 @@ public class DeviceThreadService {
}, 0, 500, TimeUnit.MILLISECONDS);
}
public static double calculateDistance(
double lat1, double lon1,
double lat2, double lon2) {
double earthRadius = 6371000;
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2))
* Math.sin(dLon / 2) * Math.sin(dLon / 2);
return earthRadius * 2 * Math.atan2(
Math.sqrt(a), Math.sqrt(1 - a));
}
private boolean isAtRestPoint(String deviceId, SiteMemory siteMemory) {
DeviceMapPushDTO pushDto = siteMemory.getDeviceMapPusById(deviceId);
SiteMapInfo siteMapInfo = siteMemory.getSysSite().getSiteMapInfo();
if (pushDto == null || siteMapInfo == null)
return false;
List<RestPoint> restPoints = objectMapper.convertValue(
siteMapInfo.getRestPoints(),
new TypeReference<List<RestPoint>>() {
});
if (CollectionUtils.isEmpty(restPoints))
return false;
RestPoint switchRestPoint = restPoints.stream()
.filter(x -> deviceId.equals(x.getBindDeviceId())).findFirst().orElse(null);
if (switchRestPoint == null)
return false;
// 距离小于5米认为到位了
double distance = calculateDistance(
pushDto.getLat(), pushDto.getLng(),
switchRestPoint.getPosition().getLat(), switchRestPoint.getPosition().getLng());
boolean arrived = distance <= 2;
return arrived;
}
public void handleAutoRule(IotAutoRule rule, SiteMemory siteMemory) {
if (RuleType.LOW_BATTERY_RETURN.equals(rule.getRuleType())) {
@@ -518,6 +604,10 @@ public class DeviceThreadService {
vList.forEach(x -> {
String deviceId = x.getDeviceId();
String value = classFieldFinding(key, x);
// 电量小于0 不信任电量上报
if (Integer.parseInt(value) <= 0) {
return;
}
boolean result = CompareUtils.compare(compareType, value, compareValue);
log.debug("conditions:{},compareValue:{},value:{},result:{}", conditions, compareValue, value,
result);
@@ -529,6 +619,10 @@ public class DeviceThreadService {
if (siteMemory.hasTask(deviceId)) {
return;
}
if (isAtRestPoint(deviceId, siteMemory)) {
return;
}
DeviceTaskCommandDTO dto = new DeviceTaskCommandDTO();
dto.setDeviceId(deviceId);
dto.setOrgId(siteMemory.getSysSite().getOrgId());