Advertisement
Guest User

GoTrafficStart

a guest
Nov 22nd, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.31 KB | None | 0 0
  1.  
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.util.Date;
  7. import java.util.Calendar;
  8. import javax.microedition.io.CommConnection;
  9. import javax.microedition.midlet.*;
  10. import SMSHandler;
  11. import SerialHandler;
  12. import SocketServidor;
  13. import SocketServidorC;
  14. import com.cinterion.io.ATCommand;
  15. import com.cinterion.io.ATCommandFailedException;
  16. import com.cinterion.io.ATCommandListener;
  17.  
  18.  
  19.  
  20.  
  21. public class GoTrafficStart extends MIDlet {
  22.    
  23.     private ATCommand atc;
  24.     String imei_response = null;
  25.    
  26.    
  27.     CommConnection  commConn;
  28.     InputStream     inStream;
  29.     OutputStream    outStream;
  30.    
  31.    
  32.    
  33.     SocketServidor socketS;
  34.     SocketServidorC socketC;
  35.     SerialHandler serialX;
  36.     FileExec fileX;
  37.     SerialHandler serial;
  38.     SMSHandler sms;
  39.    
  40.    
  41.    
  42.     String porto =null;
  43.     String apn =null;
  44.     String username =null;
  45.     String password =null;
  46.     String dns =null;
  47.     String ip1="";
  48.     String ip2="";
  49.     String teste="";
  50.     String debugcompare="";
  51.     public String debugstate ="";
  52.     boolean flagserial = false;
  53.    
  54.    
  55.    
  56.    
  57.     public GoTrafficStart () throws MIDletStateChangeException {
  58.        
  59.        
  60.             System.out.println("Bem-vindo ao GoTraffic");
  61.  
  62.        
  63.  
  64.  
  65.    
  66.     }
  67.  
  68.     protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  69.         // TODO Auto-generated method stub
  70.        
  71.         notifyDestroyed();
  72.        
  73.    
  74.    
  75.     }
  76.  
  77.  
  78. /**
  79. *
  80. * @author luis.tavares
  81. */
  82.     protected void pauseApp() {
  83.         // TODO Auto-generated method stub
  84.  
  85.     }
  86.    
  87.     protected void startApp() throws MIDletStateChangeException {
  88.         // TODO Auto-generated method stub
  89.      //   System.out.println("Startapp");
  90.        flagserial = false;
  91.         try {
  92.             if(debugstate.compareTo(debugcompare)==0)
  93.             {
  94.                 System.out.println("Inicializando, por favor aguarde...");
  95.  
  96.             }
  97.         //  DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  98.            
  99.             Date date = new Date();
  100.  
  101.             Calendar cal = Calendar.getInstance();
  102.            
  103.             System.out.println(cal.getTime());
  104.          
  105.  //         System.out.println(dateFormat.format(cal)); //2016/11/16 12:08:43
  106.      //      System.out.println( date1.getTime());
  107.              // create an instance of ATCommand
  108.              readFile();
  109.              atc = new ATCommand(false);
  110.              // check the registration
  111.              boolean registered = false;
  112.              while (registered == false) {
  113.              
  114.              imei_response = atc.send("AT+CGSN\r");  
  115.              imei_response = imei_response.substring(10,25);
  116.              String registration_response = atc.send("AT+CREG?\r");
  117.              int localy_registered = registration_response.indexOf(",1");
  118.              int roaming_registered = registration_response.indexOf(",5");
  119.  
  120.              if ((localy_registered > -1) || (roaming_registered > -1)) {
  121.                  registered = true;
  122.                  if(debugstate.compareTo(debugcompare)==0)
  123.                 {
  124.                  System.out.println("Controlador registado...");
  125.                 }
  126.              } else {
  127.                  System.out.println("Module not registered to the network");
  128.                  
  129.                  
  130.                  }
  131.  
  132.              Thread.sleep(2000);
  133.             }
  134.  
  135.        
  136.        
  137.           // create the AT command listener
  138.           // Numero do Modem: 914405547
  139.  
  140.             ATCommandListener listener = new ATCommandListener() {
  141.                    public void RINGChanged(boolean SignalState) {
  142.                    }
  143.                    public void DSRChanged(boolean SignalState) {
  144.                    }
  145.                    public void DCDChanged(boolean SignalState) {
  146.                    }
  147.                    public void CONNChanged(boolean SignalState) {
  148.                    }
  149.                    public void ATEvent(final String Event) {
  150.                    //       System.out.println("received URC: " + Event);
  151.  
  152.                            // search for SMS related URCs
  153.                    if (Event.indexOf("+CMTI") > 0) {
  154.                                //String content = getSmsContent(Event, false);
  155.                                //System.out.println("Sms content: " + content);
  156.                                System.out.println("Sms content: ");
  157.                                }
  158.                    }
  159.                  };
  160.  
  161.                 // add (activate) the listener
  162.                 atc.addListener(listener);
  163.              
  164.                } catch (Exception e) {
  165.                          e.printStackTrace();
  166.                }
  167.      
  168.      
  169.        
  170.         //Objetos de invocação dos métodos
  171.         serial = new SerialHandler();
  172.         sms = new SMSHandler();
  173.         //Criação do socket para abertura comunicação GPRS
  174.         socketS= new SocketServidor(apn,username,password,dns,porto,1,0,false,true,imei_response, imei_response,serial,sms,ip1,ip2,debugstate);
  175.         socketS.start();
  176.        
  177.         try {
  178.             goRun();
  179.         } catch (IllegalStateException e) {
  180.             // TODO Auto-generated catch block
  181.             e.printStackTrace();
  182.         } catch (IllegalArgumentException e) {
  183.             // TODO Auto-generated catch block
  184.             e.printStackTrace();
  185.         } catch (ATCommandFailedException e) {
  186.             // TODO Auto-generated catch block
  187.             e.printStackTrace();
  188.         }
  189.        
  190.     }
  191.    
  192.     void startserial() throws MIDletStateChangeException{
  193.             try {
  194.                
  195.                 serial.run();
  196.                 if(debugstate.compareTo(debugcompare)==0)
  197.                 {
  198.                     System.out.println("Comunicacao Serial iniciada.");
  199.                 }
  200.             } catch (IllegalStateException e2) {
  201.                 // TODO Auto-generated catch block
  202.                 e2.printStackTrace();
  203.             } catch (IllegalArgumentException e2) {
  204.                 // TODO Auto-generated catch block
  205.                 e2.printStackTrace();
  206.             } catch (InterruptedException e2) {
  207.                 // TODO Auto-generated catch block
  208.                 e2.printStackTrace();
  209.             } catch (ATCommandFailedException e2) {
  210.                 // TODO Auto-generated catch block
  211.                 e2.printStackTrace();
  212.             } catch (IOException e) {
  213.                 // TODO Auto-generated catch block
  214.                 e.printStackTrace();
  215.             }
  216.        
  217.     }
  218.    
  219.     void goRun() throws MIDletStateChangeException, IllegalStateException, IllegalArgumentException, ATCommandFailedException{    //Inicia eventos Serial: Inicialização e leitura
  220.         if(!flagserial){  // Já iniciamos o Serial?
  221.             startserial();
  222.             flagserial = true;
  223.         }
  224.        
  225.         startRead();   // Iniciar leitura
  226.     }
  227.    
  228.     void startRead() throws MIDletStateChangeException, IllegalStateException, IllegalArgumentException, ATCommandFailedException{
  229.         serial.read();
  230.         // Não é possivel por agora fazer a comparacação com serial.chave.
  231.             try {
  232.                 String rcv_ok = "5243565f4f4b205243565f4f4b205243565f4f4b205243565f4f4b205243565f4f4b205243565f4f4b205243565f4f4b205243565f4f4b205243565f4f4b205243565f4f4b205243565f4f4b205243565f4f4b205243565f4f4bd";
  233.                 if(serial.hexch.equals(rcv_ok)==false)
  234.                 {
  235.                     teste = serial.hexch;
  236.                     if(serial.hexch!="")
  237.                     {
  238.                     Thread.sleep(1000);
  239.                     socketS.enviarGet(10,teste);
  240.                     }
  241.                     serial.hexch = "";
  242.                     serial.chave = "";
  243.                 }
  244.                 else if (serial.hexch.equals(rcv_ok)==true)
  245.                 {
  246.                     System.out.println("Recebeu RCV_OK");
  247.                     rcv_ok = "";
  248.                 }
  249.         } catch (InterruptedException e1) {
  250.             // TODO Auto-generated catch block
  251.             e1.printStackTrace();
  252.         } catch (IOException e) {
  253.             // TODO Auto-generated catch block
  254.             e.printStackTrace();
  255.         } catch (IllegalStateException e) {
  256.             // TODO Auto-generated catch block
  257.             e.printStackTrace();
  258.         } catch (IllegalArgumentException e) {
  259.             // TODO Auto-generated catch block
  260.             e.printStackTrace();
  261.     } catch (ATCommandFailedException e) {
  262.             // TODO Auto-generated catch block
  263.             e.printStackTrace();
  264.         }
  265.        
  266.         goRun();
  267.        
  268.         }
  269.        
  270.     public void readFile() throws MIDletStateChangeException, IOException
  271.     {
  272.             System.out.println("Inicializando a leitura do ficheiro config.txt");
  273.  
  274.        
  275.            
  276.             File myFile = new File("config.txt","myDir");
  277.            
  278.              {
  279.                 myFile.open(true);
  280.                 byte[] buffer = new byte[1024];
  281.                 int len = myFile.read(buffer);
  282.                 String dados = null;
  283.                 dados = new String(buffer,0,len);
  284.                 int val1 = dados.indexOf("=");
  285.                 int val2 = dados.indexOf("APN=");
  286.                 int val3 = dados.indexOf("Username=");
  287.                 int val4 = dados.indexOf("Password=");
  288.                 int val5 = dados.indexOf("DNS=");
  289.                 int val11 = dados.indexOf("Debug=");
  290.                 int val6 = dados.indexOf(";");
  291.                 int val7 = dados.indexOf("IP1=");
  292.                 int val8 = dados.indexOf("/");
  293.                 int val9 = dados.indexOf("IP2=");
  294.                 int val10 = dados.indexOf(">");
  295.                 porto = dados.substring(val1+1,val2-1);
  296.                 apn = dados.substring(val2+4,val3-1);
  297.                 username = dados.substring(val3+9,val4-1);
  298.                 password = dados.substring(val4+9,val5-1);
  299.                 dns = dados.substring(val5+4,val11-1);
  300.                 debugstate = dados.substring(val11+6,val6);
  301.                 ip1 = dados.substring(val7+4,val8);
  302.                 ip2 = dados.substring(val9+4,val10);
  303.                 myFile.close();
  304.                
  305.                 System.out.println("Ficheiro de configuracao lido...");
  306.                
  307.             }}
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement