mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-21 19:45:09 +08:00
新增 netty整合mqtt协议,与车位相机通讯并保存通讯信息
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package com.jsowell.netty.service.camera;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*
|
||||
* @author Lemon
|
||||
* @Date 2023/12/20 15:15:26
|
||||
*/
|
||||
public interface CameraBusinessService {
|
||||
public void sendGroundLockCommand(String sn, String msgType, String msgPrefix, String topic) throws InterruptedException;
|
||||
|
||||
|
||||
public void processConnectMsg(Channel channel) throws UnknownHostException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.jsowell.netty.service.camera.impl;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.jsowell.common.constant.CacheConstants;
|
||||
import com.jsowell.common.core.redis.RedisCache;
|
||||
import com.jsowell.common.util.DateUtils;
|
||||
import com.jsowell.common.util.sign.MD5Util;
|
||||
import com.jsowell.netty.domain.MqttRequest;
|
||||
import com.jsowell.netty.server.mqtt.BootNettyMqttChannelInboundHandler;
|
||||
import com.jsowell.netty.service.camera.CameraBusinessService;
|
||||
import io.netty.buffer.Unpooled;
|
||||
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.stereotype.Service;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* 车位相机业务Service
|
||||
*
|
||||
* @author Lemon
|
||||
* @Date 2023/12/19 16:07:45
|
||||
*/
|
||||
@Service
|
||||
public class CameraBusinessServiceImpl implements CameraBusinessService {
|
||||
|
||||
@Autowired
|
||||
private RedisCache redisCache;
|
||||
|
||||
@Autowired
|
||||
private BootNettyMqttChannelInboundHandler handler;
|
||||
|
||||
public void sendGroundLockCommand(String sn, String msgType, String msgPrefix, String topic) throws InterruptedException {
|
||||
JSONObject jsonObject = spliceStr(sn, msgType, msgPrefix);
|
||||
// 通过sn查找出对应的channelId
|
||||
String mqttConnectRedisKey = CacheConstants.MQTT_CONNECT_CHANNEL + sn;
|
||||
Object cacheObject = redisCache.getCacheObject(mqttConnectRedisKey);
|
||||
if (cacheObject == null) {
|
||||
return;
|
||||
}
|
||||
String channelId = (String) cacheObject;
|
||||
|
||||
// 发送消息
|
||||
handler.sendMsg(channelId, topic, jsonObject.toJSONString());
|
||||
}
|
||||
|
||||
public void processConnectMsg(Channel channel) throws UnknownHostException {
|
||||
// 解析 channel 中的 ip 地址
|
||||
String remoteAddress = channel.remoteAddress().toString();
|
||||
// System.out.println(ip);
|
||||
String[] parts = remoteAddress.substring(1).split(":");
|
||||
String ipAddress = parts[0]; // IP 地址部分
|
||||
String port = parts[1]; // 端口号部分
|
||||
// 使用 InetAddress 解析IP地址部分
|
||||
InetAddress inetAddress = InetAddress.getByName(ipAddress);
|
||||
|
||||
// 输出标准的IP地址格式和端口号
|
||||
String standardIPAddress = inetAddress.getHostAddress();
|
||||
// System.out.println("端口号:" + port);
|
||||
|
||||
// 获取相机心跳包的缓存信息,将 sn 和 channelId 进行绑定
|
||||
String redisKey = CacheConstants.CAMERA_HEARTBEAT + standardIPAddress;
|
||||
Object cacheObject = redisCache.getCacheObject(redisKey);
|
||||
if (cacheObject != null) {
|
||||
String sn = (String) cacheObject;
|
||||
// 将 sn 和 channelId 绑定关系存入缓存
|
||||
String mqttConnectRedisKey = CacheConstants.MQTT_CONNECT_CHANNEL + sn;
|
||||
redisCache.setCacheObject(mqttConnectRedisKey, channel.id().asShortText());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据规则拼装字符串
|
||||
* @param sn 设备 sn
|
||||
* @param msgType 消息类型
|
||||
* @param msgPrefix 消息前缀
|
||||
* @return 拼装好的json对象
|
||||
*/
|
||||
private JSONObject spliceStr(String sn, String msgType, String msgPrefix) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String msgId = msgPrefix + DateUtils.dateTimeNow(DateUtils.YYYYMMDDHHMMSS) + "01";
|
||||
String timeStamp = DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD_HH_MM_SS);
|
||||
sb.append("sn=").append(sn)
|
||||
.append("×tamp=").append(timeStamp)
|
||||
.append("&msg_id=").append(msgId)
|
||||
.append("&msg_type=").append(msgType);
|
||||
// 进行 32 位 MD5 计算
|
||||
String sign = MD5Util.MD5Encode(sb.toString()).toUpperCase(Locale.ROOT);
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("sign", sign);
|
||||
jsonObject.put("sn", sn);
|
||||
jsonObject.put("timestamp", timeStamp);
|
||||
jsonObject.put("msg_id", msgId);
|
||||
jsonObject.put("msg_type", msgType);
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user