新增sick扫码枪
parent
a665ac26ae
commit
5078ee97b6
@ -0,0 +1,31 @@
|
|||||||
|
package com.zhehekeji.web.controller;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.zhehekeji.common.util.HttpUtil;
|
||||||
|
import com.zhehekeji.core.pojo.Result;
|
||||||
|
import com.zhehekeji.core.util.Assert;
|
||||||
|
import com.zhehekeji.web.pojo.IndexVO;
|
||||||
|
import com.zhehekeji.web.service.sick.SickConnMap;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@Api( tags = "传感器")
|
||||||
|
@RequestMapping(value = "/sensor")
|
||||||
|
@RestController()
|
||||||
|
@Slf4j
|
||||||
|
public class SensorController {
|
||||||
|
|
||||||
|
@GetMapping("/start")
|
||||||
|
@ApiOperation(value = "sick扫码枪开始扫")
|
||||||
|
public Result userCenter() {
|
||||||
|
SickConnMap.write(1);
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
package com.zhehekeji.web.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class SensorGun {
|
||||||
|
|
||||||
|
@TableId(type = IdType.AUTO)
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
private Integer streetId;
|
||||||
|
|
||||||
|
private Integer direction;
|
||||||
|
|
||||||
|
private String ip;
|
||||||
|
|
||||||
|
private Integer port;
|
||||||
|
}
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
package com.zhehekeji.web.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.zhehekeji.web.entity.SensorGun;
|
||||||
|
|
||||||
|
public interface SensorGunMapper extends BaseMapper<SensorGun> {
|
||||||
|
}
|
||||||
@ -0,0 +1,47 @@
|
|||||||
|
package com.zhehekeji.web.service.sick;
|
||||||
|
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 与PLC心跳 处理
|
||||||
|
*
|
||||||
|
* @author Administrator
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class SickClientHandler extends ChannelInboundHandlerAdapter {
|
||||||
|
|
||||||
|
private SickNettyClient nettyClient;
|
||||||
|
|
||||||
|
private Integer sensorId;
|
||||||
|
|
||||||
|
public SickClientHandler(Integer sensorId, SickNettyClient nettyClient){
|
||||||
|
|
||||||
|
this.nettyClient = nettyClient;
|
||||||
|
this.sensorId = sensorId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 建立连接时
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||||
|
log.info("sensorId:{}连接成功",sensorId);
|
||||||
|
SickConnMap.conn(sensorId,ctx);
|
||||||
|
ctx.fireChannelActive();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭连接时
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||||
|
log.info("sensorId:{} closed",sensorId);
|
||||||
|
SickConnMap.disConn(sensorId);
|
||||||
|
log.info(" streetId reconnect......");
|
||||||
|
//nettyClient.reconnect(sensorId);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,45 @@
|
|||||||
|
package com.zhehekeji.web.service.sick;
|
||||||
|
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class SickConnMap {
|
||||||
|
|
||||||
|
private static Map<Integer, ChannelHandlerContext> connChanel = new HashMap<>();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static void conn(Integer id,ChannelHandlerContext channel){
|
||||||
|
connChanel.put(id,channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void disConn(Integer id){
|
||||||
|
connChanel.remove(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ChannelHandlerContext getChannel(Integer id){
|
||||||
|
return connChanel.get(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 向sick发送开始扫码指令
|
||||||
|
* return 是否发送成功
|
||||||
|
* @param sensorId
|
||||||
|
*/
|
||||||
|
public static Boolean write(Integer sensorId){
|
||||||
|
if(connChanel.get(sensorId) != null){
|
||||||
|
try {
|
||||||
|
connChanel.get(sensorId).channel().writeAndFlush("start");
|
||||||
|
return true;
|
||||||
|
}catch (Exception e){
|
||||||
|
log.error(""+e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,65 @@
|
|||||||
|
package com.zhehekeji.web.service.sick;
|
||||||
|
|
||||||
|
import com.zhehekeji.web.service.PlcService;
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.handler.codec.LineBasedFrameDecoder;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class SickDecoder extends LineBasedFrameDecoder {
|
||||||
|
|
||||||
|
private static final Logger tcpLogger = LoggerFactory.getLogger("sick");
|
||||||
|
|
||||||
|
private static Map<Integer,String> OCRMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
private PlcService plcService;
|
||||||
|
private Integer sensorId;
|
||||||
|
|
||||||
|
public SickDecoder(PlcService plcService,Integer sensorId) {
|
||||||
|
super(1000);
|
||||||
|
this.plcService = plcService;
|
||||||
|
this.sensorId = sensorId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析TCP数据包
|
||||||
|
*
|
||||||
|
* @param ctx
|
||||||
|
* @param in
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
|
||||||
|
in = (ByteBuf) super.decode(ctx, in);
|
||||||
|
if (in == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
String code = in.toString(StandardCharsets.UTF_8);
|
||||||
|
code = code.replace("\\n","");
|
||||||
|
if(!code.equals("NoRead")){
|
||||||
|
System.out.println(code);
|
||||||
|
OCRMap.put(sensorId,code);
|
||||||
|
//todo 更改stock
|
||||||
|
}else {
|
||||||
|
log.warn("no read");
|
||||||
|
OCRMap.put(sensorId,null);
|
||||||
|
}
|
||||||
|
|
||||||
|
in.release();
|
||||||
|
ctx.channel().close();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
package com.zhehekeji.web.service.sick;
|
||||||
|
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.handler.codec.MessageToByteEncoder;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
public class SickEncoder extends MessageToByteEncoder<String> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void encode(ChannelHandlerContext ctx, String msg, ByteBuf out) throws Exception {
|
||||||
|
if(msg == null){
|
||||||
|
throw new Exception("msg is null");
|
||||||
|
}
|
||||||
|
out.writeBytes(msg.getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,80 @@
|
|||||||
|
package com.zhehekeji.web.service.sick;
|
||||||
|
|
||||||
|
import com.zhehekeji.web.entity.SensorGun;
|
||||||
|
import com.zhehekeji.web.mapper.SensorGunMapper;
|
||||||
|
import com.zhehekeji.web.service.PlcService;
|
||||||
|
import io.netty.bootstrap.Bootstrap;
|
||||||
|
import io.netty.channel.*;
|
||||||
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class SickNettyClient {
|
||||||
|
|
||||||
|
private static EventLoopGroup group = new NioEventLoopGroup();
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private SensorGunMapper sensorGunMapper;
|
||||||
|
@Resource
|
||||||
|
private PlcService plcService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重连最大次数
|
||||||
|
*/
|
||||||
|
private static int RECONNECT_NUM = 5;
|
||||||
|
|
||||||
|
public void createClient(SensorGun sensorGun) throws InterruptedException {
|
||||||
|
if (StringUtils.isEmpty(sensorGun.getIp()) || sensorGun.getPort() == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Bootstrap client = new Bootstrap();
|
||||||
|
client.group(group);
|
||||||
|
client.channel(NioSocketChannel.class);
|
||||||
|
client.handler(new SickNettyClientFilter(sensorGun.getId(),plcService,this));
|
||||||
|
// 连接服务端
|
||||||
|
Channel channel = client.connect(sensorGun.getIp(), sensorGun.getPort()).sync().channel();
|
||||||
|
channel.writeAndFlush("start");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 断线重连 尝试 RECONNECT_NUM 次
|
||||||
|
*
|
||||||
|
* @param sensorId
|
||||||
|
*/
|
||||||
|
public void reconnect(Integer sensorId) {
|
||||||
|
Boolean isConnected = false;
|
||||||
|
int num = 0;
|
||||||
|
SensorGun sensorGun = sensorGunMapper.selectById(sensorId);
|
||||||
|
if (sensorGun == null) {
|
||||||
|
log.error("reconnect ,sensorGun is null ,id:{}", sensorId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
while (num < RECONNECT_NUM && !isConnected) {
|
||||||
|
try {
|
||||||
|
createClient(sensorGun);
|
||||||
|
} catch (Exception e) {
|
||||||
|
//没连上 继续
|
||||||
|
log.error("sensorGun reconnect error num:{}", num);
|
||||||
|
try {
|
||||||
|
Thread.sleep(5000);
|
||||||
|
} catch (InterruptedException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
num++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
isConnected = true;
|
||||||
|
}
|
||||||
|
if (isConnected) {
|
||||||
|
log.info("sensorGun reconnect success");
|
||||||
|
} else {
|
||||||
|
log.error("sensorGun reconnect error .sensorGunId:{},reconnect num:{},ip:{},port:{}", sensorId, num,sensorGun.getIp(),sensorGun.getPort());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,70 @@
|
|||||||
|
package com.zhehekeji.web.service.sick;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sick扫码枪
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class SickSocket {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String code = readOCR("192.168.8.236", 2002);
|
||||||
|
System.out.println(code);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String readOCR(String ip,int port){
|
||||||
|
Socket socket = new Socket();
|
||||||
|
String code = null;
|
||||||
|
OutputStream os = null;
|
||||||
|
InputStream is = null;
|
||||||
|
try {
|
||||||
|
socket.connect(new InetSocketAddress(ip,port),3000);
|
||||||
|
os = socket.getOutputStream();
|
||||||
|
writeCmd(os);
|
||||||
|
is = socket.getInputStream();
|
||||||
|
code = read(is);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("sick time out,ip:{},info:{}",ip,e);
|
||||||
|
}finally {
|
||||||
|
if(os != null){
|
||||||
|
try {
|
||||||
|
os.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(is != null){
|
||||||
|
try {
|
||||||
|
is.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
socket.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void writeCmd(OutputStream os) throws IOException {
|
||||||
|
String startCmd = "start";
|
||||||
|
byte[]bytes = startCmd.getBytes(StandardCharsets.UTF_8);
|
||||||
|
os.write(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String read(InputStream inStream) throws IOException {
|
||||||
|
BufferedReader bd = new BufferedReader(new InputStreamReader(inStream));
|
||||||
|
return bd.readLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue