diff --git a/netty-pad/src/main/java/com/sv/netty/config/Constant.java b/netty-pad/src/main/java/com/sv/netty/config/Constant.java index 19e7612..61bb2ee 100644 --- a/netty-pad/src/main/java/com/sv/netty/config/Constant.java +++ b/netty-pad/src/main/java/com/sv/netty/config/Constant.java @@ -1,6 +1,6 @@ package com.sv.netty.config; -import com.sv.netty.netty.ChannelParam; +import com.sv.netty.netty.message.ChannelParam; import io.netty.util.AttributeKey; /** diff --git a/netty-pad/src/main/java/com/sv/netty/controller/MessageControler.java b/netty-pad/src/main/java/com/sv/netty/controller/MessageControler.java index 189f98c..3b14fb6 100644 --- a/netty-pad/src/main/java/com/sv/netty/controller/MessageControler.java +++ b/netty-pad/src/main/java/com/sv/netty/controller/MessageControler.java @@ -1,13 +1,10 @@ package com.sv.netty.controller; import com.sv.entity.face.FaceRecognizeResponse; -import com.sv.netty.netty.MemberDto; -import com.sv.netty.netty.ResponseDTO; -import com.sv.netty.service.MessageService; +import com.sv.netty.dto.ResponseDTO; import com.sv.netty.service.impl.TcpMessageHandlerAdapter; import com.sv.service.oms.DeviceService; import com.sv.service.oms.VenueService; -import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; diff --git a/netty-pad/src/main/java/com/sv/netty/controller/QRCodeControler.java b/netty-pad/src/main/java/com/sv/netty/controller/QRCodeControler.java index 93bbca7..0a0facc 100644 --- a/netty-pad/src/main/java/com/sv/netty/controller/QRCodeControler.java +++ b/netty-pad/src/main/java/com/sv/netty/controller/QRCodeControler.java @@ -1,17 +1,11 @@ package com.sv.netty.controller; -import com.sv.entity.face.FaceRecognizeResponse; import com.sv.netty.config.ErrorCode; -import com.sv.netty.netty.ResponseDTO; -import com.sv.netty.service.impl.TcpMessageHandlerAdapter; -import com.sv.service.oms.DeviceService; -import com.sv.service.oms.VenueService; +import com.sv.netty.dto.ResponseDTO; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import javax.annotation.Resource; - @RestController public class QRCodeControler { diff --git a/netty-pad/src/main/java/com/sv/netty/netty/MemberDto.java b/netty-pad/src/main/java/com/sv/netty/netty/MemberDto.java deleted file mode 100644 index 726e266..0000000 --- a/netty-pad/src/main/java/com/sv/netty/netty/MemberDto.java +++ /dev/null @@ -1,148 +0,0 @@ -package com.sv.netty.netty; - - -import java.io.Serializable; -import java.math.BigDecimal; - - -/** - * 会员基本信息 - * MemberDto.java - * - * @author peakren - * @date 2018/12/20 8:39 PM - */ -public class MemberDto implements Serializable { - - /** - * 头像 - */ - - private String avatar; - - /** - * 姓名 - */ - - private String name; - - /** - * 手机号码 - */ - - private String mobile; - - /** - * 余额 - */ - - private BigDecimal amount; - - /** - * 是否第一次进入 - */ - private boolean first = false; - - /** - * 场地价格 - */ - private BigDecimal placePrice; - /** - * 场地名称 - */ - - private String placeName; - - /** - * 会员卡名称 - */ - - private String cardName; - - private String message; - - /** - * 1成功进场 0不允许进场 - */ - private int code; - - public String getAvatar() { - return avatar; - } - - public void setAvatar(String avatar) { - this.avatar = avatar; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getMobile() { - return mobile; - } - - public void setMobile(String mobile) { - this.mobile = mobile; - } - - public BigDecimal getAmount() { - return amount; - } - - public void setAmount(BigDecimal amount) { - this.amount = amount; - } - - public String getPlaceName() { - return placeName; - } - - public void setPlaceName(String placeName) { - this.placeName = placeName; - } - - public String getCardName() { - return cardName; - } - - public void setCardName(String cardName) { - this.cardName = cardName; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } - - public BigDecimal getPlacePrice() { - return placePrice; - } - - public void setPlacePrice(BigDecimal placePrice) { - this.placePrice = placePrice; - } - - public boolean isFirst() { - return first; - } - - public void setFirst(boolean first) { - this.first = first; - } -} diff --git a/netty-pad/src/main/java/com/sv/netty/netty/MessageEncoder.java b/netty-pad/src/main/java/com/sv/netty/netty/MessageEncoder.java index 9dd2f80..b575a73 100644 --- a/netty-pad/src/main/java/com/sv/netty/netty/MessageEncoder.java +++ b/netty-pad/src/main/java/com/sv/netty/netty/MessageEncoder.java @@ -1,6 +1,6 @@ package com.sv.netty.netty; -import com.sv.netty.utils.CommonUtils; +import com.sv.netty.netty.message.BaseDto; import com.sv.netty.utils.JsonUtils; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; @@ -19,11 +19,11 @@ import java.nio.charset.Charset; */ public class MessageEncoder extends MessageToByteEncoder { - private final static int MESSAGE_LENGTH = 4; - private final static int MESSAGE_SEQNO = 8; - private final static int MESSAGE_HEAD = 4; - private final static String mSeqno = "doll"; - private final static int MAGIC_WORD = 0x9DDD; + /** + * TODO 客户端没用就删了 + */ +// private final static String mSeqno = "doll"; + private final static String DELIMITER_WORD = "$_$"; private static Logger logger = LoggerFactory.getLogger(MessageEncoder.class); @@ -33,11 +33,8 @@ public class MessageEncoder extends MessageToByteEncoder { protected void encode(ChannelHandlerContext ctx, BaseDto msg, ByteBuf out) throws Exception { String message = JsonUtils.encode(msg); logger.info("send message content:" + message); - byte[] bodys = message.getBytes(charset.name()); - - int len = bodys.length; - out.writeInt(MAGIC_WORD); //发送预留数据字节码 - out.writeInt(len); //发送head字节码 - out.writeBytes(bodys); //发送消息内容 + message = message + DELIMITER_WORD; + byte[] content = message.getBytes(charset.name()); + out.writeBytes(content); //发送消息内容 } } diff --git a/netty-pad/src/main/java/com/sv/netty/netty/ResponseDTO.java b/netty-pad/src/main/java/com/sv/netty/netty/ResponseDTO.java deleted file mode 100644 index 7f4b6fb..0000000 --- a/netty-pad/src/main/java/com/sv/netty/netty/ResponseDTO.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.sv.netty.netty; - - -import org.springframework.util.Assert; - -import java.util.LinkedHashMap; - -public class ResponseDTO extends LinkedHashMap { - public static final String ERR_CODE = "err_code"; - public static final String ERR_MSG = "err_msg"; - public static final String TIMESTAMP = "timestamp"; - private static final long serialVersionUID = 8410965932046471023L; - - public ResponseDTO() { - this(0, "OK"); - } - - public ResponseDTO(Integer errCode, String errMsg) { - this(errCode, errMsg, System.currentTimeMillis() / 1000L); - } - - public ResponseDTO(Integer errCode, String errMsg, Long timestamp) { - this.addAttribute("err_code", errCode); - this.addAttribute("err_msg", errMsg); - this.addAttribute("timestamp", timestamp); - } - - - - public ResponseDTO(Integer errCode, String errMsg, String attrName, Object attrValue) { - this(errCode, errMsg); - this.addAttribute(attrName, attrValue); - } - - public static ResponseDTO ok() { - return new ResponseDTO(); - } - - public static ResponseDTO ok(String msg) { - ResponseDTO ret = ok(); - ret.setErrorMsg(msg); - return ret; - } - - public ResponseDTO addAttribute(String attrName, Object attrValue) { - Assert.notNull(attrName, "属性名称不能为空"); - this.put(attrName, attrValue); - return this; - } - - - - public void setErrorCode(Integer errCode) { - this.addAttribute("err_code", errCode); - } - - public void setErrorMsg(String errMsg) { - this.addAttribute("err_msg", errMsg); - } -} diff --git a/netty-pad/src/main/java/com/sv/netty/netty/ServerHandler.java b/netty-pad/src/main/java/com/sv/netty/netty/ServerHandler.java index 0fda373..feadfb2 100644 --- a/netty-pad/src/main/java/com/sv/netty/netty/ServerHandler.java +++ b/netty-pad/src/main/java/com/sv/netty/netty/ServerHandler.java @@ -2,6 +2,8 @@ package com.sv.netty.netty; import com.sv.netty.config.Constant; import com.sv.netty.config.SpringContextHolder; +import com.sv.netty.netty.message.BaseDto; +import com.sv.netty.netty.message.ChannelParam; import com.sv.netty.service.MessageService; import io.netty.channel.*; import io.netty.handler.timeout.IdleState; @@ -38,17 +40,6 @@ public class ServerHandler extends SimpleChannelInboundHandler { super.channelActive(ctx); } - /** - * 读取客户端发送的消息 - * @param ctx 上下文对象 管道(pipeline),通道channel , 地址 - * @param msg 客户端发送的数据 - * @throws Exception - */ - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - - } - @Override protected void channelRead0(ChannelHandlerContext ctx, BaseDto msg) throws Exception { super.channelRead(ctx, msg); @@ -100,12 +91,11 @@ public class ServerHandler extends SimpleChannelInboundHandler { * @throws Exception */ @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) - throws Exception { - super.exceptionCaught(ctx, cause); + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { logger.error("ServerHandler exceptionCaught",cause); Channel channel = ctx.channel(); if(channel.isActive()) { + // 错误产生,关闭连接 ctx.close(); } } diff --git a/netty-pad/src/main/java/com/sv/netty/netty/ServerProtocolInitializer.java b/netty-pad/src/main/java/com/sv/netty/netty/ServerProtocolInitializer.java index 7f64364..157d6c3 100644 --- a/netty-pad/src/main/java/com/sv/netty/netty/ServerProtocolInitializer.java +++ b/netty-pad/src/main/java/com/sv/netty/netty/ServerProtocolInitializer.java @@ -30,7 +30,7 @@ public class ServerProtocolInitializer extends ChannelInitializer ChannelPipeline pipeline = ch.pipeline(); logger.info("ServerProtocolInitializer"); // 通过指定的长度来标识整包的信息,这样就可以自动的处理粘包和半包的问题 -// pipeline.addFirst(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 4, 4, 0, 0)); + pipeline.addFirst(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 4, 4, 0, 0)); pipeline.addLast(new ReadTimeoutHandler(READ_TIMEOUT)); pipeline.addLast(new MessageDecoder()); pipeline.addLast(new MessageEncoder()); diff --git a/netty-pad/src/main/java/com/sv/netty/netty/BaseDto.java b/netty-pad/src/main/java/com/sv/netty/netty/message/BaseDto.java similarity index 97% rename from netty-pad/src/main/java/com/sv/netty/netty/BaseDto.java rename to netty-pad/src/main/java/com/sv/netty/netty/message/BaseDto.java index d5f3ec8..e63eb0f 100644 --- a/netty-pad/src/main/java/com/sv/netty/netty/BaseDto.java +++ b/netty-pad/src/main/java/com/sv/netty/netty/message/BaseDto.java @@ -1,4 +1,4 @@ -package com.sv.netty.netty; +package com.sv.netty.netty.message; import com.sv.entity.Device; diff --git a/netty-pad/src/main/java/com/sv/netty/netty/ChannelParam.java b/netty-pad/src/main/java/com/sv/netty/netty/message/ChannelParam.java similarity index 95% rename from netty-pad/src/main/java/com/sv/netty/netty/ChannelParam.java rename to netty-pad/src/main/java/com/sv/netty/netty/message/ChannelParam.java index 00e4f28..b951784 100644 --- a/netty-pad/src/main/java/com/sv/netty/netty/ChannelParam.java +++ b/netty-pad/src/main/java/com/sv/netty/netty/message/ChannelParam.java @@ -1,7 +1,7 @@ -package com.sv.netty.netty; +package com.sv.netty.netty.message; /** - * 会话中存储的对象 + * 会话中存储的客户端对象 * * @author peakren * @since 16/05/2017 11:09 PM diff --git a/netty-pad/src/main/java/com/sv/netty/netty/Cmd.java b/netty-pad/src/main/java/com/sv/netty/netty/message/Cmd.java similarity index 94% rename from netty-pad/src/main/java/com/sv/netty/netty/Cmd.java rename to netty-pad/src/main/java/com/sv/netty/netty/message/Cmd.java index 181c42b..4456374 100644 --- a/netty-pad/src/main/java/com/sv/netty/netty/Cmd.java +++ b/netty-pad/src/main/java/com/sv/netty/netty/message/Cmd.java @@ -1,4 +1,4 @@ -package com.sv.netty.netty; +package com.sv.netty.netty.message; /** * 消息协议指令定义 diff --git a/netty-pad/src/main/java/com/sv/netty/netty/MessageDto.java b/netty-pad/src/main/java/com/sv/netty/netty/message/MessageDto.java similarity index 82% rename from netty-pad/src/main/java/com/sv/netty/netty/MessageDto.java rename to netty-pad/src/main/java/com/sv/netty/netty/message/MessageDto.java index 3bfef09..285e58a 100644 --- a/netty-pad/src/main/java/com/sv/netty/netty/MessageDto.java +++ b/netty-pad/src/main/java/com/sv/netty/netty/message/MessageDto.java @@ -1,4 +1,6 @@ -package com.sv.netty.netty; +package com.sv.netty.netty.message; + +import com.sv.netty.netty.message.BaseDto; public class MessageDto extends BaseDto { diff --git a/netty-pad/src/main/java/com/sv/netty/netty/Result.java b/netty-pad/src/main/java/com/sv/netty/netty/message/Result.java similarity index 88% rename from netty-pad/src/main/java/com/sv/netty/netty/Result.java rename to netty-pad/src/main/java/com/sv/netty/netty/message/Result.java index cd38461..e9a1eb1 100644 --- a/netty-pad/src/main/java/com/sv/netty/netty/Result.java +++ b/netty-pad/src/main/java/com/sv/netty/netty/message/Result.java @@ -1,14 +1,14 @@ -package com.sv.netty.netty; +package com.sv.netty.netty.message; +/** + * 请求门禁的灯光管理 + */ public class Result { - - /** * Code : 0 * Message : success * Data : false */ - private int Code; private String Message; private boolean Data; diff --git a/netty-pad/src/main/java/com/sv/netty/service/MessageService.java b/netty-pad/src/main/java/com/sv/netty/service/MessageService.java index c88ccf2..6df8528 100644 --- a/netty-pad/src/main/java/com/sv/netty/service/MessageService.java +++ b/netty-pad/src/main/java/com/sv/netty/service/MessageService.java @@ -1,6 +1,6 @@ package com.sv.netty.service; -import com.sv.netty.netty.MemberDto; +import com.sv.netty.dto.MemberDto; import io.netty.channel.Channel; /** diff --git a/netty-pad/src/main/java/com/sv/netty/service/impl/TcpMessageHandlerAdapter.java b/netty-pad/src/main/java/com/sv/netty/service/impl/TcpMessageHandlerAdapter.java index 0b686a8..7805420 100644 --- a/netty-pad/src/main/java/com/sv/netty/service/impl/TcpMessageHandlerAdapter.java +++ b/netty-pad/src/main/java/com/sv/netty/service/impl/TcpMessageHandlerAdapter.java @@ -2,14 +2,16 @@ package com.sv.netty.service.impl; import com.enums.FaceRecognizeEnum; import com.sv.dto.EnterResult; -import com.sv.dto.api.MemberMessageDto; import com.sv.entity.Device; import com.sv.entity.Member; import com.sv.entity.MemberEnterVenueLog; import com.sv.entity.Venue; import com.sv.entity.face.FaceRecognizeResponse; -import com.sv.netty.config.Constant; -import com.sv.netty.netty.*; +import com.sv.netty.dto.MemberDto; +import com.sv.netty.netty.message.BaseDto; +import com.sv.netty.netty.message.Cmd; +import com.sv.netty.netty.message.MessageDto; +import com.sv.netty.netty.message.Result; import com.sv.netty.service.MessageService; import com.sv.netty.utils.JsonMapper; import com.sv.service.api.MemberEnterVenueLogService; @@ -17,28 +19,20 @@ import com.sv.service.api.MemberService; import com.sv.service.api.VenueService; import com.sv.service.oms.ConfigService; import com.sv.service.oms.DeviceService; -import com.ydd.framework.core.common.dto.ResponseDTO; -import com.ydd.oms.entity.sys.Admin; import com.ydd.oms.entity.sys.Config; import io.netty.channel.Channel; -import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.*; -import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; -import java.math.BigDecimal; import java.util.*; -import java.util.concurrent.Executor; import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -297,6 +291,10 @@ public class TcpMessageHandlerAdapter implements MessageService { } } + /** + * 控制硬件,门禁灯光控制 + * @param number + */ public void sendNumberChange(Integer number) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);