00001
00002 package main;
00003
00004 import javax.comm.SerialPort;
00005
00006 import radioInterface.NewAssociationIndication;
00007 import radioInterface.NewAssociationIndicationListener;
00008 import radioInterface.NewDataIndication;
00009 import radioInterface.NewDataIndicationListener;
00010 import radioInterface.NewDisassociationIndication;
00011 import radioInterface.NewDisassociationIndicationListener;
00012 import radioInterface.RadioInterface;
00013 import radioInterface.RadioInterfaceStartException;
00014 import serialInterface.SerialPortUtilities;
00015
00016 import gui.ChatWindow;
00017 import gui.ChatWindowEvent;
00018 import gui.ChatWindowEventListener;
00019 import gui.Dialogs;
00020 import gui.MainFrame;
00021 import gui.MainFrameEvent;
00022 import gui.MainFrameEventListener;
00023
00024
00042 public class EventHandler implements NewAssociationIndicationListener, NewDisassociationIndicationListener,
00043 NewDataIndicationListener, MainFrameEventListener, ChatWindowEventListener{
00044
00045 private static final int RETRY_LIMIT = 4;
00046 private static final int MAX_DATA_LENGTH = 30;
00047 private MainFrame Main;
00048 private ChatWindow chat;
00049 private RadioInterface radio;
00050
00051 private boolean isConnected;
00052 private boolean isCoordinator;
00053 private boolean networkStarted;
00054
00067 public EventHandler( MainFrame MainTab, ChatWindow ChatTab ){
00068
00069 this.Main = MainTab;
00070 this.chat = ChatTab;
00071 this.radio = null;
00072
00073 isConnected = false;
00074 isCoordinator = false;
00075 networkStarted = false;
00076
00077
00078 this.Main.addMainFrameEventListener( this );
00079 this.chat.addChatWindowEventListener( this );
00080 }
00081
00082
00083
00084
00093 public void NewAssociationIndicationEvent( NewAssociationIndication newAssociation ){
00094
00095 if( isCoordinator ){
00096
00097 networkStarted = true;
00098 chat.clearConversation( );
00099 chat.enableMessageWindow( );
00100 }
00101
00102 else{
00103 Dialogs.showPlainMessage( Main, "End-devices are not supposed to be open for association!" );
00104 }
00105 }
00106
00115 public void NewDisassociationIndicationEvent( NewDisassociationIndication newDisAssociation ){
00116
00117 }
00118
00127 public void NewDataIndicationEvent( NewDataIndication newData ){
00128
00129
00130 chat.appendIncommingMessage( newData.getNewData( ) );
00131 }
00132
00141 public void MainFrameEvent( MainFrameEvent event ) {
00142
00143
00144 int eventType = event.getEventType( );
00145
00146
00147 if( eventType == MainFrameEvent.CONNECT ){
00148
00149 Main.clearDebugMesasges( );
00150 Main.appendDebugMessage( "EVENT: Connect" );
00151
00152
00153 if( !isConnected ){
00154
00155 Main.appendDebugMessage( "Not connected...initialize radio interface." );
00156
00157
00158 if( initializeRadio( ) ){
00159
00160 Main.appendDebugMessage( "Radio interface initialized." );
00161
00162
00163 if( sendResetWithRetry( ) ){
00164
00165 isConnected = true;
00166 networkStarted = false;
00167
00168
00169 if( configureRadioPlatform( ) ){
00170
00171
00172 if( isCoordinator ){
00173
00174 chat.clearConversation( );
00175
00176 Main.appendDebugMessage( "Coordinator Started." );
00177 chat.appendTextToConversation( "Network not started yet." );
00178 }
00179
00180 else{
00181
00182 Main.appendDebugMessage( "End-device Started." );
00183 networkStarted = true;
00184 chat.clearConversation( );
00185 chat.enableMessageWindow( );
00186 }
00187
00188 Main.addChatTab( chat );
00189 radio.addNewAssociationIndicationListeners( this );
00190 radio.addNewDataIndicationListeners( this );
00191 radio.addNewDisassociationIndicationListeners( this );
00192 radio.enableIndications( );
00193 }
00194
00195 else{
00196
00197 isConnected = false;
00198 radio.disableIndications( );
00199 radio.removeNewAssociationIndicationListeners( this );
00200 radio.removeNewDataIndicationListeners( this );
00201 radio.removeNewDisassociationIndicationListeners( this );
00202 radio.terminateCommunication( );
00203
00204 Main.appendDebugMessage( "Network configuration and start-up failed." );
00205 Dialogs.showPlainMessage( Main, "Unable to start network." );
00206 }
00207 }
00208
00209 else{
00210
00211 radio.terminateCommunication( );
00212
00213 Main.appendDebugMessage( "AT-Command AT+R failed. Check serial connection." );
00214 Dialogs.showPlainMessage( Main, "Please connect radio platform first." );
00215 }
00216 }
00217
00218
00219 else{
00220
00221 Main.appendDebugMessage( "Something went wrong during initialization." );
00222 Dialogs.showPlainMessage( Main, "Radio Interface could not be started." +
00223 "\r\nPlease try to reconnect!" );
00224 }
00225 }
00226
00227
00228 else{
00229
00230 Main.appendDebugMessage( "Already connected." );
00231 Dialogs.showPlainMessage( Main, "You are already connected." );
00232 }
00233 }
00234
00235
00236 else if( eventType == MainFrameEvent.DISCONNECT ){
00237
00238 Main.appendDebugMessage( "EVENT: Disconnect" );
00239
00240
00241 if( isConnected == false ){
00242
00243 Main.appendDebugMessage( "You are not connected." );
00244 Dialogs.showPlainMessage( Main, "You are not connected." );
00245 }
00246
00247
00248 else{
00249
00250 Main.appendDebugMessage( "You are connected...terminating RF link." );
00251 Main.clearDebugMesasges( );
00252
00253 isConnected = false;
00254 isCoordinator = false;
00255 networkStarted = false;
00256 radio.terminateCommunication( );
00257
00259 chat.disableMessageWindow( );
00260 Main.closeChatTab( chat );
00261 }
00262 }
00263
00264
00265 else{
00266
00267 Main.clearDebugMesasges( );
00268 }
00269 }
00270
00279 public void ChatWindowEvent( ChatWindowEvent event ) {
00280
00281 int eventType = event.getEventType( );
00282 String Message = event.getData( );
00283
00284
00285 if( ( eventType == ChatWindowEvent.ENTER ) || ( eventType == ChatWindowEvent.SEND ) ){
00286
00287
00288 if( networkStarted ){
00289
00290 sendMessage( Message );
00291 }
00292
00293 else{
00294
00295 Dialogs.showPlainMessage( Main, "Not possible to send data until connection is established.");
00296 }
00297 }
00298
00299
00300 else{
00301
00302 chat.clearMessage( );
00303 }
00304 }
00305
00306
00307
00316 private boolean initializeRadio( ){
00317
00318
00319 SerialPort portToUse = Dialogs.chooseSerialPort( Main );
00320
00321 if( ( portToUse != null ) && ( SerialPortUtilities.setBaudRate( portToUse, 38400 ) ) ){
00322
00323 Main.appendDebugMessage( "Serial Port: " + portToUse.getName() + " open." );
00324
00325 try{
00326 radio = new RadioInterface( portToUse );
00327 }catch( RadioInterfaceStartException rise ){
00328
00329 Main.appendDebugMessage( "Failed to open RadioInterface." );
00330 rise.printStackTrace( );
00331 portToUse.close( );
00332
00333 return false;
00334 }
00335
00336 return true;
00337 }
00338
00339
00340 else{
00341
00342 if( portToUse != null ){
00343 portToUse.close( );
00344 }
00345
00346 return false;
00347 }
00348 }
00349
00363 private boolean sendResetWithRetry( ){
00364
00365 int retryCount = 0;
00366
00367 do{
00368
00369 Main.appendDebugMessage( "Send AT+R." );
00370
00371 if( radio.sendReset( ) ){
00372
00373 Main.appendDebugMessage( "AT+R command successful." );
00374 return true;
00375 }
00376
00377 else{
00378
00379 int responseFromUser = Dialogs.showRetryMessage( Main );
00380
00381
00382 if( responseFromUser != 0 ){
00383
00384 Main.appendDebugMessage( "AT+R command failed." );
00385 return false;
00386 }
00387 }
00388
00389 retryCount++;
00390 }
00391 while( retryCount < RETRY_LIMIT );
00392
00393 Main.appendDebugMessage( "AT+R command failed." );
00394 Dialogs.showErrorMessage( Main, "Too many retries." );
00395 return false;
00396 }
00397
00411 private boolean configureRadioPlatform( ){
00412
00413 String[ ] userOptions = Dialogs.showConfigurationDialog( Main );
00414
00415 if( userOptions[ 2 ].equals( "EndDevice" ) ){
00416
00417 isCoordinator = false;
00418 }
00419
00420 else{
00421
00422 isCoordinator = true;
00423 }
00424
00425 return sendConfigureWithRetry( userOptions[ 0 ], userOptions[ 1 ], userOptions[ 2 ] );
00426 }
00427
00441 private boolean sendConfigureWithRetry( String channel, String panID, String deviceType ){
00442
00443 int retryCount = 0;
00444
00445 do{
00446
00447 Main.appendDebugMessage( "Send AT+C." );
00448
00449
00450 if( radio.sendConfigure( channel, panID, deviceType ) ){
00451
00452 Main.appendDebugMessage( "AT+C command successful." );
00453 return true;
00454 }
00455
00456 else{
00457
00458 int responseFromUser = Dialogs.showRetryMessage( Main );
00459
00460
00461 if( responseFromUser != 0 ){
00462
00463 Main.appendDebugMessage( "AT+C command failed." );
00464 return false;
00465 }
00466 }
00467
00468 retryCount++;
00469 }
00470 while( retryCount < RETRY_LIMIT );
00471
00472 Main.appendDebugMessage( "AT+C command failed." );
00473 Dialogs.showErrorMessage( Main, "Too many retries." );
00474 return false;
00475 }
00476
00490 public void sendMessage( String Message ){
00491
00492
00493 int dataLength = Message.length( );
00494
00495
00496 if( dataLength == 0 ){
00497
00498 Dialogs.showErrorMessage( Main, "Pointless to send empty message." );
00499 }
00500
00501
00502 else if( dataLength < MAX_DATA_LENGTH ){
00503
00504 if( sendData( Message ) ){
00505
00506 chat.appendOutgoingMessage( Message );
00507 chat.clearMessage( );
00508 }
00509 }
00510
00511
00512 else{
00513
00514 Dialogs.showErrorMessage( Main, "Message too long." );
00515 chat.clearMessage( );
00516 }
00517 }
00518
00529 private boolean sendData( String data ){
00530
00531 int retryCount = 0;
00532
00533 do{
00534
00535 Main.appendDebugMessage( "Send AT+T. Message: ' " + data + " '" );
00536
00537
00538 if( radio.sendData( data ) ){
00539
00540 Main.appendDebugMessage( "AT+T command successful." );
00541 return true;
00542 }
00543
00544 else{
00545
00546 int responseFromUser = Dialogs.showRetryMessage( Main );
00547
00548
00549 if( responseFromUser != 0 ){
00550
00551 Main.appendDebugMessage( "AT+T command failed." );
00552 return false;
00553 }
00554 }
00555
00556 retryCount++;
00557 }
00558 while( retryCount < RETRY_LIMIT );
00559
00560 Main.appendDebugMessage( "AT+T command failed." );
00561 Dialogs.showErrorMessage( Main, "Too many retries." );
00562 return false;
00563 }
00564 }