teste de publish

Dependencies:   DS1820 HighSpeedAnalogIn devices mbed

Revision:
10:263c093f8977
Parent:
9:cf406384efd9
Child:
11:631bea162800
--- a/modem.cpp	Wed Aug 09 20:38:10 2017 +0000
+++ b/modem.cpp	Mon Aug 14 13:59:27 2017 +0000
@@ -87,9 +87,8 @@
     sdCardBuf.fill(aux,strlen(aux));
     
     sprintf(aux,";imsi=%s}log",modemCom::status.MAC);    
-    sdCardBuf.fill(aux,strlen(aux));
-    
-    return sdCard::insereDadosArquivo(&sdCard::armazenamento,sdCardBuf.get(),sdCardBuf.getLength());    
+    sdCardBuf.fill(aux,strlen(aux));    
+    return sdCard::insereDadosBank(sdCardBuf.get(),sdCardBuf.getLength());    
 }
 
 bool modemCom::montaStatus(void)
@@ -106,45 +105,58 @@
 
 
 
-uint8_t modemCom::enviaDados(char tipoDado)
+uint8_t modemCom::enviaDados()
 {
-    uint8_t result=0;
+    uint8_t result=0,currentBank=3;
     modemCom::status.emComunicacao = true;
-
-    switch(tipoDado) {
-        case dadosArmazenados:
-            if(!sdCard::preparaEnvio()){
-                //pc.printf("Nao foi possivel preparar o envio.\n");
-                return false;                
-            }
-            sdCard::getFileTam(&sdCard::envio);            
-            //pc.printf("Preparado o envio do arquivo com %lu bytes.\n",sdCard::envio.bytes);
-            break;
-        case dadosStatus:
-            modemCom::montaStatus();
-            break;
+    pc.printf("modemCom::enviaDados[1].\r\n");
+    if(!sdCard::preparaEnvio()){
+        pc.printf("modemCom::enviaDados[2].\r\n");
+        //pc.printf("Nao foi possivel preparar o envio.\n");
+        return false;                
     }
-    
-    switch(tipoDado) {
-        case dadosArmazenados:
-                result = modemCom::postFileCommandMode(modemCom::status.host,"/drome/Parser/",&sdCard::envio);                
-            break;
-        case dadosStatus:
-                result = modemCom::postFileCommandMode(modemCom::status.host,"/drome/Parser/",&sdCard::status);
+    currentBank = sdCard::verificaCurrentBank();
+    pc.printf("modemCom::enviaDados[3], currentBank = %lu.\r\n",currentBank);
+    switch(currentBank){
+        case 0:
+                pc.printf("modemCom::enviaDados[4].\r\n");
+                if(sdCard::getFileTam(&sdCard::bank1)){
+                   pc.printf("modemCom::enviaDados[11].\r\n");
+                   if(modemCom::postFileCommandMode(modemCom::status.host,"/drome/Parser/",&sdCard::bank1)){
+                    pc.printf("Enviado o bank1.\r\n");
+                    if(sdCard::deleteSentFiles){                        
+                        sdCard::deleteBanks(1);
+                        sdCard::modificaCurrentBank(1);
+                    }
+                   }
+                }else{
+                    pc.printf("modemCom::enviaDados[5].\r\n");
+                    sdCard::modificaCurrentBank(1);
+                 }                
             break;
-    }
-    
-    if(!result){
-        return result;    
-    }
-
-    //Envia uma string nula e faz o processador esperar a resposta do server!
-    if(modemCom::status.ServerAck) {
-        //pc.printf("Enviado e recebido resposta do server (*ServerAck*).\n");
-    } else {
-        //pc.printf("Enviado mas nao recebido resposta do server (*ServerAck*).\n");
-        //pc.printf("Conteudo de getRowBuffer <%s>.\n",bufModem.getRowBuffer());
-    }        
+        case 1:
+                pc.printf("modemCom::enviaDados[6].\r\n");
+                if(sdCard::getFileTam(&sdCard::bank0)){
+                   pc.printf("modemCom::enviaDados[7].\r\n");
+                   if(modemCom::postFileCommandMode(modemCom::status.host,"/drome/Parser/",&sdCard::bank0)){
+                    pc.printf("Enviado o bank0.\r\n");
+                    if(sdCard::deleteSentFiles){
+                        sdCard::deleteBanks(0);
+                        pc.printf("modemCom::enviaDados[8].\r\n");
+                        sdCard::modificaCurrentBank(0);
+                    }
+                   }
+                }else{
+                    pc.printf("modemCom::enviaDados[9].\r\n");
+                    sdCard::modificaCurrentBank(0);
+                 }                
+            break;        
+        case 2:
+                pc.printf("modemCom::enviaDados[10].\r\n");
+                modemCom::status.emComunicacao = false;
+                return false;
+            break;
+    }    
     return result;
 }
 
@@ -165,6 +177,15 @@
     uint32_t dataTam=0;
     char c;
     bool passaParaProximaParte = false;
+    char nomeArquivo[50];
+
+    sdCard::deleteSentFiles = false;
+
+    if(strstr(arquivo->nome,"bank0.txt")||strstr(arquivo->nome,"bank1.txt")){
+        strcpy(nomeArquivo,"/sd/RAD/envio.txt");
+    }else{
+        strcpy(nomeArquivo,arquivo->nome);
+     }
 
     //Para garantir que não seja duvidoso proveniente de outra conexão.
     modemCom::status.ServerAck=0;
@@ -181,10 +202,10 @@
     
     pc.printf("Abrindo o socket em <%s>.\n",host);
     sprintf(header,"AT+CIPSTART=1,\"TCP\",\"%s\",80\r\n",host);
-    if(modemCom::sendToModem(header,1,&modemCom::status.OK,NULL,20000,1,1)) {
+    if(modemCom::sendToModem(header,1,&modemCom::status.OK,NULL,20000,3,1000)) {
         modemCom::status.NO_CARRIER=0; //Socket Aberto!
         pc.printf("Aberto o socket em command mode.\n");
-        pc.printf("Enviando arquivo com nome %s.\n",arquivo->nome);
+        pc.printf("Enviando arquivo com nome %s.\n",nomeArquivo);
         modemCom::status.serverConnected=1;
         modemCom::timeOutModem = 100;
         modemCom::status.emComunicacao = true;
@@ -209,7 +230,7 @@
     
     //Monta Header aqui!
     //application/octet-stream
-    sprintf(header,"--xxBOUNDARYxx\r\nContent-Type: text/plain\r\nContent-Disposition: form-data; name=\"IMEI\"\r\n\r\n%s\r\n--xxBOUNDARYxx\r\nContent-Type: application/octet-stream\r\nContent-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n\r\n",modemCom::status.MAC,arquivo->nome);
+    sprintf(header,"--xxBOUNDARYxx\r\nContent-Type: text/plain\r\nContent-Disposition: form-data; name=\"IMEI\"\r\n\r\n%s\r\n--xxBOUNDARYxx\r\nContent-Type: application/octet-stream\r\nContent-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n\r\n",modemCom::status.MAC,nomeArquivo);
     
     dataTam = strlen(header);   
     dataTam+=arquivo->bytes;
@@ -218,7 +239,7 @@
     sprintf(header,"POST %s HTTP/1.1\r\nHost: %s\r\nContent-Type: multipart/form-data; boundary=xxBOUNDARYxx\r\nContent-Length: %lu\r\n\r\n",uri,host,dataTam); 
     headerTam = strlen(header); //Pego o tamanho parcial para fazer a concatenaçao a fim de predizer o tamanho do post total
     
-    sprintf(&header[headerTam],"--xxBOUNDARYxx\r\nContent-Type: text/plain\r\nContent-Disposition: form-data; name=\"IMEI\"\r\n\r\n%s\r\n--xxBOUNDARYxx\r\nContent-Type: application/octet-stream\r\nContent-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n\r\n",modemCom::status.MAC,arquivo->nome);
+    sprintf(&header[headerTam],"--xxBOUNDARYxx\r\nContent-Type: text/plain\r\nContent-Disposition: form-data; name=\"IMEI\"\r\n\r\n%s\r\n--xxBOUNDARYxx\r\nContent-Type: application/octet-stream\r\nContent-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n\r\n",modemCom::status.MAC,nomeArquivo);
     headerTam = strlen(header);
 
     sprintf(aux,"AT+CIPSEND=1,%u\r\n",headerTam);
@@ -341,7 +362,7 @@
     sdCard::fechaArquivo(arquivo);
 
     //Logica de timeout esperando confirmação modemCom::status.ServerAck 20 segundos
-    parteSendoEnviada = 200;    
+    parteSendoEnviada = 50;    
     while((parteSendoEnviada)&&(!modemCom::status.ServerAck)&&(!modemCom::status.NO_CARRIER)) {
         osDelay(100);
         parteSendoEnviada--;
@@ -480,7 +501,7 @@
 
     if(strstr(webServerBuff,"lecomandocontrole.htm")&&(!encontrado)){        
         
-        uint16_t timeout = 5000;
+        uint16_t timeout = 5000;        
         IrDetect.reset();
         IrDetect.start();
         
@@ -826,95 +847,7 @@
     
     modemCom::status.modemResponse=true;
     
-    pc.printf("Vem do modem <%s>.\r\n",modemCom::bufIn);    
-    
-    if(strstr(modemCom::bufIn,"GET /")){
-        getFound = true;
-    }
-    
-    //Buscando connect
-    if(!modemCom::atendendoWebServer){
-        ptr=strstr(modemCom::bufIn,"CONNECT\r\n");
-        //if(strstr(modemCom::bufIn,"WIFI")){ptr=NULL;}
-        if(ptr) {                
-            ptr = strtok(modemCom::bufIn,",");
-            connID = atoi(ptr);                                    
-            modemCom::status.serverConnected=1;        
-            pc.printf("Recebido string CONNECT socket na conexao %u.\n",connID);
-            if(!modemCom::status.emComunicacao){                                                
-                if(!getFound){
-                    pc.printf("Atendendo socket entrante na conexao %u.\n",connID);
-                    modemCom::status.SRINGsockEntrante = true;
-                    modemCom::status.connIDServerCommand = connID;
-                }
-            }
-            modemCom::status.emComunicacao=true;
-            modemCom::bufIn = strtok(NULL,"");
-        }    
-    }
-    
-    IPDNumBytes = 0;
-    ptr = strstr(modemCom::bufIn,"+IPD");
-    if(ptr!=NULL){
-        //+IPD,0,480:GET
-        strtok(ptr,",");           //Separando +IPD
-        
-        ptr = strtok(NULL,",");    //Pegando o numero da conexão
-        connID = atoi(ptr);
-        
-        ptr = strtok(NULL,":");    //Pegando o numero de caracteres IPD
-        IPDNumBytes = atoi(ptr);
-        
-        modemCom::bufIn = strtok(NULL,"");
-        
-        //printf("Recebido pacote via conexao %u com %lu bytes.\n",connID,IPDNumBytes);
-        
-    }
-    
-    //Reconhecendo GET HTTP e atendendo com um servidor web    
-    if(getFound && strstr(modemCom::bufIn,"HTTP/1.1")){
-        ptr=strstr(modemCom::bufIn,"GET /");
-        if(ptr) {            
-            ptr = strtok(ptr,"/");
-            ptr = strtok(NULL," ");
-            bufInPtr = ptr;
-            strcpy(webServerBuff,ptr);
-            modemCom::atendendoWebServer = true;
-            modemCom::status.connIDWebServer = connID;
-        }
-    }
-    
-    ptr=strstr(modemCom::bufIn,"*ServerAck*");
-    if(ptr) {        
-        modemCom::status.ServerAck=1;        
-    }        
-    
-    ptr=strstr(modemCom::bufIn,"*ServerCommand*");
-    if(ptr) {        
-        scanPtr = strtok (ptr,"\\");
-        scanPtr = strtok (NULL,">");        
-        strcpy(commands::buffer,scanPtr);
-        pc.printf("Comandos <%s>.\n",commands::buffer);
-        executaComandoServer = true;
-    }
-    
-    //if(modemCom::status.recebendoArquivoDoServer){
-    if(IPDNumBytes && modemCom::status.recebendoArquivoDoServer && (strstr(modemCom::bufIn,"CONNECT")==NULL) && (strstr(modemCom::bufIn,"*ServerCommand*")==NULL) ){
-    
-        sdCard::insereDadosArquivo(&sdCard::tempFile,modemCom::bufIn,IPDNumBytes);
-        bufModem.del();
-        sprintf(modemCom::bufIn,"AT+CIPSEND=%u,%lu\r\n",connID,10);
-        modem.puts(modemCom::bufIn);
-        timeOut = 5000;
-        while(timeOut && (strstr(bufModem.getRowBuffer(),">")==NULL)){
-            osDelay(1);
-            timeOut--;
-        }
-        modemCom::status.timeOut = 30;
-        modem.puts("sendData\r\n");
-        pc.printf("sendData\n");        
-        return;
-    }    
+    pc.printf("Vem do modem <%s>.\r\n",modemCom::bufIn);   
     
     //Reconhecimento dos status
     modemCom::status.PROMPT_ENVIO_COMMAND_MODE=0;
@@ -926,13 +859,14 @@
         modemCom::status.NO_CARRIER=0;
         modemCom::status.PROMPT_ENVIO_COMMAND_MODE=1;
     }
-
     modemCom::status.OK=0;
     ptr=strstr(modemCom::bufIn,"OK\r");
     if(ptr) {        
         modemCom::status.OK=1;
     }        
 
+    
+
     modemCom::status.busy=false;
     ptr=strstr(modemCom::bufIn,"busy s...");
     if(ptr) {
@@ -963,12 +897,102 @@
    
     //modemCom::status.CLOSED=false;    
     ptr=strstr(modemCom::bufIn,"CLOSED");
-    if(ptr) {
-        
+    if(ptr) {        
         modemCom::status.CLOSED=1;
     }
-
+    
+    if(strstr(modemCom::bufIn,"GET /")){
+        getFound = true;
+    }
+    
+    //Buscando connect
+    if(!modemCom::atendendoWebServer){
+        ptr=strstr(modemCom::bufIn,"CONNECT\r\n");
+        //if(strstr(modemCom::bufIn,"WIFI")){ptr=NULL;}
+        if(ptr) {                
+            ptr = strtok(modemCom::bufIn,",");
+            connID = atoi(ptr);                                    
+            modemCom::status.serverConnected=1;        
+            pc.printf("Recebido string CONNECT socket na conexao %u.\n",connID);
+            if(!modemCom::status.emComunicacao){                                                
+                if(!getFound){
+                    pc.printf("Atendendo socket entrante na conexao %u.\n",connID);
+                    modemCom::status.SRINGsockEntrante = true;
+                    modemCom::status.connIDServerCommand = connID;
+                }
+            }
+            modemCom::status.emComunicacao=true;
+            modemCom::bufIn = strtok(NULL,"");
+        }    
+    }    
+    
+    IPDNumBytes = 0;
+    ptr = strstr(modemCom::bufIn,"+IPD");
+    if(ptr!=NULL){
+        //+IPD,0,480:GET
+        strtok(ptr,",");           //Separando +IPD
+        
+        ptr = strtok(NULL,",");    //Pegando o numero da conexão
+        connID = atoi(ptr);
+        
+        ptr = strtok(NULL,":");    //Pegando o numero de caracteres IPD
+        IPDNumBytes = atoi(ptr);
+        
+        modemCom::bufIn = strtok(NULL,"");
+        
+        //printf("Recebido pacote via conexao %u com %lu bytes.\n",connID,IPDNumBytes);
+        
+    }     
     
+    //Reconhecendo GET HTTP e atendendo com um servidor web    
+    if(getFound && strstr(modemCom::bufIn,"HTTP/1.1")){
+        ptr=strstr(modemCom::bufIn,"GET /");
+        if(ptr) {            
+            ptr = strtok(ptr,"/");
+            ptr = strtok(NULL," ");
+            bufInPtr = ptr;
+            strcpy(webServerBuff,ptr);
+            modemCom::atendendoWebServer = true;
+            modemCom::status.connIDWebServer = connID;
+        }
+    }
+    
+    ptr=strstr(modemCom::bufIn,"*ServerAck*");
+    if(ptr) {        
+        modemCom::status.ServerAck=1;        
+    }
+    
+    ptr=strstr(modemCom::bufIn,"DELETE_SENT_FILES");
+    if(ptr) {        
+        sdCard::deleteSentFiles=1;
+    }        
+    
+    ptr=strstr(modemCom::bufIn,"*ServerCommand*");
+    if(ptr) {        
+        scanPtr = strtok (ptr,"\\");
+        scanPtr = strtok (NULL,">");        
+        strcpy(commands::buffer,scanPtr);
+        pc.printf("Comandos <%s>.\n",commands::buffer);
+        executaComandoServer = true;
+    }
+    
+    //if(modemCom::status.recebendoArquivoDoServer){
+    if(IPDNumBytes && modemCom::status.recebendoArquivoDoServer && (strstr(modemCom::bufIn,"CONNECT")==NULL) && (strstr(modemCom::bufIn,"*ServerCommand*")==NULL) ){
+    
+        sdCard::insereDadosArquivo(&sdCard::tempFile,modemCom::bufIn,IPDNumBytes);
+        bufModem.del();
+        sprintf(modemCom::bufIn,"AT+CIPSEND=%u,%lu\r\n",connID,10);
+        modem.puts(modemCom::bufIn);
+        timeOut = 5000;
+        while(timeOut && (strstr(bufModem.getRowBuffer(),">")==NULL)){
+            osDelay(1);
+            timeOut--;
+        }
+        modemCom::status.timeOut = 30;
+        modem.puts("sendData\r\n");
+        pc.printf("sendData\n");        
+        return;
+    }
 }
 
 uint8_t modemCom::inicializaModem(void)