Fixed bug in new-account, added error dialogs.
[FinalKeyGui.git] / src / fkgui / SerialWorker.java
index 93027d5..7ebc929 100644 (file)
@@ -1,7 +1,5 @@
 package fkgui;
 
-import java.awt.Menu;
-import java.awt.MenuItem;
 import java.util.List;
 
 import jssc.SerialPort;
@@ -15,16 +13,17 @@ import org.eclipse.swt.widgets.Display;
 public class SerialWorker extends javax.swing.SwingWorker<Void, String> implements SerialPortEventListener {
        public String dev;
        public String pass;
-       private SerialPort serialPort;
+       public SerialPort serialPort;
        SerialState state;
        private ConsoleMsg delegate; 
        
-       public enum SerialState { Connecting, Connected, Disconnected };
+       public enum SerialState { Connecting,Working, Connected, Disconnected };
 
 
        public SerialWorker(ConsoleMsg d) {
                delegate=d;
                state = SerialState.Disconnected;
+               FkManager.getInstance().setWorker(this);
        }
        
        public void connect(String d, String p)
@@ -33,26 +32,28 @@ public class SerialWorker extends javax.swing.SwingWorker<Void, String> implemen
                pass=p;
                
                serialPort = new SerialPort(dev);               
-               firePropertyChange("serialState", state, SerialState.Connecting );
+
                state = SerialState.Connecting;
-               
+               postStateChange( state );
+               FkManager.getInstance().listClear();
                execute();
        }
        
        public void disconnect()
        {
-               firePropertyChange("serialState", state, SerialState.Disconnected );
-
-               
                if(serialPort != null && serialPort.isOpened() )
                {
                        try {
+                               serialPort.writeByte( (byte)'q'); //Machine commands with uppercase X
+                               Thread.sleep(400);
                                serialPort.closePort();
-                       } catch (SerialPortException e) {
+                       } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
+               state = SerialState.Disconnected;
+               postStateChange( SerialState.Disconnected);
        }
 
        public String expectString(String expect, int timeOut)
@@ -95,7 +96,7 @@ public class SerialWorker extends javax.swing.SwingWorker<Void, String> implemen
        
        @Override
        protected Void doInBackground() throws Exception {
-               publish("Trying to connect to "+dev);
+               publish(Messages.SerialWorker_0+dev);
                /**
                 * Connection strategy:
                 * Open the port, wait for "The Final Key" followed by # on next line, (getLoginHeader)
@@ -106,49 +107,60 @@ public class SerialWorker extends javax.swing.SwingWorker<Void, String> implemen
                
                int numAccounts=0;
                try {
-                       System.out.println("Port opened: " + serialPort.openPort());
-                       System.out.println("Params setted: " + serialPort.setParams(9600, 8, 1, 0));
-                       
+                       System.out.println(Messages.SerialWorker_1 + serialPort.openPort());
+                       System.out.println(Messages.SerialWorker_2 + serialPort.setParams(9600, 8, 1, 0));
+
+                       int mask = SerialPort.MASK_BREAK + SerialPort.MASK_ERR + SerialPort.MASK_RLSD;
+                       serialPort.setEventsMask(mask);
+
                        serialPort.addEventListener(this);
-                       String test = expectString("The Final Key", 1000);
+                       String test = expectString("The Final Key", 1000); //$NON-NLS-1$
                        if( test != null )
                        {
-                               publish("Ready to log in, press button now.");
-                               publish("Waiting for button press...");
+                               publish(Messages.SerialWorker_4);
                        } else {
                                //Try logging out.
                                serialPort.writeByte( (byte)'q');
-                               publish("State error, try again.");
+                               publish(Messages.SerialWorker_5);
                                disconnect();
                                return null;
                        }
                        
-                       if( expectString( "Pass:", 0 ) != null )
+                       if( expectString( "Pass:", 0 ) != null ) //$NON-NLS-1$
                        {
-                               publish("Logging in...");
+                               publish(Messages.SerialWorker_7);
+                               postStateChange(SerialState.Working);
                        } else {
-                               publish("Error: Did not get password prompt. Unplug and try again.");
+                               publish(Messages.SerialWorker_8);
                                disconnect();
                                return null;
                        }
 
-                       serialPort.writeBytes(pass.getBytes());
+                       enterString(pass);
+
                        serialPort.writeByte( (byte)13 );
-                       pass = "";
+                       pass = ""; //$NON-NLS-1$
                        
+                       /*String str = expectString( "[Keyboard: ", 200 );
+                       if( str != null )
+                       {
+                               FkManager.getInstance().setCurrentLayout( str );
+                       } else {
+                               publish("Did not get Keyboard layout.");
+                       }*/
                        
-                       if( expectString( "[Granted]", 200 ) != null )
+                       if( expectString( "[Granted]", 200 ) != null ) //$NON-NLS-1$
                        {
-                               publish("Access Granted.");
+                               publish(Messages.SerialWorker_11);
                        } else {
-                               publish("Error: Access Denied.");
+                               publish(Messages.SerialWorker_12);
                                disconnect();
                                return null;
                        }
 
-                       publish("Getting account list...");
+                       publish(Messages.SerialWorker_13);
                        serialPort.writeByte( (byte)'X'); //Machine commands with uppercase X
-                       expectString("[auto]", 200);
+                       expectString("[auto]", 200); //$NON-NLS-1$
                        serialPort.writeByte( (byte)'l'); //Full list 
 
                        
@@ -162,7 +174,7 @@ public class SerialWorker extends javax.swing.SwingWorker<Void, String> implemen
                                {
                                        accounts += serialPort.readString();
                                        String sub = accounts.substring( accounts.length()-3 );
-                                       if( sub.equals("\r\n>") )
+                                       if( sub.equals("\r\n>") ) //$NON-NLS-1$
                                        {
                                                accounts = accounts.substring( 0, accounts.length()-3 );
                                                break;
@@ -172,7 +184,7 @@ public class SerialWorker extends javax.swing.SwingWorker<Void, String> implemen
                                        timeOut-=10;
                                        if(timeOut < 1)
                                        {
-                                               publish("Error getting account list.");
+                                               publish(Messages.SerialWorker_16);
                                                disconnect();
                                                return null;
                                        }
@@ -182,73 +194,61 @@ public class SerialWorker extends javax.swing.SwingWorker<Void, String> implemen
                        //Trim first 3
                        accounts = accounts.substring(3);
 
-                       String[] lines = accounts.split( "\r\n" );
+                       String[] lines = accounts.split( "\r\n" ); //$NON-NLS-1$
                        numAccounts=lines.length;
+                       Boolean kbList=false;
                        for(String l:lines)
                        {
-                               String ac = l.substring(0,2);
-                               String an = l.substring(2);
-                               
-                               //publish( "Account number: "+ac+" ["+an+"]");
-
-                               Menu menu = new Menu(an+" ["+ac+"]");
-                               MenuItem both = new MenuItem("User + Pass");
-                               MenuItem usr = new MenuItem("User");
-                               MenuItem psw = new MenuItem("Pass");
-                               menu.add(both);
-                               menu.add(usr);
-                               menu.add(psw);
-
-                               FireActionListener fal = new FireActionListener();
-                               fal.action = "%";
-                               fal.name = an;
-                               fal.num = ac;
-                               fal.port = serialPort;
-                               both.addActionListener(fal);
-
-
-                               fal = new FireActionListener();
-                               fal.action = "p";
-                               fal.name = an;
-                               fal.num = ac;
-                               fal.port = serialPort;
-                               psw.addActionListener(fal);                     
-
-                               fal = new FireActionListener();
-                               fal.action = "u";
-                               fal.name = an;
-                               fal.num = ac;
-                               fal.port = serialPort;
-                               usr.addActionListener(fal);                     
-
-                               delegate.getPopup().add(menu);
+                               if( ! kbList )
+                               {
+                                       if( l.compareTo("[KBL]") == 0 ) //$NON-NLS-1$
+                                       {
+                                               kbList=true;
+                                       } else {
+                                               String ac = l.substring(0,2);
+                                               String an = l.substring(2);
+                                               FkManager.getInstance().listAddAcc(ac, an);
+                                       }
+                               } else {
+                                       //Next entries are supported keyboard layouts
+                                       publish(Messages.SerialWorker_3 + l);
+                               }
                        }
 
                }
-               catch (SerialPortException ex){
-                       System.out.println(ex);
-               } catch (Exception e )
-               {
-                       System.out.println("Other exception: "+e.getMessage() );
-                       e.printStackTrace();
+               catch (Exception ex){
+                       publish("Error: Exception: "+ex.getMessage() ); //$NON-NLS-1$
+                       disconnect();
                }
 
-               if(numAccounts==1)
+               if( state != SerialState.Disconnected )
                {
-                       publish(numAccounts+" account.");
-               } else {
-                       publish(numAccounts+" accounts ready.");
-               }
-
-               publish("Use the systray icon to trigger.");
+                       if(numAccounts==1)
+                       {
+                               publish(numAccounts+" account."); //$NON-NLS-1$
+                       } else {
+                               publish(numAccounts+" accounts ready."); //$NON-NLS-1$
+                       }
+       
+                       publish(Messages.SerialWorker_23);
 
-               firePropertyChange("serialState", state, SerialState.Connected );
-               state = SerialState.Connected;
+                       state = SerialState.Connected;
+                       postStateChange( state);
+               }
                
                return null;
        }
 
 
+       private void enterString(String str) throws SerialPortException, InterruptedException {
+               for(int i=0; i < str.length(); i++)
+               {
+                       serialPort.writeByte( str.getBytes()[i] );
+                       Thread.sleep(20);
+               }
+       }
+
+
        private class MainWinMsg implements Runnable {
                private String msg;
                private ConsoleMsg delegate;
@@ -262,6 +262,25 @@ public class SerialWorker extends javax.swing.SwingWorker<Void, String> implemen
                }
        }
        
+       private class MainWinSerialChange implements Runnable  {
+               private ConsoleMsg delegate;
+               private SerialState state;
+               private MainWinSerialChange(ConsoleMsg d, SerialState s )
+               {
+                       delegate=d;
+                       state=s;
+               }
+               @Override
+               public void run() {
+                       delegate.serialEvent(state);
+               }
+       }
+
+       private void postStateChange(SerialState state)
+       {
+               Display.getDefault().asyncExec( new MainWinSerialChange(delegate, state) );
+       }
+
        @Override
        protected void process(List<String> msgs) {
                for(String s : msgs)
@@ -274,12 +293,31 @@ public class SerialWorker extends javax.swing.SwingWorker<Void, String> implemen
        @Override
        public void serialEvent(SerialPortEvent event) {
                
+               System.out.println("Event!" + event.getEventType() ); //$NON-NLS-1$
+
+               if( event.isRXCHAR() )
+               {
+                       System.out.print("{char}");
+               }
+               
                if(event.isBREAK())
-                       System.out.println(">>BREAK);");
+               {
+                       if(state!=SerialState.Disconnected)
+                       {
+                               disconnect();
+                               System.out.println(">>Break);"); //$NON-NLS-1$
+                       }
+               }
                if(event.isERR())
-                       System.out.println(">>Err");
+               {
+                       if(state!=SerialState.Disconnected)
+                       {
+                               disconnect();
+                               System.out.println(">>Error"); //$NON-NLS-1$
+                       }
+               }
                if(serialPort == null)
-                       System.out.println(">>Null");
+                       System.out.println(">>Null"); //$NON-NLS-1$
 
        }