first commit

This commit is contained in:
MaddoScientisto 2026-03-14 20:04:39 +01:00
commit 4d332ef662
27586 changed files with 3281783 additions and 0 deletions

View 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);
}
}

View file

@ -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) {}
}

View file

@ -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);
}
}
}
}

View file

@ -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);
}
}

View file

@ -0,0 +1,168 @@
package com.ablia.serial;
import com.ablia.util.Debug;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
public class SerialClient extends Debug {
private int serialServerPort;
private String serialServerHost;
private Socket socket;
private StringBuffer serialSendCommand;
public static final String P_CLIENT_LOG_FILE = "SERIAL_CLIENT_LOG_FILE";
public SerialClient(String l_cashServerHost, int l_cashServerPort) {
setSerialServerHost(l_cashServerHost);
setSerialServerPort(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.replace("\n", "|");
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();
} finally {
try {
if (getSocket() != null) {
handleDebug("Clossing socket...", 1);
getSocket().close();
}
} catch (IOException ioe) {
handleDebug(ioe);
}
setSocket(null);
}
}
public int getSerialServerPort() {
return this.serialServerPort;
}
public void setSerialServerPort(int cashServerPort) {
this.serialServerPort = cashServerPort;
}
public String getSerialServerHost() {
return (this.serialServerHost == null) ? "" : this.serialServerHost;
}
public void setSerialServerHost(String cashServerHost) {
this.serialServerHost = cashServerHost;
}
public Socket getSocket() {
if (this.socket == null)
try {
System.out.println("Connecting to serial server " +
getSerialServerHost() + ":" + getSerialServerPort() +
" .....");
InetAddress addr = InetAddress.getByName(getSerialServerHost());
this.socket = new Socket(addr, getSerialServerPort());
} 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("SERIAL_CLIENT_LOG_FILE");
}
public void addSerialSendCommand(String aCommand) {
getSerialSendCommand().append(aCommand);
getSerialSendCommand().append("|");
}
public StringBuffer getSerialSendCommand() {
if (this.serialSendCommand == null)
this.serialSendCommand = new StringBuffer();
return this.serialSendCommand;
}
public void resetSerialSendCommand() {
setSerialSendCommand(null);
}
public void setSerialSendCommand(StringBuffer stringCommand) {
this.serialSendCommand = stringCommand;
}
public String sendSerialSendCommand() {
return sendCommand("SEND", getSerialSendCommand()
.toString());
}
public static void main(String[] args) {
SerialClient cc = new SerialClient("localhost", 441);
String res = cc.sendCommand("SEND",
"KXCL|KXSEPRE 2/2009|KXCBSDFSADFASDF|KXSEDSFSADFADSFSAF|KXCE");
System.out.println(res);
cc.closeConnection();
System.exit(0);
}
}

View file

