netty-确定互联完成

This commit is contained in:
2023-08-22 22:26:56 +08:00
parent 0d275239a8
commit 9b614251ca
8 changed files with 30 additions and 54 deletions

View File

@@ -18,7 +18,6 @@ import io.netty.util.internal.PlatformDependent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.text.MessageFormat;
@@ -46,27 +45,24 @@ public class AppMessageHandlerAdapter implements MessageService {
*/
private ConcurrentMap<String, Channel> links = PlatformDependent.newConcurrentHashMap();
// @Resource
@Resource
private MemberService memberService;
@Resource
private VenueService venueService;
// @Resource
@Resource
private DeviceService deviceService;
// @Resource
@Resource
private ConfigService configService;
// @Resource(name = "scheduledExecutorService")
@Resource(name = "scheduledExecutorService")
private ScheduledExecutorService scheduledExecutorService;
// @Resource
@Resource
private MemberEnterVenueLogService memberEnterVenueLogService;
// @Resource
private RestTemplate restTemplate;
/**
* 处理心跳信息,存储心跳信息
* @param clientId
@@ -84,9 +80,9 @@ public class AppMessageHandlerAdapter implements MessageService {
logger.error("this client choose venue Error! venueId == " + heartBeat.getVenueId());
} else {
deviceService.online(heartBeat.getDeviceName(),heartBeat.getVenueId(),thisVenue.getType(),clientId);
putChannelType(heartBeat.getDeviceName(),heartBeat.getVenueId(),channel);
VenueMessage VenueMessage = new VenueMessage(MessageType.LINK,"欢迎扫码进场!");
channel.writeAndFlush(VenueMessage);
addLinks(heartBeat.getDeviceName(),heartBeat.getVenueId(),channel);
VenueMessage venueMessage = new VenueMessage(MessageType.LINK,"欢迎扫码进场!");
ServerMessageUtils.INSTANCE.sendMsg(channel,venueMessage);
}
}
}
@@ -129,7 +125,7 @@ public class AppMessageHandlerAdapter implements MessageService {
@Override
public void testLoad(String deviceName, Integer venueId) {
Channel currentChannel = getCurrentChannel(deviceName, venueId);
currentChannel.writeAndFlush("Test Links" + deviceName + venueId);
ServerMessageUtils.INSTANCE.sendMsg(currentChannel,new VenueMessage(MessageType.LINK,"测试链接"));
}
@@ -160,7 +156,7 @@ public class AppMessageHandlerAdapter implements MessageService {
// VenueMessage.setCmdId(Cmd.OPEN_DOOR.id);
// VenueMessage.setDoor(2);
// sendOpenMessage(VenueMessage, device.getId());
// 校验就可以出场了
// 校验就可以出场了
// sendMessage(memberMessageDto, device.getId());
// venueService.addNumber(venue.getId(), -1, member.getId());
// venue = venueService.findById(venue.getId());
@@ -245,7 +241,7 @@ public class AppMessageHandlerAdapter implements MessageService {
/**
* 缓存通道
*/
public void putChannelType(String deviceName, Integer venueId, Channel channel) {
public void addLinks(String deviceName, Integer venueId, Channel channel) {
String clientId = deviceName + NettyConstant.SPIT_WORD + venueId;
links.put(clientId, channel);
}
@@ -254,7 +250,7 @@ public class AppMessageHandlerAdapter implements MessageService {
* 获取当前通道
*/
public Channel getCurrentChannel(String deviceName, Integer venueId){
String clientId = deviceName + NettyConstant.SPIT_WORD + venueId + NettyConstant.SPIT_WORD;
String clientId = deviceName + NettyConstant.SPIT_WORD + venueId;
return links.get(clientId);
}

View File

@@ -82,7 +82,7 @@ public class ClientHandler extends SimpleChannelInboundHandler<String> {
private HeartBeat getHbMessage() {
HeartBeat hb = new HeartBeat();
hb.setDeviceName("dsadasdasd");
hb.setVenueId(123);
hb.setVenueId(32);
return hb;
}

View File

@@ -12,7 +12,7 @@ import io.netty.handler.timeout.IdleStateHandler;
public class ClientInitializer extends ChannelInitializer<SocketChannel> {
private final static int TIME_HEART_BEAT = 5;
private final static int TIME_HEART_BEAT = 60;
public ClientThread.ReConnectHandler reConnectHandler;
public ClientHandler dmClientHandler;

View File

@@ -23,6 +23,10 @@
<artifactId>slf4j-api</artifactId>
<version>1.7.33</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>

View File

@@ -9,8 +9,8 @@ public interface NettyConstant {
/**
* session中存储终端发送的额外参数
*/
public static AttributeKey<ChannelParam> CHANNEL_PARAM = AttributeKey.newInstance("CHANNEL_PARAM");
AttributeKey<ChannelParam> CHANNEL_PARAM = AttributeKey.newInstance("CHANNEL_PARAM");
public final static String SPIT_WORD = "#";
String SPIT_WORD = "#";
}

View File

@@ -7,9 +7,12 @@ public class VenueMessage implements Serializable {
private MessageType messageType;
private String message;
public VenueMessage(MessageType type, String msg) {
this.messageType = type;
this.message = msg;
public VenueMessage() {
}
public VenueMessage(MessageType messageType, String message) {
this.messageType = messageType;
this.message = message;
}
public MessageType getMessageType() {

View File

@@ -8,6 +8,8 @@ public enum ServerMessageUtils {
INSTANCE;
public void sendMsg(Channel channel, VenueMessage message) {
channel.writeAndFlush(EncodeMsg.INSTANCE.encode(message));
if (channel != null) {
channel.writeAndFlush(EncodeMsg.INSTANCE.encode(message));
}
}
}

View File

@@ -8,7 +8,6 @@
<result column="stream" property="stream"/>
<result column="venue_id" property="venueId"/>
<result column="venue_type" property="venueType"/>
<result column="device_type" property="deviceType"/>
<result column="bind_member" property="bindMember"/>
<result column="bind_time" property="bindTime"/>
<result column="status" property="status"/>
@@ -53,7 +52,6 @@
#{stream, jdbcType=VARCHAR},
#{venue_id, jdbcType=INTEGER},
#{venue_type, jdbcType=INTEGER},
#{device_type, jdbcType=VARCHAR},
#{bind_member, jdbcType=INTEGER},
#{bind_time, jdbcType=TIMESTAMP},
#{createdId, jdbcType=INTEGER},
@@ -94,9 +92,6 @@
<if test="venueType != null">
venue_type,
</if>
<if test="deviceType != null">
device_type,
</if>
<if test="bindMember != null">
bind_member,
</if>
@@ -138,9 +133,6 @@
<if test="venueType != null">
#{venueType},
</if>
<if test="deviceType != null">
#{deviceType},
</if>
<if test="bindMember != null">
#{bindMember},
</if>
@@ -211,9 +203,6 @@
<if test="venueType != null">
venue_type = #{venueType},
</if>
<if test="deviceType != null">
device_type = #{deviceType},
</if>
<if test="bindMember != null">
bind_member = #{bindMember},
</if>
@@ -271,49 +260,31 @@
<update id="offline">
UPDATE sv_device set status = 0,modified_time = now()
WHERE name = #{deviceName} and venue_id=#{venueId}
<if test="deviceType != null">
and device_type = #{deviceType}
</if>
</update>
<update id="online">
UPDATE sv_device set status = 2,modified_time = now()
WHERE name = #{deviceName} and venue_id=#{venueId}
<if test="deviceType != null">
and device_type = #{deviceType}
</if>
</update>
<select id="checkDevice" resultType="java.lang.Integer">
SELECT count(1) FROM sv_device
WHERE 1=1 AND name = #{deviceName} and venue_id=#{venueId} and deleted = 0
<if test="deviceType != null">
and device_type = #{deviceType}
</if>
</select>
<select id="findByDevice" resultType="com.sv.entity.Device">
SELECT
<include refid="Field"></include> FROM sv_device
WHERE 1=1 AND name = #{deviceName} and venue_id=#{venueId} and deleted = 0
<if test="deviceType != null">
and device_type = #{deviceType}
</if>
</select>
<update id="bindMember">
UPDATE sv_device set bind_member = #{bindMember},bind_time = now()
WHERE name = #{deviceName} and venue_id=#{venueId}
<if test="deviceType != null">
and device_type = #{deviceType}
</if>
</update>
<update id="unBindMember">
UPDATE sv_device set bind_member = null,bind_time = null
WHERE name = #{deviceName} and venue_id=#{venueId}
<if test="deviceType != null">
and device_type = #{deviceType}
</if>
</update>
</mapper>