扫描二维码入场逻辑
This commit is contained in:
44
netty-client/src/main/java/com/sv/barcode/ClientConfig.java
Normal file
44
netty-client/src/main/java/com/sv/barcode/ClientConfig.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package com.sv.barcode;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class ClientConfig implements Serializable {
|
||||
|
||||
String serverIp;
|
||||
String serverPort;
|
||||
|
||||
String clientSN;
|
||||
String clientVid;
|
||||
|
||||
public String getServerIp() {
|
||||
return serverIp;
|
||||
}
|
||||
|
||||
public void setServerIp(String serverIp) {
|
||||
this.serverIp = serverIp;
|
||||
}
|
||||
|
||||
public String getServerPort() {
|
||||
return serverPort;
|
||||
}
|
||||
|
||||
public void setServerPort(String serverPort) {
|
||||
this.serverPort = serverPort;
|
||||
}
|
||||
|
||||
public String getClientSN() {
|
||||
return clientSN;
|
||||
}
|
||||
|
||||
public void setClientSN(String clientSN) {
|
||||
this.clientSN = clientSN;
|
||||
}
|
||||
|
||||
public String getClientVid() {
|
||||
return clientVid;
|
||||
}
|
||||
|
||||
public void setClientVid(String clientVid) {
|
||||
this.clientVid = clientVid;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.sv.barcode;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class ListenKeyFrame extends Thread{
|
||||
|
||||
Frame frame;
|
||||
|
||||
public void newFrame (int i) {
|
||||
frame = new Frame("智慧云馆门禁" + i);
|
||||
frame.setBounds(300,100,600,500);
|
||||
frame.setLayout(new FlowLayout(FlowLayout.CENTER,5,200));//设计布局
|
||||
JLabel label = new JLabel("请保持该界面被选中,否则将无法监听二维码扫描!");
|
||||
frame.addKeyListener(new ScanBarCode());
|
||||
frame.add(label);
|
||||
frame.setVisible(true);
|
||||
frame.setFocusable(true);
|
||||
}
|
||||
|
||||
public void close() {
|
||||
if (frame != null) {
|
||||
frame.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
38
netty-client/src/main/java/com/sv/barcode/ScanBarCode.java
Normal file
38
netty-client/src/main/java/com/sv/barcode/ScanBarCode.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package com.sv.barcode;
|
||||
|
||||
import com.sv.netty.ClientThread;
|
||||
import com.sv.netty.config.NettyConstant;
|
||||
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class ScanBarCode extends KeyAdapter {
|
||||
|
||||
private List<Character> barcode = new LinkedList<>();
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
if (barcode.size() > 200) {
|
||||
barcode.clear();
|
||||
}
|
||||
char keyChar = e.getKeyChar();
|
||||
if (NettyConstant.BARCODE_END == keyChar) {
|
||||
String barStr = "";
|
||||
if (barcode.size() > 0 && barcode != null) {
|
||||
for (Character character : barcode) {
|
||||
barStr = barStr + character;
|
||||
}
|
||||
}
|
||||
System.err.println(barStr);
|
||||
ClientThread.getInstance().checkBarcode(barStr);
|
||||
barcode.clear();
|
||||
}
|
||||
barcode.add(keyChar);
|
||||
if (NettyConstant.BARCODE_BEGIN == keyChar) {
|
||||
barcode.clear();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.sv.intergration;
|
||||
|
||||
/**
|
||||
* 对接场馆门禁
|
||||
*/
|
||||
public interface DoorService {
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.sv.service;
|
||||
package com.sv.intergration;
|
||||
|
||||
import com.sv.netty.config.VenueMessage;
|
||||
import org.slf4j.Logger;
|
||||
@@ -34,8 +34,11 @@ public class MessageService {
|
||||
*/
|
||||
public void execute(VenueMessage message) {
|
||||
switch (message.getMessageType()) {
|
||||
case OPEN_DOOR:
|
||||
openDoor();
|
||||
case ENTER_DOOR:
|
||||
enterDoor();
|
||||
break;
|
||||
case OUT_DOOR:
|
||||
outDoor();
|
||||
break;
|
||||
default:
|
||||
logger.info( "default");
|
||||
@@ -45,7 +48,15 @@ public class MessageService {
|
||||
/**
|
||||
* 门禁开门
|
||||
*/
|
||||
public void openDoor() {
|
||||
public void enterDoor() {
|
||||
// 开门
|
||||
logger.info("开门成功!!!");
|
||||
}
|
||||
|
||||
/**
|
||||
* 门禁开门
|
||||
*/
|
||||
public void outDoor() {
|
||||
// 开门
|
||||
logger.info("开门成功!!!");
|
||||
}
|
||||
@@ -1,10 +1,8 @@
|
||||
package com.sv.netty;
|
||||
|
||||
import com.sv.netty.config.HeartBeat;
|
||||
import com.sv.intergration.MessageService;
|
||||
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;
|
||||
@@ -69,23 +67,12 @@ public class ClientHandler extends SimpleChannelInboundHandler<String> {
|
||||
if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {
|
||||
IdleStateEvent event = (IdleStateEvent) evt;
|
||||
if (event.state() == IdleState.ALL_IDLE) {
|
||||
ctx.write(EncodeMsg.INSTANCE.encode(getHbMessage()));
|
||||
ctx.write(MessageHandler.sendHeartBeat());
|
||||
ctx.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 封装心跳请求包
|
||||
* @throws Exception
|
||||
*/
|
||||
private HeartBeat getHbMessage() {
|
||||
HeartBeat hb = new HeartBeat();
|
||||
hb.setDeviceName("dsadasdasd");
|
||||
hb.setVenueId(32);
|
||||
return hb;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理异常
|
||||
* @param ctx
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package com.sv.netty;
|
||||
|
||||
import com.sv.netty.config.NettyConstant;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.channel.*;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import io.netty.handler.logging.LoggingHandler;
|
||||
import io.netty.util.internal.StringUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -36,7 +38,9 @@ public class ClientThread extends Thread{
|
||||
if (instance == null) {
|
||||
synchronized (ClientThread.class) {
|
||||
if (instance == null) {
|
||||
instance = new ClientThread("127.0.0.1", 56792);
|
||||
String serverIp = System.getProperty(NettyConstant.VENUE_SERVER_IP);
|
||||
String serverPort = System.getProperty(NettyConstant.VENUE_SERVER_PORT);
|
||||
instance = new ClientThread(serverIp, Integer.parseInt(serverPort));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -67,6 +71,23 @@ public class ClientThread extends Thread{
|
||||
doConnect();
|
||||
}
|
||||
|
||||
public void checkBarcode(String barcode) {
|
||||
try {
|
||||
if (future != null) {
|
||||
Channel channel = future.channel();
|
||||
if (channel != null) {
|
||||
String sendCode = MessageHandler.checkBarcode(barcode);
|
||||
if (StringUtil.isNullOrEmpty(sendCode)){
|
||||
return;
|
||||
}
|
||||
channel.writeAndFlush(sendCode);
|
||||
}
|
||||
}
|
||||
}catch (Exception e) {
|
||||
logger.error("链接失败,发送二维码失败",e);
|
||||
}
|
||||
}
|
||||
|
||||
public void clearFuture(){
|
||||
future = null;
|
||||
}
|
||||
|
||||
42
netty-client/src/main/java/com/sv/netty/MessageHandler.java
Normal file
42
netty-client/src/main/java/com/sv/netty/MessageHandler.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package com.sv.netty;
|
||||
|
||||
import com.sv.netty.config.*;
|
||||
import com.sv.netty.utils.EncodeMsg;
|
||||
import com.sv.netty.utils.JsonUtils;
|
||||
import com.sv.netty.utils.MakeCode;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class MessageHandler {
|
||||
private static final Logger logger = LoggerFactory.getLogger(MessageHandler.class);
|
||||
|
||||
public static String sendHeartBeat() {
|
||||
VenueMessage venueMessage = new VenueMessage();
|
||||
venueMessage.setMessageType(MessageType.HB);
|
||||
String clientSn = System.getProperty(NettyConstant.VENUE_CLIENT_SN);
|
||||
String clientVid = System.getProperty(NettyConstant.VENUE_CLIENT_VID);
|
||||
HeartBeat hb = new HeartBeat();
|
||||
hb.setDeviceName(clientSn);
|
||||
hb.setVenueId(Integer.parseInt(clientVid));
|
||||
venueMessage.setMessage(JsonUtils.encode(hb));
|
||||
return EncodeMsg.INSTANCE.encode(venueMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 核销二维码
|
||||
* @return
|
||||
*/
|
||||
public static String checkBarcode(String barcode) {
|
||||
VenueBarCode venueBarCode = MakeCode.decodeCode(barcode);
|
||||
if (venueBarCode == null) {
|
||||
logger.error("二维码不合法" + barcode);
|
||||
return null;
|
||||
}
|
||||
VenueMessage venueMessage = new VenueMessage();
|
||||
venueMessage.setMessageType(MessageType.SCAN_CODE);
|
||||
venueMessage.setMessage(barcode);
|
||||
logger.info("核销二维码===" + barcode);
|
||||
return EncodeMsg.INSTANCE.encode(venueMessage);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,15 @@
|
||||
package com.sv.service;
|
||||
|
||||
import com.sv.barcode.ClientConfig;
|
||||
import com.sv.barcode.ListenKeyFrame;
|
||||
import com.sv.netty.ClientThread;
|
||||
import com.sv.netty.config.NettyConstant;
|
||||
import com.sv.netty.utils.JsonUtils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 启动socker和websocket服务
|
||||
@@ -10,9 +19,41 @@ import com.sv.netty.ClientThread;
|
||||
*/
|
||||
public class ClientService {
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
File file = new File("/home/venue/client/venue.conf");
|
||||
// File file = new File("/Users/limqhz/home/venue/client/venue.conf");
|
||||
FileReader fileReader = new FileReader(file);
|
||||
BufferedReader bufferedReader = new BufferedReader(fileReader);
|
||||
String line = bufferedReader.readLine();
|
||||
if (line != null) {
|
||||
ClientConfig decode = JsonUtils.decode(line, ClientConfig.class);
|
||||
System.setProperty(NettyConstant.VENUE_SERVER_IP,decode.getServerIp());
|
||||
System.setProperty(NettyConstant.VENUE_SERVER_PORT,decode.getServerPort());
|
||||
System.setProperty(NettyConstant.VENUE_CLIENT_SN,decode.getClientSN());
|
||||
System.setProperty(NettyConstant.VENUE_CLIENT_VID,decode.getClientVid());
|
||||
}
|
||||
bufferedReader.close();
|
||||
fileReader.close();
|
||||
|
||||
ClientThread instance = ClientThread.getInstance();
|
||||
instance.start();
|
||||
|
||||
Thread.sleep(3000);
|
||||
ListenKeyFrame listenKeyFrame = new ListenKeyFrame();
|
||||
listenKeyFrame.newFrame(1);
|
||||
Thread.sleep(3000);
|
||||
listenKeyFrame.close();
|
||||
ListenKeyFrame listenKeyFrame1 = new ListenKeyFrame();
|
||||
listenKeyFrame1.newFrame(2);
|
||||
Thread.sleep(3000);
|
||||
listenKeyFrame1.close();
|
||||
ListenKeyFrame listenKeyFrame2 = new ListenKeyFrame();
|
||||
listenKeyFrame2.newFrame(3);
|
||||
Thread.sleep(3000);
|
||||
listenKeyFrame2.close();
|
||||
ListenKeyFrame listenKeyFrame3 = new ListenKeyFrame();
|
||||
listenKeyFrame3.newFrame(4);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF8"?>
|
||||
<configuration>
|
||||
<jmxConfigurator />
|
||||
<property name="LOG_HOME" value="./home/logs/netty_log"/>
|
||||
<property name="LOG_HOME" value="/home/venue/logs/netty_log"/>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
|
||||
Reference in New Issue
Block a user