senzhi
2026-01-21 3b1b756f1a619e60b2243d28817c39e0a7a0cb0c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package com.eternal.rfid.service;
 
import com.eternal.event.RfidReadEvent;
import com.eternal.monitor.vo.DeviceStatusVo;
import com.rfid.CReader;
import com.rfid.ReadTag;
import com.rfid.ReaderParameter;
import com.rfid.TagCallback;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import com.eternal.rfid.params.UF3ReaderConfig;
import org.springframework.context.ApplicationEvent;
 
import java.util.function.Consumer;
 
@Slf4j
public class UF3RFIDReader implements TagCallback {
    @Getter
    private final String readerKey;
 
    @Getter
    private final String readerTypeKey;
 
    @Getter
    private final String[] antennasKey;
 
    @Getter
    private final String antennaTypeKey;
 
    @Getter
    private int index;
 
    private UF3ReaderConfig props;
    private CReader reader;
 
    @Getter
    private String ip;
    private int port;
    private int readerType;
    private int readerLogOn_Off;
    private String mode;
 
    @Setter
    private Consumer<ApplicationEvent> eventConsumer;
 
    @Setter
    private Consumer<DeviceStatusVo> updateDeviceStatusConsumer;
 
    public UF3RFIDReader(String readerKey, String readerTypeKey, String[] antennasKey, String antennaTypeKey) {
        this.readerKey = readerKey;
        this.readerTypeKey = readerTypeKey;
        this.antennasKey = antennasKey;
        this.antennaTypeKey = antennaTypeKey;
    }
 
    public void init(int index, UF3ReaderConfig params) {
        this.index = index;
        this.ip = params.getIp();
        this.port = params.getPort();
        this.readerType = params.getReadType();
        this.readerLogOn_Off = params.getReadLogOn_Off();
        this.mode = params.getMode();
        this.props = params;
    }
 
 
    public void start() {
        log.info("ip:{},port:{},readerType:{},log:{}", ip, port, readerType, readerLogOn_Off);
        reader = new CReader(ip, port, readerType, readerLogOn_Off);
 
        int conn = reader.Connect();
        if (conn != 0) {
            log.error("连接UF3设备失败,返回码={}", conn);
            return;
        }
        log.info("UF3 Reader设备连接成功:{}:{}", ip, props.getPort());
 
        //0x00关闭蜂鸣器,0x01打开蜂鸣器
        reader.SetBeepNotification(0x00);
 
        ReaderParameter param = new ReaderParameter();
        try {
            param.SetAntenna(0x09);
            param.SetReadType(0);
            reader.SetInventoryParameter(param);
        } catch (Exception e) {
            log.warn("设置读参数时发生异常:{}", e.getMessage());
        }
 
        applyPowerConfig();
 
        reader.SetCallBack(this);
        startRead();
    }
 
    public void startRead() {
        int start = reader.StartRead();
        if (start != 0) {
            log.error("开启读失败,返回码={}", start);
        } else {
            getAntennaPowers();
            log.info("读流程已开启");
        }
    }
 
    public void stopRead() {
        if (reader != null) {
            try {
                reader.StopRead();
            } catch (Exception e) {
                log.warn("停止读过程异常:{}", e.getMessage());
            }
        }
    }
 
    public void disconnectReader() {
        if (reader != null) {
 
            try {
                reader.DisConnect();
            } catch (Exception e) {
                log.warn("断开连接异常:{}", e.getMessage());
            }
            log.info("已停止并断开UF3设备连接");
        }
    }
 
    public boolean isOnline() {
        return reader != null;
    }
 
    @Override
    public void tagCallback(ReadTag tag) {
        if (tag == null) {
            return;
        }
        String memoryData = "";
        if (mode.equals("user")) {
            memoryData = readDataByEpcMask(tag.epcId, (byte) 3, (byte) 0000, (byte) 32, "00000000");
        } else if (mode.equals("tid")) {
            memoryData = readDataByEpcMask(tag.epcId, (byte) 2, (byte) 0000, (byte) 32, "00000000");
        }
        long timeInReader = System.currentTimeMillis();
        long timeInPc = System.currentTimeMillis();
        int ant = tag.antId;
        String epc = tag.epcId;
        float rssi = tag.rssi;
        double phase = 0;
        int doppler = 0;
 
        RfidReadEvent rfidReadEvent = null;
        if (mode.equals("tid")) {
            rfidReadEvent = new RfidReadEvent(this, timeInReader, timeInPc, ant, epc, rssi, phase, doppler, 0L, 0, "", memoryData);
        } else if (mode.equals("user")) {
            rfidReadEvent = new RfidReadEvent(this, timeInReader, timeInPc, ant, epc, rssi, phase, doppler, 0L, 0, memoryData, "");
        } else {
            rfidReadEvent = new RfidReadEvent(this, timeInReader, timeInPc, ant, epc, rssi, phase, doppler, 0L, 0, "", "");
        }
 
        publishEvent(rfidReadEvent);
 
        if (props.isRfidReaderReadLog()) {
            log.info("标签: ip={} epc={} mem={} rssi={} ant={}",
                    tag.ipAddr, tag.epcId, tag.memId, tag.rssi, tag.antId);
            if (!memoryData.isEmpty()) {
                log.info("readDataByEpcMask 数据:{}", memoryData);
            }
        }
    }
 