@ -0,0 +1,201 @@
package com.ablia.serial;
import com.ablia.util.StringTokenizer;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
public abstract class SerialPortWriter implements SerialPortEventListener {
private String portName;
private CommPortIdentifier portId;
private SerialPort serialPort;
private InputStreamReader inputStream;
protected Thread readThread;
protected SerialPortWriter(String portName, long l_comDelayStart, long l_comDelayCmd, long l_comDelayEnd) {
setPortName(portName);
setComDelayStart(l_comDelayStart);
setComDelayCmd(l_comDelayCmd);
setComDelayEnd(l_comDelayEnd);
}
private long comDelayStart = 1100L;
private long comDelayCmd = 100L;
private long comDelayEnd = 1000L;
protected static Hashtable htSerialPortWriter = new Hashtable();
protected SerialPortWriter() {}
public String getPortName() {
return this.portName;
}
public void setPortName(String portName) {
this.portName = portName;
}
public CommPortIdentifier getPortId() {
if (this.portId == null) {
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
boolean portNotFound = true;
while (portList.hasMoreElements() && portNotFound) {
this.portId = (CommPortIdentifier)portList.nextElement();
if (this.portId.getPortType() == 1 &&
this.portId.getName().equals(getPortName())) {
System.out.println("Found port: " + getPortName());
portNotFound = false;
}
}
if (portNotFound)
System.out.println("port " + getPortName() + " not found.");
}
return this.portId;
}
public SerialPort getSerialPort() {
if (this.serialPort == null)
try {
this.serialPort = (SerialPort)getPortId().open("SimpleWrite", 2000);
} catch (Exception e) {
System.out.println("Port in use." + e.getMessage());
}
return this.serialPort;
}
public void setSerialPort(SerialPort l_serialPort) {
this.serialPort = l_serialPort;
}
public String sendToSerial(String theMsg) {
String res = "";
try {
long delayIntestazione = this.comDelayStart;
long delayFine = this.comDelayEnd;
long delayComando = this.comDelayCmd;
OutputStream outputStream = getSerialPort().getOutputStream();
getSerialPort().setSerialPortParams(9600, 8,
1, 0);
getSerialPort().notifyOnOutputEmpty(true);
StringTokenizer st = new StringTokenizer(theMsg, "|");
boolean firstCmd = true;
while (st.hasMoreTokens()) {
String theToken = st.nextToken();
String theCmd = String.valueOf(theToken) + "\n\r";
System.out.println("sendToSerial: " + theCmd);
outputStream.write(theCmd.getBytes());
if (firstCmd) {
Thread.sleep(delayIntestazione);
firstCmd = false;
} else {
Thread.sleep(delayComando);
}
res = readFromSerial();
System.out.println(String.valueOf(theToken) + ": " + getResponseMessage(res));
}
Thread.sleep(delayFine);
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
res = e.getMessage();
} catch (Exception e) {
System.out.println("Error setting event notification");
System.out.println(e.toString());
res = "Error setting event notification! " + e.toString();
System.exit(-1);
}
getSerialPort().close();
setSerialPort(null);
return res;
}
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case 1:
notifyAll();
break;
}
}
public String readFromSerial() {
try {
while (!getInputStream().ready())
Thread.sleep(10L);
BufferedReader bufRead = new BufferedReader(getInputStream());
String temp = bufRead.readLine();
getInputStream().close();
setInputStream(null);
return temp;
} catch (Exception e) {
return e.getMessage();
}
}
public InputStreamReader getInputStream() {
if (this.inputStream == null)
try {
this.inputStream = new InputStreamReader(getSerialPort()
.getInputStream());
getSerialPort().notifyOnDataAvailable(true);
} catch (Exception e) {
e.printStackTrace();
}
return this.inputStream;
}
public void setInputStream(InputStreamReader inputStream) {
this.inputStream = inputStream;
}
public abstract String getResponseMessage(String paramString);
public long getComDelayStart() {
return this.comDelayStart;
}
public void setComDelayStart(long comDelayStart) {
this.comDelayStart = comDelayStart;
}
public long getComDelayCmd() {
return this.comDelayCmd;
}
public void setComDelayCmd(long comDelayCmd) {
this.comDelayCmd = comDelayCmd;
}
public long getComDelayEnd() {
return this.comDelayEnd;
}
public void setComDelayEnd(long comDelayEnd) {
this.comDelayEnd = comDelayEnd;
}
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 null;
}
}

View file

@ -0,0 +1,9 @@
package com.ablia.serial;
public interface SerialPortWriterInterface {
String sendToSerial(String paramString);
String readFromSerial();
String getPortName();
}

View file

