#update
This commit is contained in:
@@ -17,6 +17,8 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.maibu.core.business.Device;
|
||||
import com.maibu.core.business.DevicePlan;
|
||||
import com.maibu.core.business.DevicePlanTask;
|
||||
@@ -26,20 +28,26 @@ import com.maibu.core.business.device.NettyDevice;
|
||||
import com.maibu.core.business.dto.DeviceTaskCommandDTO;
|
||||
import com.maibu.core.domain.entity.SysRegion;
|
||||
import com.maibu.core.domain.entity.SysSite;
|
||||
import com.maibu.core.domain.map.RestPoint;
|
||||
import com.maibu.core.domain.map.SiteMapInfo;
|
||||
import com.maibu.core.domain.model.LoginUser;
|
||||
import com.maibu.core.enums.DeviceTaskStaus;
|
||||
import com.maibu.core.enums.RouteType;
|
||||
import com.maibu.dto.DevicePlanStatisticsDTO;
|
||||
import com.maibu.dto.DevicePlanTaskStatisticsDTO;
|
||||
import com.maibu.dto.DeviceTaskQueryDTO;
|
||||
import com.maibu.dto.DeviceWorkStatisticsDTO;
|
||||
import com.maibu.exception.ServiceException;
|
||||
import com.maibu.mapper.DeviceMapper;
|
||||
import com.maibu.mapper.DevicePlanMapper;
|
||||
import com.maibu.mapper.DevicePlanTaskMapper;
|
||||
import com.maibu.mapper.SiteMapInfoMapper;
|
||||
import com.maibu.mapper.SysRegionMapper;
|
||||
import com.maibu.mapper.SysSiteMapper;
|
||||
import com.maibu.mapper.WorkRecordMapper;
|
||||
import com.maibu.memory.GlobalMemory;
|
||||
import com.maibu.memory.SiteMemory;
|
||||
import com.maibu.utils.MessageUtils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@@ -68,6 +76,8 @@ public class DeviceTaskService {
|
||||
@Autowired
|
||||
private SysRegionMapper sysRegionMapper;
|
||||
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
public int insertOrUpdate(DevicePlan devicePlan, LoginUser loginUser) {
|
||||
Long id = devicePlan.getId();
|
||||
SiteMemory siteMemory = GlobalMemory.getSiteMemory(devicePlan.getOrgId(), devicePlan.getSiteId());
|
||||
@@ -470,6 +480,12 @@ public class DeviceTaskService {
|
||||
task.setCreateTime(LocalDateTime.now());
|
||||
task.setRouteId(dto.getRouteId());
|
||||
task.setStartTime(dto.getStartTime());
|
||||
|
||||
WorkRecord workRecord = workRecordMapper.selectById(dto.getRouteId());
|
||||
if (workRecord != null) {
|
||||
task.setRouteType(workRecord.getRouteType());
|
||||
}
|
||||
|
||||
devicePlanTaskMapper.saveOrUpdate(task);
|
||||
if (dto.getOrgId() == null) {
|
||||
SysSite site = sysSiteMapper.selectById(dto.getSiteId());
|
||||
@@ -487,11 +503,22 @@ public class DeviceTaskService {
|
||||
queryWrapper.eq(SysRegion::getId, dto.getRegionId());
|
||||
SysRegion region = sysRegionMapper.selectOne(queryWrapper);
|
||||
|
||||
if (region == null || region.getBindRoute() == null) {
|
||||
throw new RuntimeException("区域未绑定线路!");
|
||||
if (region == null) {
|
||||
throw new RuntimeException("区域未找到!");
|
||||
}
|
||||
Long routeId = dto.getRouteId();
|
||||
if (routeId == null) {
|
||||
routeId = region.getDefaultRoute();
|
||||
}
|
||||
|
||||
if (dto.getOrgId() == null) {
|
||||
SysSite site = sysSiteMapper.selectById(dto.getSiteId());
|
||||
if (site != null) {
|
||||
dto.setOrgId(site.getOrgId());
|
||||
}
|
||||
}
|
||||
DevicePlanTask task = new DevicePlanTask();
|
||||
task.setRouteId(region.getBindRoute());
|
||||
task.setRouteId(routeId);
|
||||
task.setOrgId(dto.getOrgId());
|
||||
task.setSiteId(dto.getSiteId());
|
||||
task.setTaskStaus(DeviceTaskStaus.NEW);
|
||||
@@ -499,13 +526,14 @@ public class DeviceTaskService {
|
||||
task.setCreateBy(username);
|
||||
task.setCreateTime(LocalDateTime.now());
|
||||
task.setStartTime(dto.getStartTime());
|
||||
devicePlanTaskMapper.saveOrUpdate(task);
|
||||
if (dto.getOrgId() == null) {
|
||||
SysSite site = sysSiteMapper.selectById(dto.getSiteId());
|
||||
if (site != null) {
|
||||
dto.setOrgId(site.getOrgId());
|
||||
}
|
||||
|
||||
WorkRecord workRecord = workRecordMapper.selectById(region.getDefaultRoute());
|
||||
if (workRecord != null) {
|
||||
task.setRouteType(workRecord.getRouteType());
|
||||
}
|
||||
|
||||
devicePlanTaskMapper.saveOrUpdate(task);
|
||||
|
||||
SiteMemory siteMemory = GlobalMemory.getSiteMemory(task.getOrgId(), task.getSiteId());
|
||||
siteMemory.addDevicePlanPrepare(task);
|
||||
return task.getId();
|
||||
@@ -584,4 +612,60 @@ public class DeviceTaskService {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Long backToRestPoint(DeviceTaskCommandDTO dto, String username) {
|
||||
|
||||
if (dto.getOrgId() == null) {
|
||||
SysSite site = sysSiteMapper.selectById(dto.getSiteId());
|
||||
if (site != null) {
|
||||
dto.setOrgId(site.getOrgId());
|
||||
}
|
||||
}
|
||||
|
||||
SiteMemory siteMemory = GlobalMemory.getSiteMemory(dto.getOrgId(), dto.getSiteId());
|
||||
SiteMapInfo siteMapInfo;
|
||||
if (siteMemory == null) {
|
||||
throw new ServiceException(MessageUtils.message("任务下发出错!"));
|
||||
}
|
||||
|
||||
siteMapInfo = siteMemory.getSysSite().getSiteMapInfo();
|
||||
|
||||
if (siteMapInfo == null || CollectionUtils.isEmpty(siteMapInfo.getRestPoints())) {
|
||||
throw new ServiceException(MessageUtils.message("当前机器未配置返回点!"));
|
||||
}
|
||||
|
||||
List<RestPoint> restPoints = objectMapper.convertValue(
|
||||
siteMapInfo.getRestPoints(),
|
||||
new TypeReference<List<RestPoint>>() {
|
||||
});
|
||||
|
||||
RestPoint switchRestPoint = restPoints.stream()
|
||||
.filter(x -> dto.getDeviceId().equals(x.getBindDeviceId())).findFirst().orElse(null);
|
||||
|
||||
if (switchRestPoint == null) {
|
||||
throw new ServiceException(MessageUtils.message("当前机器未配置返回点!"));
|
||||
}
|
||||
|
||||
LambdaQueryWrapper<WorkRecord> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(WorkRecord::getRouteType, RouteType.BACK_TO_REST_POINT);
|
||||
WorkRecord workRecord = workRecordMapper.selectOne(queryWrapper);
|
||||
|
||||
DevicePlanTask task = new DevicePlanTask();
|
||||
|
||||
task.setRouteId(workRecord.getId());
|
||||
task.setRouteType(workRecord.getRouteType());
|
||||
|
||||
task.setBackTaskPoint(switchRestPoint);
|
||||
task.setOrgId(dto.getOrgId());
|
||||
task.setSiteId(dto.getSiteId());
|
||||
task.setTaskStaus(DeviceTaskStaus.NEW);
|
||||
task.setDeviceId(dto.getDeviceId());
|
||||
task.setCreateBy(username);
|
||||
task.setCreateTime(LocalDateTime.now());
|
||||
task.setStartTime(dto.getStartTime());
|
||||
devicePlanTaskMapper.saveOrUpdate(task);
|
||||
|
||||
siteMemory.addDevicePlanPrepare(task);
|
||||
return task.getId();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ 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.*;
|
||||
@@ -40,8 +41,13 @@ import com.maibu.core.business.ErrorIdentificationStandard;
|
||||
import com.maibu.core.business.alarm_center.AlarmMessage;
|
||||
import com.maibu.core.business.device.NettyDevice;
|
||||
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.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;
|
||||
@@ -95,6 +101,9 @@ public class DeviceThreadService {
|
||||
@Autowired
|
||||
private DeviceRunStatisticsMapper deviceRunStatisticsMapper;
|
||||
|
||||
@Autowired
|
||||
private DeviceTaskService deviceTaskService;
|
||||
|
||||
public void deviceErrorMonitor() throws InterruptedException, IOException {
|
||||
initStandard();
|
||||
|
||||
@@ -456,4 +465,101 @@ public class DeviceThreadService {
|
||||
}, 0, 100, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 规则触发自动判断
|
||||
*/
|
||||
public void autoRule() {
|
||||
executor.scheduleWithFixedDelay(() -> {
|
||||
try {
|
||||
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);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("执行规则出发线程错误:{}", e.getMessage());
|
||||
}
|
||||
}, 0, 500, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
public void handleAutoRule(IotAutoRule rule, SiteMemory siteMemory) {
|
||||
|
||||
if (RuleType.LOW_BATTERY_RETURN.equals(rule.getRuleType())) {
|
||||
TriggerConditions conditions = rule.getConditions();
|
||||
String key = conditions.getKey();
|
||||
CompareEnum compareType = conditions.getOperator();
|
||||
String compareValue = conditions.getValue();
|
||||
List<VehicleMessage> vList = siteMemory.getAllVehicleMessage();
|
||||
if (!CollectionUtils.isEmpty(vList)) {
|
||||
vList.forEach(x -> {
|
||||
String deviceId = x.getDeviceId();
|
||||
String value = classFieldFinding(key, x);
|
||||
boolean result = CompareUtils.compare(compareType, value, compareValue);
|
||||
if (result) {
|
||||
ActionConfig aConfig = rule.getActionConfig();
|
||||
if (aConfig != null && aConfig.getActionType() != null) {
|
||||
ActionType actionType = aConfig.getActionType();
|
||||
if (ActionType.BACK_TO_REST.equals(actionType)) {
|
||||
if (siteMemory.hasTask(deviceId)) {
|
||||
return;
|
||||
}
|
||||
DeviceTaskCommandDTO dto = new DeviceTaskCommandDTO();
|
||||
dto.setDeviceId(deviceId);
|
||||
dto.setOrgId(siteMemory.getSysSite().getOrgId());
|
||||
dto.setSiteId(siteMemory.getSysSite().getId());
|
||||
|
||||
deviceTaskService.backToRestPoint(dto, "system");
|
||||
}
|
||||
if (ActionType.AUTO_TASK.equals(actionType)) {
|
||||
if (siteMemory.hasTask(deviceId)) {
|
||||
return;
|
||||
}
|
||||
Long routeId = aConfig.getRouteId();
|
||||
DeviceTaskCommandDTO dto = new DeviceTaskCommandDTO();
|
||||
dto.setDeviceId(deviceId);
|
||||
dto.setOrgId(siteMemory.getSysSite().getOrgId());
|
||||
dto.setSiteId(siteMemory.getSysSite().getId());
|
||||
dto.setRouteId(routeId);
|
||||
deviceTaskService.createDeviceTask(dto, "system");
|
||||
}
|
||||
if (ActionType.ALARM.equals(actionType)) {
|
||||
|
||||
}
|
||||
if (ActionType.SEND_COMMAND.equals(actionType)) {
|
||||
|
||||
}
|
||||
if (ActionType.WORK_ORDER.equals(actionType)) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static String classFieldFinding(String fieldName, VehicleMessage history) {
|
||||
try {
|
||||
// 1. 获取类的Class对象
|
||||
Class<?> vehicleClass = VehicleMessage.class;
|
||||
Field field = vehicleClass.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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user