#更新
This commit is contained in:
@@ -2,7 +2,6 @@ package com.maibu.service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -14,16 +13,7 @@ import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.maibu.core.business.dto.DeviceMapPushDTO;
|
||||
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.enums.*;
|
||||
import com.maibu.utils.json.JsonUtils;
|
||||
import com.maibu.utils.spring.SpringUtils;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.checkerframework.checker.units.qual.s;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.Resource;
|
||||
@@ -44,17 +34,27 @@ import com.maibu.core.business.ErrorIdentificationStandard;
|
||||
import com.maibu.core.business.RegionConsecutiveTaskRecord;
|
||||
import com.maibu.core.business.alarm_center.AlarmMessage;
|
||||
import com.maibu.core.business.device.NettyDevice;
|
||||
import com.maibu.core.business.dto.DeviceMapPushDTO;
|
||||
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.AjaxResult;
|
||||
import com.maibu.core.domain.rule.ActionConfig;
|
||||
import com.maibu.core.domain.rule.IotAutoRule;
|
||||
import com.maibu.core.domain.rule.TriggerConditions;
|
||||
import com.maibu.core.enums.ActionType;
|
||||
import com.maibu.core.enums.CompareEnum;
|
||||
import com.maibu.core.enums.ConnectorStatus;
|
||||
import com.maibu.core.enums.DeviceStatus;
|
||||
import com.maibu.core.enums.ErrorSource;
|
||||
import com.maibu.core.enums.MesType;
|
||||
import com.maibu.core.enums.RespondCode;
|
||||
import com.maibu.core.enums.RuleType;
|
||||
import com.maibu.core.host.HeartBeatDTO;
|
||||
import com.maibu.core.host.LocationMessage;
|
||||
import com.maibu.core.host.VehicleMessage;
|
||||
import com.maibu.core.redis.RedisCache;
|
||||
import com.maibu.dto.DeviceErrorPushDTO;
|
||||
import com.maibu.dto.DeviceTaskStatusMessageDTO;
|
||||
import com.maibu.mapper.DeviceRunStatisticsMapper;
|
||||
import com.maibu.mapper.ErrorIdentificationStandardMapper;
|
||||
import com.maibu.mapper.HostLocationMapper;
|
||||
@@ -64,6 +64,7 @@ import com.maibu.memory.GlobalMemory;
|
||||
import com.maibu.memory.SiteMemory;
|
||||
import com.maibu.mqtt.MqttTopic;
|
||||
import com.maibu.utils.CompareUtils;
|
||||
import com.maibu.utils.json.JsonUtils;
|
||||
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
@@ -162,7 +163,7 @@ public class DeviceThreadService {
|
||||
});
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("执行监测错误线程错误:{}", e.getMessage());
|
||||
log.error("执行监测错误线程错误", e);
|
||||
}
|
||||
}, 0, 5, TimeUnit.SECONDS);
|
||||
}
|
||||
@@ -237,7 +238,7 @@ public class DeviceThreadService {
|
||||
});
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("推送错误信息失败 device:{}error:{}", deviceId, e.getMessage());
|
||||
log.error("推送错误信息失败 device:{}error", deviceId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -287,8 +288,7 @@ public class DeviceThreadService {
|
||||
Object value = field.get(history);
|
||||
return (String) value;
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
System.out.println("获取字段失败: " + e.getMessage());
|
||||
log.error("获取属性值失败:{}", e.getMessage());
|
||||
log.error("获取属性值失败", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -318,63 +318,44 @@ public class DeviceThreadService {
|
||||
// 超时了判断离线
|
||||
if (nettyDevice != null && nettyDevice.getOnlineStatus() != null
|
||||
&& nettyDevice.getOnlineStatus() == 1) {
|
||||
nettyDevice.status(DeviceStatus.offline.getType());
|
||||
// todo 推送设备离线
|
||||
String key = NettyCacheKey.latestPositonKey + deviceId;
|
||||
// 保存一下最新一次的数据到 数据库
|
||||
LocationMessage locationMessage = redisCache.getCacheObject(key);
|
||||
if (locationMessage != null) {
|
||||
LambdaQueryWrapper<LocationMessage> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(LocationMessage::getDeviceId, deviceId);
|
||||
LocationMessage existing = hostLocationMapper.selectOne(queryWrapper);
|
||||
if (existing != null) {
|
||||
locationMessage.setId(existing.getId());
|
||||
} else {
|
||||
locationMessage.setCreateTime(LocalDateTime.now());
|
||||
}
|
||||
locationMessage.setUpdateTime(LocalDateTime.now());
|
||||
hostLocationMapper.saveOrUpdate(locationMessage);
|
||||
}
|
||||
Device device = deviceService.selectDeviceBySerialNumber(deviceId);
|
||||
if (device != null) {
|
||||
device.setOnlineStatus(DeviceStatus.offline.getType());
|
||||
device.setUpdateTime(LocalDateTime.now());
|
||||
deviceService.updateDeviceBySelf(device);
|
||||
}
|
||||
|
||||
Long orgId = nettyDevice.getDevice().getOrgId();
|
||||
Long siteId = nettyDevice.getDevice().getSiteId();
|
||||
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
|
||||
if (siteMemory != null) {
|
||||
DeviceRunStatistics deviceRunStatistics = siteMemory.getRunStatistics(deviceId);
|
||||
if (deviceRunStatistics != null) {
|
||||
LambdaQueryWrapper<DeviceRunStatistics> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(DeviceRunStatistics::getDeviceId, deviceId);
|
||||
DeviceRunStatistics existing = deviceRunStatisticsMapper
|
||||
.selectOne(queryWrapper);
|
||||
if (existing != null) {
|
||||
deviceRunStatistics.setId(existing.getId());
|
||||
deviceRunStatistics.setDistance(
|
||||
existing.getDistance() + deviceRunStatistics.getDistance());
|
||||
deviceRunStatistics.setWorkArea(
|
||||
existing.getWorkArea() + deviceRunStatistics.getWorkArea());
|
||||
long time = System.currentTimeMillis() - nettyDevice.getLatestLoginTime();
|
||||
deviceRunStatistics.setTime(existing.getTime() + time / (1000 * 60)); // 累加时长,单位为分钟
|
||||
} else {
|
||||
long time = System.currentTimeMillis() - nettyDevice.getLatestLoginTime();
|
||||
deviceRunStatistics.setTime(time / (1000 * 60)); // 累加时长,单位为分钟
|
||||
}
|
||||
deviceRunStatisticsMapper.saveOrUpdate(deviceRunStatistics);
|
||||
|
||||
try {
|
||||
// 标记离线
|
||||
nettyDevice.status(DeviceStatus.offline.getType());
|
||||
updatelatestLocation(deviceId);
|
||||
Device device = deviceService.selectDeviceBySerialNumber(deviceId);
|
||||
if (device != null) {
|
||||
device.setOnlineStatus(DeviceStatus.offline.getType());
|
||||
device.setUpdateTime(LocalDateTime.now());
|
||||
deviceService.updateDeviceBySelf(device);
|
||||
}
|
||||
|
||||
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
|
||||
if (siteMemory != null) {
|
||||
updateTotalArea(nettyDevice, siteMemory, deviceId);
|
||||
}
|
||||
// TODO 统计总里程、总作业面积
|
||||
statusChangePush(deviceId, siteId, 0);
|
||||
sessionManager.removeDevice(deviceId);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("设备离线处理异常,deviceId={}", deviceId, e);
|
||||
|
||||
} finally {
|
||||
// 必须执行:清理实时缓存
|
||||
try {
|
||||
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
|
||||
if (siteMemory != null) {
|
||||
siteMemory.removeMapPushDTO(deviceId);
|
||||
siteMemory.removeVehicleMessage(deviceId);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("设备离线缓存清理失败,deviceId={}", deviceId, e);
|
||||
}
|
||||
// todo 删除实时消息推送里面的数据
|
||||
siteMemory.removeMapPushDTO(deviceId);
|
||||
siteMemory.removeVehicleMessage(deviceId);
|
||||
}
|
||||
|
||||
statusChangePush(deviceId, siteId, 0);
|
||||
sessionManager.removeDevice(deviceId);
|
||||
// TODO 统计总里程、总作业面积
|
||||
|
||||
}
|
||||
} else {
|
||||
Device device = deviceService.selectDeviceBySerialNumber(deviceId);
|
||||
@@ -428,6 +409,50 @@ public class DeviceThreadService {
|
||||
}, 0, 500, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
public void updatelatestLocation(String deviceId) {
|
||||
// todo 推送设备离线
|
||||
String key = NettyCacheKey.latestPositonKey + deviceId;
|
||||
// 保存一下最新一次的数据到 数据库
|
||||
LocationMessage locationMessage = redisCache.getCacheObject(key);
|
||||
if (locationMessage != null) {
|
||||
LambdaQueryWrapper<LocationMessage> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(LocationMessage::getDeviceId, deviceId);
|
||||
LocationMessage existing = hostLocationMapper.selectOne(queryWrapper);
|
||||
if (existing != null) {
|
||||
locationMessage.setId(existing.getId());
|
||||
} else {
|
||||
locationMessage.setCreateTime(LocalDateTime.now());
|
||||
}
|
||||
locationMessage.setUpdateTime(LocalDateTime.now());
|
||||
hostLocationMapper.saveOrUpdate(locationMessage);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateTotalArea(NettyDevice nettyDevice, SiteMemory siteMemory, String deviceId) {
|
||||
DeviceRunStatistics deviceRunStatistics = siteMemory.getRunStatistics(deviceId);
|
||||
if (deviceRunStatistics != null) {
|
||||
LambdaQueryWrapper<DeviceRunStatistics> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(DeviceRunStatistics::getDeviceId, deviceId);
|
||||
DeviceRunStatistics existing = deviceRunStatisticsMapper
|
||||
.selectOne(queryWrapper);
|
||||
if (existing != null) {
|
||||
deviceRunStatistics.setId(existing.getId());
|
||||
deviceRunStatistics.setDistance(
|
||||
existing.getDistance() + deviceRunStatistics.getDistance());
|
||||
deviceRunStatistics.setWorkArea(
|
||||
existing.getWorkArea() + deviceRunStatistics.getWorkArea());
|
||||
long time = System.currentTimeMillis()
|
||||
- nettyDevice.getLatestLoginTime();
|
||||
deviceRunStatistics.setTime(existing.getTime() + time / (1000 * 60)); // 累加时长,单位为分钟
|
||||
} else {
|
||||
long time = System.currentTimeMillis()
|
||||
- nettyDevice.getLatestLoginTime();
|
||||
deviceRunStatistics.setTime(time / (1000 * 60)); // 累加时长,单位为分钟
|
||||
}
|
||||
deviceRunStatisticsMapper.saveOrUpdate(deviceRunStatistics);
|
||||
}
|
||||
}
|
||||
|
||||
public void statusChangePush(String deviceId, Long siteId, Integer status) {
|
||||
// todo 推送设备离线
|
||||
WebOlineStatusMessageDTO dto = new WebOlineStatusMessageDTO();
|
||||
@@ -474,7 +499,7 @@ public class DeviceThreadService {
|
||||
});
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("执行监测心跳线程错误:{}", e.getMessage());
|
||||
log.error("执行监测心跳线程错误", e);
|
||||
}
|
||||
}, 0, 100, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
@@ -488,17 +513,24 @@ public class DeviceThreadService {
|
||||
List<SiteMemory> list = GlobalMemory.getAllSiteMemory();
|
||||
if (!CollectionUtils.isEmpty(list)) {
|
||||
list.forEach(x -> {
|
||||
List<IotAutoRule> rules = x.getAllIotAutoRule();
|
||||
if (!CollectionUtils.isEmpty(rules)) {
|
||||
rules.forEach(rule -> {
|
||||
// 根据不同的 类型区分处理方式
|
||||
handleAutoRule(rule, x);
|
||||
if (!CollectionUtils.isEmpty(x.iotAutoRuleMap)) {
|
||||
x.iotAutoRuleMap.keySet().forEach(y -> {
|
||||
List<IotAutoRule> rules = x.iotAutoRuleMap.get(y);
|
||||
if (!CollectionUtils.isEmpty(rules)) {
|
||||
rules.forEach(rule -> {
|
||||
// 根据不同的 类型区分处理方式
|
||||
if (rule != null && rule.getEnabled()) {
|
||||
handleAutoRule(rule, x);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("执行规则出发线程错误:{}", e.getMessage());
|
||||
log.error("执行规则出发线程错误", e);
|
||||
}
|
||||
}, 0, 500, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
@@ -529,7 +561,12 @@ public class DeviceThreadService {
|
||||
dto.setOrgId(siteMemory.getSysSite().getOrgId());
|
||||
dto.setSiteId(siteMemory.getSysSite().getId());
|
||||
|
||||
deviceTaskService.backToRestPoint(dto, "system");
|
||||
try {
|
||||
deviceTaskService.backToRestPoint(dto, "system");
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (ActionType.AUTO_TASK.equals(actionType)) {
|
||||
if (siteMemory.hasTask(deviceId)) {
|
||||
@@ -541,7 +578,12 @@ public class DeviceThreadService {
|
||||
dto.setOrgId(siteMemory.getSysSite().getOrgId());
|
||||
dto.setSiteId(siteMemory.getSysSite().getId());
|
||||
dto.setRouteId(routeId);
|
||||
deviceTaskService.createDeviceTask(dto, "system");
|
||||
try {
|
||||
deviceTaskService.createDeviceTask(dto, "system");
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (ActionType.ALARM.equals(actionType)) {
|
||||
|
||||
@@ -568,10 +610,9 @@ public class DeviceThreadService {
|
||||
Field field = vehicleClass.getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
Object value = field.get(history);
|
||||
return (String) value;
|
||||
return String.valueOf(value);
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
System.out.println("获取字段失败: " + e.getMessage());
|
||||
log.error("获取属性值失败:{}", e.getMessage());
|
||||
log.error("获取属性值失败:", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -602,7 +643,13 @@ public class DeviceThreadService {
|
||||
dto.setOrgId(siteMemory.getSysSite().getOrgId());
|
||||
dto.setSiteId(siteMemory.getSysSite().getId());
|
||||
dto.setRouteId(routeId);
|
||||
Long taskId = deviceTaskService.createDeviceTask(dto, "system");
|
||||
Long taskId = null;
|
||||
try {
|
||||
taskId = deviceTaskService.createDeviceTask(dto, "system");
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (taskId != null) {
|
||||
record.getTaskIds().add(taskId);
|
||||
record.setCurrentIndex(currentIndex + 1);
|
||||
@@ -616,7 +663,7 @@ public class DeviceThreadService {
|
||||
});
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("区域连续任务处理线程错误:{}", e.getMessage());
|
||||
log.error("区域连续任务处理线程错误", e);
|
||||
}
|
||||
}, 0, 500, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user