Advertisement
Guest User

Untitled

a guest
Mar 31st, 2016
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. import com.cinterion.io.ATCommand;
  2. import coreb.events.CallbackATCommand;
  3. import java.util.Random;
  4.  
  5. /**
  6.  *
  7.  * @author Jure
  8.  */
  9. public class ATCommandIssuer
  10. {
  11.     ATCommandData[] ATCommands;
  12.     ATCommandHandler atCommandHandler = ATCommandHandler.getInstance();
  13.     String id;
  14.     String lastResponse = "";
  15.  
  16.     public ATCommandIssuer(ATCommandData[] ATCommands)
  17.     {
  18.         this.ATCommands = ATCommands;
  19.         Random rand = new Random();
  20.         id = rand.nextInt() + "";
  21.         atCommandHandler.addATCommand(id);
  22.     }
  23.    
  24.     public void exectute(final CallbackATCommand callback)
  25.     {
  26.         new Thread()
  27.         {
  28.             public void run()
  29.             {
  30.                 try
  31.                 {
  32.                     while(!id.equalsIgnoreCase(atCommandHandler.getThreadId()))
  33.                     {
  34.                         try
  35.                         {
  36.                             Thread.sleep(300);
  37.                         } catch (Exception e)
  38.                         {
  39.                        
  40.                         }
  41.                     }
  42.                     ATCommand m_Cmd = null;
  43.                    
  44.                     for(int i = 0; i < ATCommands.length; i++)
  45.                     {
  46.                         try
  47.                         {
  48.                             m_Cmd = new ATCommand(true);
  49.                             lastResponse = m_Cmd.send(ATCommands[i].atCommand + "\r");
  50.                             Thread.sleep (200); // Remember at least to implement 100 ms sleep in order to recevie any URC
  51.                             m_Cmd.release();
  52.                         }
  53.                         catch (Exception e)
  54.                         {
  55.                             m_Cmd.release();
  56.                         }
  57.                     }
  58.                     atCommandHandler.commandFinished();
  59.                     if(lastResponse.length() > 4)
  60.                     {
  61.                         if(lastResponse.substring(lastResponse.length() - 4, lastResponse.length()-2).equalsIgnoreCase("OK"))
  62.                         {
  63.                             callback.onSuccess(lastResponse);
  64.                         }
  65.                         else
  66.                         {
  67.                             callback.onFailure(lastResponse);
  68.                         }
  69.                     }
  70.                 }
  71.                 catch (Exception e)
  72.                 {
  73.                     atCommandHandler.commandFinished();
  74.                     callback.onError(e.toString());
  75.                     errorLogger.addError(e.toString(), false);
  76.                 }
  77.             }
  78.         }.start();
  79.     }    
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement