RfidAction.java
4.41 KB
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
package org.streamparty.remoterfid;
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RfidAction {
private SerialPort opened;
private InputStream istream;
private OutputStream ostream;
private static final Logger logger = LoggerFactory.getLogger(RfidAction.class);
private static RfidAction thisElement = null;
public static RfidAction getInstance() {
return thisElement;
}
private RfidAction(CommPortIdentifier portId) {
try {
logger.debug("Opening port " + portId.getName());
opened = (SerialPort) portId.open("Stream cashier", 100);
opened.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
istream = opened.getInputStream();
ostream = opened.getOutputStream();
} catch (PortInUseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedCommOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String listenPort() {
// Tyhjennetn mahdollinen bufferi
logger.info("Tyhjennetn bufferi");
try {
while (istream.available() != 0) {
readChar();
}
} catch (IOException e) {
}
logger.info("Starting to read");
String tag = "";
// logger.debug((int)'\n');
try {
logger.info("Starting read loop");
JButton close = new JButton("Close");
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
JDialog readingDiag = new JDialog();
JPanel readingLabel = new JPanel(new GridLayout(1, 1));
readingLabel.add(new JLabel("Insert tag"));
readingDiag.add(readingLabel);
readingDiag.add(close);
// readingDiag.setAlwaysOnTop(true);
readingDiag.setSize(200, 200);
readingDiag.setVisible(true);
// JButton okLabelButton = new JButton("Ok");
/*
* okLabelButton.addActionListener(new ActionListener(){ public void
* actionPerformed(ActionEvent arg0) {
* ((JDialog)((JPanel)((JButton)arg0
* .getSource()).getParent()).getParent()).dispose(); }});
*/
while (true) {
if (opened.isCTS()) {
ostream.write('U');
logger.info("Wrote U to reader");
Thread.sleep(300);
if (istream.available() < 1) {
logger.info("istream size < 1");
continue;
}
String rresponse = Integer.toHexString(istream.read());
logger.info("rresponse: " + rresponse);
if (rresponse.equals("86") || rresponse.toUpperCase().equals("A6")) {
logger.info("Found valid code");
List<String> hexVals = new ArrayList<String>();
Collections.reverse(hexVals);
while (istream.available() > 0) {
String val = Integer.toHexString(istream.read());
if (val.length() < 2) {
val = "0" + val;
}
hexVals.add(val.toUpperCase());
}
hexVals.add("00");
Collections.reverse(hexVals);
Iterator<String> iter = hexVals.iterator();
tag = "";
while (iter.hasNext()) {
tag += iter.next();
}
readingDiag.dispose();
break;
}
} else {
// logger.info("CTS not open. waiting...");
// Thread.sleep(50);
}
}
} catch (IOException e) {
logger.warn("Tag reading error: ", e);
} catch (InterruptedException e) {
logger.info("Tag reading interrupted ", e);
}
logger.debug("Read tag " + tag);
return tag.trim();
}
private int readChar() {
int merkki = -1;
try {
if (istream.available() > 0) {
merkki = istream.read();
}
} catch (IOException e) {
logger.debug("Char reading error", e);
}
return merkki;
}
public static RfidAction init(CommPortIdentifier portId) {
logger.info("Initializing rfid reader for port: " + portId.getName());
thisElement = new RfidAction(portId);
return thisElement;
}
}