From: gumartinm Date: Sun, 16 Oct 2011 16:53:41 +0000 (+0200) Subject: JavaPOS KeyBoardDriver X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=31e7e6387987f116222594f75e920e4fb96589e3;p=JavaForFun JavaPOS KeyBoardDriver --- diff --git a/.gitignore b/.gitignore index cc9a220..cc11a1f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ bin/ .classpath .project +.settings diff --git a/JavaPOS/KeyBoardDriver/pom.xml b/JavaPOS/KeyBoardDriver/pom.xml new file mode 100644 index 0000000..dda8b95 --- /dev/null +++ b/JavaPOS/KeyBoardDriver/pom.xml @@ -0,0 +1,21 @@ + + + 4.0.0 + es.dia.pos.n2a + javapos-keyboard-driver + 1.0 + javapos-keyboard-driver + http://maven.apache.org + + + jpos + jpos + 1.12.2 + + + jpos + jpos-controls + 1.12.2 + + + diff --git a/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/BaseKeyBoardDriver.java b/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/BaseKeyBoardDriver.java new file mode 100644 index 0000000..ef44764 --- /dev/null +++ b/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/BaseKeyBoardDriver.java @@ -0,0 +1,49 @@ +/** + * + */ +package es.dia.pos.n2a.gus.jpos; + +import jpos.JposException; + +/** + * @author + * + */ +public interface BaseKeyBoardDriver extends Runnable { + + public boolean isOpened(); + + public void close() throws JposException; + + public void claim() throws JposException; + + public void claim(int paramInt) throws JposException; + + public void release() throws JposException; + + public boolean isClaimed(); + + public void enable() throws JposException; + + public void disable() throws JposException; + + public boolean isEnabled(); + + public void addEventListener(DCALEventListener paramDCALEventListener) throws JposException; + + public void removeEventListener(DCALEventListener paramDCALEventListener); + + public boolean write(byte[] paramArrayOfByte, int paramInt1, int paramInt2, + int paramInt3) throws JposException; + + public int read(byte[] paramArrayOfByte, int paramInt1, int paramInt2, + int paramInt3) throws JposException; + + public int writeRead(byte[] paramArrayOfByte1, int paramInt1, + int paramInt2, byte[] paramArrayOfByte2, int paramInt3, int paramInt4, + int paramInt5) throws JposException; + + public String getDescription(int paramInt); + + public void flush(int paramInt) throws JposException; +} diff --git a/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/EventQueue.java b/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/EventQueue.java new file mode 100644 index 0000000..dc880ea --- /dev/null +++ b/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/EventQueue.java @@ -0,0 +1,28 @@ +package es.dia.pos.n2a.gus.jpos; + + + + +/** + * Instance factory to get the JPOS device services + * @author + */ +public class EventQueue extends Thread +{ + //Esto se ejectua en el constructor que hay por defecto (y que no se ve en este caso, + //si quisiera podria hacerlo tambien en ese constructor pero así queda mas ordenado) + private boolean isEnabled = false; + + + public void run() { + + + } + + /*private void enable () + { + threadEventSpool = new Thread(this); + threadEventSpool.start(); + threadEventSpool.setName("Thread-GUSPOSKeyboard"); + }*/ +} diff --git a/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/JposDriverInstanceFactory.java b/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/JposDriverInstanceFactory.java new file mode 100644 index 0000000..4de9b91 --- /dev/null +++ b/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/JposDriverInstanceFactory.java @@ -0,0 +1,24 @@ +/** + * + */ +package es.dia.pos.n2a.gus.jpos; + +import jpos.JposException; +import jpos.config.JposEntry; + +/** + * @author + * + */ +public interface JposDriverInstanceFactory { + + /** + * + * @param logicalName + * @param jposEntry + * @param entry + * @return + * @throws JposException + */ + public E createInstance(String logicalName, JposEntry entry, Class pruebaClass) throws JposException; +} diff --git a/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/JposDriverInstanceFactoryImpl.java b/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/JposDriverInstanceFactoryImpl.java new file mode 100644 index 0000000..48b56d4 --- /dev/null +++ b/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/JposDriverInstanceFactoryImpl.java @@ -0,0 +1,50 @@ +package es.dia.pos.n2a.gus.jpos; + + +import java.lang.reflect.Constructor; + +import jpos.JposConst; +import jpos.JposException; +import jpos.config.JposEntry; + +/** + * Retrieve the device HW driver using this instance factory. + * @author + */ +public class JposDriverInstanceFactoryImpl implements JposDriverInstanceFactory +{ + + /** + * @input_parameters: + */ + @Override + public E createInstance(String logicalName, JposEntry entry, Class typeClass) throws JposException + { + if (entry.getPropertyValue("driverClass") == null) + { + throw new JposException(JposConst.JPOS_E_NOSERVICE, "Missing driverClass JposEntry"); + } + E driverInstance = null; + try + { + String serviceClassName = (String)entry.getPropertyValue("driverClass"); + Class serviceClass = Class.forName(serviceClassName); + Class[] params = new Class[0]; + Constructor ctor = serviceClass.getConstructor(params); + if (typeClass.isInstance(ctor.newInstance((Object[])params)) ) + { + //This cast is correct (IMHO) because we are checking the right type + //with the method isInstance. + //Why must I declare this local variable with SuppressWarnings? This is weird... + @SuppressWarnings("unchecked") E aux = (E)ctor.newInstance((Object[])params); + driverInstance = aux; + } + } + catch(Exception e) + { + throw new JposException(JposConst.JPOS_E_NOSERVICE, "Could not create " + + "the driver instance for device with logicalName= " + logicalName, e); + } + return driverInstance; + } +} diff --git a/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/JposServiceInstanceFactoryImpl.java b/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/JposServiceInstanceFactoryImpl.java new file mode 100644 index 0000000..941a2e6 --- /dev/null +++ b/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/JposServiceInstanceFactoryImpl.java @@ -0,0 +1,45 @@ +package es.dia.pos.n2a.gus.jpos; + + +import java.lang.reflect.Constructor; + +import jpos.JposConst; +import jpos.JposException; +import jpos.config.JposEntry; +import jpos.loader.JposServiceInstance; +import jpos.loader.JposServiceInstanceFactory; + +/** + * Instance factory to get the JPOS device services + * @author + */ +public class JposServiceInstanceFactoryImpl implements JposServiceInstanceFactory +{ + /** + * @input_parameters: + * entry: (for jpos.xml and jpos.properties must be serviceClass) + * logicalName: device's name (in jpos.xml or jpos.properties file, jpos.xml file by default) + */ + public JposServiceInstance createInstance(String logicalName, JposEntry entry) throws JposException + { + if(!entry.hasPropertyWithName(JposEntry.SERVICE_CLASS_PROP_NAME)) + { + throw new JposException(JposConst.JPOS_E_NOSERVICE, "The JposEntry does not contain the 'serviceClass' property"); + } + JposServiceInstance serviceInstance = null; + try + { + String serviceClassName = (String)entry.getPropertyValue(JposEntry.SERVICE_CLASS_PROP_NAME); + Class serviceClass = Class.forName(serviceClassName); + Class[] params = new Class[0]; + Constructor ctor = serviceClass.getConstructor(params); + serviceInstance = (JposServiceInstance)ctor.newInstance((Object[])params); + } + catch(Exception e) + { + throw new JposException(JposConst.JPOS_E_NOSERVICE, "Could not create " + + "the service instance with logicalName= " + logicalName, e); + } + return serviceInstance; + } +} diff --git a/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/KBDDeviceLinux.java b/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/KBDDeviceLinux.java new file mode 100644 index 0000000..3b4b70a --- /dev/null +++ b/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/KBDDeviceLinux.java @@ -0,0 +1,162 @@ +/** + * + */ +package es.dia.pos.n2a.gus.jpos; + +import jpos.JposException; + +/** + * @author + * + */ +public class KBDDeviceLinux implements BaseKeyBoardDriver { + + /* (non-Javadoc) + * @see es.dia.pos.n2a.gus.jpos.KeyBoardDriver#isOpened() + */ + @Override + public boolean isOpened() { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see es.dia.pos.n2a.gus.jpos.KeyBoardDriver#close() + */ + @Override + public void close() throws JposException { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see es.dia.pos.n2a.gus.jpos.KeyBoardDriver#claim() + */ + @Override + public void claim() throws JposException { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see es.dia.pos.n2a.gus.jpos.KeyBoardDriver#claim(int) + */ + @Override + public void claim(int paramInt) throws JposException { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see es.dia.pos.n2a.gus.jpos.KeyBoardDriver#release() + */ + @Override + public void release() throws JposException { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see es.dia.pos.n2a.gus.jpos.KeyBoardDriver#isClaimed() + */ + @Override + public boolean isClaimed() { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see es.dia.pos.n2a.gus.jpos.KeyBoardDriver#enable() + */ + @Override + public void enable() throws JposException { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see es.dia.pos.n2a.gus.jpos.KeyBoardDriver#disable() + */ + @Override + public void disable() throws JposException { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see es.dia.pos.n2a.gus.jpos.KeyBoardDriver#isEnabled() + */ + @Override + public boolean isEnabled() { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see es.dia.pos.n2a.gus.jpos.KeyBoardDriver#write(byte[], int, int, int) + */ + @Override + public boolean write(byte[] paramArrayOfByte, int paramInt1, int paramInt2, + int paramInt3) throws JposException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see es.dia.pos.n2a.gus.jpos.KeyBoardDriver#read(byte[], int, int, int) + */ + @Override + public int read(byte[] paramArrayOfByte, int paramInt1, int paramInt2, + int paramInt3) throws JposException { + // TODO Auto-generated method stub + return 0; + } + + /* (non-Javadoc) + * @see es.dia.pos.n2a.gus.jpos.KeyBoardDriver#writeRead(byte[], int, int, byte[], int, int, int) + */ + @Override + public int writeRead(byte[] paramArrayOfByte1, int paramInt1, + int paramInt2, byte[] paramArrayOfByte2, int paramInt3, + int paramInt4, int paramInt5) throws JposException { + // TODO Auto-generated method stub + return 0; + } + + /* (non-Javadoc) + * @see es.dia.pos.n2a.gus.jpos.KeyBoardDriver#getDescription(int) + */ + @Override + public String getDescription(int paramInt) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see es.dia.pos.n2a.gus.jpos.KeyBoardDriver#flush(int) + */ + @Override + public void flush(int paramInt) throws JposException { + // TODO Auto-generated method stub + + } + + @Override + public void addEventListener(DCALEventListener paramDCALEventListener) + throws JposException { + // TODO Auto-generated method stub + + } + + @Override + public void removeEventListener(DCALEventListener paramDCALEventListener) { + // TODO Auto-generated method stub + + } + + @Override + public void run() { + // TODO Auto-generated method stub + + } +} diff --git a/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/POSKeyboard.java b/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/POSKeyboard.java new file mode 100644 index 0000000..123b4e7 --- /dev/null +++ b/JavaPOS/KeyBoardDriver/src/main/java/es/dia/pos/n2a/gus/jpos/POSKeyboard.java @@ -0,0 +1,346 @@ +package es.dia.pos.n2a.gus.jpos; + + +import java.io.FileInputStream; +import jpos.JposConst; +import jpos.JposException; +import jpos.POSKeyboardConst; +import jpos.config.simple.SimpleEntryRegistry; +import jpos.loader.JposServiceLoader; +import jpos.loader.JposServiceManager; +import jpos.services.EventCallbacks; +import jpos.services.POSKeyboardService17; +import jpos.config.JposEntry; +import jpos.config.JposEntryRegistry; +import jpos.events.DataEvent; +import jpos.events.DataListener; + +public class POSKeyboard implements POSKeyboardService17, JposConst, POSKeyboardConst +{ + private String logicalname; + private EventCallbacks callbacks = null; + private JposEntryRegistry jposEntryRegistry = null; + private JposEntry jposEntry = null; + private String device; + private int maxEvents; + private BaseKeyBoardDriver deviceDriver; + private JposDriverInstanceFactory jposDriverFactory; + + @Override + public int getCapPowerReporting() throws JposException { + // TODO Auto-generated method stub + return 0; + } + + @Override + public int getPowerNotify() throws JposException { + // TODO Auto-generated method stub + return 0; + } + + @Override + public int getPowerState() throws JposException { + // TODO Auto-generated method stub + return 0; + } + + @Override + public void setPowerNotify(int arg0) throws JposException { + // TODO Auto-generated method stub + + } + + @Override + public void clearInput() throws JposException { + // TODO Auto-generated method stub + + } + + @Override + public boolean getAutoDisable() throws JposException { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean getCapKeyUp() throws JposException { + // TODO Auto-generated method stub + return false; + } + + @Override + public int getDataCount() throws JposException { + // TODO Auto-generated method stub + return 0; + } + + @Override + public boolean getDataEventEnabled() throws JposException { + // TODO Auto-generated method stub + return false; + } + + @Override + public int getEventTypes() throws JposException { + // TODO Auto-generated method stub + return 0; + } + + @Override + public int getPOSKeyData() throws JposException { + // TODO Auto-generated method stub + return 0; + } + + @Override + public int getPOSKeyEventType() throws JposException { + // TODO Auto-generated method stub + return 0; + } + + @Override + public void setAutoDisable(boolean arg0) throws JposException { + // TODO Auto-generated method stub + + } + + @Override + public void setDataEventEnabled(boolean arg0) throws JposException { + // TODO Auto-generated method stub + + } + + @Override + public void setEventTypes(int arg0) throws JposException { + // TODO Auto-generated method stub + + } + + @Override + public void checkHealth(int arg0) throws JposException { + // TODO Auto-generated method stub + + } + + @Override + public void claim(int arg0) throws JposException { + // TODO Auto-generated method stub + + } + + @Override + public void close() throws JposException { + // TODO Auto-generated method stub + + } + + @Override + public void directIO(int arg0, int[] arg1, Object arg2) + throws JposException { + // TODO Auto-generated method stub + + } + + @Override + public String getCheckHealthText() throws JposException { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean getClaimed() throws JposException { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean getDeviceEnabled() throws JposException { + // TODO Auto-generated method stub + return false; + } + + @Override + public String getDeviceServiceDescription() throws JposException { + // TODO Auto-generated method stub + return null; + } + + @Override + public int getDeviceServiceVersion() throws JposException { + // TODO Auto-generated method stub + return 0; + } + + @Override + public boolean getFreezeEvents() throws JposException { + // TODO Auto-generated method stub + return false; + } + + @Override + public String getPhysicalDeviceDescription() throws JposException { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getPhysicalDeviceName() throws JposException { + // TODO Auto-generated method stub + return null; + } + + @Override + public int getState() throws JposException { + // TODO Auto-generated method stub + return 0; + } + + @Override + public void open(String paramString, EventCallbacks paramEventCallbacks) throws JposException { + this.logicalname = paramString; + + //La clase EventCallbacks se crea en la clase POSKeyboard, y contiene + //los objectos listeners generados en nuestro wrapper + //Esos objectos lo que contienen son punteros a funciones (en Java + //realmente todo son punteros) conocidas como callbacks + // + // this.dataListener = new DataListener() { + // @Override + // public void dataOccurred(final DataEvent dataEvent) { + // JPosScaleWrapper.this.processDataOccurredOccurred(dataEvent); <--- Esto es el "puntero" a funcion que será lo que se almacene en la clase EventCallbacs (se almacena el objecto DataListener que implementa la callback o puntero a funcion) ya a nivel Jpos (se introduce en el nivel Jpos algo creado en un nivel superior, es decir, en nuestro Wrapper) + // } + // } + // this.scale.addDataListener(this.dataListener); <-- addDataListener es un método definido en POSKeyboard que introduce el objeto DataListener en un Vector (al introducir ese objecto lo que está haciendo es pasar el callback, en Java lo han hecho así, en C,C++ por ejemplo podría haberse pasado directamente el puntero a función + this.callbacks = paramEventCallbacks; //<---- En este objeto dentro del Vector está el objecto DataListener creado en el Wrapper + + //Podemos extraer los valores de configuracion del jpos.xml o jpos.properties tal como sigue: + //(Wincord usa la clase OSServiceConfiguration para hacer lo mismo) + JposServiceManager localJposServiceManager = JposServiceLoader.getManager(); + //Esto contiene todo el jpos.xml o el jpos.properties correctamente ordenado y parseado + this.jposEntryRegistry = localJposServiceManager.getEntryRegistry(); + //Así podemos obtener toda la configuracion localizada en el jpos.xml o .properties + //para un determinado dispostivo + //cuyo nombre pasamos aqui como parametro de entrada. + //NOTA: Roberto lo que hacia era en la Factoria hacer esto mismo (recuperar + //el jposEntryRegistry y el jposEntry + //y pasarselo directamente al constructor. Mi constructor no hace nada + //(no lo he implementado, al menos todavia) + //En mi caso el constructor no hace eso y tengo que recuperarlo aquí + //(keine Ahnung was ist besser) + this.jposEntry = this.jposEntryRegistry.getJposEntry(paramString); + + + //Aqui comienzo a leer toda la posible configuracion para cumplir con un Keyboard POS. + //Finalmente de este modo podemos ir obteniendo la configuracion de ese + //dispositivo pasado los campos + String str = readJposConfiguration(this.jposEntry,this.jposEntryRegistry); + if ( str != null) + { + //En caso de devolver un string, este string es el mensaje de error. + throw new JposException(JPOS_E_ILLEGAL, str); + } + + //Recuperamos el codigo Java que lee eventos HW del teclado y los almacena en el + //DataEvent. Si hubiera que modificar el driver podria hacerse creando un nuevo + //interfaz que extiende de BaseKeyBoardDriver y aqui deberiamos aniadir algo + //que hiciera cast al nuevo interfaz que extiende de BaseKeyBoardDriver. De esta forma + //si hay que aniadir algo al driver no hay que modificar casi nada del codigo. + //Ademas si queremos cambiar el driver lo unico que hay que hacer es crear una nueva + //clase que implemente el interfaz BaseKeyBoardDriver y poner el nombre de la clase + //en el jpos.xml o en el jpos.properties en el campo driverClass que es donde he definido + //que se ponga el nombre de la clase que implementa el driver. En Wincord era en dcalClass. + //Por ejemplo yo ahora tendria que aniadir un campos driverClass al jpos.xml de la N2A + //con la clase es.dia.pos.n2a.gus.jpos.GUSKBDDeviceLinux + //Lo que no me gusta es que la factoria si se cambiara debe hacerse aquí en el codigo :S + this.jposDriverFactory = new JposDriverInstanceFactoryImpl(); + BaseKeyBoardDriver instanceHWDriver = this.jposDriverFactory.createInstance(paramString, + this.jposEntry, BaseKeyBoardDriver.class); + + } + + private String readJposConfiguration(JposEntry jposEntry, JposEntryRegistry jposEntryRegistry) + { + String str; + final String device = "device"; + final String maxEvents = "maxEvents"; + final String keyTable = "keyTable"; + JposEntry tableJposKey; + + //Primero: obtener el dispositivo de caracteres. + if ( jposEntry.hasPropertyWithName(device) ){ + //Chequeamos que lo que estamos leyendo es un String (deberia ser algo como /dev/wn_javapos_kbd0) + if (String.class == jposEntry.getPropertyType(device)) + { + this.device = (String)jposEntry.getPropertyValue(device); + }else + return "open-readJposConfiguration(): illegal device name."; + }else + return "open-readJposConfiguration(): A property called 'device' with " + + "the path of the character device of the keyboard is needed"; + + + //Segundo: obtener el numero maximo de eventos. + if ( jposEntry.hasPropertyWithName(maxEvents) ){ + if ((str = (String)jposEntry.getPropertyValue(maxEvents)) != null) + { + try + { + this.maxEvents = Integer.decode(str).intValue(); + } + catch (NumberFormatException localNumberFormatException) + { + return "open-readJposConfiguration(): illegal jpos property maxEvents '" + + str + "is not a number"; + } + } + }else + return "open-readJposConfiguration(): A property called 'maxEvents' with is needed"; + + + //Tercero: obtener la tabla de caracteres del teclado (si existe) puede ser de Dia o de Wincor + //PASO DE HACER ESTO AHORA, ME ABURRE LOL. + if ( jposEntry.hasPropertyWithName(keyTable)){ + if (String.class == jposEntry.getPropertyType(keyTable)){ + str = (String)jposEntry.getPropertyValue(keyTable); + tableJposKey=jposEntryRegistry.getJposEntry(str); + }else + return "open-readJposConfiguration(): illegal jpos property keyTable '" + + str + "is not a String"; + + /*PASO DE HACER ESTO DE MOMENTO, PARSEAR ESO ME ABURRE LOL*/ + + + } + + return null; + } + + @Override + public void release() throws JposException { + // TODO Auto-generated method stub + + } + + + @Override + public void setDeviceEnabled(boolean arg0) throws JposException { + // TODO Auto-generated method stub + + } + + @Override + public void setFreezeEvents(boolean arg0) throws JposException { + // TODO Auto-generated method stub + + } + + @Override + public void deleteInstance() throws JposException { + // TODO Auto-generated method stub + + } + + + + +} \ No newline at end of file