@ -0,0 +1,273 @@
package com.ablia.serial;
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 SerialServer 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 SerialServer this$0;
public CommServer(SerialServer 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();
this.this$0.handleDebug("\nInvio alla seriale: " + cashString,
1);
String temp = this.this$0.sendString(cashString);
this.this$0.handleDebug("\nfine invio alla seriale. Risultato: " +
temp, 1);
bw.write(temp);
bw.write("\r\n");
bw.write("##END##");
bw.write("\r\n");
bw.flush();
} else if (!getCommand(str).equals(
"")) {
bw.write("Abl Serial Server V. " +
SerialServer.getSoftwareVersion());
bw.write("\r\nError! 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 serial: SEND");
bw.write("\r\n");
bw.write("##END##");
bw.write("\r\n");
bw.flush();
}
}
i++;
}
} catch (Exception 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 P_COM_PORT = "SERIAL_COM_PORT";
public static final String P_COM_DELAY_END = "COM_DELAY_END";
public static final String P_COM_DELAY_START = "COM_DELAY_START";
public static final String SEP = "|";
public static final String P_SERVER_PORT = "SERIAL_SERVER_PORT";
public static final String CMD_CLOSE = "CLOSE";
public static final String P_SERVER_LOG_FILE = "SERIAL_SERVER_LOG_FILE";
public static final String P_SERVER_HOSTNAME = "SERIAL_SERVER_HOSTNAME";
public static final String P_COM_DELAY_CMD = "COM_DELAY_CMD";
public static void main(String[] args) {}
public SerialServer() {
String temp = "\nServer Properties.\nproperty file:" +
getPropertyFileName() + "\ncom port: " +
getResource("SERIAL_COM_PORT") + "\ncom delay start: " +
getResourceLong("COM_DELAY_START") + "\ncom delay cmd: " +
getResourceLong("COM_DELAY_CMD") + "\ncom delay end: " +
getResourceLong("COM_DELAY_END") + "\nserver port: " +
getServerPort() + "\n-------------------------";
handleDebug("\n-------------------------\nSTART Serial Server V. " +
getSoftwareVersion() + temp, 1);
startServer();
}
public String getLogFile() {
return getResource("SERIAL_SERVER_LOG_FILE");
}
public int getDebugLevel() {
return new Integer(getResource("DEBUG_LEVEL"))
.intValue();
}
public String getPropertyFileName() {
return "serial";
}
public int getServerPort() {
if (getResource("SERIAL_SERVER_PORT").equals(
""))
return 443;
return Integer.parseInt(getResource("SERIAL_SERVER_PORT"));
}
public static String getSoftwareVersion() {
return "AbSerial_0_07_310510";
}
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 {
String temp = getPropertyResource().getString(key);
return temp;
} catch (Exception e) {
handleDebug(e, 2);
System.out.println("SerialServer: " + e.getMessage() +
" . Property file:" + getPropertyFileName());
return "";
}
}
public String sendString(String strMsg) {
SerialPortWriterInterface spw = getSerialWriter();
return spw.sendToSerial(strMsg);
}
public String getDebugFile() {
return getLogFile();
}
private void startServer() {
ServerSocket srv = null;
Socket socket = null;
try {
int port = getServerPort();
srv = new ServerSocket(port);
while (true) {
handleDebug(" waiting for command...", 1);
socket = srv.accept();
}
} catch (Exception e) {
e.printStackTrace();
handleDebug(e);
} finally {
try {
if (socket != null) {
socket.close();
srv.close();
}
} catch (IOException ioe) {
handleDebug(ioe);
}
}
}
protected SerialPortWriterInterface getSerialWriter() {
return SerialPortWriter.getInstance(getResource("SERIAL_COM_PORT"),
getResourceLong("COM_DELAY_START"),
getResourceLong("COM_DELAY_CMD"),
getResourceLong("COM_DELAY_END"));
}
public boolean getDebug() {
return true;
}
public long getResourceLong(String key) {
try {
String temp = getPropertyResource().getString(key);
return Long.parseLong(temp);
} catch (Exception e) {
handleDebug(e, 2);
System.out.println("SerialServer: " + e.getMessage() +
" . Property file:" + getPropertyFileName());
return 0L;
}
}
}

View file

