netty-确定客户端和微信端的沟通方式
This commit is contained in:
@@ -27,6 +27,10 @@
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>6.0.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.sv.api.interceptor;
|
||||
|
||||
import com.sv.api.context.PlatformContext;
|
||||
import com.sv.netty.config.Constant;
|
||||
import com.ydd.framework.core.common.utils.ReflectUtil;
|
||||
import com.ydd.framework.core.common.utils.RequestUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
@@ -30,6 +29,11 @@ public class PlatformIdInterceptor implements Interceptor {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(PlatformIdInterceptor.class);
|
||||
|
||||
/**
|
||||
* 哪些接口不需要增加 platform 平台限制
|
||||
*/
|
||||
public static final List<String> executeControllerList = Arrays.asList(new String [] {"/qrCode/init","/qrCode/enter"});
|
||||
|
||||
/**
|
||||
* 搜索前缀
|
||||
*/
|
||||
@@ -97,7 +101,7 @@ public class PlatformIdInterceptor implements Interceptor {
|
||||
}
|
||||
|
||||
private boolean checkUrl(HttpServletRequest request) {
|
||||
return Constant.executeControllerList.contains(request.getRequestURI());
|
||||
return executeControllerList.contains(request.getRequestURI());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
package com.sv.netty.config;
|
||||
|
||||
import com.sv.netty.netty.message.ChannelParam;
|
||||
import io.netty.util.AttributeKey;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by ranfi on 2/22/16.
|
||||
*/
|
||||
public class Constant {
|
||||
|
||||
public static final String ACCESS_TOKEN_KEY = "access_token_key";
|
||||
|
||||
//消息队列发送消息到netty
|
||||
public static final String sendToMachine = "netty-doll-machine";
|
||||
|
||||
//消息队列发送消息到netty
|
||||
public static final String sendToService = "netty-doll-service";
|
||||
|
||||
//消息队列发送消息到netty
|
||||
public static final String ROOM_SERVICE_TOPIC = "gt-room-service-topic";
|
||||
|
||||
/**
|
||||
* session中存储终端发送的额外参数
|
||||
*/
|
||||
public static AttributeKey<ChannelParam> CHANNEL_PARAM = AttributeKey.newInstance("CHANNEL_PARAM");
|
||||
|
||||
public final static String SPIT_WORD = "#";
|
||||
|
||||
/**
|
||||
* 哪些接口不需要增加 platform 平台限制
|
||||
*/
|
||||
public static final List<String> executeControllerList = Arrays.asList(new String [] {"/qrCode/init","/qrCode/enter"});
|
||||
|
||||
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.sv.netty.controller;
|
||||
|
||||
import com.sv.entity.Venue;
|
||||
import com.sv.netty.config.Constant;
|
||||
import com.sv.netty.config.NettyConstant;
|
||||
import com.sv.netty.netty.service.MessageService;
|
||||
import com.sv.service.api.QRCodeService;
|
||||
import com.ydd.framework.core.common.dto.ResponseDTO;
|
||||
@@ -73,8 +73,8 @@ public class QREnterController extends BaseApiController {
|
||||
*/
|
||||
private Integer getVenueId(String deviceId){
|
||||
String venueId = "0";
|
||||
if (deviceId!=null && deviceId.contains(Constant.SPIT_WORD)){
|
||||
venueId = deviceId.split(Constant.SPIT_WORD)[1];
|
||||
if (deviceId!=null && deviceId.contains(NettyConstant.SPIT_WORD)){
|
||||
venueId = deviceId.split(NettyConstant.SPIT_WORD)[1];
|
||||
}
|
||||
try {
|
||||
return Integer.parseInt(venueId);
|
||||
@@ -90,7 +90,7 @@ public class QREnterController extends BaseApiController {
|
||||
*/
|
||||
private String getDeviceName(String deviceId){
|
||||
if (deviceId!=null){
|
||||
return deviceId.split(Constant.SPIT_WORD)[0];
|
||||
return deviceId.split(NettyConstant.SPIT_WORD)[0];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package com.sv.netty.netty;
|
||||
|
||||
import com.sv.netty.config.Constant;
|
||||
import com.sv.netty.config.ChannelParam;
|
||||
import com.sv.netty.config.HeartBeat;
|
||||
import com.sv.netty.config.NettyConstant;
|
||||
import com.sv.netty.config.SpringContextHolder;
|
||||
import com.sv.netty.netty.message.ChannelParam;
|
||||
import com.sv.netty.netty.message.HeartBeat;
|
||||
import com.sv.netty.netty.service.MessageService;
|
||||
import com.sv.netty.utils.JsonUtils;
|
||||
import io.netty.channel.*;
|
||||
import io.netty.handler.timeout.IdleState;
|
||||
import io.netty.handler.timeout.IdleStateEvent;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -47,13 +49,12 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
|
||||
*/
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, String msg) {
|
||||
System.err.println("发送的数据========" + msg);
|
||||
String clientIp = ctx.channel().attr(Constant.CHANNEL_PARAM).get().getClientIp();
|
||||
String clientIp = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getClientIp();
|
||||
try {
|
||||
HeartBeat hb = JsonUtils.decode(msg,HeartBeat.class);
|
||||
logger.info("客户端【" + clientIp + "】上报心跳...");
|
||||
ctx.channel().attr(Constant.CHANNEL_PARAM).get().setVenueId(hb.getVenueId());
|
||||
ctx.channel().attr(Constant.CHANNEL_PARAM).get().setDeviceName(hb.getDeviceName());
|
||||
ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().setVenueId(hb.getVenueId());
|
||||
ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().setDeviceName(hb.getDeviceName());
|
||||
messageService.online(clientIp,ctx.channel(), hb);
|
||||
} catch (Exception e) {
|
||||
logger.error("[" + clientIp + "] host unknown error",e);
|
||||
@@ -64,7 +65,7 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
|
||||
public void channelRegistered(ChannelHandlerContext ctx) {
|
||||
String clientIP = ((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().getHostAddress();
|
||||
logger.info("There is a client Registered. ip:" + clientIP);
|
||||
ctx.channel().attr(Constant.CHANNEL_PARAM).set(new ChannelParam(clientIP));
|
||||
ctx.channel().attr(NettyConstant.CHANNEL_PARAM).set(new ChannelParam(clientIP));
|
||||
}
|
||||
|
||||
|
||||
@@ -82,8 +83,8 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
|
||||
public void channelInactive(ChannelHandlerContext ctx) {
|
||||
String clientIP = ((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().getHostAddress();
|
||||
logger.error("Client ip [" + clientIP + "] has inactive");
|
||||
Integer venueId = ctx.channel().attr(Constant.CHANNEL_PARAM).get().getVenueId();
|
||||
String deviceName = ctx.channel().attr(Constant.CHANNEL_PARAM).get().getDeviceName();
|
||||
Integer venueId = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getVenueId();
|
||||
String deviceName = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceName();
|
||||
messageService.Offline(deviceName,venueId);
|
||||
}
|
||||
|
||||
@@ -97,9 +98,9 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||
logger.error("ServerHandler exceptionCaught",cause);
|
||||
Channel channel = ctx.channel();
|
||||
// Integer venueId = ctx.channel().attr(Constant.CHANNEL_PARAM).get().getVenueId();
|
||||
// String deviceName = ctx.channel().attr(Constant.CHANNEL_PARAM).get().getDeviceName();
|
||||
// DeviceType deviceType = ctx.channel().attr(Constant.CHANNEL_PARAM).get().getDeviceType();
|
||||
// Integer venueId = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getVenueId();
|
||||
// String deviceName = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceName();
|
||||
// DeviceType deviceType = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceType();
|
||||
// messageService.Offline(deviceName,venueId,deviceType);
|
||||
if(channel.isActive()) {
|
||||
// 错误产生,关闭连接
|
||||
@@ -113,16 +114,16 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
|
||||
@Override
|
||||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
|
||||
// 服务器统计现在有的客户端数量, 客户端这个方法用来发心跳
|
||||
// if (evt instanceof IdleStateEvent){
|
||||
// IdleState state = ((IdleStateEvent) evt).state();
|
||||
// if (state == IdleState.READER_IDLE){
|
||||
// logger.info("IdleStateEvent READER_IDLE 超时");
|
||||
// Integer venueId = ctx.channel().attr(Constant.CHANNEL_PARAM).get().getVenueId();
|
||||
// String deviceName = ctx.channel().attr(Constant.CHANNEL_PARAM).get().getDeviceName();
|
||||
// messageService.Offline(deviceName,venueId);
|
||||
// ctx.channel().close();
|
||||
// }
|
||||
// }
|
||||
if (evt instanceof IdleStateEvent){
|
||||
IdleState state = ((IdleStateEvent) evt).state();
|
||||
if (state == IdleState.READER_IDLE){
|
||||
logger.info("IdleStateEvent READER_IDLE 超时");
|
||||
Integer venueId = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getVenueId();
|
||||
String deviceName = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceName();
|
||||
messageService.Offline(deviceName,venueId);
|
||||
ctx.channel().close();
|
||||
}
|
||||
}
|
||||
Set<String> connections = messageService.countConnection();
|
||||
logger.info("count connected device ! the count is " + connections.size() + " and they are + [" + connections.toString() + "]" );
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.sv.netty.netty;
|
||||
|
||||
import com.sv.netty.config.Constant;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
@@ -10,7 +8,6 @@ import io.netty.handler.codec.Delimiters;
|
||||
import io.netty.handler.codec.string.StringDecoder;
|
||||
import io.netty.handler.codec.string.StringEncoder;
|
||||
import io.netty.handler.timeout.IdleStateHandler;
|
||||
import io.netty.handler.timeout.ReadTimeoutHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
package com.sv.netty.netty.message;
|
||||
|
||||
/**
|
||||
* 会话中存储的客户端对象
|
||||
*
|
||||
* @author peakren
|
||||
* @since 16/05/2017 11:09 PM
|
||||
*/
|
||||
public class ChannelParam {
|
||||
|
||||
/**
|
||||
* 设备ip
|
||||
*/
|
||||
private String clientIp;
|
||||
|
||||
/**
|
||||
* 设备编号
|
||||
*/
|
||||
private String deviceName;
|
||||
|
||||
/**
|
||||
* 设备所在场馆
|
||||
*/
|
||||
private Integer venueId;
|
||||
|
||||
public ChannelParam(String clientIP) {
|
||||
this.clientIp = clientIP;
|
||||
}
|
||||
|
||||
public String getClientIp() {
|
||||
return clientIp;
|
||||
}
|
||||
|
||||
public void setClientIp(String clientIp) {
|
||||
this.clientIp = clientIp;
|
||||
}
|
||||
|
||||
public String getDeviceName() {
|
||||
return deviceName;
|
||||
}
|
||||
|
||||
public void setDeviceName(String deviceName) {
|
||||
this.deviceName = deviceName;
|
||||
}
|
||||
|
||||
public Integer getVenueId() {
|
||||
return venueId;
|
||||
}
|
||||
|
||||
public void setVenueId(Integer venueId) {
|
||||
this.venueId = venueId;
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package com.sv.netty.netty.message;
|
||||
|
||||
import java.io.Serializable;
|
||||
/**
|
||||
* 客户端心跳数据包
|
||||
* HeartBeat.java
|
||||
*
|
||||
* @author peakren
|
||||
* @date 07/12/2017 10:23 PM
|
||||
*/
|
||||
public class HeartBeat implements Serializable {
|
||||
|
||||
private Integer venueId; //场馆号
|
||||
|
||||
private String deviceName; //设备号
|
||||
|
||||
public Integer getVenueId() {
|
||||
return venueId;
|
||||
}
|
||||
|
||||
public void setVenueId(Integer venueId) {
|
||||
this.venueId = venueId;
|
||||
}
|
||||
|
||||
public String getDeviceName() {
|
||||
return deviceName;
|
||||
}
|
||||
|
||||
public void setDeviceName(String deviceName) {
|
||||
this.deviceName = deviceName;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.sv.netty.netty.message;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class MessageDTO implements Serializable {
|
||||
|
||||
private MessageType messageType;
|
||||
private String message;
|
||||
|
||||
public MessageDTO(MessageType messageType, String message) {
|
||||
this.messageType = messageType;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public MessageType getMessageType() {
|
||||
return messageType;
|
||||
}
|
||||
|
||||
public void setMessageType(MessageType messageType) {
|
||||
this.messageType = messageType;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package com.sv.netty.netty.message;
|
||||
|
||||
/**
|
||||
* 请求门禁的灯光管理
|
||||
*/
|
||||
public class Result {
|
||||
/**
|
||||
* Code : 0
|
||||
* Message : success
|
||||
* Data : false
|
||||
*/
|
||||
private int Code;
|
||||
private String Message;
|
||||
private boolean Data;
|
||||
|
||||
public int getCode() {
|
||||
return Code;
|
||||
}
|
||||
|
||||
public void setCode(int Code) {
|
||||
this.Code = Code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return Message;
|
||||
}
|
||||
|
||||
public void setMessage(String Message) {
|
||||
this.Message = Message;
|
||||
}
|
||||
|
||||
public boolean isData() {
|
||||
return Data;
|
||||
}
|
||||
|
||||
public void setData(boolean Data) {
|
||||
this.Data = Data;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.sv.netty.netty.service;
|
||||
|
||||
import com.sv.entity.Venue;
|
||||
import com.sv.netty.netty.message.HeartBeat;
|
||||
import com.sv.netty.config.HeartBeat;
|
||||
import io.netty.channel.Channel;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@@ -3,13 +3,10 @@ package com.sv.netty.netty.service.impl;
|
||||
import com.sv.entity.Member;
|
||||
import com.sv.entity.MemberEnterVenueLog;
|
||||
import com.sv.entity.Venue;
|
||||
import com.sv.netty.config.Constant;
|
||||
import com.sv.netty.config.ErrorCode;
|
||||
import com.sv.netty.netty.message.HeartBeat;
|
||||
import com.sv.netty.netty.message.MessageDTO;
|
||||
import com.sv.netty.netty.message.MessageType;
|
||||
import com.sv.netty.config.*;
|
||||
import com.sv.netty.netty.service.MessageService;
|
||||
import com.sv.netty.utils.JsonUtils;
|
||||
import com.sv.netty.utils.ServerMessageUtils;
|
||||
import com.sv.service.api.MemberEnterVenueLogService;
|
||||
import com.sv.service.api.MemberService;
|
||||
import com.sv.service.api.VenueService;
|
||||
@@ -88,8 +85,8 @@ public class AppMessageHandlerAdapter implements MessageService {
|
||||
} else {
|
||||
deviceService.online(heartBeat.getDeviceName(),heartBeat.getVenueId(),thisVenue.getType(),clientId);
|
||||
putChannelType(heartBeat.getDeviceName(),heartBeat.getVenueId(),channel);
|
||||
MessageDTO messageDTO = new MessageDTO(MessageType.LINK,"欢迎扫码进场!");
|
||||
channel.writeAndFlush(messageDTO);
|
||||
VenueMessage VenueMessage = new VenueMessage(MessageType.LINK,"欢迎扫码进场!");
|
||||
channel.writeAndFlush(VenueMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -117,9 +114,9 @@ public class AppMessageHandlerAdapter implements MessageService {
|
||||
Member thisMember = memberService.findByMember(memberId);
|
||||
if (thisMember!=null){
|
||||
String nickname = thisMember.getNickname();
|
||||
MessageDTO messageDTO = new MessageDTO(MessageType.LOAD,"欢迎光临!" + nickname + ",请您60s内操作进场。");
|
||||
VenueMessage venueMessage = new VenueMessage(MessageType.LOAD,"欢迎光临!" + nickname + ",请您60s内操作进场。");
|
||||
Channel currentChannel = getCurrentChannel(deviceName, venueId);
|
||||
sendMessage(currentChannel,messageDTO);
|
||||
ServerMessageUtils.INSTANCE.sendMsg(currentChannel,venueMessage);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -159,10 +156,10 @@ public class AppMessageHandlerAdapter implements MessageService {
|
||||
// memberMessageDto.setPlaceName("");
|
||||
// memberMessageDto.setMessage("欢迎下次再来" + venue.getName());
|
||||
// memberMessageDto.setCode(2);
|
||||
// MessageDto messageDto = new MessageDto();
|
||||
// messageDto.setCmdId(Cmd.OPEN_DOOR.id);
|
||||
// messageDto.setDoor(2);
|
||||
// sendOpenMessage(messageDto, device.getId());
|
||||
// VenueMessage VenueMessage = new VenueMessage();
|
||||
// VenueMessage.setCmdId(Cmd.OPEN_DOOR.id);
|
||||
// VenueMessage.setDoor(2);
|
||||
// sendOpenMessage(VenueMessage, device.getId());
|
||||
// 校验玩就可以出场了
|
||||
// sendMessage(memberMessageDto, device.getId());
|
||||
// venueService.addNumber(venue.getId(), -1, member.getId());
|
||||
@@ -187,16 +184,16 @@ public class AppMessageHandlerAdapter implements MessageService {
|
||||
if(venueService.qrCodeEnterVenue(memberId,deviceName,venueId,venue)){
|
||||
// 可以进场
|
||||
scheduledExecutorService.schedule(() -> {
|
||||
sendMessage(channel,new MessageDTO(MessageType.OPEN_DOOR,"OK"));
|
||||
ServerMessageUtils.INSTANCE.sendMsg(channel,new VenueMessage(MessageType.OPEN_DOOR,"OK"));
|
||||
}, 5, TimeUnit.SECONDS);
|
||||
} else {
|
||||
// 进场失败
|
||||
sendMessage(channel,new MessageDTO(MessageType.FAILED, ErrorCode.NO_USE_CARD.getMsg()));
|
||||
ServerMessageUtils.INSTANCE.sendMsg(channel,new VenueMessage(MessageType.FAILED, ErrorCode.NO_USE_CARD.getMsg()));
|
||||
}
|
||||
} else {
|
||||
logger.info(member == null ? "null" : member.getId() + "入场失败:连续入场");
|
||||
Config config = configService.findById(1);
|
||||
sendMessage(channel,new MessageDTO(MessageType.OPEN_DOOR, MessageFormat.format(ErrorCode.CHECK_ENTER_INTERVAL.getMsg(),config.getValue())));
|
||||
ServerMessageUtils.INSTANCE.sendMsg(channel,new VenueMessage(MessageType.OPEN_DOOR, MessageFormat.format(ErrorCode.CHECK_ENTER_INTERVAL.getMsg(),config.getValue())));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -242,30 +239,6 @@ public class AppMessageHandlerAdapter implements MessageService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 给客户端发送信息
|
||||
* @param channel
|
||||
* @param messageDTO
|
||||
*/
|
||||
private void sendMessage(Channel channel, MessageDTO messageDTO) {
|
||||
channel.writeAndFlush(messageDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 控制硬件,篮球馆的门禁灯光控制
|
||||
* @param number
|
||||
*/
|
||||
public void sendNumberChange(Integer number) {
|
||||
// HttpHeaders headers = new HttpHeaders();
|
||||
// headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||
// MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
|
||||
// param.add("LingtekID", "5d14229fcb1f5c1a9046f429");
|
||||
// param.add("Number", number.toString());
|
||||
// HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(param, headers);
|
||||
// ResponseEntity<Result> result = restTemplate.exchange("http://lingtek.jalasmart.com/api/v1/lingtek/number", HttpMethod.PUT, request, Result.class);
|
||||
// logger.info("灯光结果" + JsonMapper.nonDefaultMapper().toJson(result));
|
||||
}
|
||||
|
||||
/**
|
||||
* 操作连接
|
||||
*/
|
||||
@@ -273,7 +246,7 @@ public class AppMessageHandlerAdapter implements MessageService {
|
||||
* 缓存通道
|
||||
*/
|
||||
public void putChannelType(String deviceName, Integer venueId, Channel channel) {
|
||||
String clientId = deviceName + Constant.SPIT_WORD + venueId;
|
||||
String clientId = deviceName + NettyConstant.SPIT_WORD + venueId;
|
||||
links.put(clientId, channel);
|
||||
}
|
||||
|
||||
@@ -281,7 +254,7 @@ public class AppMessageHandlerAdapter implements MessageService {
|
||||
* 获取当前通道
|
||||
*/
|
||||
public Channel getCurrentChannel(String deviceName, Integer venueId){
|
||||
String clientId = deviceName + Constant.SPIT_WORD + venueId + Constant.SPIT_WORD;
|
||||
String clientId = deviceName + NettyConstant.SPIT_WORD + venueId + NettyConstant.SPIT_WORD;
|
||||
return links.get(clientId);
|
||||
}
|
||||
|
||||
@@ -289,7 +262,7 @@ public class AppMessageHandlerAdapter implements MessageService {
|
||||
* 获取通道
|
||||
*/
|
||||
public boolean contains(String deviceName, Integer venueId) {
|
||||
String clientId = deviceName + Constant.SPIT_WORD + venueId + Constant.SPIT_WORD;
|
||||
String clientId = deviceName + NettyConstant.SPIT_WORD + venueId + NettyConstant.SPIT_WORD;
|
||||
return links.containsKey(clientId);
|
||||
}
|
||||
|
||||
@@ -297,7 +270,7 @@ public class AppMessageHandlerAdapter implements MessageService {
|
||||
* 移除通道
|
||||
*/
|
||||
public void removeChannelType(String deviceName, Integer venueId) {
|
||||
String clientId = deviceName + Constant.SPIT_WORD + venueId ;
|
||||
String clientId = deviceName + NettyConstant.SPIT_WORD + venueId ;
|
||||
links.remove(clientId);
|
||||
}
|
||||
|
||||
@@ -309,4 +282,20 @@ public class AppMessageHandlerAdapter implements MessageService {
|
||||
public Set<String> countConnection() {
|
||||
return links.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* 控制硬件,篮球馆的门禁灯光控制
|
||||
* @param number
|
||||
*/
|
||||
// public void sendNumberChange(Integer number) {
|
||||
// HttpHeaders headers = new HttpHeaders();
|
||||
// headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||
// MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
|
||||
// param.add("LingtekID", "5d14229fcb1f5c1a9046f429");
|
||||
// param.add("Number", number.toString());
|
||||
// HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(param, headers);
|
||||
// ResponseEntity<Result> result = restTemplate.exchange("http://lingtek.jalasmart.com/api/v1/lingtek/number", HttpMethod.PUT, request, Result.class);
|
||||
// logger.info("灯光结果" + JsonMapper.nonDefaultMapper().toJson(result));
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
package com.sv.netty;
|
||||
|
||||
import com.sv.netty.message.HeartBeat;
|
||||
import com.sv.netty.config.HeartBeat;
|
||||
import com.sv.netty.config.VenueMessage;
|
||||
import com.sv.netty.utils.EncodeMsg;
|
||||
import com.sv.netty.utils.JsonUtils;
|
||||
import com.sv.service.MessageService;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.handler.timeout.IdleState;
|
||||
import io.netty.handler.timeout.IdleStateEvent;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
@@ -19,6 +24,7 @@ import java.net.UnknownHostException;
|
||||
*/
|
||||
@ChannelHandler.Sharable
|
||||
public class ClientHandler extends SimpleChannelInboundHandler<String> {
|
||||
private final Logger logger = LoggerFactory.getLogger(ClientHandler.class);
|
||||
|
||||
/**
|
||||
* 当通道就绪就会触发
|
||||
@@ -48,9 +54,9 @@ public class ClientHandler extends SimpleChannelInboundHandler<String> {
|
||||
*/
|
||||
@Override
|
||||
public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
|
||||
System.out.println("接收服务器响应msg:[" + msg + "]");
|
||||
// MessageDTO message = JsonMapper.fromJson(msg, MessageDTO.class);
|
||||
// MessageService.getInstance().execute(message);
|
||||
logger.info("接收服务器响应msg:[" + msg + "]");
|
||||
VenueMessage message = JsonUtils.decode(msg, VenueMessage.class);
|
||||
MessageService.getInstance().execute(message);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,7 +94,7 @@ public class ClientHandler extends SimpleChannelInboundHandler<String> {
|
||||
*/
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||
System.out.println("ClientHandler exceptionCaught");
|
||||
logger.info("ClientHandler exceptionCaught");
|
||||
cause.printStackTrace();
|
||||
Channel channel = ctx.channel();
|
||||
if(channel.isActive()) {
|
||||
|
||||
@@ -5,6 +5,8 @@ import io.netty.channel.*;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import io.netty.handler.logging.LoggingHandler;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -17,6 +19,7 @@ import java.util.concurrent.TimeUnit;
|
||||
* @date 07/12/2017 10:12 PM
|
||||
*/
|
||||
public class ClientThread extends Thread{
|
||||
private final Logger logger = LoggerFactory.getLogger(ClientThread.class);
|
||||
|
||||
private static ClientThread instance;
|
||||
|
||||
@@ -69,11 +72,11 @@ public class ClientThread extends Thread{
|
||||
}
|
||||
|
||||
public void doConnect() {
|
||||
System.out.println("现在开始链接了");
|
||||
logger.info("现在开始链接了");
|
||||
if (closed) {
|
||||
return;
|
||||
}
|
||||
System.out.println("连接 = " + remoteHost + " " + remotePort);
|
||||
logger.info("连接 = " + remoteHost + " " + remotePort);
|
||||
future = bootstrap.connect(new InetSocketAddress(remoteHost, remotePort));
|
||||
future.addListener(new ChannelFutureListener() {
|
||||
public void operationComplete(ChannelFuture f) throws Exception {
|
||||
@@ -82,9 +85,9 @@ public class ClientThread extends Thread{
|
||||
public void run() {
|
||||
if (!f.isSuccess()) {
|
||||
doConnect();
|
||||
System.out.println("等待连接");
|
||||
logger.info("等待连接");
|
||||
} else {
|
||||
System.out.println("已连接");
|
||||
logger.info("已连接");
|
||||
}
|
||||
}
|
||||
}, 2, TimeUnit.SECONDS);
|
||||
@@ -110,7 +113,7 @@ public class ClientThread extends Thread{
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
super.channelInactive(ctx);
|
||||
System.out.println(ctx.toString() + "======inactive");
|
||||
logger.info(ctx.toString() + "======inactive");
|
||||
ctx.channel().eventLoop().schedule(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.sv.netty.message;
|
||||
|
||||
public enum MessageType {
|
||||
HB("连接"),
|
||||
OPEN_DOOR("开门");
|
||||
|
||||
private String message;
|
||||
|
||||
MessageType(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.sv.service;
|
||||
|
||||
import com.sv.netty.message.VenueMessage;
|
||||
import com.sv.netty.config.VenueMessage;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 消息服务
|
||||
@@ -11,6 +13,8 @@ import com.sv.netty.message.VenueMessage;
|
||||
*/
|
||||
public class MessageService {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(MessageService.class);
|
||||
|
||||
static private MessageService sInstance;
|
||||
|
||||
static public MessageService getInstance() {
|
||||
@@ -34,7 +38,7 @@ public class MessageService {
|
||||
openDoor();
|
||||
break;
|
||||
default:
|
||||
System.out.println( "default");
|
||||
logger.info( "default");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +47,7 @@ public class MessageService {
|
||||
*/
|
||||
public void openDoor() {
|
||||
// 开门
|
||||
System.out.println("开门成功!!!");
|
||||
logger.info("开门成功!!!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:mysql://127.0.0.1:3306/smart_venue?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8&useAffectedRows=true&useSSL=false
|
||||
username: root
|
||||
password: 123456
|
||||
|
||||
jpa:
|
||||
show-sql: true
|
||||
|
||||
sv:
|
||||
file:
|
||||
store:
|
||||
image: imagetest/
|
||||
video: videotest/
|
||||
health: health-docstest/
|
||||
@@ -1,15 +0,0 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:mysql://127.0.0.1:3306/smart_venue?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8&useAffectedRows=true
|
||||
username: root
|
||||
password: hyty1234
|
||||
|
||||
jpa:
|
||||
show-sql: true
|
||||
|
||||
sv:
|
||||
file:
|
||||
store:
|
||||
image: image/
|
||||
video: video/
|
||||
health: health-docs/
|
||||
@@ -1,111 +0,0 @@
|
||||
server:
|
||||
port: 8023
|
||||
context-path: /netty
|
||||
tomcat:
|
||||
uri-encoding: utf-8
|
||||
|
||||
|
||||
|
||||
spring:
|
||||
profiles:
|
||||
include:
|
||||
-dev
|
||||
-prod
|
||||
active: dev
|
||||
|
||||
|
||||
# 数据库连接池配置
|
||||
druid:
|
||||
filters: stat
|
||||
initialSize: 1
|
||||
minIdle: 1
|
||||
maxActive: 40
|
||||
maxWait: 600000
|
||||
timeBetweenEvictionRunsMillis: 60000
|
||||
minEvictableIdleTimeMillis: 300000
|
||||
validationQuery: SELECT 'x'
|
||||
testWhileIdle: true
|
||||
testOnBorrow: false
|
||||
testOnReturn: false
|
||||
poolPreparedStatements: true
|
||||
maxPoolPreparedStatementPerConnectionSize: 20
|
||||
WebStatFilter:
|
||||
enabled: false
|
||||
urlPattern:
|
||||
exclusions:
|
||||
sessionStatMaxCount:
|
||||
sessionStatEnable:
|
||||
principalSessionName:
|
||||
principalCookieName:
|
||||
profileEnable:
|
||||
StatViewServlet:
|
||||
enabled: true
|
||||
urlPattern: /druid/*
|
||||
resetEnable: true
|
||||
loginUsername: druid
|
||||
loginPassword: druid
|
||||
allow:
|
||||
deny:
|
||||
aop:
|
||||
auto: true
|
||||
http:
|
||||
encoding:
|
||||
force: true
|
||||
charset: utf-8
|
||||
enabled: true
|
||||
|
||||
jackson:
|
||||
date-format: yyyy-MM-dd HH:mm:ss
|
||||
property-naming-strategy: CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES
|
||||
|
||||
redis:
|
||||
pool:
|
||||
max-active: 1000
|
||||
max-wait: -1
|
||||
max-idle: 8
|
||||
min-idle: 8
|
||||
timeout: 60000
|
||||
|
||||
# MyBatis Configuration
|
||||
mybatis:
|
||||
type-aliases-package: com.ydd.oms.entity
|
||||
config-location: classpath:mybatis/mybatis-config.xml
|
||||
mapper-locations: classpath:mybatis/mapper/*/*.xml
|
||||
|
||||
oss:
|
||||
accessKeyId: LTAIlbtS4W2Xe4OV
|
||||
accessKeySecret: qWMYkSfmXFtRoIv9q9OCbszcF9U7dX
|
||||
protocol: http
|
||||
name: smartvenue
|
||||
endPoint: http://oss-cn-beijing.aliyuncs.com
|
||||
url: https://smartvenue.oss-cn-beijing.aliyuncs.com/
|
||||
|
||||
face:
|
||||
url: 192.168.1.111
|
||||
account: test@test.com
|
||||
pwd: 123456
|
||||
|
||||
nettym:
|
||||
url: http://127.0.0.1:8021/netty/message/send
|
||||
|
||||
|
||||
#netty服务器配置
|
||||
netty:
|
||||
port: 56791
|
||||
boss:
|
||||
thread:
|
||||
count: 1
|
||||
worker:
|
||||
thread:
|
||||
count: 2
|
||||
so:
|
||||
keepalive: true
|
||||
backlog: 128
|
||||
reuseaddr: true
|
||||
tcp_nodelay: true
|
||||
|
||||
logging:
|
||||
level:
|
||||
com:
|
||||
sv:
|
||||
mapper: DEBUG
|
||||
@@ -1,41 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE configuration
|
||||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
|
||||
|
||||
|
||||
<configuration>
|
||||
<settings>
|
||||
<setting name="cacheEnabled" value="true" />
|
||||
<!-- 打印sql日志 -->
|
||||
<setting name="logImpl" value="SLF4J" />
|
||||
</settings>
|
||||
<!--<settings>-->
|
||||
<!--<setting name="cacheEnabled" value="true"/>-->
|
||||
<!--<setting name="lazyLoadingEnabled" value="true"/>-->
|
||||
<!--<setting name="multipleResultSetsEnabled" value="true"/>-->
|
||||
<!--<setting name="useColumnLabel" value="true"/>-->
|
||||
<!--<setting name="useGeneratedKeys" value="false"/>-->
|
||||
<!--<setting name="autoMappingBehavior" value="PARTIAL"/>-->
|
||||
<!--<setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>-->
|
||||
<!--<setting name="defaultExecutorType" value="SIMPLE"/>-->
|
||||
<!--<setting name="defaultStatementTimeout" value="25"/>-->
|
||||
<!--<setting name="defaultFetchSize" value="100"/>-->
|
||||
<!--<setting name="safeRowBoundsEnabled" value="false"/>-->
|
||||
<!--<setting name="mapUnderscoreToCamelCase" value="false"/>-->
|
||||
<!--<setting name="localCacheScope" value="SESSION"/>-->
|
||||
<!--<setting name="jdbcTypeForNull" value="OTHER"/>-->
|
||||
<!--<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>-->
|
||||
<!--</settings>-->
|
||||
|
||||
<typeAliases>
|
||||
<typeAlias alias="Integer" type="java.lang.Integer" />
|
||||
<typeAlias alias="Byte" type="java.lang.Byte"/>
|
||||
<typeAlias alias="String" type="java.lang.String" />
|
||||
<typeAlias alias="Long" type="java.lang.Long" />
|
||||
<typeAlias alias="HashMap" type="java.util.HashMap" />
|
||||
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
|
||||
<typeAlias alias="ArrayList" type="java.util.ArrayList" />
|
||||
<typeAlias alias="LinkedList" type="java.util.LinkedList" />
|
||||
</typeAliases>
|
||||
|
||||
</configuration>
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.sv.netty.message;
|
||||
package com.sv.netty.config;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.sv.netty.netty.message;
|
||||
package com.sv.netty.config;
|
||||
|
||||
public enum MessageType {
|
||||
LINK("连接"),
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.sv.netty.message;
|
||||
package com.sv.netty.config;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@@ -7,6 +7,11 @@ public class VenueMessage implements Serializable {
|
||||
private MessageType messageType;
|
||||
private String message;
|
||||
|
||||
public VenueMessage(MessageType type, String msg) {
|
||||
this.messageType = type;
|
||||
this.message = msg;
|
||||
}
|
||||
|
||||
public MessageType getMessageType() {
|
||||
return messageType;
|
||||
}
|
||||
@@ -43,8 +43,9 @@ public class WeiXinSendUtils implements InitializingBean {
|
||||
}
|
||||
|
||||
private void getToken() {
|
||||
// AccessTokenResponse accessToken = WeChatHelper.getAccessToken("wx51d82eba3f5f4858", "d28320bcb885cc208b6fde2253d8663e");
|
||||
// token = accessToken.getAccessToken();
|
||||
//TODO
|
||||
AccessTokenResponse accessToken = WeChatHelper.getAccessToken("wx51d82eba3f5f4858", "d28320bcb885cc208b6fde2253d8663e");
|
||||
token = accessToken.getAccessToken();
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelay=60*60*1000)
|
||||
|
||||
Reference in New Issue
Block a user