# update 权限相关
This commit is contained in:
@@ -66,11 +66,7 @@ public class TransferDeviceController extends BaseController {
|
||||
if (StringUtils.isEmpty(bindDTO.getPlatform())) {
|
||||
return AjaxResult.error("平台为空!");
|
||||
}
|
||||
LoginUser user = getLoginUser();
|
||||
if (user == null) {
|
||||
return AjaxResult.error("当前token用户不存在!");
|
||||
}
|
||||
return AjaxResult.success(transferDeviceService.switchDevice(bindDTO, user));
|
||||
return AjaxResult.success(transferDeviceService.switchDevice(bindDTO));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,8 +83,7 @@ public class TransferDeviceController extends BaseController {
|
||||
if (StringUtils.isEmpty(bindDTO.getPlatform())) {
|
||||
return AjaxResult.error("平台为空!");
|
||||
}
|
||||
LoginUser user = getLoginUser();
|
||||
return AjaxResult.success(transferDeviceService.remoteControl(bindDTO, user));
|
||||
return AjaxResult.success(transferDeviceService.remoteControl(bindDTO));
|
||||
} catch (Exception e) {
|
||||
logger.error("获取设备控制权出错", e);
|
||||
return AjaxResult.error();
|
||||
|
||||
@@ -192,6 +192,8 @@ public class DeviceConnectHandler extends SimpleChannelInboundHandler<String> {
|
||||
if (masterDevice == null) {
|
||||
// sessionManager.getControl(sessionId);
|
||||
handleMasterConnect(ctx, sessionId, null, ConnectorStatus.ACTIVE);
|
||||
} else {
|
||||
ctx.channel().attr(Constant.ATT_DEVICE_ID).set(sessionId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -250,7 +252,7 @@ public class DeviceConnectHandler extends SimpleChannelInboundHandler<String> {
|
||||
changeDTO.setTaskId(taskId);
|
||||
|
||||
GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId, String.format(MqttTopic.DEVICE_TASK_STATUS_TOPIC, taskId),
|
||||
JsonUtils.toJsonString(changeDTO));
|
||||
JsonUtils.toJsonString(changeDTO));
|
||||
List<NettyDevice> controlMasters = sessionManager
|
||||
.getAllSlaveControl(deviceId);
|
||||
if (!CollectionUtils.isEmpty(controlMasters)) {
|
||||
@@ -273,7 +275,7 @@ public class DeviceConnectHandler extends SimpleChannelInboundHandler<String> {
|
||||
reportDTO.setEntity(device.getCurrentPoint());
|
||||
reportDTO.setType(MesType.device_task_arrive_point);
|
||||
GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId, String.format(MqttTopic.DEVICE_TASK_ARRIVE_TOPIC, taskId),
|
||||
JsonUtils.toJsonString(reportDTO));
|
||||
JsonUtils.toJsonString(reportDTO));
|
||||
|
||||
List<NettyDevice> controlMasters = sessionManager
|
||||
.getAllSlaveControl(deviceId);
|
||||
@@ -422,11 +424,7 @@ public class DeviceConnectHandler extends SimpleChannelInboundHandler<String> {
|
||||
private void handlePermissionRequest(DeviceRequestDTO requestDTO, byte[] bytes, ChannelHandlerContext ctx) {
|
||||
logger.info("转发控制请求 :{}", JsonUtils.toJsonString(requestDTO));
|
||||
String slaveId = requestDTO.getDeviceId();
|
||||
List<NettyDevice> controlList = sessionManager.getSlaveControl(slaveId);
|
||||
String token = requestDTO.getToken();
|
||||
// String masterId = requestDTO.getUserId() + ":" + requestDTO.getPlatform();
|
||||
logger.info("转发控制请求 controlList :{}", JsonUtils.toJsonString(controlList));
|
||||
|
||||
String master = sessionManager.getMaster(slaveId);
|
||||
if (!StringUtils.isEmpty(master)) {
|
||||
// 如果当前设备有控制
|
||||
|
||||
@@ -31,6 +31,8 @@ public class HeartbeatHandler extends ChannelInboundHandlerAdapter {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(HeartbeatHandler.class);
|
||||
|
||||
private static final long DEVICE_REGISTER_TIMEOUT_MS = 10_000; // 10秒注册超时
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
// 连接建立后启动定时任务,主动发心跳
|
||||
@@ -39,6 +41,7 @@ public class HeartbeatHandler extends ChannelInboundHandlerAdapter {
|
||||
}
|
||||
|
||||
private void scheduleHeartbeat(ChannelHandlerContext ctx) {
|
||||
final long startTime = System.currentTimeMillis();
|
||||
ctx.executor().scheduleAtFixedRate(() -> {
|
||||
try {
|
||||
if (ctx.channel().isActive()) {
|
||||
@@ -47,6 +50,12 @@ public class HeartbeatHandler extends ChannelInboundHandlerAdapter {
|
||||
ByteBuf heartbeat = Unpooled.wrappedBuffer(HEARTBEAT_PACKET);
|
||||
ctx.writeAndFlush(heartbeat);
|
||||
logger.debug("发送心跳包给客户端:{},deviceId:{}", ctx.channel().remoteAddress(), deviceId);
|
||||
} else {
|
||||
long elapsed = System.currentTimeMillis() - startTime;
|
||||
if (elapsed > DEVICE_REGISTER_TIMEOUT_MS) {
|
||||
logger.warn("设备超过{}ms未注册,断开连接:{}", DEVICE_REGISTER_TIMEOUT_MS, ctx.channel().remoteAddress());
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -55,6 +64,24 @@ public class HeartbeatHandler extends ChannelInboundHandlerAdapter {
|
||||
}, 0, HEARTBEAT_INTERVAL, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
|
||||
// private void scheduleHeartbeat(ChannelHandlerContext ctx) {
|
||||
// ctx.executor().scheduleAtFixedRate(() -> {
|
||||
// try {
|
||||
// if (ctx.channel().isActive()) {
|
||||
// String deviceId = ctx.channel().attr(Constant.ATT_DEVICE_ID).get();
|
||||
// if (!StringUtils.isEmpty(deviceId)) {
|
||||
// ByteBuf heartbeat = Unpooled.wrappedBuffer(HEARTBEAT_PACKET);
|
||||
// ctx.writeAndFlush(heartbeat);
|
||||
// logger.debug("发送心跳包给客户端:{},deviceId:{}", ctx.channel().remoteAddress(), deviceId);
|
||||
// }
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("heartbeat error", e);
|
||||
// }
|
||||
// }, 0, HEARTBEAT_INTERVAL, TimeUnit.SECONDS);
|
||||
// }
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
if (msg instanceof ByteBuf) {
|
||||
|
||||
@@ -19,6 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -44,13 +45,12 @@ public class DevicePlanTaskMonitorService {
|
||||
@Autowired
|
||||
private WorkRecordMapper workRecordMapper;
|
||||
|
||||
|
||||
@Autowired
|
||||
private ScheduledExecutorService executor;
|
||||
/**
|
||||
* 计划迟的任务生成机器执行任务
|
||||
*/
|
||||
public void generatePlanTask() throws InterruptedException {
|
||||
|
||||
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
||||
executor.scheduleWithFixedDelay(() -> {
|
||||
try {
|
||||
Map<String, List<DevicePlan>> planMap = SiteMemory.devicePlanMap;
|
||||
@@ -204,7 +204,6 @@ public class DevicePlanTaskMonitorService {
|
||||
|
||||
public void doLoop() throws InterruptedException {
|
||||
//prepare 进执行execute的逻辑
|
||||
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
||||
executor.scheduleWithFixedDelay(() -> {
|
||||
try {
|
||||
List<SiteMemory> list = GlobalMemory.getAllSiteMemory();
|
||||
@@ -250,7 +249,6 @@ public class DevicePlanTaskMonitorService {
|
||||
|
||||
public void doExecute() {
|
||||
//prepare 进执行execute的逻辑
|
||||
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
||||
executor.scheduleWithFixedDelay(() -> {
|
||||
try {
|
||||
List<SiteMemory> list = GlobalMemory.getAllSiteMemory();
|
||||
@@ -270,5 +268,4 @@ public class DevicePlanTaskMonitorService {
|
||||
}
|
||||
}, 0, 1000, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,61 +1,48 @@
|
||||
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;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.maibu.constant.CommandConstant;
|
||||
import com.maibu.constant.Constant;
|
||||
import com.maibu.core.business.Device;
|
||||
import com.maibu.core.enums.*;
|
||||
import com.maibu.core.host.HeartBeatDTO;
|
||||
import com.maibu.dto.DeviceStatusChangeDTO;
|
||||
import com.maibu.memory.DeviceSessionManager;
|
||||
import com.maibu.utils.CommandUtils;
|
||||
import com.maibu.utils.json.JsonUtils;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.Constants;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.maibu.common.NettyCacheKey;
|
||||
import com.maibu.constant.Constant;
|
||||
import com.maibu.core.business.Device;
|
||||
import com.maibu.core.business.DeviceRunningStatusHistory;
|
||||
import com.maibu.core.business.DeviceStatusRecordDTO;
|
||||
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.enums.*;
|
||||
import com.maibu.core.host.HeartBeatDTO;
|
||||
import com.maibu.core.redis.RedisCache;
|
||||
import com.maibu.dto.DeviceErrorPushDTO;
|
||||
import com.maibu.mapper.ErrorIdentificationStandardMapper;
|
||||
import com.maibu.memory.DeviceSessionManager;
|
||||
import com.maibu.memory.GlobalMemory;
|
||||
import com.maibu.memory.SiteMemory;
|
||||
import com.maibu.mqtt.MqttTopic;
|
||||
import com.maibu.utils.CompareUtils;
|
||||
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@@ -85,13 +72,14 @@ public class DeviceThreadService {
|
||||
@Value("${mqtt.client-id}")
|
||||
private String mqttClientId;
|
||||
|
||||
@Autowired
|
||||
private ScheduledExecutorService executor;
|
||||
|
||||
public void deviceErrorMonitor() throws InterruptedException, IOException {
|
||||
initStandard();
|
||||
|
||||
// TODO fix 后续是否分多个线程 一个site一个线程
|
||||
// 有还需要更新接受故障方式。
|
||||
|
||||
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
||||
executor.scheduleWithFixedDelay(() -> {
|
||||
try {
|
||||
String key = NettyCacheKey.deviceRunningStatusKey;
|
||||
@@ -270,8 +258,6 @@ public class DeviceThreadService {
|
||||
* 监控心跳
|
||||
*/
|
||||
public void heartBeatMonitor() {
|
||||
|
||||
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
||||
executor.scheduleWithFixedDelay(() -> {
|
||||
try {
|
||||
Long nowTime = System.currentTimeMillis();
|
||||
|
||||
@@ -220,7 +220,7 @@ public class TransferDeviceService {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
public synchronized boolean switchDevice(DeviceBindDTO bindDTO, LoginUser user) {
|
||||
public synchronized boolean switchDevice(DeviceBindDTO bindDTO) {
|
||||
try {
|
||||
String slaveId = bindDTO.getDeviceId();
|
||||
// String platform = bindDTO.getPlatform();
|
||||
@@ -270,9 +270,9 @@ public class TransferDeviceService {
|
||||
}
|
||||
|
||||
|
||||
public DeviceControlDTO remoteControl(DeviceBindDTO bindDTO, LoginUser user) {
|
||||
public DeviceControlDTO remoteControl(DeviceBindDTO bindDTO) {
|
||||
try {
|
||||
String token = user.getToken();
|
||||
String token = bindDTO.getToken();
|
||||
String masterToken = sessionManager.getMaster(bindDTO.getDeviceId());
|
||||
DeviceControlDTO dto = new DeviceControlDTO();
|
||||
if (!StringUtils.isEmpty(token) && token.equals(masterToken)) {
|
||||
|
||||
Reference in New Issue
Block a user