@ -0,0 +1,261 @@
package com.ablia.sms;
import com.ablia.serial.SerialClient;
import com.ablia.util.StringTokenizer;
public class SmsClient extends SerialClient {
private static char[] hexDigits = new char[] {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
static {
char[] gsmiso = {
'\000', '@', '\001', '£', '\002', '$', '\016', 'Å', '\017', 'å',
'\021', '_',
'[', 'Ä', '\\', 'Ö', '^', 'Ü', '_', '§',
'{', 'ä', '|', 'ö', '~',
'ü', '"', '"', '\005', 'é',
'\004', 'è' };
char[] gsmisoext = {
'\024', '^', '(', '{', ')', '}', '/', '\\', '<',
'[',
'=', '~', '>', ']', '@', '|' };
int lastindex = 255;
}
private static char[] gsmToIsoMap = new char[256];
private static char[] gsmToIsoExtMap = new char[256];
private static char[] isoToGsmMap = new char[256];
private static char[] isoToGsmExtMap = new char[256];
static {
for (int k = 0; k <= 255; k++) {
isoToGsmMap[k] = (char)k;
gsmToIsoExtMap[k] = (char)k;
gsmToIsoMap[k] = (char)k;
}
int gsmisolen = gsmiso.length;
for (int j = 0; j + 1 < gsmisolen; j += 2) {
gsmToIsoMap[gsmiso[j]] = gsmiso[j + 1];
isoToGsmMap[gsmiso[j + 1]] = gsmiso[j];
}
int gsmisoextlen = gsmisoext.length;
for (int i = 0; i + 1 < gsmisoextlen; i += 2)
gsmToIsoExtMap[gsmisoext[i]] = gsmisoext[i + 1];
}
private boolean usePdu = false;
private static final char EMPTYCHAR = 'Ā';
private static final char EXTTABLEESCAPE = '\033';
private String cellulare;
private String messaggio;
public SmsClient(String serverHost, int serverPort, boolean isPdu) {
super(serverHost, serverPort);
this.usePdu = isPdu;
}
public static void main(String[] args) {
SmsClient cc = new SmsClient("192.168.1.12", 441, false);
cc.setCellulare("+393285726937");
cc.setMessaggio("hellxxxohello");
String res = cc.inviaSms();
System.out.println(res);
cc.closeConnection();
System.exit(0);
}
public String getCellulare() {
return (this.cellulare == null) ? "" : this.cellulare;
}
public void setCellulare(String cellulare) {
this.cellulare = cellulare;
}
public String getMessaggio() {
return (this.messaggio == null) ? "" : this.messaggio;
}
public void setMessaggio(String messaggio) {
this.messaggio = messaggio;
}
public String inviaSms() {
String temp = getMessaggio();
String msgPart = "";
boolean flgAncoraTesto = true;
StringBuffer resInvio = new StringBuffer();
do {
if (temp.length() >= 160) {
StringTokenizer st = new StringTokenizer(temp, " ");
StringBuffer res = new StringBuffer();
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (res.length() + token.length() < 160) {
res.append(token);
res.append(" ");
continue;
}
break;
}
msgPart = res.toString();
temp = temp.substring(res.length());
} else {
msgPart = temp;
flgAncoraTesto = false;
}
System.out.println(String.valueOf(flgAncoraTesto) + " l: " + msgPart.length() +
" : " + msgPart);
if (this.usePdu) {
resInvio.append(inviaSmsPDU(msgPart));
} else {
resInvio.append(inviaSmsStandard(msgPart));
}
resInvio.append(" - ");
} while (flgAncoraTesto);
return resInvio.toString();
}
private String inviaSmsPDU(String l_msg) {
String temp;
resetSerialSendCommand();
String fineCmd = "\n";
addSerialSendCommand("AT");
addSerialSendCommand("AT+CMGF=0" + fineCmd);
addSerialSendCommand("AT+CSMS=0" + fineCmd);
String ottetto = "001100";
ottetto = String.valueOf(ottetto) + toHexString(getCellulare().length());
ottetto = String.valueOf(ottetto) + "91";
if (getCellulare().length() % 2 == 1) {
temp = swapDigits(String.valueOf(getCellulare()) + "F");
} else {
temp = swapDigits(getCellulare());
}
ottetto = String.valueOf(ottetto) + temp.toUpperCase();
ottetto = String.valueOf(ottetto) + "0000AA";
String tempMsg = l_msg.replaceAll("[\n\r]", "\n");
ottetto = String.valueOf(ottetto) + toHexString(tempMsg.length());
ottetto = String.valueOf(ottetto) + sevenBitEncode(tempMsg).toUpperCase();
System.out.println(ottetto.length());
int ottettoLen = ottetto.length() / 2 - 1;
addSerialSendCommand("AT+CMGS=" + ottettoLen + fineCmd);
addSerialSendCommand(ottetto);
addSerialSendCommand("\032");
System.out.println(getSerialSendCommand());
return sendSerialSendCommand();
}
private String inviaSmsStandard(String l_msg) {
resetSerialSendCommand();
addSerialSendCommand("AT");
addSerialSendCommand("AT+CMGF=1");
addSerialSendCommand("AT+CMGS=\"" + getCellulare() + "\"");
String temp = l_msg.replaceAll("[\n\r]", "\n");
addSerialSendCommand(temp);
addSerialSendCommand("\032");
System.out.println(getSerialSendCommand());
return sendSerialSendCommand();
}
public static String sevenBitEncode(String message) {
if (message == null)
return message;
StringBuffer msg = new StringBuffer(message);
StringBuffer encmsg = new StringBuffer(320);
int bb = 0, bblen = 0;
char o = '\000', c = '\000';
for (int i = 0; i < msg.length() || bblen >= 8; i++) {
if (i < msg.length()) {
c = msg.charAt(i);
char tc = isoToGsmMap[c];
c = tc;
c = (char)(c & 0xFFFFFF7F);
bb |= c << bblen;
bblen += 7;
}
while (bblen >= 8) {
o = (char)(bb & 0xFF);
encmsg.append(toHexString(o));
bb >>>= 8;
bblen -= 8;
}
}
if (bblen > 0)
encmsg.append(toHexString(bb));
return encmsg.toString();
}
public static String sevenBitDecode(String encmsg) {
return sevenBitDecode(encmsg, encmsg.length());
}
public static String sevenBitDecode(String encmsg, int msglen) throws NumberFormatException {
int r = 0, rlen = 0, olen = 0, charcnt = 0;
StringBuffer msg = new StringBuffer(160);
int encmsglen = encmsg.length();
boolean exttableescape = false;
for (int i = 0; i + 1 < encmsglen && charcnt < msglen; i += 2) {
String ostr = encmsg.substring(i, i + 2);
int o = Integer.parseInt(ostr, 16);
olen = 8;
if (rlen >= 7) {
char c1 = (char)(r & 0x7F);
r >>>= 7;
rlen -= 7;
msg.append(c1);
charcnt++;
}
o <<= rlen;
o |= r;
olen += rlen;
char c = (char)(o & 0x7F);
o >>>= 7;
olen -= 7;
r = o;
rlen = olen;
c = gsmToIsoMap[c];
if (c == '\033') {
exttableescape = true;
} else {
if (exttableescape) {
exttableescape = false;
c = gsmToIsoExtMap[c];
}
msg.append(c);
charcnt++;
}
}
if (rlen > 0 && charcnt < msglen)
msg.append((char)r);
return msg.toString();
}
public static String toHexString(int b) {
char[] digits = new char[2];
b &= 0xFF;
digits[0] = hexDigits[b / 16];
digits[1] = hexDigits[b % 16];
return new String(digits);
}
public static String swapDigits(String str) {
if (str == null)
return str;
int strlen = str.length();
StringBuffer sb = new StringBuffer(strlen);
for (int i = 0; i + 1 < strlen; i += 2) {
sb.append(str.charAt(i + 1));
sb.append(str.charAt(i));
}
return new String(sb);
}
}

