mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-06-16 13:19:57 +08:00
update mqtt 服务器
This commit is contained in:
@@ -65,7 +65,7 @@ public class CameraController extends BaseController {
|
|||||||
@PostMapping("/sendMsg2Topic")
|
@PostMapping("/sendMsg2Topic")
|
||||||
public void sendMsg2Topic(@RequestBody SendMsg2TopicDTO dto) {
|
public void sendMsg2Topic(@RequestBody SendMsg2TopicDTO dto) {
|
||||||
try {
|
try {
|
||||||
cameraBusinessService.sendGroundLockCommand(dto.getSn(), dto.getMsgType(), dto.getMsgPrefix(), dto.getTopic());
|
cameraBusinessService.sendGroundLockCommand(dto.getSn(), dto.getMsgType(), dto.getMsgPrefix(), dto.getTopic(), dto.getMsgData().toJSONString());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("发送消息 error, ", e);
|
logger.error("发送消息 error, ", e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -202,12 +202,12 @@ public class CacheConstants {
|
|||||||
/**
|
/**
|
||||||
* 相机心跳数据
|
* 相机心跳数据
|
||||||
*/
|
*/
|
||||||
public static final String CAMERA_HEARTBEAT = "CAMERA_HEARTBEAT_";
|
public static final String CAMERA_HEARTBEAT = "CAMERA_HEARTBEAT_IP:";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 相机设备 sn 和 channelId 进行绑定
|
* 相机设备 sn 和 channelId 进行绑定
|
||||||
*/
|
*/
|
||||||
public static final String MQTT_CONNECT_CHANNEL = "MQTT_CONNECT_CHANNEL_";
|
public static final String MQTT_CONNECT_SN = "MQTT_CONNECT_SN:";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 桩硬件故障
|
* 桩硬件故障
|
||||||
|
|||||||
@@ -64,41 +64,9 @@ public class NettyServer implements CommandLineRunner {
|
|||||||
} else {
|
} else {
|
||||||
log.error("NettyServer启动失败", future.cause());
|
log.error("NettyServer启动失败", future.cause());
|
||||||
}
|
}
|
||||||
//
|
|
||||||
// ServerBootstrap mqttBootstrap = new ServerBootstrap();
|
|
||||||
// mqttBootstrap.group(bossGroup, workerGroup);
|
|
||||||
// mqttBootstrap.channel(NioServerSocketChannel.class);
|
|
||||||
//
|
|
||||||
// mqttBootstrap.option(ChannelOption.SO_REUSEADDR, true)
|
|
||||||
// .option(ChannelOption.SO_BACKLOG, 1024)
|
|
||||||
// .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
|
|
||||||
// .option(ChannelOption.SO_RCVBUF, 10485760);
|
|
||||||
//
|
|
||||||
// mqttBootstrap.childOption(ChannelOption.TCP_NODELAY, true)
|
|
||||||
// .childOption(ChannelOption.SO_KEEPALIVE, true)
|
|
||||||
// .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
|
|
||||||
//
|
|
||||||
// mqttBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
|
|
||||||
// protected void initChannel(SocketChannel ch) {
|
|
||||||
// ChannelPipeline channelPipeline = ch.pipeline();
|
|
||||||
// // 设置读写空闲超时时间
|
|
||||||
// channelPipeline.addLast(new IdleStateHandler(600, 600, 1200));
|
|
||||||
// channelPipeline.addLast("encoder", MqttEncoder.INSTANCE);
|
|
||||||
// channelPipeline.addLast("decoder", new MqttDecoder());
|
|
||||||
// channelPipeline.addLast(new BootNettyMqttChannelInboundHandler());
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// ChannelFuture future2 = mqttBootstrap.bind(address2.getPort()).sync();
|
|
||||||
// if(future2.isSuccess()){
|
|
||||||
// log.info("MqttServer启动成功, 开始监听端口:{}", address2.getPort());
|
|
||||||
// future2.channel().closeFuture().sync();
|
|
||||||
// } else {
|
|
||||||
// log.error("MqttServer启动失败", future2.cause());
|
|
||||||
// }
|
|
||||||
|
|
||||||
//关闭channel和块,直到它被关闭
|
// 关闭channel和块,直到它被关闭 (若取消注释,则 MQTT 服务器无法启动)
|
||||||
future.channel().closeFuture().sync();
|
// future.channel().closeFuture().sync();
|
||||||
// future2.channel().closeFuture().sync();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("NettyServer.start error", e);
|
log.error("NettyServer.start error", e);
|
||||||
bossGroup.shutdownGracefully();
|
bossGroup.shutdownGracefully();
|
||||||
|
|||||||
@@ -12,9 +12,22 @@ import java.net.UnknownHostException;
|
|||||||
* @Date 2023/12/20 15:15:26
|
* @Date 2023/12/20 15:15:26
|
||||||
*/
|
*/
|
||||||
public interface CameraBusinessService {
|
public interface CameraBusinessService {
|
||||||
public void sendGroundLockCommand(String sn, String msgType, String msgPrefix, String topic) throws InterruptedException;
|
/**
|
||||||
|
* 发送具体指令到某主题
|
||||||
|
* @param sn 设备 sn
|
||||||
|
* @param msgType 消息类型
|
||||||
|
* @param msgPrefix 消息前缀
|
||||||
|
* @param topic 主题
|
||||||
|
* @param msgData 消息内容
|
||||||
|
* @throws InterruptedException
|
||||||
|
*/
|
||||||
|
public void sendGroundLockCommand(String sn, String msgType, String msgPrefix, String topic, String msgData) throws InterruptedException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析channel中的ip地址, 并将 sn 和 channelId 进行绑定,存入缓存
|
||||||
|
* @param channel
|
||||||
|
* @throws UnknownHostException
|
||||||
|
*/
|
||||||
public void processConnectMsg(Channel channel) throws UnknownHostException;
|
public void processConnectMsg(Channel channel) throws UnknownHostException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,14 +4,11 @@ import com.alibaba.fastjson2.JSONObject;
|
|||||||
import com.jsowell.common.constant.CacheConstants;
|
import com.jsowell.common.constant.CacheConstants;
|
||||||
import com.jsowell.common.core.redis.RedisCache;
|
import com.jsowell.common.core.redis.RedisCache;
|
||||||
import com.jsowell.common.util.DateUtils;
|
import com.jsowell.common.util.DateUtils;
|
||||||
|
import com.jsowell.common.util.StringUtils;
|
||||||
import com.jsowell.common.util.sign.MD5Util;
|
import com.jsowell.common.util.sign.MD5Util;
|
||||||
import com.jsowell.netty.domain.MqttRequest;
|
|
||||||
import com.jsowell.netty.server.mqtt.BootNettyMqttChannelInboundHandler;
|
import com.jsowell.netty.server.mqtt.BootNettyMqttChannelInboundHandler;
|
||||||
import com.jsowell.netty.service.camera.CameraBusinessService;
|
import com.jsowell.netty.service.camera.CameraBusinessService;
|
||||||
import io.netty.buffer.Unpooled;
|
|
||||||
import io.netty.channel.Channel;
|
import io.netty.channel.Channel;
|
||||||
import io.netty.channel.ChannelFuture;
|
|
||||||
import io.netty.handler.codec.mqtt.*;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -34,20 +31,37 @@ public class CameraBusinessServiceImpl implements CameraBusinessService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private BootNettyMqttChannelInboundHandler handler;
|
private BootNettyMqttChannelInboundHandler handler;
|
||||||
|
|
||||||
public void sendGroundLockCommand(String sn, String msgType, String msgPrefix, String topic) throws InterruptedException {
|
/**
|
||||||
|
* 发送具体指令到某主题
|
||||||
|
* @param sn 设备 sn
|
||||||
|
* @param msgType 消息类型
|
||||||
|
* @param msgPrefix 消息前缀
|
||||||
|
* @param topic 主题
|
||||||
|
* @param msgData 消息内容
|
||||||
|
* @throws InterruptedException
|
||||||
|
*/
|
||||||
|
public void sendGroundLockCommand(String sn, String msgType, String msgPrefix, String topic, String msgData) throws InterruptedException {
|
||||||
JSONObject jsonObject = spliceStr(sn, msgType, msgPrefix);
|
JSONObject jsonObject = spliceStr(sn, msgType, msgPrefix);
|
||||||
// 通过sn查找出对应的channelId
|
// 通过sn查找出对应的channelId
|
||||||
String mqttConnectRedisKey = CacheConstants.MQTT_CONNECT_CHANNEL + sn;
|
String mqttConnectRedisKey = CacheConstants.MQTT_CONNECT_SN + sn;
|
||||||
Object cacheObject = redisCache.getCacheObject(mqttConnectRedisKey);
|
Object cacheObject = redisCache.getCacheObject(mqttConnectRedisKey);
|
||||||
if (cacheObject == null) {
|
if (cacheObject == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String channelId = (String) cacheObject;
|
String channelId = (String) cacheObject;
|
||||||
|
if (StringUtils.isNotBlank(msgData)) {
|
||||||
|
jsonObject.put("msg_data", msgData);
|
||||||
|
}
|
||||||
|
|
||||||
// 发送消息
|
// 发送消息
|
||||||
handler.sendMsg(channelId, topic, jsonObject.toJSONString());
|
handler.sendMsg(channelId, topic, jsonObject.toJSONString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析channel中的ip地址, 并将 sn 和 channelId 进行绑定,存入缓存
|
||||||
|
* @param channel
|
||||||
|
* @throws UnknownHostException
|
||||||
|
*/
|
||||||
public void processConnectMsg(Channel channel) throws UnknownHostException {
|
public void processConnectMsg(Channel channel) throws UnknownHostException {
|
||||||
// 解析 channel 中的 ip 地址
|
// 解析 channel 中的 ip 地址
|
||||||
String remoteAddress = channel.remoteAddress().toString();
|
String remoteAddress = channel.remoteAddress().toString();
|
||||||
@@ -68,7 +82,7 @@ public class CameraBusinessServiceImpl implements CameraBusinessService {
|
|||||||
if (cacheObject != null) {
|
if (cacheObject != null) {
|
||||||
String sn = (String) cacheObject;
|
String sn = (String) cacheObject;
|
||||||
// 将 sn 和 channelId 绑定关系存入缓存
|
// 将 sn 和 channelId 绑定关系存入缓存
|
||||||
String mqttConnectRedisKey = CacheConstants.MQTT_CONNECT_CHANNEL + sn;
|
String mqttConnectRedisKey = CacheConstants.MQTT_CONNECT_SN + sn;
|
||||||
redisCache.setCacheObject(mqttConnectRedisKey, channel.id().asShortText());
|
redisCache.setCacheObject(mqttConnectRedisKey, channel.id().asShortText());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.jsowell.pile.dto.camera;
|
package com.jsowell.pile.dto.camera;
|
||||||
|
|
||||||
import com.alibaba.fastjson2.JSON;
|
import com.alibaba.fastjson2.JSON;
|
||||||
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,6 +19,7 @@ public class SendMsg2TopicDTO {
|
|||||||
private String sn;
|
private String sn;
|
||||||
private String msgType;
|
private String msgType;
|
||||||
private String msgPrefix;
|
private String msgPrefix;
|
||||||
|
private JSONObject msgData;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public static class RequestMsg{
|
public static class RequestMsg{
|
||||||
|
|||||||
Reference in New Issue
Block a user