Compare commits
2 Commits
dev_map
...
be27d2b56e
| Author | SHA1 | Date | |
|---|---|---|---|
| be27d2b56e | |||
| f690786847 |
@@ -102,11 +102,6 @@ public class DeviceTask {
|
|||||||
return builder(FastBeeConstant.TASK.DEVICE_HEARTBEAT_MONITOR);
|
return builder(FastBeeConstant.TASK.DEVICE_HEARTBEAT_MONITOR);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean(FastBeeConstant.TASK.MAP_MESSAGE_PUSH)
|
|
||||||
public Executor mapMessagePush() {
|
|
||||||
return builder(FastBeeConstant.TASK.MAP_MESSAGE_PUSH);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean(FastBeeConstant.TASK.DEVICE_TASK_HANDLER)
|
@Bean(FastBeeConstant.TASK.DEVICE_TASK_HANDLER)
|
||||||
public Executor deviceTaskHandler() {
|
public Executor deviceTaskHandler() {
|
||||||
return builder(FastBeeConstant.TASK.DEVICE_TASK_HANDLER);
|
return builder(FastBeeConstant.TASK.DEVICE_TASK_HANDLER);
|
||||||
|
|||||||
@@ -105,13 +105,6 @@ public interface FastBeeConstant {
|
|||||||
* 设备错误
|
* 设备错误
|
||||||
*/
|
*/
|
||||||
String DEVICE_HEARTBEAT_MONITOR = "deviceHeartBeatMonitor";
|
String DEVICE_HEARTBEAT_MONITOR = "deviceHeartBeatMonitor";
|
||||||
|
|
||||||
/**
|
|
||||||
* 设备错误
|
|
||||||
*/
|
|
||||||
String MAP_MESSAGE_PUSH = "mapMessagePush";
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设备任务处理线程
|
* 设备任务处理线程
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
package com.maibu.core.business.dto;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public class DeviceMapPushDTO {
|
|
||||||
private String id;
|
|
||||||
private double lat;
|
|
||||||
private double lng;
|
|
||||||
private double y; //yaw
|
|
||||||
private String f; //fix_status
|
|
||||||
private double a; //altitude
|
|
||||||
private String s;//status 运行状态
|
|
||||||
private Long t; //taskId 任务id
|
|
||||||
private double b; //电量
|
|
||||||
private boolean ha = false;//hasAlarm 有无告警
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
package com.maibu.core.business.dto;
|
|
||||||
|
|
||||||
import com.maibu.core.enums.MesType;
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public class WebMapMessageDTO {
|
|
||||||
private List<DeviceMapPushDTO> data;
|
|
||||||
private MesType type;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.maibu.core.business.inter;
|
||||||
|
|
||||||
|
public interface DeviceWebSocketInterace {
|
||||||
|
|
||||||
|
void sendWebsoketMes(String key, String data);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package com.maibu.core.business.inter;
|
|
||||||
|
|
||||||
public interface DeviceWebSocketInterface {
|
|
||||||
|
|
||||||
void sendWebsocketMes(String key, String data);
|
|
||||||
|
|
||||||
|
|
||||||
void sendMapWebsocketMes(Long siteId,String data);
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -14,19 +14,10 @@ public class WebsocketMesDispather {
|
|||||||
private ApplicationContext applicationContext;
|
private ApplicationContext applicationContext;
|
||||||
|
|
||||||
public void dispather(String key, String data) {
|
public void dispather(String key, String data) {
|
||||||
Map<String, DeviceWebSocketInterface> beanMap = applicationContext.getBeansOfType(DeviceWebSocketInterface.class);
|
Map<String, DeviceWebSocketInterace> beanMap = applicationContext.getBeansOfType(DeviceWebSocketInterace.class);
|
||||||
// 遍历
|
// 遍历
|
||||||
beanMap.values().forEach(x -> {
|
beanMap.values().forEach(x -> {
|
||||||
x.sendWebsocketMes(key, data);
|
x.sendWebsoketMes(key, data);
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void mapMessageDispather(Long siteId, String data) {
|
|
||||||
Map<String, DeviceWebSocketInterface> beanMap = applicationContext.getBeansOfType(DeviceWebSocketInterface.class);
|
|
||||||
// 遍历
|
|
||||||
beanMap.values().forEach(x -> {
|
|
||||||
x.sendMapWebsocketMes(siteId, data);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,5 @@ public enum MesType {
|
|||||||
detectionReport, // 障碍物消息
|
detectionReport, // 障碍物消息
|
||||||
switchPermission, // 切换控制申请
|
switchPermission, // 切换控制申请
|
||||||
switchResult,// 切换结果
|
switchResult,// 切换结果
|
||||||
route_post,// 路径规划 点位推送
|
route_post;// 路径规划 点位推送
|
||||||
mapPush;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,11 +12,11 @@ import lombok.Getter;
|
|||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public enum DeviceStatusEnum {
|
public enum DeviceStatusEnum {
|
||||||
|
|
||||||
working("1", "工作中"),
|
offline("0", "离线"),
|
||||||
idle("2", "空闲中"),
|
idle("1", "空闲中"),
|
||||||
offline("3", "离线"),
|
working("2", "工作中"),
|
||||||
charging("4", "充电中"),
|
charging("3", "充电中"),
|
||||||
error("5", "故障");
|
error("4", "故障");
|
||||||
|
|
||||||
String code;
|
String code;
|
||||||
String message;
|
String message;
|
||||||
|
|||||||
@@ -5,8 +5,12 @@ import com.maibu.core.business.device.Connector;
|
|||||||
import com.maibu.core.business.device.NettyDevice;
|
import com.maibu.core.business.device.NettyDevice;
|
||||||
import com.maibu.core.enums.ConnectorStatus;
|
import com.maibu.core.enums.ConnectorStatus;
|
||||||
import com.maibu.core.enums.ConnectorType;
|
import com.maibu.core.enums.ConnectorType;
|
||||||
|
import com.maibu.utils.json.JsonUtils;
|
||||||
|
|
||||||
import io.netty.channel.Channel;
|
import io.netty.channel.Channel;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -23,6 +27,7 @@ import java.util.stream.Collectors;
|
|||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
@Data
|
@Data
|
||||||
|
@Slf4j
|
||||||
public class DeviceSessionManager {
|
public class DeviceSessionManager {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(DeviceSessionManager.class);
|
private static final Logger logger = LoggerFactory.getLogger(DeviceSessionManager.class);
|
||||||
@@ -187,7 +192,7 @@ public class DeviceSessionManager {
|
|||||||
public List<NettyDevice> getSlaveControl(String slaveId) {
|
public List<NettyDevice> getSlaveControl(String slaveId) {
|
||||||
// 获取所有连接的主机 且是active状态的
|
// 获取所有连接的主机 且是active状态的
|
||||||
return deviceMap.values().stream()
|
return deviceMap.values().stream()
|
||||||
.filter(device -> ObjectUtil.equals(device.getCurrentSlaveId(), slaveId)
|
.filter(device -> Objects.equals(device.getCurrentSlaveId(), slaveId)
|
||||||
&& ConnectorStatus.ACTIVE.equals(device.getConnectorStatus()))
|
&& ConnectorStatus.ACTIVE.equals(device.getConnectorStatus()))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
@@ -195,14 +200,16 @@ public class DeviceSessionManager {
|
|||||||
public List<NettyDevice> getAllSlaveControl(String slaveId) {
|
public List<NettyDevice> getAllSlaveControl(String slaveId) {
|
||||||
// 获取所有连接的主机 不区分状态
|
// 获取所有连接的主机 不区分状态
|
||||||
return deviceMap.values().stream()
|
return deviceMap.values().stream()
|
||||||
.filter(device -> ObjectUtil.equals(device.getCurrentSlaveId(), slaveId))
|
.filter(device -> !StringUtils.isEmpty(device.getCurrentSlaveId())
|
||||||
|
&& Objects.equals(device.getCurrentSlaveId(), slaveId)
|
||||||
|
&& device.getDeviceType().equals(ConnectorType.MASTER))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public NettyDevice getCurrentControl(String slaveId) {
|
public NettyDevice getCurrentControl(String slaveId) {
|
||||||
// 获取所有连接的主机 不区分状态
|
// 获取所有连接的主机 不区分状态
|
||||||
return deviceMap.values().stream()
|
return deviceMap.values().stream()
|
||||||
.filter(device -> ObjectUtil.equals(device.getCurrentSlaveId(), slaveId)
|
.filter(device -> Objects.equals(device.getCurrentSlaveId(), slaveId)
|
||||||
&& ConnectorStatus.ACTIVE.equals(device.getConnectorStatus()))
|
&& ConnectorStatus.ACTIVE.equals(device.getConnectorStatus()))
|
||||||
.findFirst().orElse(null);
|
.findFirst().orElse(null);
|
||||||
}
|
}
|
||||||
@@ -312,8 +319,11 @@ public class DeviceSessionManager {
|
|||||||
if (taskId == null) {
|
if (taskId == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
log.debug("sendMessage deviceMap:{},taskId={}", JsonUtils.toJsonString(deviceMap), taskId);
|
||||||
for (String key : deviceMap.keySet()) {
|
for (String key : deviceMap.keySet()) {
|
||||||
NettyDevice nettyDevice = deviceMap.get(key);
|
NettyDevice nettyDevice = deviceMap.get(key);
|
||||||
|
log.debug("sendMessage currentTaskId:{},equal {}", JsonUtils.toJsonString(nettyDevice.getCurrentTaskId()),
|
||||||
|
Objects.equals(nettyDevice.getCurrentTaskId(), taskId));
|
||||||
if (nettyDevice != null && Objects.equals(nettyDevice.getCurrentTaskId(), taskId)) {
|
if (nettyDevice != null && Objects.equals(nettyDevice.getCurrentTaskId(), taskId)) {
|
||||||
return nettyDevice.getConnectorId();
|
return nettyDevice.getConnectorId();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import java.util.Objects;
|
|||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import com.maibu.core.business.*;
|
import com.maibu.core.business.*;
|
||||||
import com.maibu.core.business.dto.DeviceMapPushDTO;
|
|
||||||
import com.maibu.mqtt.CustomMqttDeviceMonitor;
|
import com.maibu.mqtt.CustomMqttDeviceMonitor;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||||
@@ -57,17 +56,6 @@ public class SiteMemory {
|
|||||||
// 记录 设备上线后的运行数据 下线后清除并保存到数据库
|
// 记录 设备上线后的运行数据 下线后清除并保存到数据库
|
||||||
public static ConcurrentHashMap<String, DeviceRunStatistics> deviceRunStatisticsMap = new ConcurrentHashMap<>();
|
public static ConcurrentHashMap<String, DeviceRunStatistics> deviceRunStatisticsMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
public static ConcurrentHashMap<String, DeviceMapPushDTO> deviceMapPushMap = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
public Map<String, DeviceMapPushDTO> getAllDeviceMapPush() {
|
|
||||||
return deviceMapPushMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void saveMaPushDTO(String deviceId, DeviceMapPushDTO dto) {
|
|
||||||
deviceMapPushMap.put(deviceId, dto);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void saveRunStatistics(DeviceRunStatistics deviceRunStatistics) {
|
public void saveRunStatistics(DeviceRunStatistics deviceRunStatistics) {
|
||||||
if (deviceRunStatistics != null && !StringUtils.isEmpty(deviceRunStatistics.getDeviceId())) {
|
if (deviceRunStatistics != null && !StringUtils.isEmpty(deviceRunStatistics.getDeviceId())) {
|
||||||
deviceRunStatisticsMap.put(deviceRunStatistics.getDeviceId(), deviceRunStatistics);
|
deviceRunStatisticsMap.put(deviceRunStatistics.getDeviceId(), deviceRunStatistics);
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package com.maibu.mqtt;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.maibu.core.business.dto.DeviceMapPushDTO;
|
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
@@ -218,27 +218,6 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
|
|||||||
|
|
||||||
webStatusMessageDTO.setData(transferData);
|
webStatusMessageDTO.setData(transferData);
|
||||||
webStatusMessageDTO.setType(MesType.locationInfo);
|
webStatusMessageDTO.setType(MesType.locationInfo);
|
||||||
|
|
||||||
if (nettyDevice != null && nettyDevice.getDevice() != null) {
|
|
||||||
Long siteId = nettyDevice.getDevice().getSiteId();
|
|
||||||
Long orgId = nettyDevice.getDevice().getOrgId();
|
|
||||||
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
|
|
||||||
if (siteMemory != null) {
|
|
||||||
DeviceMapPushDTO dto = new DeviceMapPushDTO();
|
|
||||||
dto.setA(Double.parseDouble(altitude.getValue()));
|
|
||||||
dto.setId(deviceId);
|
|
||||||
// dto.setB();
|
|
||||||
// dto.setHa();
|
|
||||||
dto.setF(fix_status.getValue());
|
|
||||||
dto.setS(status.getValue());
|
|
||||||
dto.setLat(Double.parseDouble(lat.getValue()));
|
|
||||||
dto.setLng(Double.parseDouble(lng.getValue()));
|
|
||||||
dto.setT(nettyDevice.getCurrentTaskId());
|
|
||||||
dto.setY(Double.parseDouble(yaw.getValue()));
|
|
||||||
siteMemory.saveMaPushDTO(deviceId, dto);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return webStatusMessageDTO;
|
return webStatusMessageDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -376,6 +355,14 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//todo 推送实时位置和状态消息到外部
|
||||||
|
try {
|
||||||
|
GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId, String.format(MqttTopic.DEVICE_TASK_STATUS_TOPIC, taskId),
|
||||||
|
JsonUtils.toJsonString(sendDto));
|
||||||
|
} catch (MqttException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
if ("cancelled".equals(dto.getStatus())) {
|
if ("cancelled".equals(dto.getStatus())) {
|
||||||
|
|||||||
@@ -42,9 +42,6 @@ public class InitThread implements ApplicationRunner {
|
|||||||
@Resource(name = FastBeeConstant.TASK.DEVICE_HEARTBEAT_MONITOR)
|
@Resource(name = FastBeeConstant.TASK.DEVICE_HEARTBEAT_MONITOR)
|
||||||
private Executor deviceHeartBeatMonitorExecutor;
|
private Executor deviceHeartBeatMonitorExecutor;
|
||||||
|
|
||||||
@Resource(name = FastBeeConstant.TASK.MAP_MESSAGE_PUSH)
|
|
||||||
private Executor mapMessagePushExecutor;
|
|
||||||
|
|
||||||
@Resource(name = FastBeeConstant.TASK.COMMON_DEVICE_MONITOR)
|
@Resource(name = FastBeeConstant.TASK.COMMON_DEVICE_MONITOR)
|
||||||
private Executor commonDeviceMonitorExecutor;
|
private Executor commonDeviceMonitorExecutor;
|
||||||
|
|
||||||
@@ -130,11 +127,6 @@ public class InitThread implements ApplicationRunner {
|
|||||||
|
|
||||||
GlobalMemory.initMqttDeviceMonitor(mqttHostUrl, mqttClientId, mqttUsername, mqttPassword);
|
GlobalMemory.initMqttDeviceMonitor(mqttHostUrl, mqttClientId, mqttUsername, mqttPassword);
|
||||||
|
|
||||||
// 地图监控推送
|
|
||||||
mapMessagePushExecutor.execute(() -> {
|
|
||||||
deviceThreadService.mapMessagePush();
|
|
||||||
});
|
|
||||||
|
|
||||||
// 上位机设备心跳监控
|
// 上位机设备心跳监控
|
||||||
deviceHeartBeatMonitorExecutor.execute(() -> {
|
deviceHeartBeatMonitorExecutor.execute(() -> {
|
||||||
deviceThreadService.heartBeatMonitor();
|
deviceThreadService.heartBeatMonitor();
|
||||||
|
|||||||
@@ -229,9 +229,9 @@ public class DevicePlanTaskMonitorService {
|
|||||||
String deviceId = devicePlanTask.getDeviceId();
|
String deviceId = devicePlanTask.getDeviceId();
|
||||||
if (!StringUtils.isEmpty(deviceId) && deviceId.equals(key)) {
|
if (!StringUtils.isEmpty(deviceId) && deviceId.equals(key)) {
|
||||||
devicePlanTask.setTaskStaus(DeviceTaskStaus.EXECUTING);
|
devicePlanTask.setTaskStaus(DeviceTaskStaus.EXECUTING);
|
||||||
|
siteMemory.addDevicePlanExecute(devicePlanTask);
|
||||||
device.setCurrentTaskId(devicePlanTask.getId());
|
device.setCurrentTaskId(devicePlanTask.getId());
|
||||||
device.setTask(devicePlanTask);
|
device.setTask(devicePlanTask);
|
||||||
siteMemory.addDevicePlanExecute(devicePlanTask);
|
|
||||||
siteMemory.removeDevicePlanPrepare(devicePlanTask);
|
siteMemory.removeDevicePlanPrepare(devicePlanTask);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,10 +14,6 @@ import java.util.concurrent.ScheduledExecutorService;
|
|||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import com.maibu.core.business.dto.DeviceMapPushDTO;
|
|
||||||
import com.maibu.core.business.dto.WebMapMessageDTO;
|
|
||||||
import com.maibu.core.enums.*;
|
|
||||||
import com.maibu.utils.json.JsonUtils;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.checkerframework.checker.units.qual.s;
|
import org.checkerframework.checker.units.qual.s;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -39,6 +35,11 @@ import com.maibu.core.business.ErrorIdentificationStandard;
|
|||||||
import com.maibu.core.business.alarm_center.AlarmMessage;
|
import com.maibu.core.business.alarm_center.AlarmMessage;
|
||||||
import com.maibu.core.business.device.NettyDevice;
|
import com.maibu.core.business.device.NettyDevice;
|
||||||
import com.maibu.core.business.inter.WebsocketMesDispather;
|
import com.maibu.core.business.inter.WebsocketMesDispather;
|
||||||
|
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.RespondCode;
|
||||||
import com.maibu.core.host.HeartBeatDTO;
|
import com.maibu.core.host.HeartBeatDTO;
|
||||||
import com.maibu.core.host.LocationMessage;
|
import com.maibu.core.host.LocationMessage;
|
||||||
import com.maibu.core.redis.RedisCache;
|
import com.maibu.core.redis.RedisCache;
|
||||||
@@ -393,36 +394,4 @@ public class DeviceThreadService {
|
|||||||
}, 0, 500, TimeUnit.MILLISECONDS);
|
}, 0, 500, TimeUnit.MILLISECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 监控心跳
|
|
||||||
*/
|
|
||||||
public void mapMessagePush() {
|
|
||||||
executor.scheduleWithFixedDelay(() -> {
|
|
||||||
try {
|
|
||||||
List<SiteMemory> list = GlobalMemory.getAllSiteMemory();
|
|
||||||
if (!CollectionUtils.isEmpty(list)) {
|
|
||||||
list.forEach(x -> {
|
|
||||||
Map<String, DeviceMapPushDTO> map = x.getAllDeviceMapPush();
|
|
||||||
if (!CollectionUtils.isEmpty(map)) {
|
|
||||||
List<DeviceMapPushDTO> pushList = new ArrayList<>(map.values());
|
|
||||||
WebMapMessageDTO dto = new WebMapMessageDTO();
|
|
||||||
dto.setData(pushList);
|
|
||||||
dto.setType(MesType.mapPush);
|
|
||||||
try {
|
|
||||||
websocketMesDispather.mapMessageDispather(x.getSiteId(), JsonUtils.toJsonString(pushList));
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("实时地图数据websocket推送失败: siteId={}, error={}", x.getSiteId(),
|
|
||||||
e.getMessage(), e);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("执行监测心跳线程错误:{}", e.getMessage());
|
|
||||||
}
|
|
||||||
}, 0, 100, TimeUnit.MILLISECONDS);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import lombok.Data;
|
|||||||
public class WebMessageBaseDTO {
|
public class WebMessageBaseDTO {
|
||||||
private MesType type;
|
private MesType type;
|
||||||
private String userName;
|
private String userName;
|
||||||
private Long siteId;
|
|
||||||
private Long orgId;
|
|
||||||
private String deviceId;
|
private String deviceId;
|
||||||
private String token;
|
private String token;
|
||||||
private String message;
|
private String message;
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@@ -24,8 +25,6 @@ public class MiddleGlobalMemory {
|
|||||||
*/
|
*/
|
||||||
public static Vertx vertx;
|
public static Vertx vertx;
|
||||||
|
|
||||||
//TODO 推送全局的时候需要 判断是不是单个的控制
|
|
||||||
public static ConcurrentHashMap<Long, Map<String, WebSocket>> mapPushMap = new ConcurrentHashMap<>();
|
|
||||||
// 存储所有在线WebSocket连接
|
// 存储所有在线WebSocket连接
|
||||||
// key: test:web:token
|
// key: test:web:token
|
||||||
public static final ConcurrentHashMap<String, WebSocket> onlineSockets = new ConcurrentHashMap<>();
|
public static final ConcurrentHashMap<String, WebSocket> onlineSockets = new ConcurrentHashMap<>();
|
||||||
@@ -85,7 +84,6 @@ public class MiddleGlobalMemory {
|
|||||||
.findFirst().orElse(null);
|
.findFirst().orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String findNettyClient(NettyClient nettyClient) {
|
public static String findNettyClient(NettyClient nettyClient) {
|
||||||
return nettyClientMap.entrySet().stream()
|
return nettyClientMap.entrySet().stream()
|
||||||
.filter(entry -> Objects.equals(entry.getValue(), nettyClient))
|
.filter(entry -> Objects.equals(entry.getValue(), nettyClient))
|
||||||
@@ -93,38 +91,5 @@ public class MiddleGlobalMemory {
|
|||||||
.findFirst().orElse(null);
|
.findFirst().orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void addMapSocket(Long siteId, String token, WebSocket webSocket) {
|
|
||||||
mapPushMap
|
|
||||||
.computeIfAbsent(siteId, k -> new ConcurrentHashMap<>())
|
|
||||||
.put(token, webSocket);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void removeByWebSocket(WebSocket webSocket) {
|
|
||||||
if (webSocket == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
mapPushMap.forEach((siteId, socketMap) -> {
|
|
||||||
socketMap.entrySet().removeIf(entry ->
|
|
||||||
entry.getValue() == webSocket
|
|
||||||
);
|
|
||||||
|
|
||||||
// siteId 下没有连接了,删除整个 siteId
|
|
||||||
if (socketMap.isEmpty()) {
|
|
||||||
mapPushMap.remove(siteId, socketMap);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public static List<WebSocket> getMapSockets(Long siteId) {
|
|
||||||
Map<String, WebSocket> map = mapPushMap.get(siteId);
|
|
||||||
|
|
||||||
if (map == null || map.isEmpty()) {
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ArrayList<>(map.values());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,7 +64,6 @@ public class WebsocketHandler extends WebSocketServer {
|
|||||||
releaseResource(key);
|
releaseResource(key);
|
||||||
logger.info("客户端断开连接: remoteAddress={}, code={}, reason={}, 当前在线数: {}", conn.getRemoteSocketAddress(), code, reason, MiddleGlobalMemory.onlineSockets.size());
|
logger.info("客户端断开连接: remoteAddress={}, code={}, reason={}, 当前在线数: {}", conn.getRemoteSocketAddress(), code, reason, MiddleGlobalMemory.onlineSockets.size());
|
||||||
}
|
}
|
||||||
MiddleGlobalMemory.removeByWebSocket(conn);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -78,15 +77,22 @@ public class WebsocketHandler extends WebSocketServer {
|
|||||||
String deviceId = webMessageBaseDTO.getDeviceId();
|
String deviceId = webMessageBaseDTO.getDeviceId();
|
||||||
MesType type = webMessageBaseDTO.getType();
|
MesType type = webMessageBaseDTO.getType();
|
||||||
String key = userName + ":web" + ":" + token;
|
String key = userName + ":web" + ":" + token;
|
||||||
if (MesType.mapPush.equals(type)) {
|
if (MesType.AUTH.equals(type)) {
|
||||||
Long siteId = webMessageBaseDTO.getSiteId();
|
|
||||||
MiddleGlobalMemory.addMapSocket(siteId, token, conn);
|
|
||||||
} else if (MesType.AUTH.equals(type)) {
|
|
||||||
if (StringUtils.isEmpty(deviceId)) {
|
if (StringUtils.isEmpty(deviceId)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
//todo 先关闭原先的
|
||||||
|
// WebSocket oldWebsocket = MiddleGlobalMemory.onlineSockets.get(key);
|
||||||
NettyClient client = MiddleGlobalMemory.nettyClientMap.get(key);
|
NettyClient client = MiddleGlobalMemory.nettyClientMap.get(key);
|
||||||
|
|
||||||
|
// if (oldWebsocket != null && oldWebsocket.isOpen()) {
|
||||||
|
// oldWebsocket.close();
|
||||||
|
// }
|
||||||
|
// if (oldNettyClient != null && oldNettyClient.isConnected()) {
|
||||||
|
// oldNettyClient.disconnect();
|
||||||
|
// }
|
||||||
MiddleGlobalMemory.onlineSockets.put(key, conn);
|
MiddleGlobalMemory.onlineSockets.put(key, conn);
|
||||||
|
//这里可以根据需要初始化Netty客户端
|
||||||
if (client == null) {
|
if (client == null) {
|
||||||
client = new NettyClient();
|
client = new NettyClient();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,16 @@
|
|||||||
package com.maibu.websocket;
|
package com.maibu.websocket;
|
||||||
|
|
||||||
import com.maibu.core.business.inter.DeviceWebSocketInterface;
|
import com.maibu.core.business.inter.DeviceWebSocketInterace;
|
||||||
import com.maibu.memory.MiddleGlobalMemory;
|
import com.maibu.memory.MiddleGlobalMemory;
|
||||||
|
|
||||||
import org.java_websocket.WebSocket;
|
import org.java_websocket.WebSocket;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.util.CollectionUtils;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class WebsocketMesHandler implements DeviceWebSocketInterface {
|
public class WebsocketMesHandler implements DeviceWebSocketInterace {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendWebsocketMes(String key, String data) {
|
public void sendWebsoketMes(String key, String data) {
|
||||||
MiddleGlobalMemory.onlineSockets.keySet().forEach(x -> {
|
MiddleGlobalMemory.onlineSockets.keySet().forEach(x -> {
|
||||||
// key: test:web:token
|
// key: test:web:token
|
||||||
if (x.contains(key)) {
|
if (x.contains(key)) {
|
||||||
@@ -24,14 +21,4 @@ public class WebsocketMesHandler implements DeviceWebSocketInterface {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void sendMapWebsocketMes(Long siteId, String data) {
|
|
||||||
List<WebSocket> webSocketList = MiddleGlobalMemory.getMapSockets(siteId);
|
|
||||||
if (!CollectionUtils.isEmpty(webSocketList)) {
|
|
||||||
webSocketList.forEach(webSocket -> {
|
|
||||||
WebsocketHandler.sendMessageToClient(webSocket, data);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user