View file

@ -0,0 +1,21 @@
package com.ablia.sms;
import com.ablia.serial.SerialPortWriterInterface;
import com.ablia.serial.SerialServer;
public class SmsServer extends SerialServer {
protected SerialPortWriterInterface getSerialWriter() {
return SmsWriter.getInstance(getResource("SERIAL_COM_PORT"),
getResourceLong("COM_DELAY_START"),
getResourceLong("COM_DELAY_CMD"),
getResourceLong("COM_DELAY_END"));
}
public String getPropertyFileName() {
return "sms";
}
public static void main(String[] args) {
System.out.println(System.getProperty("java.library.path"));
}
}

View file

@ -0,0 +1,84 @@
package com.ablia.sms;
import com.ablia.serial.SerialPortWriter;
import com.ablia.serial.SerialPortWriterInterface;
import com.ablia.util.StringTokenizer;
import gnu.io.UnsupportedCommOperationException;
import java.io.OutputStream;
public class SmsWriter extends SerialPortWriter implements SerialPortWriterInterface {
public static final String RES_FINE_CARTA = "DE2";
public static final String FINE_SMS_CRTRLZ = "\032";
public static final String RES_ERRORE_STRING = "DE69";
public static final String RES_SEQUENZA_ERRATA = "DE6";
public static final String RES_OK = "";
public SmsWriter() {}
public SmsWriter(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) {
return l_res;
}
public String sendToSerial(String theMsg) {
String res = "";
try {
long delayIntestazione = getComDelayStart();
long delayFine = getComDelayEnd();
long delayComando = getComDelayCmd();
OutputStream outputStream = getSerialPort().getOutputStream();
getSerialPort().setSerialPortParams(9600, 8,
1, 0);
getSerialPort().notifyOnOutputEmpty(true);
StringTokenizer st = new StringTokenizer(theMsg, "|");
boolean firstCmd = true;
while (st.hasMoreTokens()) {
String theToken = st.nextToken();
String theCmd = String.valueOf(theToken) + "\r";
System.out.println("sendToSerial: " + theCmd);
outputStream.write(theCmd.getBytes());
if (firstCmd) {
Thread.sleep(delayIntestazione);
firstCmd = false;
} else {
Thread.sleep(delayComando);
}
res = readFromSerial();
}
Thread.sleep(delayFine);
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
res = e.getMessage();
} catch (Exception e) {
System.out.println("Error setting event notification");
System.out.println(e.toString());
res = "Error setting event notification! " + e.toString();
System.exit(-1);
}
getSerialPort().close();
setSerialPort(null);
return 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 SmsWriter(l_portName, l_comDelayStart, l_comDelayCmd,
l_comDelayEnd);
}
}