Made it connect/disconnect more correctly.
authorJimmy Christensen <dusted@dusted.dk>
Thu, 24 Jul 2014 14:14:55 +0000 (16:14 +0200)
committerJimmy Christensen <dusted@dusted.dk>
Thu, 24 Jul 2014 14:14:55 +0000 (16:14 +0200)
src/fkgui/ConsoleMsg.java
src/fkgui/MainWin.java
src/fkgui/SerialWorker.java

index c481c8f..795ea61 100644 (file)
@@ -2,7 +2,10 @@ package fkgui;
 
 import java.awt.PopupMenu;
 
 
 import java.awt.PopupMenu;
 
+import fkgui.SerialWorker.SerialState;
+
 public interface ConsoleMsg {
        public void log(String msg);
        public PopupMenu getPopup();
 public interface ConsoleMsg {
        public void log(String msg);
        public PopupMenu getPopup();
+       public void serialEvent( SerialState state );
 }
 }
index 791f0ac..461d295 100644 (file)
@@ -26,7 +26,7 @@ import org.eclipse.wb.swt.SWTResourceManager;
 
 import fkgui.SerialWorker.SerialState;
 
 
 import fkgui.SerialWorker.SerialState;
 
-public class MainWin implements PropertyChangeListener, ConsoleMsg {
+public class MainWin implements ConsoleMsg {
 
        protected Shell shell;
        private Text txtPsw;
 
        protected Shell shell;
        private Text txtPsw;
@@ -34,17 +34,24 @@ public class MainWin implements PropertyChangeListener, ConsoleMsg {
        public PopupMenu popup;
        public MenuItem showMain;
        public MenuItem hideMain;
        public PopupMenu popup;
        public MenuItem showMain;
        public MenuItem hideMain;
+
        public Text txtLog;
        public Button btnStart;
        public Text txtLog;
        public Button btnStart;
+       public Label lblPort;
+       public Label lblPassword;
+
        private Text txtDev;
        Preferences prefs;
        private Text txtDev;
        Preferences prefs;
+       MainWin mySelf;
+       private SerialState lastState = SerialState.Disconnected;
        
        SerialWorker fkSerial;
        private boolean sysTrayIconVisible;
 
        
        
        SerialWorker fkSerial;
        private boolean sysTrayIconVisible;
 
        
-       static final String PORT_PREF ="lastUsedPortPref";
+       static final String PREF_PORT ="lastUsedPortPref";
        static final String DEFAULT_DEVICE = "/dev/FinalKey";
        static final String DEFAULT_DEVICE = "/dev/FinalKey";
+       static final String PREF_AUTOHIDE = "hideMainWinAfterConnect";
  
        
        /**
  
        
        /**
@@ -70,7 +77,9 @@ public class MainWin implements PropertyChangeListener, ConsoleMsg {
                createContents();
                shell.open();
                shell.layout();
                createContents();
                shell.open();
                shell.layout();
-               
+               createSysTrayIcon();
+               serialEvent(SerialState.Disconnected);
+
                while (!shell.isDisposed()) {
                        if (!display.readAndDispatch()) {
                                display.sleep();                                
                while (!shell.isDisposed()) {
                        if (!display.readAndDispatch()) {
                                display.sleep();                                
@@ -152,13 +161,8 @@ public class MainWin implements PropertyChangeListener, ConsoleMsg {
                        }
                                
                });
                        }
                                
                });
-        
-
 
 
-        
-        //Add components to pop-up menu
-        popup.add(hideMain);
-        popup.addSeparator();
+        clearSystray();
 
 
         trayIcon.setPopupMenu(popup);
 
 
         trayIcon.setPopupMenu(popup);
@@ -192,6 +196,18 @@ public class MainWin implements PropertyChangeListener, ConsoleMsg {
                SystemTray.getSystemTray().remove(trayIcon);
        }
        
                SystemTray.getSystemTray().remove(trayIcon);
        }
        
+       private void clearSystray() {
+                       popup.removeAll();
+               //Add components to pop-up menu
+                       if(shell.isVisible()==true)
+                       {
+                               popup.add(hideMain);
+                       } else {
+                               popup.add(showMain);
+                       }
+               popup.addSeparator();
+       }
+
        public void shutDownApp()
        {
                if(fkSerial != null)
        public void shutDownApp()
        {
                if(fkSerial != null)
@@ -220,8 +236,7 @@ public class MainWin implements PropertyChangeListener, ConsoleMsg {
                prefs = Preferences.userNodeForPackage(this.getClass());
                
 
                prefs = Preferences.userNodeForPackage(this.getClass());
                
 
-               fkSerial = new SerialWorker(this);
-               fkSerial.addPropertyChangeListener(this);
+               mySelf = this;
                
 
                btnStart = new Button(shell, SWT.NONE);
                
 
                btnStart = new Button(shell, SWT.NONE);
@@ -229,29 +244,19 @@ public class MainWin implements PropertyChangeListener, ConsoleMsg {
                btnStart.addSelectionListener(new SelectionAdapter() {
                        public void widgetSelected(SelectionEvent e) {
                                
                btnStart.addSelectionListener(new SelectionAdapter() {
                        public void widgetSelected(SelectionEvent e) {
                                
-                               if( fkSerial.state == SerialState.Connected )
+                               if( fkSerial!=null && fkSerial.state == SerialState.Connected )
                                {
                                {
-                                       shell.setText("Final Key (Not connected)");
                                        fkSerial.disconnect();
                                        fkSerial.disconnect();
-                                       txtPsw.setVisible(true);
-                                       txtDev.setVisible(true);
-                                       
-                                       btnStart.setText("Connect");
                                } else {
                                } else {
-                                       prefs.put(PORT_PREF, txtDev.getText() );
+                                       fkSerial = new SerialWorker(mySelf);
+                                       prefs.put(PREF_PORT, txtDev.getText() );
                                        fkSerial.connect(txtDev.getText(),txtPsw.getText());
                                        fkSerial.connect(txtDev.getText(),txtPsw.getText());
-                                       //Eat the password for security reasons.
                                        txtPsw.setText("");
                                        txtPsw.setText("");
-                                       txtPsw.setVisible(false);
-                                       txtDev.setVisible(false);
-                                       btnStart.setText("Disconnect");
-                                       shell.setText("Final Key (Connected)");
-                                       
                                }
                        }
                });
                                }
                        }
                });
-               btnStart.setText("Connect");
-               Label lblPassword = new Label(shell, SWT.NONE);
+//             btnStart.setText("Connect");
+               lblPassword = new Label(shell, SWT.NONE);
                lblPassword.setText("Password");
                lblPassword.setBounds(10, 39, 85, 23);
                
                lblPassword.setText("Password");
                lblPassword.setBounds(10, 39, 85, 23);
                
@@ -262,31 +267,25 @@ public class MainWin implements PropertyChangeListener, ConsoleMsg {
                txtLog.setEditable(false);
                txtLog.setBounds(10, 68, 424, 388);
                
                txtLog.setEditable(false);
                txtLog.setBounds(10, 68, 424, 388);
                
-               Label lblPort = new Label(shell, SWT.NONE);
+               lblPort = new Label(shell, SWT.NONE);
                lblPort.setBounds(10, 10, 76, 23);
                lblPort.setText("Port");
                
                txtDev = new Text(shell, SWT.BORDER);
                txtDev.setFont(SWTResourceManager.getFont("Cantarell", 9, SWT.NORMAL));
                lblPort.setBounds(10, 10, 76, 23);
                lblPort.setText("Port");
                
                txtDev = new Text(shell, SWT.BORDER);
                txtDev.setFont(SWTResourceManager.getFont("Cantarell", 9, SWT.NORMAL));
-               txtDev.setText( prefs.get(PORT_PREF, DEFAULT_DEVICE));
+               txtDev.setText( prefs.get(PREF_PORT, DEFAULT_DEVICE));
                txtDev.setBounds(101, 10, 223, 23);
                shell.setTabList(new Control[]{txtPsw, btnStart});
                
                log("Welcome!\nConnect your Final Key and enter password.\nThen press connect.\nPress the button when it blinks.\n----------\n");
 
 
                txtDev.setBounds(101, 10, 223, 23);
                shell.setTabList(new Control[]{txtPsw, btnStart});
                
                log("Welcome!\nConnect your Final Key and enter password.\nThen press connect.\nPress the button when it blinks.\n----------\n");
 
 
-               createSysTrayIcon();
-               
-               
-               
                shell.addShellListener( new ShellListener() {
                        
                        public void shellIconified(ShellEvent e) {
 
                shell.addShellListener( new ShellListener() {
                        
                        public void shellIconified(ShellEvent e) {
 
-                                       
-
                        }
                        }
-                       
+
                        public void shellDeiconified(ShellEvent e) {
                                // TODO Auto-generated method stub
                                
                        public void shellDeiconified(ShellEvent e) {
                                // TODO Auto-generated method stub
                                
@@ -312,19 +311,56 @@ public class MainWin implements PropertyChangeListener, ConsoleMsg {
 
        }
 
 
        }
 
+
+
        @Override
        @Override
-       public void propertyChange(PropertyChangeEvent evt) {
-               
-               if( evt.getPropertyName().equals("serialState") )
+       public PopupMenu getPopup() {
+               return this.popup;
+       }
+
+       @Override
+       public void serialEvent(SerialState state) {
+               switch(state)
                {
                {
-                       System.out.println( "serialState changed from "+((SerialState)evt.getOldValue())+" to "+((SerialState)evt.getNewValue()) );
+               case Connected:
+                       btnStart.setText("Disconnect");
+                       btnStart.setVisible(true);
+                       //Should we hide?
+                       if( prefs.getBoolean(PREF_AUTOHIDE, false) == true)
+                       {
+                               hideToTray();
+                       }
+                       log("* Connected *");
+                       break;
+               case Connecting:
+                       shell.setText("Final Key (Connecting...)");
+                       txtPsw.setVisible(false);
+                       txtDev.setVisible(false);
+                       btnStart.setVisible(false);
+                       lblPort.setVisible(false);
+                       lblPassword.setVisible(false);
+                       break;
+               case Disconnected:
+                       fkSerial=null;
+                       shell.setText("Final Key (Not connected)");
+                       txtPsw.setVisible(true);
+                       txtDev.setVisible(true);
+                       btnStart.setText("Connect");
+                       btnStart.setVisible(true);
+                       lblPort.setVisible(true);
+                       lblPassword.setVisible(true);
+                       clearSystray();
+                       if(lastState != state)
+                       {
+                               log("* Disconnected *");
+                       }
+                       break;
+               default:
+                       break;
                }
                }
-               
+               lastState=state;
                
        }
 
                
        }
 
-       @Override
-       public PopupMenu getPopup() {
-               return this.popup;
-       }
+
 }
 }
index 93027d5..f97c1e3 100644 (file)
@@ -33,26 +33,28 @@ public class SerialWorker extends javax.swing.SwingWorker<Void, String> implemen
                pass=p;
                
                serialPort = new SerialPort(dev);               
                pass=p;
                
                serialPort = new SerialPort(dev);               
-               firePropertyChange("serialState", state, SerialState.Connecting );
+
                state = SerialState.Connecting;
                state = SerialState.Connecting;
+               postStateChange( state );
                
                execute();
        }
        
        public void disconnect()
        {
                
                execute();
        }
        
        public void disconnect()
        {
-               firePropertyChange("serialState", state, SerialState.Disconnected );
-
-               
                if(serialPort != null && serialPort.isOpened() )
                {
                        try {
                if(serialPort != null && serialPort.isOpened() )
                {
                        try {
+                               serialPort.writeByte( (byte)'q'); //Machine commands with uppercase X
+                               Thread.sleep(400);
                                serialPort.closePort();
                                serialPort.closePort();
-                       } catch (SerialPortException e) {
+                       } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
+               state = SerialState.Disconnected;
+               postStateChange( SerialState.Disconnected);
        }
 
        public String expectString(String expect, int timeOut)
        }
 
        public String expectString(String expect, int timeOut)
@@ -108,7 +110,10 @@ public class SerialWorker extends javax.swing.SwingWorker<Void, String> implemen
                try {
                        System.out.println("Port opened: " + serialPort.openPort());
                        System.out.println("Params setted: " + serialPort.setParams(9600, 8, 1, 0));
                try {
                        System.out.println("Port opened: " + serialPort.openPort());
                        System.out.println("Params setted: " + 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);
                        if( test != null )
                        serialPort.addEventListener(this);
                        String test = expectString("The Final Key", 1000);
                        if( test != null )
@@ -132,7 +137,8 @@ public class SerialWorker extends javax.swing.SwingWorker<Void, String> implemen
                                return null;
                        }
 
                                return null;
                        }
 
-                       serialPort.writeBytes(pass.getBytes());
+                       enterString(pass);
+
                        serialPort.writeByte( (byte)13 );
                        pass = "";
                        
                        serialPort.writeByte( (byte)13 );
                        pass = "";
                        
@@ -242,13 +248,22 @@ public class SerialWorker extends javax.swing.SwingWorker<Void, String> implemen
 
                publish("Use the systray icon to trigger.");
 
 
                publish("Use the systray icon to trigger.");
 
-               firePropertyChange("serialState", state, SerialState.Connected );
                state = SerialState.Connected;
                state = SerialState.Connected;
+               postStateChange( state);
                
                return null;
        }
 
 
                
                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;
        private class MainWinMsg implements Runnable {
                private String msg;
                private ConsoleMsg delegate;
@@ -262,6 +277,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)
        @Override
        protected void process(List<String> msgs) {
                for(String s : msgs)
@@ -274,10 +308,24 @@ public class SerialWorker extends javax.swing.SwingWorker<Void, String> implemen
        @Override
        public void serialEvent(SerialPortEvent event) {
                
        @Override
        public void serialEvent(SerialPortEvent event) {
                
+               System.out.println("Event!" + event.getEventType() );
+
                if(event.isBREAK())
                if(event.isBREAK())
-                       System.out.println(">>BREAK);");
+               {
+                       if(state!=SerialState.Disconnected)
+                       {
+                               disconnect();
+                               System.out.println(">>Break);");
+                       }
+               }
                if(event.isERR())
                if(event.isERR())
-                       System.out.println(">>Err");
+               {
+                       if(state!=SerialState.Disconnected)
+                       {
+                               disconnect();
+                               System.out.println(">>Error");
+                       }
+               }
                if(serialPort == null)
                        System.out.println(">>Null");
 
                if(serialPort == null)
                        System.out.println(">>Null");