#消息推送相关

This commit is contained in:
2026-10-29 09:10:26 +08:00
parent 965b38158f
commit e501d58563
8 changed files with 123 additions and 65 deletions

View File

@@ -26,6 +26,9 @@ public class DevicePlanTask extends OrgBaseDO {
private String deviceId; private String deviceId;
@TableField(exist = false)
private String deviceAlias; // 别名
private DeviceTaskStaus taskStaus; // 任务状态 private DeviceTaskStaus taskStaus; // 任务状态
@TableField(exist = false) @TableField(exist = false)

View File

@@ -163,8 +163,9 @@ public class NettyDevice extends Connector {
return true; return true;
} }
public synchronized boolean canceledTask() { public synchronized Long canceledTask() {
if (task != null) { if (task != null) {
Long taskId = task.getId();
task.setTaskStaus(DeviceTaskStaus.CANCELED); task.setTaskStaus(DeviceTaskStaus.CANCELED);
task.setUpdateTime(LocalDateTime.now()); task.setUpdateTime(LocalDateTime.now());
devicePlanTaskMapper.saveOrUpdate(task); devicePlanTaskMapper.saveOrUpdate(task);
@@ -177,12 +178,14 @@ public class NettyDevice extends Connector {
currentPoint = null; currentPoint = null;
locationQueue.clear(); locationQueue.clear();
executingTask = false; executingTask = false;
return taskId;
} }
return true; return null;
} }
public synchronized boolean failedTask() { public synchronized Long failedTask() {
if (task != null) { if (task != null) {
Long taskId = task.getId();
task.setTaskStaus(DeviceTaskStaus.FAILED); task.setTaskStaus(DeviceTaskStaus.FAILED);
task.setUpdateTime(LocalDateTime.now()); task.setUpdateTime(LocalDateTime.now());
devicePlanTaskMapper.saveOrUpdate(task); devicePlanTaskMapper.saveOrUpdate(task);
@@ -195,8 +198,9 @@ public class NettyDevice extends Connector {
currentPoint = null; currentPoint = null;
locationQueue.clear(); locationQueue.clear();
executingTask = false; executingTask = false;
return taskId;
} }
return true; return null;
} }
public void sendPathCommand(byte commandType, short pointCounts, double lat, double lng, short speed) { public void sendPathCommand(byte commandType, short pointCounts, double lat, double lng, short speed) {

View File

@@ -40,11 +40,7 @@ public class IotAutoRuleRecord extends OrgNoUserDO {
*/ */
private String deviceId; private String deviceId;
/** private String triggerValue;
* 触发时的指标快照
*/
@TableField(typeHandler = JacksonTypeHandler.class)
private Object triggerValue;
/** /**
* 触发时间 * 触发时间

View File

@@ -1,8 +1,15 @@
package com.maibu.mapper; package com.maibu.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.maibu.core.business.DevicePlanTask; import com.maibu.core.business.DevicePlanTask;
import com.maibu.mybatis.mapper.BaseMapperX; import com.maibu.mybatis.mapper.BaseMapperX;
import org.springframework.stereotype.Repository;
/** /**
* 设备历史状态 * 设备历史状态
@@ -10,4 +17,11 @@ import org.springframework.stereotype.Repository;
@Repository @Repository
public interface DevicePlanTaskMapper extends BaseMapperX<DevicePlanTask> { public interface DevicePlanTaskMapper extends BaseMapperX<DevicePlanTask> {
@Select("SELECT t.*, d.device_alias " +
"FROM iot_device_task t " +
"LEFT JOIN iot_device d ON d.device_id = t.device_id " +
"${ew.customSqlSegment}")
List<DevicePlanTask> selectTaskHistoryWithDeviceAlias(
@Param(Constants.WRAPPER) Wrapper<DevicePlanTask> wrapper);
} }

View File

@@ -465,6 +465,16 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
} }
} }
} }
try {
NettyDevice device = deviceSessionManager.getDevice(deviceId);
if (device != null && device.getDevice() != null) {
Long siteId = device.getDevice().getSiteId();
websocketMesDispather.mapMessageDispather(siteId,
JsonUtils.toJsonString(dto));
}
} catch (Exception e) {
log.error("实时地图数据websocket推送失败: siteId={}, error={}", e);
}
// todo 推送实时位置和状态消息到外部 // todo 推送实时位置和状态消息到外部
try { try {
GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId, GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId,
@@ -512,11 +522,40 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId); NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId);
if ("completed".equals(dto.getStatus())) { if ("completed".equals(dto.getStatus())) {
Long taskId = nettyDevice.finishTask(); Long taskId = nettyDevice.finishTask();
pushTaskChangeMessage(deviceId, taskId, DeviceTaskStaus.FINISH.toString());
}
if ("cancelled".equals(dto.getStatus())) {
Long taskId = nettyDevice.canceledTask();
pushTaskChangeMessage(deviceId, taskId, DeviceTaskStaus.CANCELED.toString());
}
// if("paused".equals(dto.getStatus())){
// NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId);
// nettyDevice.finishTask();
// }
if ("failed".equals(dto.getStatus())) {
Long taskId = nettyDevice.failedTask();
pushTaskChangeMessage(deviceId, taskId, DeviceTaskStaus.FAILED.toString());
}
// if ("accepted".equals(dto.getStatus())) {
// NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId);
// nettyDevice.finishTask();
// }
// if("running".equals(dto.getStatus())){
// NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId);
// nettyDevice.finishTask();
// }
}
}
public void pushTaskChangeMessage(String deviceId, Long taskId, String status) {
WebDeviceTaskStatusMessageDTO sendDto = new WebDeviceTaskStatusMessageDTO(); WebDeviceTaskStatusMessageDTO sendDto = new WebDeviceTaskStatusMessageDTO();
sendDto.setDeviceId(deviceId); sendDto.setDeviceId(deviceId);
sendDto.setTaskId(taskId); sendDto.setTaskId(taskId);
sendDto.setStatus(DeviceTaskStaus.FINISH.toString()); sendDto.setStatus(status);
sendDto.setType(MesType.device_task_change); sendDto.setType(MesType.device_task_change);
List<NettyDevice> controlMasters = deviceSessionManager List<NettyDevice> controlMasters = deviceSessionManager
@@ -531,6 +570,16 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
} }
} }
} }
// try {
// NettyDevice device = deviceSessionManager.getDevice(deviceId);
// if (device != null && device.getDevice() != null) {
// Long siteId = device.getDevice().getSiteId();
// websocketMesDispather.mapMessageDispather(siteId,
// JsonUtils.toJsonString(sendDto));
// }
// } catch (Exception e) {
// log.error("实时地图数据websocket推送失败: siteId={}, error={}", e);
// }
// todo 推送实时位置和状态消息到外部 // todo 推送实时位置和状态消息到外部
try { try {
GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId, GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId,
@@ -539,28 +588,6 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
} catch (MqttException e) { } catch (MqttException e) {
log.error("实时数据websocket任务状态推送失败: deviceId={}, error={}", deviceId, e.getMessage(), e); log.error("实时数据websocket任务状态推送失败: deviceId={}, error={}", deviceId, e.getMessage(), e);
} }
}
if ("cancelled".equals(dto.getStatus())) {
nettyDevice.canceledTask();
}
// if("paused".equals(dto.getStatus())){
// NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId);
// nettyDevice.finishTask();
// }
if ("failed".equals(dto.getStatus())) {
nettyDevice.failedTask();
}
// if ("accepted".equals(dto.getStatus())) {
// NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId);
// nettyDevice.finishTask();
// }
// if("running".equals(dto.getStatus())){
// NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId);
// nettyDevice.finishTask();
// }
}
} }
/** /**
@@ -609,6 +636,15 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
} }
} }
} }
try {
if (device != null && device.getDevice() != null) {
Long siteId = device.getDevice().getSiteId();
websocketMesDispather.mapMessageDispather(siteId,
JsonUtils.toJsonString(sendDto));
}
} catch (Exception e) {
log.error("实时地图数据websocket推送失败: siteId={}, error={}", e);
}
// todo 推送实时位置和状态消息到外部 // todo 推送实时位置和状态消息到外部
try { try {
GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId, GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId,

View File

@@ -17,6 +17,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.maibu.core.business.Device; import com.maibu.core.business.Device;
@@ -420,29 +421,29 @@ public class DeviceTaskService {
} }
public List<DevicePlanTask> getSiteDeviceTaskHistory(DeviceTaskQueryDTO dto) { public List<DevicePlanTask> getSiteDeviceTaskHistory(DeviceTaskQueryDTO dto) {
LambdaQueryWrapper<DevicePlanTask> queryWrapper = new LambdaQueryWrapper<>(); QueryWrapper<DevicePlanTask> queryWrapper = new QueryWrapper<>();
if (dto.getSiteId() != null) { if (dto.getSiteId() != null) {
queryWrapper.eq(DevicePlanTask::getSiteId, dto.getSiteId()); queryWrapper.eq("t.site_id", dto.getSiteId());
} }
if (dto.getOrgId() != null) { if (dto.getOrgId() != null) {
queryWrapper.eq(DevicePlanTask::getOrgId, dto.getOrgId()); queryWrapper.eq("t.org_id", dto.getOrgId());
} }
if (dto.getTaskStaus() != null) { if (dto.getTaskStaus() != null) {
queryWrapper.eq(DevicePlanTask::getTaskStaus, dto.getTaskStaus()); queryWrapper.eq("t.task_staus", dto.getTaskStaus());
} }
if (dto.getPlanId() != null) { if (dto.getPlanId() != null) {
queryWrapper.eq(DevicePlanTask::getPlanId, dto.getPlanId()); queryWrapper.eq("t.plan_id", dto.getPlanId());
} }
if (!StringUtils.isEmpty(dto.getDeviceId())) { if (StringUtils.isNotEmpty(dto.getDeviceId())) {
queryWrapper.eq(DevicePlanTask::getDeviceId, dto.getDeviceId()); queryWrapper.eq("t.device_id", dto.getDeviceId());
} }
if (dto.getStartTime() != null) { if (dto.getStartTime() != null) {
queryWrapper.ge(DevicePlanTask::getCreateTime, dto.getStartTime()); queryWrapper.ge("t.create_time", dto.getStartTime());
} }
if (dto.getEndTime() != null) { if (dto.getEndTime() != null) {
queryWrapper.le(DevicePlanTask::getCreateTime, dto.getEndTime()); queryWrapper.le("t.create_time", dto.getEndTime());
} }
return transferDeviceTaskData(devicePlanTaskMapper.selectList(queryWrapper)); return transferDeviceTaskData(devicePlanTaskMapper.selectTaskHistoryWithDeviceAlias(queryWrapper));
} }
public List<DevicePlan> transferDevicePlanData(List<DevicePlan> list) { public List<DevicePlan> transferDevicePlanData(List<DevicePlan> list) {
@@ -552,7 +553,7 @@ public class DeviceTaskService {
NettyDevice device = deviceSessionManager.getDevice(deviceId); NettyDevice device = deviceSessionManager.getDevice(deviceId);
if (device != null && device.getTask() != null) { if (device != null && device.getTask() != null) {
if (dto.isForceCancel()) { if (dto.isForceCancel()) {
return device.canceledTask(); return device.canceledTask() != null;
} else { } else {
return device.cancelingTask(); return device.cancelingTask();
} }

View File

@@ -399,11 +399,16 @@ public class DeviceThreadService {
statusChangePush(deviceId, device.getSiteId(), 1); statusChangePush(deviceId, device.getSiteId(), 1);
} }
} else { } else {
Integer lastOnlineStatus = nettyDevice.getOnlineStatus();
if (lastOnlineStatus == 0) {
statusChangePush(deviceId, device.getSiteId(), 1);
}
// todo 推送设备上线 // todo 推送设备上线
nettyDevice.status(1); nettyDevice.status(1);
nettyDevice.setDevice(device); nettyDevice.setDevice(device);
nettyDevice.setConnectorStatus(ConnectorStatus.ACTIVE); nettyDevice.setConnectorStatus(ConnectorStatus.ACTIVE);
statusChangePush(deviceId, device.getSiteId(), 1);
} }
} }
}); });

View File

@@ -7,13 +7,14 @@ import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import com.maibu.constant.Constants;
@Service @Service
public class HttpService { public class HttpService {
@Autowired @Autowired
private RestTemplate restTemplate; private RestTemplate restTemplate;
/** /**
* 通用 GET 请求(手动传入 Token,每次可传不同值) * 通用 GET 请求(手动传入 Token,每次可传不同值)
* *
@@ -32,8 +33,7 @@ public class HttpService {
apiUrl, apiUrl,
HttpMethod.GET, HttpMethod.GET,
httpEntity, httpEntity,
String.class String.class);
);
// 校验响应状态并返回结果 // 校验响应状态并返回结果
if (response.getStatusCode().is2xxSuccessful()) { if (response.getStatusCode().is2xxSuccessful()) {
return response.getBody() == null ? "" : response.getBody(); return response.getBody() == null ? "" : response.getBody();
@@ -68,8 +68,7 @@ public class HttpService {
apiUrl, apiUrl,
HttpMethod.POST, HttpMethod.POST,
httpEntity, httpEntity,
String.class String.class);
);
// 校验响应状态并返回结果 // 校验响应状态并返回结果
if (response.getStatusCode().is2xxSuccessful()) { if (response.getStatusCode().is2xxSuccessful()) {
return response.getBody() == null ? "" : response.getBody(); return response.getBody() == null ? "" : response.getBody();
@@ -93,18 +92,18 @@ public class HttpService {
*/ */
private HttpHeaders buildHeadersWithToken(String token) { private HttpHeaders buildHeadersWithToken(String token) {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
// 设置默认 Content-Type 为 JSON 格式
headers.setContentType(MediaType.APPLICATION_JSON_UTF8); headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Accept", "application/json"); headers.set("Accept", "application/json");
// 若 Token 不为空,添加 Authorization 头(默认 Bearer 格式,可按需修改)
// 与 LanguageInterceptor 读取的请求头保持一致
headers.set(Constants.LANGUAGE, "zh-CN");
if (token != null && !token.trim().isEmpty()) { if (token != null && !token.trim().isEmpty()) {
// 如需去掉 Bearer 前缀,直接改为 headers.set("Authorization", token.trim())
headers.set("Authorization", "Bearer " + token.trim()); headers.set("Authorization", "Bearer " + token.trim());
} }
// 可按需添加其他通用请求头(如 User-Agent)
// headers.set("User-Agent", "RestTemplate-Common/1.0");
return headers; return headers;
} }
} }