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