Newer
Older
casic-metering-readwriter / src / main / java / com / casic / missiles / cotroller / BusinessReadWriterController.java
package com.casic.missiles.cotroller;

import com.casic.missiles.model.ReturnDTO;
import com.casic.missiles.service.IBusinessReadWriterService;
import com.rfid.API.CommonReader;
import com.rfid.API.ProtocolVersion;
import com.rfid.API.Util;
import com.rfid.API.core.BaseReader;
import com.rfid.API.entityclass.MemoryBank;
import com.rfid.API.entityclass.ScanTagParameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.ExecutionException;

/**
 * @Description:
 * @Author: wangpeng
 * @Date: 2023/6/15 16:01
 */
@Slf4j
@RestController
@RequestMapping("/business/readWriter")
public class BusinessReadWriterController {
    @Autowired
    private IBusinessReadWriterService readWriterService;

    /**
     * 获取大读写器识别的标签TID列表
     * 大读写器
     * TCP连接,需配置和192.168.1.201一个段
     * 读取读写器获取标签TID列表,用于标签绑定,前端从结果中选取一个标签传入标签绑定接口
     */
    @CrossOrigin
    @GetMapping("/tId/readList")
    public ReturnDTO<Set<String>> tIdNewReadList() {
        return readWriterService.tIdNewReadList();
    }

    /**
     * 获取小读写器识别的标签TID列表
     * USB-HID连接(HID是一种USB通信协议,无需安装驱动就能进行交互)
     * 读取读写器获取TID列表,用于标签绑定,前端从结果中选取一个标签传入标签绑定接口
     */
    @CrossOrigin
    @GetMapping("/tId/mini/readList")
    public ReturnDTO<Set<String>> tIdMiniReadList() {
        return readWriterService.tIdMiniReadList();
    }

    /**
     * 获取有线扫码枪识别列表(二维码)
     * USB串口连接(模拟COM口),USB模拟串行通讯模式,连接后在设备管理器-端口(COM和LPT)可查看连接设备
     * 目前设置:扫码一次读取一个,扫码结果无回车
     * 二维码由样品编号/设备编号生成
     * 扫描收入、扫描检完等,以及大读写器的补充(包括新建委托书和待收入中使用)
     */
    @CrossOrigin
    @GetMapping("/code/gun/readList")
    public ReturnDTO<Set<String>> codeGunReadList() {
        return readWriterService.codeGunReadList();
    }

    /**
     * 有线扫码枪(二维码)和大读写器结合扫描(有线扫码枪已去掉,前端已做)
     */
    @CrossOrigin
    @GetMapping("/gunAndTId/readList")
    public ReturnDTO<Set<String>> equipmentLabelReadList() throws ExecutionException, InterruptedException {
        return readWriterService.gunAndTIdNewReadList();
    }

    /**
     * 大读写器和小读写器识别结合的结果TID列表
     */
    @CrossOrigin
    @GetMapping("/tIdAndMini/readList")
    public ReturnDTO<Set<String>> allTidReadList() throws ExecutionException, InterruptedException {
        return readWriterService.tIdAndMiniReadList();
    }

    /**
     * 大读写器和小读写器和有线扫码枪(二维码)识别结合的结果TID列表(有线扫码枪已去掉,前端已做)
     */
    @CrossOrigin
    @GetMapping("/tIdAndMiniAndGun/readList")
    public ReturnDTO<Set<String>> tIdAndMiniAndGun() throws ExecutionException, InterruptedException {
        return readWriterService.tIdAndMiniAndGunReadList();
    }


