This commit is contained in:
Lemon
2023-12-27 08:17:22 +08:00
parent 2174a550f5
commit a596dea3f2
3 changed files with 12 additions and 14 deletions

View File

@@ -33,9 +33,6 @@ public class CameraController extends BaseController {
@Autowired
private CameraBusinessService cameraBusinessService;
@Autowired
private BootNettyMqttChannelInboundHandler handler;
/**
* 心跳
* @param dto
@@ -62,19 +59,16 @@ public class CameraController extends BaseController {
return result.successResponse();
}
/**
* 给某个 Topic 发消息
* @param dto
*/
@PostMapping("/sendMsg2Topic")
public void sendMsg2Topic(@RequestBody SendMsg2TopicDTO dto) {
try {
cameraBusinessService.sendGroundLockCommand(dto.getSn(), dto.getMsgType(), dto.getMsgPrefix(), dto.getTopic(), dto.getMsgData().toJSONString());
cameraBusinessService.sendGroundLockCommand(dto.getSn(), dto.getMsgType(), dto.getMsgPrefix(), dto.getTopic(), dto.getMsgData());
} catch (Exception e) {
logger.error("发送消息 error, ", e);
}
}
// @PostMapping("/sendMsg2Topic")
// public void sendMsg2Topic(@RequestBody SendMsg2TopicDTO dto) throws InterruptedException {
// ChannelFuture channelFuture = handler.sendMsg(dto.getChannelId(), dto.getTopic(), dto.getRequestMsg().toString());
// System.out.println("123");
// }
}

View File

@@ -1,5 +1,6 @@
package com.jsowell.netty.service.camera;
import com.alibaba.fastjson2.JSONObject;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
@@ -21,7 +22,7 @@ public interface CameraBusinessService {
* @param msgData 消息内容
* @throws InterruptedException
*/
public void sendGroundLockCommand(String sn, String msgType, String msgPrefix, String topic, String msgData) throws InterruptedException;
public void sendGroundLockCommand(String sn, String msgType, String msgPrefix, String topic, JSONObject msgData) throws InterruptedException;
/**
* 解析channel中的ip地址 并将 sn 和 channelId 进行绑定,存入缓存

View File

@@ -9,6 +9,7 @@ import com.jsowell.common.util.sign.MD5Util;
import com.jsowell.netty.server.mqtt.BootNettyMqttChannelInboundHandler;
import com.jsowell.netty.service.camera.CameraBusinessService;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -23,6 +24,7 @@ import java.util.Locale;
* @Date 2023/12/19 16:07:45
*/
@Service
@Slf4j
public class CameraBusinessServiceImpl implements CameraBusinessService {
@Autowired
@@ -40,7 +42,7 @@ public class CameraBusinessServiceImpl implements CameraBusinessService {
* @param msgData 消息内容
* @throws InterruptedException
*/
public void sendGroundLockCommand(String sn, String msgType, String msgPrefix, String topic, String msgData) throws InterruptedException {
public void sendGroundLockCommand(String sn, String msgType, String msgPrefix, String topic, JSONObject msgData) throws InterruptedException {
JSONObject jsonObject = spliceStr(sn, msgType, msgPrefix);
// 通过sn查找出对应的channelId
String mqttConnectRedisKey = CacheConstants.MQTT_CONNECT_SN + sn;
@@ -49,10 +51,11 @@ public class CameraBusinessServiceImpl implements CameraBusinessService {
return;
}
String channelId = (String) cacheObject;
if (StringUtils.isNotBlank(msgData)) {
if (msgData != null) {
jsonObject.put("msg_data", msgData);
}
log.info("给相机发送远程命令sn:{}, 消息类型:{}, 主题:{}, 最终发送数据:{}", sn, msgType, topic, jsonObject.toJSONString());
// 发送消息
handler.sendMsg(channelId, topic, jsonObject.toJSONString());
}