    @Override
    public void StopReadCallback() {
        log.info("设备已停止读回调");
    }
 
    private void applyPowerConfig() {
        String[] powers = props.getAntennasPower().split(",");
        if (powers != null && powers.length >= 4) {
            byte[] byAnt = new byte[4];
            for (int i = 0; i < 4; i++) {
                int p = Integer.parseInt(powers[i]);
                if (p < 0) p = 0;
                if (p > 33) p = 33;
                byAnt[i] = (byte) p;
            }
            int ret = reader.SetRfPowerByAnt(byAnt);
            if (ret == 0) {
                log.info("已设置四天线功率: A1={} A2={} A3={} A4={}", byAnt[0] & 0xFF, byAnt[1] & 0xFF, byAnt[2] & 0xFF, byAnt[3] & 0xFF);
            } else {
                log.warn("按天线设置功率失败,返回码={}", ret);
            }
        }
    }
 
    /**
     * EPC掩码读取
     *
     * @param epc       EPC数据 (Mask)
     * @param memBank   存储区 (0:Reserved, 1:EPC, 2:TID, 3:User)
     * @param startAddr 起始地址 (Word)  起始地址都是从 0000 开始
     * @param length    读取长度 (Word)
     * @param password  访问密码 (8位16进制字符串)
     * @return 读取到的数据
     */
    public String readDataByEpcMask(String epc, byte memBank, byte startAddr, byte length, String password) {
        if (reader == null) {
            log.error("Reader未初始化");
            return null;
        }
        try {
            // 注意:调用此方法可能需要先停止自动读(StartRead),取决于SDK实现。
            // 如果是在Inventory过程中调用,可能会失败或阻塞。
            String data = reader.ReadDataByEPC(epc, memBank, startAddr, length, password);
//            log.info("EPC掩码读取结果: epc={} mem={} addr={} len={} data={}", epc, memBank, startAddr, length, data);
            return data;
        } catch (Exception e) {
            log.error("EPC掩码读取异常", e);
            return null;
        }
    }
 
    /**
     * TID掩码读取
     *
     * @param tid       TID数据 (Mask)
     * @param memBank   存储区 (0:Reserved, 1:EPC, 2:TID, 3:User)
     * @param startAddr 起始地址 (Word)
     * @param length    读取长度 (Word)
     * @param password  访问密码 (8位16进制字符串)
     * @return 读取到的数据
     */
    public String readDataByTidMask(String tid, byte memBank, byte startAddr, byte length, String password) {
        if (reader == null) {
            log.error("Reader未初始化");
            return null;
        }
        try {
            String data = reader.ReadDataByTID(tid, memBank, startAddr, length, password);
            log.info("TID掩码读取结果: tid={} mem={} addr={} len={} data={}", tid, memBank, startAddr, length, data);
            return data;
        } catch (Exception e) {
            log.error("TID掩码读取异常", e);
            return null;
        }
    }
 
    public int[] getAntennaPowers() {
        if (reader == null) {
            log.error("Reader未初始化");
            return null;
        }
        byte[] byAnt = new byte[4];
        int ret = reader.GetRfPowerByAnt(byAnt);
        if (ret != 0) {
            log.warn("查询天线功率失败,返回码={}", ret);
            return null;
        }
        int[] powers = new int[4];
        for (int i = 0; i < 4; i++) {
            powers[i] = byAnt[i] & 0xFF;
        }
        log.info("当前四天线功率: A1={} A2={} A3={} A4={}", powers[0], powers[1], powers[2], powers[3]);
        return powers;
    }
 
    private void publishEvent(ApplicationEvent event) {
        if (eventConsumer != null) {
            eventConsumer.accept(event);
        } else {
            log.error("{}", "No eventConsumer");
        }
    }
 
    protected void updateDeviceStatus(DeviceStatusVo deviceStatusVo) {
        if (updateDeviceStatusConsumer != null) {
            updateDeviceStatusConsumer.accept(deviceStatusVo);
        } else {
            log.warn("updateDeviceStatusConsumer is null");
        }
    }
}