Dépôt officiel du code source de l'ERP OpenConcerto
Rev 132 | Blame | Compare with Previous | Last modification | View Log | RSS feed
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
*
* The contents of this file are subject to the terms of the GNU General Public License Version 3
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each file.
*/
package org.openconcerto.erp.core.sales.pos.io;
import org.openconcerto.erp.core.sales.pos.io.ConcertProtocol.ConcertStateListener.States;
import org.openconcerto.utils.StringUtils;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.util.HexDump;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
public class ConcertProtocol {
public interface ConcertStateListener {
enum States {
ERROR, CONNECTED, WAITING, OK, NOK
}
void stateChanged(States state);
}
// Message start
private static final char STX = (char) 0x02;
// Message end
private static final char ETX = (char) 0x03;
// End of transmission
private static final char EOT = (char) 0x04;
// Start of transmission
private static final char ENQ = (char) 0x05;
// Positive ACK
private static final char ACK = (char) 0x06;
// Negative ACK
private static final char NACK = (char) 0x15;
// Mode
private static final char MODE_CARD = '1';
// Type
private static final char TYPE_BUY = '0';
// Currency
public static final String CURRENCY_EUR = "978";
private String port;
private List<ConcertStateListener> listeners = new ArrayList<>();
public ConcertProtocol(String port) {
this.port = port;
}
public void addStateListener(ConcertStateListener listener) {
listeners.add(listener);
}
private void fireStateChanged(States connected) {
for (ConcertStateListener l : listeners)
l.stateChanged(connected);
}
public synchronized boolean sendCardPayment(int amountInCents, String currency) throws Exception {
if (currency == null) {
currency = CURRENCY_EUR;
}
return sendPrototolE(1, amountInCents, true, MODE_CARD, TYPE_BUY, currency, "OpenConcerto");
}
public boolean sendPrototolE(int posIndex, int amountInCents, boolean requireResponse, char mode, char type, String currency, String string) throws Exception {
boolean result = false;
if (posIndex > 99 || posIndex < 0) {
fireStateChanged(ConcertStateListener.States.ERROR);
throw new IllegalArgumentException("Pos index must be between 0 and 99");
}
if (amountInCents < 0) {
fireStateChanged(ConcertStateListener.States.ERROR);
throw new IllegalArgumentException("Amount must be positive");
}
if (currency.length() != 3) {
fireStateChanged(ConcertStateListener.States.ERROR);
throw new IllegalArgumentException("Bad currency code : " + currency);
}
final OutputStream out;
InputStream in;
final SerialPort serialPort;
if (isSerialPort()) {
serialPort = getSerialPort();
out = serialPort.getOutputStream();
in = serialPort.getInputStream();
} else {
serialPort = null;
in = new FileInputStream(this.port);
if (in.available() > 0) {
byte[] buffer = new byte[512];
in.read(buffer);
in.close();
}
in = new FileInputStream(this.port);
out = new FileOutputStream(this.port);
}
out.write(ENQ);
byte[] buffer = new byte[512];
int nbRead = in.read(buffer);
if (nbRead != 1 || buffer[0] != ACK) {
String r = StringUtils.bytesToHexString(buffer, nbRead);
in.close();
out.close();
if (serialPort != null) {
serialPort.close();
}
fireStateChanged(ConcertStateListener.States.ERROR);
throw new IllegalStateException("Bad response received : " + r);
}
fireStateChanged(ConcertStateListener.States.CONNECTED);
//
final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
bOut.write(STX);
// POS Index
String n = rightAlign(posIndex, 2, '0');
bOut.write(n.getBytes());
// Amount in cents
bOut.write(rightAlign(amountInCents, 8, '0').getBytes());
if (requireResponse) {
bOut.write('1');
} else {
bOut.write('0');
}
// Mode & type
bOut.write(mode);
bOut.write(type);
// Currency
bOut.write(currency.getBytes());
// Text
bOut.write(leftAlign(string, 10, ' ').getBytes());
bOut.write(ETX);
byte b = getLrc(bOut.toByteArray());
bOut.write(b);
out.write(bOut.toByteArray());
// READ ACK
nbRead = in.read(buffer);
if (nbRead != 1 || buffer[0] != ACK) {
String r = StringUtils.bytesToHexString(buffer, nbRead);
in.close();
out.close();
if (serialPort != null) {
serialPort.close();
}
fireStateChanged(ConcertStateListener.States.ERROR);
throw new IllegalStateException("Bad response received : " + nbRead + ": " + r);
}
// END
out.write(EOT);
fireStateChanged(ConcertStateListener.States.WAITING);
// Wait reply
int count = 0;
final int maxCount = 60 * 5;
while (count < maxCount) {
nbRead = in.read(buffer);
if (nbRead > 0) {
if (buffer[0] == ENQ) {
out.write(ACK);
} else if (buffer[0] == STX) {
if (buffer[3] == '0') {
result = true;
out.write(ACK);
count = maxCount;
fireStateChanged(ConcertStateListener.States.OK);
} else if (buffer[3] == '7') {
out.write(NACK);
count = maxCount;
fireStateChanged(ConcertStateListener.States.NOK);
}
} else if (buffer[0] == EOT) {
count = maxCount;
}
}
Thread.sleep(200);
count++;
}
out.close();
in.close();
if (serialPort != null) {
serialPort.close();
}
return result;
}
private byte getLrc(byte[] bytes) {
byte lrc = (byte) 0x0;
for (int i = 1; i < bytes.length; i++) {
lrc ^= bytes[i];
}
return lrc;
}
private boolean isSerialPort() {
return (this.port.startsWith("/dev") || this.port.startsWith("COM"));
}
private SerialPort getSerialPort() throws Exception {
if (port == null || port.length() == 0) {
throw new IllegalStateException("Invalid serial port name: " + port);
}
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(port);
if (portIdentifier.isCurrentlyOwned()) {
throw new IllegalAccessError("Port " + port + " is currently in use");
}
CommPort commPort = portIdentifier.open("CommUtil", 2000);
if (!(commPort instanceof SerialPort)) {
throw new IllegalStateException("Invalid serial port: " + port);
}
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
return serialPort;
}
private static String rightAlign(int value, int maxWidth, char fillChar) {
String r = String.valueOf(value);
if (r.length() > maxWidth) {
return r.substring(0, maxWidth);
}
int n = maxWidth - r.length();
for (int i = 0; i < n; i++) {
r = fillChar + r;
}
return r;
}
private static String leftAlign(String r, int maxWidth, char fillChar) {
if (r.length() > maxWidth) {
return r.substring(0, maxWidth);
}
int n = maxWidth - r.length();
for (int i = 0; i < n; i++) {
r += fillChar;
}
return r;
}
public static void main(String[] args) throws Exception {
ConcertProtocol p = new ConcertProtocol("COM9");
p.sendCardPayment(4579, ConcertProtocol.CURRENCY_EUR);
}
}