Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Date;
- import java.util.Calendar;
- import javax.microedition.io.CommConnection;
- import javax.microedition.midlet.*;
- import SMSHandler;
- import SerialHandler;
- import SocketServidor;
- import SocketServidorC;
- import com.cinterion.io.ATCommand;
- import com.cinterion.io.ATCommandFailedException;
- import com.cinterion.io.ATCommandListener;
- public class GoTrafficStart extends MIDlet {
- private ATCommand atc;
- String imei_response = null;
- CommConnection commConn;
- InputStream inStream;
- OutputStream outStream;
- SocketServidor socketS;
- SocketServidorC socketC;
- SerialHandler serialX;
- FileExec fileX;
- SerialHandler serial;
- SMSHandler sms;
- String porto =null;
- String apn =null;
- String username =null;
- String password =null;
- String dns =null;
- String ip1="";
- String ip2="";
- String teste="";
- String debugcompare="";
- public String debugstate ="";
- boolean flagserial = false;
- public GoTrafficStart () throws MIDletStateChangeException {
- System.out.println("Bem-vindo ao GoTraffic");
- }
- protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
- // TODO Auto-generated method stub
- notifyDestroyed();
- }
- /**
- *
- * @author luis.tavares
- */
- protected void pauseApp() {
- // TODO Auto-generated method stub
- }
- protected void startApp() throws MIDletStateChangeException {
- // TODO Auto-generated method stub
- // System.out.println("Startapp");
- flagserial = false;
- try {
- if(debugstate.compareTo(debugcompare)==0)
- {
- System.out.println("Inicializando, por favor aguarde...");
- }
- // DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
- Date date = new Date();
- Calendar cal = Calendar.getInstance();
- System.out.println(cal.getTime());
- // System.out.println(dateFormat.format(cal)); //2016/11/16 12:08:43
- // System.out.println( date1.getTime());
- // create an instance of ATCommand
- readFile();
- atc = new ATCommand(false);
- // check the registration
- boolean registered = false;
- while (registered == false) {
- imei_response = atc.send("AT+CGSN\r");
- imei_response = imei_response.substring(10,25);
- String registration_response = atc.send("AT+CREG?\r");
- int localy_registered = registration_response.indexOf(",1");
- int roaming_registered = registration_response.indexOf(",5");
- if ((localy_registered > -1) || (roaming_registered > -1)) {
- registered = true;
- if(debugstate.compareTo(debugcompare)==0)
- {
- System.out.println("Controlador registado...");
- }
- } else {
- System.out.println("Module not registered to the network");
- }
- Thread.sleep(2000);
- }
- // create the AT command listener
- // Numero do Modem: 914405547
- ATCommandListener listener = new ATCommandListener() {
- public void RINGChanged(boolean SignalState) {
- }
- public void DSRChanged(boolean SignalState) {
- }
- public void DCDChanged(boolean SignalState) {
- }
- public void CONNChanged(boolean SignalState) {
- }
- public void ATEvent(final String Event) {
- // System.out.println("received URC: " + Event);
- // search for SMS related URCs
- if (Event.indexOf("+CMTI") > 0) {
- //String content = getSmsContent(Event, false);
- //System.out.println("Sms content: " + content);
- System.out.println("Sms content: ");
- }
- }
- };
- // add (activate) the listener
- atc.addListener(listener);
- } catch (Exception e) {
- e.printStackTrace();
- }
- //Objetos de invocação dos métodos
- serial = new SerialHandler();
- sms = new SMSHandler();
- //Criação do socket para abertura comunicação GPRS
- socketS= new SocketServidor(apn,username,password,dns,porto,1,0,false,true,imei_response, imei_response,serial,sms,ip1,ip2,debugstate);
- socketS.start();
- try {
- goRun();
- } catch (IllegalStateException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ATCommandFailedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- void startserial() throws MIDletStateChangeException{
- try {
- serial.run();
- if(debugstate.compareTo(debugcompare)==0)
- {
- System.out.println("Comunicacao Serial iniciada.");
- }
- } catch (IllegalStateException e2) {
- // TODO Auto-generated catch block
- e2.printStackTrace();
- } catch (IllegalArgumentException e2) {
- // TODO Auto-generated catch block
- e2.printStackTrace();
- } catch (InterruptedException e2) {
- // TODO Auto-generated catch block
- e2.printStackTrace();
- } catch (ATCommandFailedException e2) {
- // TODO Auto-generated catch block
- e2.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- void goRun() throws MIDletStateChangeException, IllegalStateException, IllegalArgumentException, ATCommandFailedException{ //Inicia eventos Serial: Inicialização e leitura
- if(!flagserial){ // Já iniciamos o Serial?
- startserial();
- flagserial = true;
- }
- startRead(); // Iniciar leitura
- }
- void startRead() throws MIDletStateChangeException, IllegalStateException, IllegalArgumentException, ATCommandFailedException{
- serial.read();
- // Não é possivel por agora fazer a comparacação com serial.chave.
- try {
- String rcv_ok = "5243565f4f4b205243565f4f4b205243565f4f4b205243565f4f4b205243565f4f4b205243565f4f4b205243565f4f4b205243565f4f4b205243565f4f4b205243565f4f4b205243565f4f4b205243565f4f4b205243565f4f4bd";
- if(serial.hexch.equals(rcv_ok)==false)
- {
- teste = serial.hexch;
- if(serial.hexch!="")
- {
- Thread.sleep(1000);
- socketS.enviarGet(10,teste);
- }
- serial.hexch = "";
- serial.chave = "";
- }
- else if (serial.hexch.equals(rcv_ok)==true)
- {
- System.out.println("Recebeu RCV_OK");
- rcv_ok = "";
- }
- } catch (InterruptedException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalStateException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ATCommandFailedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- goRun();
- }
- public void readFile() throws MIDletStateChangeException, IOException
- {
- System.out.println("Inicializando a leitura do ficheiro config.txt");
- File myFile = new File("config.txt","myDir");
- {
- myFile.open(true);
- byte[] buffer = new byte[1024];
- int len = myFile.read(buffer);
- String dados = null;
- dados = new String(buffer,0,len);
- int val1 = dados.indexOf("=");
- int val2 = dados.indexOf("APN=");
- int val3 = dados.indexOf("Username=");
- int val4 = dados.indexOf("Password=");
- int val5 = dados.indexOf("DNS=");
- int val11 = dados.indexOf("Debug=");
- int val6 = dados.indexOf(";");
- int val7 = dados.indexOf("IP1=");
- int val8 = dados.indexOf("/");
- int val9 = dados.indexOf("IP2=");
- int val10 = dados.indexOf(">");
- porto = dados.substring(val1+1,val2-1);
- apn = dados.substring(val2+4,val3-1);
- username = dados.substring(val3+9,val4-1);
- password = dados.substring(val4+9,val5-1);
- dns = dados.substring(val5+4,val11-1);
- debugstate = dados.substring(val11+6,val6);
- ip1 = dados.substring(val7+4,val8);
- ip2 = dados.substring(val9+4,val10);
- myFile.close();
- System.out.println("Ficheiro de configuracao lido...");
- }}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement