Likely unstable.
[FinalKeyGui.git] / src / fkgui / FkManager.java
1 package fkgui;
2
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.Collection;
8 import java.util.Comparator;
9 import java.util.Vector;
10 import java.util.concurrent.ExecutionException;
11
12 import javax.swing.JFrame;
13 import javax.swing.JTextField;
14
15 import org.eclipse.jface.viewers.IContentProvider;
16 import org.eclipse.jface.viewers.Viewer;
17 import org.eclipse.swt.widgets.Display;
18
19 import jssc.SerialPortException;
20 import fkgui.FkActionEventListener.FkActionEvent;
21 import fkgui.FkActionEventListener.FkActionEventType;
22 import fkgui.FkManager.Account;
23 import fkgui.SerialWorker.SerialState;
24
25 public class FkManager implements ActionListener {
26         private static FkManager instance = null;
27         private SerialWorker com = null;
28
29         public class Account
30         {
31                 public String name;
32                 public String num;
33                 
34                 public Account( String acNum, String acName )
35                 {
36                         name = acName;
37                         num = acNum;
38                 }
39                 
40                 public String toString()
41                 {
42                         return(name);
43                 }
44         }
45                 
46         private Vector<Account> list;
47         
48         protected FkManager()
49         {
50                 list = new Vector<Account>(256);
51         }
52         
53         public static FkManager getInstance()
54         {
55                 if( instance == null )
56                 {
57                         instance = new FkManager();
58                 }
59
60                 return( instance );
61         }
62         
63         public void setWorker( SerialWorker sw )
64         {
65                 com = sw;
66         }
67         
68
69         private class FkActionEventMsg implements Runnable  {
70                 private FkActionEventListener delegate;
71                 private FkActionEvent event;
72
73                 private FkActionEventMsg(FkActionEventListener d, FkActionEventType t, String data, Account acc, char act )
74                 {
75                         delegate=d;
76                         event=new FkActionEvent(t, data, acc,act) ;
77                 }
78                 @Override
79                 public void run() {
80                         if( delegate != null )
81                         {
82                                 delegate.fkActionEvent(event);
83                         }
84                 }
85         }
86         
87         private class TrigTask implements Runnable
88         {
89                 private Account acc;
90                 private char action;
91                 private FkActionEventListener delegate;
92                 public TrigTask(Account ac, char act, FkActionEventListener d )
93                 {
94                         acc=ac;
95                         action=act;
96                         delegate=d;
97                 }
98                 
99                 @Override
100                 public void run() {
101                         
102                         flushSerial();
103                         
104                         if(action != '%' )
105                         {
106                                 try
107                                 {
108                                         
109                                         if( action == 'd' || action == 'o' )
110                                         {
111                                                 com.serialPort.writeByte((byte)'x');
112                                         }
113                                         com.serialPort.writeByte((byte)action);
114                                 } catch( Exception ex )
115                                 {
116                                         ex.printStackTrace();
117                                 }
118                         }
119                         
120                         try {
121                                 com.serialPort.writeBytes(acc.num.toLowerCase().getBytes());
122                 
123                                 if( action == 'd' || action == 'o' )
124                                 {
125                                         com.serialPort.writeByte((byte)'y');
126                                 }                               
127                                 
128                                 try {
129                                         Thread.sleep(200);
130                                 } catch (InterruptedException e1) {
131                                         // TODO Auto-generated catch block
132                                         e1.printStackTrace();
133                                 }
134                                 
135                                 int t=0;
136
137                                 String msg = "";
138                                 while( t < 1200 )
139                                 {
140                                         t++;
141                                         try {
142                                                 Thread.sleep(50);
143                                         } catch (InterruptedException e) {
144                                                 // TODO Auto-generated catch block
145                                                 e.printStackTrace();
146                                         }
147                                         if( com.serialPort.getInputBufferBytesCount() > 0 )
148                                         {
149                                                 msg += com.serialPort.readString();
150                                                 //System.out.println( msg );
151                                                 if( msg.contains("[done]") || msg.contains("[deleted]") )
152                                                 {
153                                                         if( action == 'd' )
154                                                         {
155                                                                 list.remove( acc );
156                                                         }
157                                                         Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_OKAY, msg, acc, action) );
158                                                         return;
159                                                 } else if( msg.contains("[abort]") )
160                                                 {
161                                                         //System.out.println("FkManager: Action Abort");;
162                                                         Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ABORTED, msg,acc,action) );
163                                                         return;
164                                                 }
165                                         }
166                                 }
167                                 Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ERROR, msg,acc,action) );
168
169                         } catch (Exception e1) {
170                                 // TODO Auto-generated catch block
171                                 System.out.println("TrigTask Exception:");
172                                 e1.printStackTrace();
173                                 Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ERROR, "EXCEPTION",acc,action) );
174                         }
175                         
176                 }
177                 
178         }
179         
180         public void trig(Account acc, char action, FkActionEventListener delegate)
181         {
182                 TrigTask trigTask = new TrigTask(acc,action,delegate);
183                 new Thread(trigTask).start();
184         }
185         
186         
187         public void listAddAcc(String num, String name)
188         {
189                 list.addElement( new Account(num,name) );
190                 list.sort( new Comparator<Account>() {
191
192                         @Override
193                         public int compare(Account o1, Account o2) {
194                                 
195                                 return o1.name.compareTo(o2.name);
196                         }
197                 } );
198         }
199         
200         public void listClear()
201         {
202                 list.clear();
203         }
204         
205         public Vector<Account> getList()
206         {
207                 return(list);
208         }
209         
210         public void getCurrentLayout()
211         {
212                 
213         }
214         
215         public void getAvailableLayouts()
216         {
217                 
218         }
219         
220         public void setLayout(int num)
221         {
222                 
223         }
224
225         @Override
226         public void actionPerformed(ActionEvent e) {
227                 Account f=null;
228                 for( Account a : list )
229                 {
230                         if( a.num.compareTo( e.getActionCommand().substring(1, 3)) == 0 )
231                         {
232                                 f=a;
233                                 break;
234                         }
235                 }
236                 if(f!=null)
237                 {
238                         trig(f, e.getActionCommand().charAt(0), null);
239                 }
240                 
241         }
242
243         public void setCurrentLayout(String str) {
244                 System.out.println("FkManager: Got current layout:" + str );
245                 
246         }
247         
248         private class NewAccountTask implements Runnable
249         {
250                 private String seq;
251                 private Account acc;
252                 FkActionEventListener delegate;
253                 public NewAccountTask(String sequence, FkActionEventListener d, String accountName)
254                 {
255                         seq=sequence;
256                         delegate=d;
257                         acc = new Account( "00", accountName );
258                         System.out.println("NewAccountTask ctor");
259                 }
260                 @Override
261                 public void run() {
262                         System.out.println("NewAccountTask run");
263
264                         flushSerial();
265                         String data="";
266                         int timeOut;
267                         try {                   
268                         //First type x a and wait for account title
269                                 
270                                 com.serialPort.writeByte((byte)'x');
271                                 com.serialPort.writeByte((byte)'a');
272                                 Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_WAITING, "WAITING",acc,'A') );
273
274                                 timeOut = 6000;
275                                 while(timeOut > 0)
276                                 {
277                                         if( com.serialPort.getInputBufferBytesCount() > 0 )
278                                         {
279                                                 String in = com.serialPort.readString(); 
280                                                 data += in;
281                                                 System.out.println("Datain:" +in);
282                                                 if( data.contains("Account Title, (0-31):") )
283                                                 {
284                                                         System.out.println("Found");
285                                                         break;
286                                                 }
287                                         } else {
288                                                 timeOut -= 50;
289                                                 Thread.sleep(50);
290                                         }
291                                 }
292                                 
293                                 if(timeOut > 0 )
294                                 {
295                                         Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_WORKING, "WORKING",acc,'A') );
296
297                                         for(int p=0; p < seq.length();p++)
298                                         {
299                                                 
300                                                 if( com.serialPort.getOutputBufferBytesCount() > 1 )
301                                                 {
302                                                         Thread.sleep(50);
303                                                 } else {
304                                                         com.serialPort.writeByte( (byte)seq.charAt(p) );
305                                                         Thread.sleep(5);
306                                                         
307                                                 }
308                                                 System.out.println( "Type:" + seq.charAt(p));
309                                                 while( com.serialPort.getInputBufferBytesCount() > 0 )
310                                                 {
311                                                         String in = com.serialPort.readString();
312                                                         data += in;
313                                                         System.out.println("Read:" + in);
314                                                 }
315                                         }
316                                         System.out.println("All chars typed, waiting for [done]");
317                                         
318                                         timeOut = 30000;
319                                         while( timeOut > 0 )
320                                         {
321                                                 timeOut -= 50;
322                                                 Thread.sleep(50);
323                                                 if( com.serialPort.getInputBufferBytesCount() > 0 )
324                                                 {
325                                                         String in = com.serialPort.readString();
326                                                         System.out.println("Read>"+in);
327                                                         data += in;
328                                                         if( data.contains("[done]") )
329                                                         {
330                                                                 int begin = data.lastIndexOf("[save entry ")+12;
331                                                                 String subStr = data.substring(begin);
332                                                                 int end = subStr.indexOf("]");
333                                                                 subStr = subStr.substring(0,end);
334                                                                 if( subStr.length()==1)
335                                                                 {
336                                                                         subStr = "0"+subStr;
337                                                                 }
338                                                                 acc.num = subStr;
339                                                                 listAddAcc( acc.num, acc.name);
340                                                                 System.out.println("Account: "+acc.num+" " + acc.name);
341                                                                 Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_OKAY, "DONE:"+data,acc,'A') );
342                                                                 break;
343                                                         } else if( data.contains("[abort]") )
344                                                         {
345                                                                 Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ABORTED, "ERROR:"+data,null,'A') );
346                                                                 break;
347                                                         }
348                                                 }
349                                         }
350                                         if(timeOut < 1)
351                                         {
352                                                 Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ABORTED, "TIMEOUT1:"+data,null,'A') );
353                                         }
354                                 } else {
355                                         Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ABORTED, "TIMEOUT2:"+data,null,'A') );
356                                 }
357                                 
358                                 
359                         } catch (Exception e) {
360                                 Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ERROR, "EXCEPTION",null,'A') );
361                         }
362                         
363                 }
364
365                 
366         }
367
368         private void flushSerial() {
369                 try {
370                 Thread.sleep(10);
371                 
372                 while( com.serialPort.getInputBufferBytesCount()!=0 )
373                 {
374                         System.out.println("Flushed "+com.serialPort.getInputBufferBytesCount()+" bytes >>>" + com.serialPort.readString() + "<<<");
375                 }
376                 } catch(Exception e)
377                 {
378                         
379                 }
380                 
381         }       
382         
383         public void createAccount(String strAccountName, String strUserName,
384                         Boolean autoPassword, int autoPassLen, Boolean autoPassAllSpecials,
385                         String autoPassSpecials, String strPassword, Boolean seperatorTab,
386                         FkActionEventListener delegate) {
387                         
388                         String seq = new String();
389                         
390                         seq += strAccountName;
391                         seq += (char)13;
392                         seq     += strUserName;
393                         seq += (char)13;
394                         
395                         if( autoPassword )
396                         {
397                                 seq += '2';
398                                 seq += autoPassLen;
399                                 seq += (char)13;
400                                 if( autoPassAllSpecials )
401                                 {
402                                         seq += '1';
403                                 } else {
404                                         if( autoPassSpecials.length() > 0 )
405                                         {
406                                                 seq += '2';
407                                                 seq += autoPassSpecials;
408                                                 seq += (char)13;
409                                         } else {
410                                                 seq += '3';
411                                         }
412                                 }
413                         } else {
414                                 //Manual entered password
415                                 seq += '1';
416                                 seq += strPassword;
417                                 seq += (char)13;
418                         }
419                         
420                         //Tab/Enter sep
421                         if( seperatorTab )
422                         {
423                                 seq += '1';
424                         } else {
425                                 seq += '2';
426                         }
427                         
428                         System.out.println("Seq ["+seq+"]");
429                         
430                         NewAccountTask newTask = new NewAccountTask(seq, delegate, strAccountName);
431                         new Thread(newTask).start();                    
432                 
433         }
434
435
436
437         
438         
439 }