Files
MiddlePlatform/maibu-netty-server/src/main/java/com/maibu/controller/DeviceExternalApiController.java

62 lines
2.3 KiB
Java
Raw Normal View History

2026-04-17 10:42:16 +08:00
package com.fastbee.controller;
import com.fastbee.common.CommandConstant;
import com.fastbee.common.core.controller.BaseController;
import com.fastbee.common.core.domain.AjaxResult;
import com.fastbee.common.core.domain.model.LoginUser;
import com.fastbee.common.utils.SecurityUtils;
import com.fastbee.common.utils.json.JsonUtils;
import com.fastbee.dto.DeviceTaskQueryDTO;
import com.fastbee.dto.NettyDevice;
import com.fastbee.dto.ObstacleData;
import com.fastbee.iot.domain.DevicePlan;
import com.fastbee.manager.DeviceSessionManager;
import com.fastbee.service.DeviceTaskService;
import com.fastbee.utils.CommandUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;
import org.apache.commons.lang3.StringUtils;
import org.java_websocket.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/iot/api")
public class DeviceExternalApiController extends BaseController {
@Autowired
private DeviceSessionManager deviceSessionManager;
@PostMapping("/obstacleData")
public AjaxResult obstacleData(@RequestBody ObstacleData obstacleData) {
Map<String, Object> data = obstacleData.getData();
String deviceId = (String) data.get("deviceId");
if (StringUtils.isEmpty(deviceId)) {
return AjaxResult.error("deviceId为空!");
}
// Map<String, Object> obstacle = (Map<String, Object>) data.get("obstacle");
// Boolean isSecure = (Boolean) data.get("isSecure");
List<NettyDevice> devices = deviceSessionManager.getSlaveControl(deviceId);
if (!CollectionUtils.isEmpty(devices)) {
devices.forEach(x -> {
ByteBuf buf = Unpooled.buffer();
CommandUtils.buildCommand(buf, obstacleData, CommandConstant.interaction);
String content = buf.toString(CharsetUtil.UTF_8);
logger.info("推送设备:{}障碍物消息:{}到:{}", deviceId, content, x.getConnectorId());
if (x.getChannel() != null && x.getChannel().isActive()) {
x.getChannel().writeAndFlush(buf);
}
});
}
return AjaxResult.success();
}
}