    public static void main(String[] args) {
//        CommonReader reader = new CommonReader("Reader1", GlobalData.ConnectType.TcpClient.name(), "192.168.1.201:9090");
        CommonReader reader = new CommonReader("Reader1", "RS232", "COM5:115200");

//        CommonReader reader = new CommonReader("Reader1", "USB-HID", "");
        reader.setProtocol(ProtocolVersion.GRP);
        reader.setReaderProtocol(ProtocolVersion.GRP);
        reader.setPortSum(4);
        reader.connect(false, new BaseReader.IConnectListener() {
            @Override
            public void callback(boolean b) {
                System.out.println("设备连接状态:" + b);
            }
        });


        if (reader.getIsConnected() == true) {
            System.out.println("连接成功...");
            boolean a = reader.BuzzerControl(true, false);
            System.out.println("**********************");
            System.out.println("1.查询功率  2.设置功率 3.盘点标签 4.查询GPI状态 5. 开门开灯");
            while (true) {
                Scanner input = new Scanner(System.in);
                System.out.println("请输入菜单编号:");
                int bh = input.nextInt();
                if (bh == 1) {
                    HashMap<Byte, Byte> powerList = reader.reader_GetPowerList();
                    for (Byte key : powerList.keySet()) {
                        System.out.println("端口:" + key + " 功率:" + powerList.get(key));
                    }
                } else if (bh == 2) {
                    HashMap<Byte, Byte> map = new HashMap();
                    System.out.println("输入需要修改的端口:");
                    int k = input.nextInt();
                    byte k1 = (byte) k;
                    System.out.println("输入需要修改的功率:");
                    int v = input.nextByte();
                    byte v1 = (byte) v;
                    map.put(k1, v1);
                    boolean set = reader.reader_SetPowerList(map);
                    System.out.println("功率修改成功");
                } else if (bh == 3) {
                    //添加标签读取回调
                    ScanTagParameter param = new ScanTagParameter();
                    param.antenna = 0x01;
                    param.isLoop = true;
                    param.tidParameter = new byte[]{0,8};
                    boolean b = reader.reader_Inventory(param, (byte) 0x00);
                    reader.OnTagDataReceivedHandle = (gateName, tagData) -> {
                        if (tagData != null && tagData.getEPC() != null) {
                            String s = Util.convertByteArrayToHexString(tagData.getTID());
                            System.out.println("1读取到TID:" + s);
                        }
                    };
                    new Timer().schedule(new TimerTask() {
                        @Override
                        public void run() {
                            boolean stop = reader.reader_StopInventory();
                            System.out.println("停止盘点:" + stop);
                        }
                    }, 5000);

                } else if (bh == 4) {

                    //  查询GPI
                    HashMap<Byte, Byte> gpiLis = reader.GetGpi();
                    for (Byte key : gpiLis.keySet()) {
                        System.out.println("gpi " + key + " 电平:" + gpiLis.get(key));
                    }
                } else if (bh == 5) {
                    /**
                     * 设置单个GPIO参数
                     */

                    if (reader != null && reader.getIsConnected()) {
                        HashMap<Byte, Byte> Gpomap = new HashMap<>();
                        Gpomap.put((byte) 1, (byte) 1);
                        Gpomap.put((byte) 2, (byte) 1);
                        if (reader.reader_SetGpo(Gpomap)) {
                            System.out.println("设置成功");
                            new Timer().schedule(new TimerTask() {
                                @Override
                                public void run() {
                                    HashMap<Byte, Byte> Gpomap = new HashMap<>();
                                    Gpomap.put((byte) 1, (byte) 0);
                                    reader.reader_SetGpo(Gpomap);
                                }
                            }, 2000);
                        } else {
                            System.out.println("设置失败");
                        }
                        do {
                            HashMap<Byte, Byte> gpiLis = reader.GetGpi();
                            Byte I1 = 1;
                            Byte gpi1 = gpiLis.get(I1);
                            Byte I2 = 2;
                            Byte gpi2 = gpiLis.get(I2);
                            if (gpi1 == 0 && gpi2 == 0) {
                                //关灯
                                HashMap<Byte, Byte> Gpo2map = new HashMap<>();
                                Gpo2map.put((byte) 2, (byte) 0);
                                reader.reader_SetGpo(Gpo2map);

                                //添加标签读取回调'

                                ScanTagParameter param = new ScanTagParameter();
                                param.antenna = 0x01;
                                param.isLoop = true;
                                boolean b = reader.reader_Inventory(param, (byte) 0x00);
                                System.out.println("开始盘点:" + b);
                                reader.OnTagDataReceivedHandle = (gateName, tagData) -> {
                                    if (tagData != null && tagData.getEPC() != null) {

                                        String s = Util.convertByteArrayToHexString(tagData.getEPC());
                                        System.out.println("读取到EPC:" + s);
                                    }
                                };
                                new Timer().schedule(new TimerTask() {
                                    @Override
                                    public void run() {
                                        boolean stop = reader.reader_StopInventory();

                                        System.out.println("停止盘点:" + stop);

                                    }
                                }, 2000);

                                break;

                            } else {
                                System.out.println("查询GPI状态中");
                            }

                        } while (true);


                    } else {
                        System.out.println("设备未连接·");
                    }
                } else if (bh == 6) {
                    //开锁、开灯
                    if (reader != null && reader.getIsConnected()) {
                        HashMap<Byte, Byte> Gpomap = new HashMap<>();
                        Gpomap.put((byte) 1, (byte) 1);
                        Gpomap.put((byte) 2, (byte) 1);
                        if (reader.reader_SetGpo(Gpomap)) {
                            System.out.println("设置成功");
                            new Timer().schedule(new TimerTask() {
                                @Override
                                public void run() {
                                    Gpomap.put((byte) 1, (byte) 0);
                                }
                            }, 2000);
                        } else {
                            System.out.println("设置失败");
                        }
                    } else {
                        System.out.println("设备未连接·");
                    }

                } else if (bh == 7) {
//                    ScanTagParameter param = new ScanTagParameter();
//                    param.antenna = 0x01;
//                    param.isLoop = true;
//                    //写标签测试,感应区一次只能写一个标签,如果没有扫瞄到标签,返回 -2;如果扫描到有多个标签,返回-1;如果参数不合法,返回-3;写入成功,返回 0
//                    System.out.println("请输入需要写入的EPC:");
//                    String Repc = input.next();
//                    int i = reader.writeEPC(param, Repc);
//                    if (i == 0) {
//                        System.out.println("标签写入成功");
//                    } else if (i == -1) {
//                        System.out.println("扫描到多张标签");
//                    } else if (i == -2) {
//                        System.out.println("未扫描到标签");
//                    } else {
//                        System.out.println("EPC输入不合格");
//                    }
//                    try {
//                        Thread.sleep(1000 * 2);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }


                } else if (bh == 8) {
                    // 读用户区
                    boolean b = reader.ScanUserData(0x01, true, 2);
                    System.out.println(b);

                    reader.OnTagDataReceivedHandle = (gateName, tagData) -> {
                        if (tagData != null && tagData.getUserData() != null) {
                            String s1 = Util.convertByteArrayToHexString(tagData.getUserData());
                            System.out.println("用户区:" + s1);
                        }
                    };
                    new Timer().schedule(new TimerTask() {
                        @Override
                        public void run() {
                            boolean stop = reader.reader_StopInventory();

                            System.out.println("停止盘点:" + stop);

                        }
                    }, 2000);

                } else if (bh == 9) {
                    // 写用户区
                    ScanTagParameter param = new ScanTagParameter();
                    param.antenna = 0x01;
                    param.isLoop = true;

                    int i = reader.writTagData(MemoryBank.UserMemory, param, "00001111");
                    System.out.println("写入用户区:" + i);
                } else if (bh == 10) {
                    // 查询盘点模式
                    reader.getEpcBasebandParameter();

                } else if (bh == 11) {
                    reader.getIsConnected();
                }
            }
        } else {
            System.out.println("连接失败...");
        }


    }

}