first commit
This commit is contained in:
commit
4d332ef662
27586 changed files with 3281783 additions and 0 deletions
128
rus/WEB-INF/lib/abliaSerial_src/com/ablia/cash/CashClient.java
Normal file
128
rus/WEB-INF/lib/abliaSerial_src/com/ablia/cash/CashClient.java
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
package com.ablia.cash;
|
||||
|
||||
import com.ablia.util.Debug;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
public class CashClient extends Debug {
|
||||
private int cashServerPort;
|
||||
|
||||
private String cashServerHost;
|
||||
|
||||
private Socket socket;
|
||||
|
||||
public static final String P_CASH_CLIENT_LOG_FILE = "CASH_CLIENT_LOG_FILE";
|
||||
|
||||
public CashClient(String l_cashServerHost, int l_cashServerPort) {
|
||||
setCashServerHost(l_cashServerHost);
|
||||
setCashServerPort(l_cashServerPort);
|
||||
}
|
||||
|
||||
public String sendCommand(String theCommand, String parms) {
|
||||
try {
|
||||
Socket theSocket = getSocket();
|
||||
if (theSocket == null)
|
||||
return "Errore! Server not ready";
|
||||
BufferedReader red = new BufferedReader(new InputStreamReader(
|
||||
theSocket.getInputStream()));
|
||||
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(
|
||||
theSocket.getOutputStream()));
|
||||
String commandString = theCommand;
|
||||
if (parms != null && !parms.equals(""))
|
||||
commandString = String.valueOf(commandString) + " " + parms;
|
||||
wr.write(commandString);
|
||||
wr.write("\n");
|
||||
wr.flush();
|
||||
StringBuffer sb = new StringBuffer();
|
||||
String str;
|
||||
while (!(str = red.readLine()).equals("##END##")) {
|
||||
sb.append(str);
|
||||
sb.append("\n");
|
||||
System.out.println(String.valueOf(str) + str.length());
|
||||
System.out.println("##END##" + "##END##".length());
|
||||
}
|
||||
handleDebug("Response from server: " + sb.toString());
|
||||
return sb.toString();
|
||||
} catch (Exception e) {
|
||||
handleDebug(e);
|
||||
return "Error! " + e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public int getCashServerPort() {
|
||||
return this.cashServerPort;
|
||||
}
|
||||
|
||||
public void setCashServerPort(int cashServerPort) {
|
||||
this.cashServerPort = cashServerPort;
|
||||
}
|
||||
|
||||
public String getCashServerHost() {
|
||||
return (this.cashServerHost == null) ? "" : this.cashServerHost;
|
||||
}
|
||||
|
||||
public void setCashServerHost(String cashServerHost) {
|
||||
this.cashServerHost = cashServerHost;
|
||||
}
|
||||
|
||||
public Socket getSocket() {
|
||||
if (this.socket == null)
|
||||
try {
|
||||
System.out.println("Connecting to cash server " +
|
||||
getCashServerHost() + ":" + getCashServerPort() +
|
||||
" .....");
|
||||
InetAddress addr = InetAddress.getByName(getCashServerHost());
|
||||
this.socket = new Socket(addr, getCashServerPort());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return this.socket;
|
||||
}
|
||||
|
||||
public void setSocket(Socket socket) {
|
||||
this.socket = socket;
|
||||
}
|
||||
|
||||
public boolean closeConnection() {
|
||||
if (this.socket != null) {
|
||||
try {
|
||||
String res = sendCommand("CLOSE", null);
|
||||
handleDebug(res, 1);
|
||||
this.socket.close();
|
||||
this.socket = null;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
handleDebug("Error! Attempting to close a null socket",
|
||||
2);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public String getDebugFile() {
|
||||
return getLogFile();
|
||||
}
|
||||
|
||||
public int getDebugLevel() {
|
||||
return new Integer(getResource("DEBUG_LEVEL"))
|
||||
.intValue();
|
||||
}
|
||||
|
||||
public String getLogFile() {
|
||||
return getResource("CASH_CLIENT_LOG_FILE");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
CashClient cc = new CashClient("192.168.1.80", 444);
|
||||
String res = cc.sendCommand("SEND", "KXCL|KXSEPRE 2/2009|KXCBSDFSADFASDF|KXSEDSFSADFADSFSAF|KXCE|");
|
||||
System.out.println(res);
|
||||
cc.closeConnection();
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package com.ablia.cash;
|
||||
|
||||
import com.ablia.serial.SerialPortWriterInterface;
|
||||
import com.ablia.serial.SerialServer;
|
||||
|
||||
public class CashServer extends SerialServer {
|
||||
protected SerialPortWriterInterface getSerialWriter() {
|
||||
return ElegantWriter.getInstance(getResource("SERIAL_COM_PORT"),
|
||||
getResourceLong("COM_DELAY_START"),
|
||||
getResourceLong("COM_DELAY_CMD"),
|
||||
getResourceLong("COM_DELAY_END"));
|
||||
}
|
||||
|
||||
public String getPropertyFileName() {
|
||||
return "cash";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {}
|
||||
}
|
||||
|
|
@ -0,0 +1,227 @@
|
|||
package com.ablia.cash;
|
||||
|
||||
import com.ablia.serial.SerialPortWriterInterface;
|
||||
import com.ablia.util.Debug;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public class CashServerOld extends Debug {
|
||||
public static final String CMD_CLEAR_LOG_FILE = "CLLOG";
|
||||
|
||||
public static final String CMD_SEND = "SEND";
|
||||
|
||||
public static final String CMD_GET_LOG_FILE = "GTLOG";
|
||||
|
||||
public static final String EOM = "##END##";
|
||||
|
||||
class CommServer extends Thread {
|
||||
private static final String nl = "\r\n";
|
||||
|
||||
private final Socket socket;
|
||||
|
||||
final CashServerOld this$0;
|
||||
|
||||
public CommServer(CashServerOld this$0, Socket l_socket) {
|
||||
this.this$0 = this$0;
|
||||
this.socket = l_socket;
|
||||
start();
|
||||
}
|
||||
|
||||
private String getCommand(String cmd) {
|
||||
return cmd.substring(Math.min(0, cmd.length()),
|
||||
Math.min(5, cmd.length())).toUpperCase().trim();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
String remoteIp = this.socket.getInetAddress().toString();
|
||||
try {
|
||||
System.out.println("Start connection with " + remoteIp);
|
||||
BufferedReader rd = new BufferedReader(new InputStreamReader(
|
||||
this.socket.getInputStream()));
|
||||
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
|
||||
this.socket.getOutputStream()));
|
||||
int i = 0;
|
||||
String str;
|
||||
while (this.socket.isConnected() && (str = rd.readLine()) != null) {
|
||||
this.this$0.handleDebug("\nReceived: " + str, 1);
|
||||
if (getCommand(str).equals("GTLOG")) {
|
||||
if (new File(this.this$0.getLogFile()).exists()) {
|
||||
BufferedReader bufferedreader = new BufferedReader(
|
||||
new FileReader(this.this$0.getLogFile()));
|
||||
String theLine;
|
||||
while ((theLine = bufferedreader.readLine()) != null) {
|
||||
bw.write(theLine);
|
||||
bw.write("\r\n");
|
||||
}
|
||||
bufferedreader.close();
|
||||
} else {
|
||||
bw.write("File " + this.this$0.getLogFile() + " non trovato.");
|
||||
}
|
||||
bw.write("##END##");
|
||||
bw.write("\r\n");
|
||||
bw.flush();
|
||||
} else {
|
||||
if (getCommand(str).equals("CLOSE")) {
|
||||
bw.write("Closing the socket");
|
||||
bw.write("\r\n");
|
||||
bw.write("##END##");
|
||||
bw.write("\r\n");
|
||||
bw.flush();
|
||||
this.socket.close();
|
||||
break;
|
||||
}
|
||||
if (getCommand(str).equals("CLLOG")) {
|
||||
new File(this.this$0.getLogFile()).delete();
|
||||
bw.write("Log file deleted.");
|
||||
bw.write("\r\n");
|
||||
bw.write("##END##");
|
||||
bw.write("\r\n");
|
||||
bw.flush();
|
||||
} else if (getCommand(str).equals("SEND")) {
|
||||
String cashString = str.substring("SEND".length())
|
||||
.trim();
|
||||
bw.write(this.this$0.inviaStringa(cashString));
|
||||
bw.write("\r\n");
|
||||
bw.write("##END##");
|
||||
bw.write("\r\n");
|
||||
bw.flush();
|
||||
} else if (!getCommand(str).equals(
|
||||
"")) {
|
||||
bw.write("Error! Command " + str + " unknow");
|
||||
bw.write("\r\nValid Command:");
|
||||
bw.write("\r\nGet log file: GTLOG");
|
||||
bw.write("\r\nClear log file: CLLOG");
|
||||
bw.write("\r\nSend string to cash: SEND");
|
||||
bw.write("\r\n");
|
||||
bw.write("##END##");
|
||||
bw.write("\r\n");
|
||||
bw.flush();
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
this.this$0.handleDebug(e);
|
||||
} finally {
|
||||
try {
|
||||
if (this.socket != null)
|
||||
this.socket.close();
|
||||
} catch (IOException ioe) {
|
||||
this.this$0.handleDebug(ioe);
|
||||
}
|
||||
}
|
||||
System.out.println("socket close with " + remoteIp);
|
||||
}
|
||||
}
|
||||
|
||||
int logLevel = 0;
|
||||
|
||||
private ResourceBundle propertyResource;
|
||||
|
||||
public static final String SEP = "|";
|
||||
|
||||
public static final String P_CASH_SERVER_PORT = "CASH_SERVER_PORT";
|
||||
|
||||
public static final String CMD_CLOSE = "CLOSE";
|
||||
|
||||
public static final String P_CASH_SERVER_LOG_FILE = "CASH_SERVER_LOG_FILE";
|
||||
|
||||
public static final String P_CASH_SERVER_HOSTNAME = "CASH_SERVER_HOSTNAME";
|
||||
|
||||
public static final String P_CASH_COM_PORT = "CASH_COM_PORT";
|
||||
|
||||
public static void main(String[] args) {}
|
||||
|
||||
public CashServerOld() {
|
||||
startServer2();
|
||||
}
|
||||
|
||||
public String getLogFile() {
|
||||
return getResource("CASH_SERVER_LOG_FILE");
|
||||
}
|
||||
|
||||
public int getDebugLevel() {
|
||||
return new Integer(getResource("DEBUG_LEVEL"))
|
||||
.intValue();
|
||||
}
|
||||
|
||||
public String getPropertyFileName() {
|
||||
return "cash";
|
||||
}
|
||||
|
||||
public int getServerPort() {
|
||||
if (getResource("CASH_SERVER_PORT").equals(
|
||||
""))
|
||||
return 4444;
|
||||
return Integer.parseInt(getResource("CASH_SERVER_PORT"));
|
||||
}
|
||||
|
||||
public boolean getDebug() {
|
||||
return true;
|
||||
}
|
||||
|
||||
private ResourceBundle getPropertyResource() {
|
||||
try {
|
||||
if (this.propertyResource == null)
|
||||
this.propertyResource =
|
||||
ResourceBundle.getBundle(getPropertyFileName());
|
||||
} catch (Exception e) {
|
||||
System.out.println("ERROR!! ApplParm.getPropertyResource() ");
|
||||
e.printStackTrace();
|
||||
}
|
||||
return this.propertyResource;
|
||||
}
|
||||
|
||||
public String getResource(String key) {
|
||||
try {
|
||||
return getPropertyResource().getString(key);
|
||||
} catch (Exception e) {
|
||||
handleDebug("CashServer: " + e.getMessage() + " . Property file:" +
|
||||
getPropertyFileName(), 1);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public String inviaStringa(String strMsg) {
|
||||
SerialPortWriterInterface spw = ElegantWriter.getInstance(getResource("CASH_COM_PORT"), 1L, 1L, 1L);
|
||||
return spw.sendToSerial(strMsg);
|
||||
}
|
||||
|
||||
public String getDebugFile() {
|
||||
return getLogFile();
|
||||
}
|
||||
|
||||
private void startServer2() {
|
||||
ServerSocket srv = null;
|
||||
Socket socket = null;
|
||||
try {
|
||||
int port = getServerPort();
|
||||
srv = new ServerSocket(port);
|
||||
while (true) {
|
||||
handleDebug("Cash Server waiting for command...", 1);
|
||||
socket = srv.accept();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
handleDebug(e);
|
||||
} finally {
|
||||
try {
|
||||
if (socket != null) {
|
||||
socket.close();
|
||||
srv.close();
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
handleDebug(ioe);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
package com.ablia.cash;
|
||||
|
||||
import com.ablia.serial.SerialPortWriter;
|
||||
import com.ablia.serial.SerialPortWriterInterface;
|
||||
|
||||
public class ElegantWriter extends SerialPortWriter implements SerialPortWriterInterface {
|
||||
public static final String RES_FINE_CARTA = "DE2";
|
||||
|
||||
public static final String RES_ERRORE_STRING = "DE69";
|
||||
|
||||
public static final String RES_SEQUENZA_ERRATA = "DE6";
|
||||
|
||||
public static final String RES_OK = "";
|
||||
|
||||
public ElegantWriter() {}
|
||||
|
||||
public ElegantWriter(String portName, long l_comDelayStart, long l_comDelayCmd, long l_comDelayEnd) {
|
||||
super(portName, l_comDelayCmd, l_comDelayCmd, l_comDelayEnd);
|
||||
}
|
||||
|
||||
public String getResponseMessage(String l_res) {
|
||||
l_res = l_res.trim();
|
||||
if (l_res.equals("DE69"))
|
||||
return "Errore stringa dati";
|
||||
if (l_res.equals("DE6"))
|
||||
return "Errore: sequenza dati errata";
|
||||
if (l_res.equals("DE2"))
|
||||
return "Errore! Fine Carta";
|
||||
if (l_res.equals(""))
|
||||
return "OK";
|
||||
return "?? " + l_res;
|
||||
}
|
||||
|
||||
public static synchronized SerialPortWriterInterface getInstance(String portName, long l_comDelayStart, long l_comDelayCmd, long l_comDelayEnd) {
|
||||
String cpKey = portName;
|
||||
if (htSerialPortWriter.containsKey(cpKey))
|
||||
return (SerialPortWriterInterface)htSerialPortWriter.get(cpKey);
|
||||
SerialPortWriterInterface sp = getNewWriterInstance(portName,
|
||||
l_comDelayStart, l_comDelayCmd, l_comDelayEnd);
|
||||
htSerialPortWriter.put(cpKey, sp);
|
||||
return sp;
|
||||
}
|
||||
|
||||
protected static SerialPortWriterInterface getNewWriterInstance(String l_portName, long l_comDelayStart, long l_comDelayCmd, long l_comDelayEnd) {
|
||||
return new ElegantWriter(l_portName, l_comDelayStart, l_comDelayCmd,
|
||||
l_comDelayEnd);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue