Marcelo Rebonatto / Mbed 2 deprecated PMED_Tempo

Dependencies:   EthernetInterface mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Http_post.cpp Source File

Http_post.cpp

00001 /*
00002  * Http_post.cpp
00003  *
00004  *  Created on: 
00005  *      Author: 
00006  */
00007 #include "Http_post.h"
00008 
00009 extern "C" void mbed_reset();
00010 
00011 void HttpPost::HttpPost_Thread(void const *arg)
00012 {
00013     printf("HTTP POST Thread starting...\r\n");    
00014     
00015     //inicializar socket
00016    // TCPSocketConnection sock;
00017     
00018     CaptureMailbox& mbox = EventDetector::GetMailbox();
00019     
00020     osEvent evt;
00021     //printf("aqui\n");
00022     while(1)
00023     {
00024         TCPSocketConnection sock;
00025         //printf("Esperando Evento\n");
00026         evt = mbox.get();
00027 
00028         if(evt.status == osEventMail)
00029         {
00030             //printf("Recebido osEventMail...\n ");
00031             CaptureEvent* cap = (CaptureEvent*)evt.value.p;
00032             DoPost(sock,Settings::get_ServerUrl(),cap);    
00033             mbox.free(cap);
00034             //printf("Enviado e Liberado Mbox ...\n ");
00035             //sock.reset_address();
00036         }
00037         //Thread::yield();
00038     }
00039 }
00040 
00041 
00042 
00043 void HttpPost::DoPost(TCPSocketConnection sock, char *host, CaptureEvent* dados){
00044     char http_cmd[600]; 
00045     int escritos, r=-1, i;
00046     FILE *f;
00047     
00048     //Timer t;
00049     //t.start();
00050     
00051     //printf("HTTP Socket %s:%d\n", host, sock.get_port());
00052     //printf("Antes Connect\n");
00053     //rs= sock.connect(host, 80);   
00054     //printf("%d\n", rs);
00055     //printf("Depois Connect "); 
00056     
00057     //t.stop();
00058     //printf("HHTP: The time taken in connection was %d useconds\n", t.read_us());
00059            
00060     //http_cmd = (char *) malloc(500);
00061     memset(http_cmd, 0, 600);
00062     PreparePost( dados,http_cmd );
00063     //printf("Fuga\n");
00064     //printf("Tamanho do comando %d\n", strlen(http_cmd));
00065     //printf("Comando: /* %s */\n", http_cmd);
00066     
00067     //http_cmd = prepare_POST( dados );
00068     
00069     //printf("Tamanho comando %d\n", strlen(http_cmd));     
00070     //printf("Request %d\n [%s]\n", Settings::get_MaxTries(), http_cmd);     
00071 
00072     
00073     for(i=0; i < Settings::get_MaxTries(); i++){
00074         r = sock.connect(host, 80);        
00075         if (r < 0) {
00076             printf("Error: Unable to connect to (%s) on port (%d) Try %d\n", host, 80, i);
00077             Thread::wait(Settings::get_DelayTry());
00078         }
00079         else
00080             break;            
00081     }     
00082     if (r == 0){
00083         for(i=0; i < Settings::get_MaxTries(); i++){
00084             escritos = sock.send_all(http_cmd, strlen(http_cmd));        
00085             if(escritos != strlen(http_cmd)){
00086                 printf("Erro ao gravar no socket HTTP!! Escritos %d\t Tam %d Try %d\n", escritos, strlen(http_cmd), i);            
00087                 Thread::wait(Settings::get_DelayTry());
00088             }
00089             else 
00090                 break;
00091         }
00092         if (i != Settings::get_MaxTries() )
00093             Thread::wait(Settings::get_DelaySend());
00094         else{
00095             //printf("Reset\n");
00096             f = fopen(FILENAMERESET, "a");
00097             if (f == NULL)            
00098                 f = fopen(FILENAMERESET, "w");
00099             fprintf(f, "Reset - Connect\n");
00100             fclose(f);
00101                 
00102             mbed_reset();
00103         }            
00104             
00105         //Codigo para buscar o retorno do servidor HTTP
00106         /*
00107         printf("Vai pegar retorno\n");
00108         
00109         char buffer[300];
00110         int ret;
00111         while (true) {
00112             ret = sock.receive(buffer, sizeof(buffer)-1);
00113             if (ret <= 0)
00114                 break;
00115             buffer[ret] = '\0';
00116             printf("Received %d chars from server:\n%s\n", ret, buffer);
00117         }
00118         */
00119     }
00120     else{
00121         printf("Reset\n");
00122         f = fopen(FILENAMERESET, "a");
00123         if (f == NULL)            
00124             f = fopen(FILENAMERESET, "w");
00125         fprintf(f, "Reset - Send\n");
00126         fclose(f);
00127             
00128         mbed_reset();
00129     }
00130     
00131     sock.close();  
00132 }
00133 
00134 void HttpPost::PreparePost(CaptureEvent* dados,char *strfinal){
00135     char str[400];
00136     char aux[NUMBER_OF_HARMONICS];
00137     int i;
00138 
00139     const char *header1 = "POST /capture.php HTTP/1.1\r\n";
00140                 //"Host: 192.168.1.26\r\n"
00141                 //"Content-Length: "
00142                 
00143     const char *header2 = "\r\n"
00144                 "Content-Type: application/x-www-form-urlencoded\r\n"
00145                 "\r\n";
00146     
00147     //str = (char *) malloc(450);
00148     //strfinal = (char *) malloc(450);
00149     memset(str,0,400);
00150     memset(strfinal,0,500);
00151             
00152     strcat(strfinal, header1);    
00153     strcat(strfinal, "Host: ");
00154     strcat(strfinal, Settings::get_ServerUrl() );
00155     strcat(strfinal, "\r\n");
00156     
00157     sprintf(aux,"TYPE=0%d",dados->get_Type());
00158     strcat(str, aux);
00159 
00160     sprintf(aux,"&OUTLET=%02d",dados->get_OutletNumber());
00161     strcat(str, aux);
00162     
00163     sprintf(aux,"&RFID=%s", dados->get_RFID());
00164     strcat(str,aux);
00165 
00166     sprintf(aux,"&OFFSET=%04d",dados->get_Offset());
00167     strcat(str,aux);
00168     
00169     float f = dados->get_Gain();
00170     sprintf(aux,"&GAIN=%08X", *(unsigned int*)&f);
00171     strcat(str,aux);
00172     
00173     f = dados->get_RMSValue();
00174     sprintf(aux,"&RMS=%08X",*(unsigned int*)&f);
00175     strcat(str,aux);
00176     
00177     f = dados->get_MeanValue();
00178     sprintf(aux,"&MV=%08X",*(unsigned int*)&f);
00179     strcat(str,aux);        
00180     
00181     /* Adicionados para alteracao */
00182     //printf("MV %f MV2 %f\n", dados->get_MeanValue(), dados->get_MV2());
00183     f = dados->get_MV2();
00184     sprintf(aux,"&MV2=%08X",*(unsigned int*)&f);
00185     strcat(str,aux);        
00186     
00187     sprintf(aux,"&UNDER=%04d",dados->get_Under());
00188     strcat(str,aux);
00189     
00190     sprintf(aux,"&OVER=%04d",dados->get_Over());
00191     strcat(str,aux);
00192     
00193     sprintf(aux,"&DURATION=%04d",dados->get_Duration());
00194     strcat(str,aux);
00195     /* Ate Aqui */
00196     
00197     strcat(str,"&SIN=");
00198     for(i=0;i<Settings::get_MaxHarmonics();i++)
00199     {
00200         char s[10];
00201         //According to RFC1738,RFC3986 the semicolon is a reserved character and must be encoded
00202         f = dados->get_SineValue(i);
00203         sprintf(s,"%08X",*(unsigned int*)&f);
00204         strcat(str,s);
00205         if (i < (Settings::get_MaxHarmonics() - 1))
00206             strcat(str, "%3B");
00207     }
00208 
00209     strcat(str,"&COS=");
00210     for(i=0;i<Settings::get_MaxHarmonics();i++)
00211     {
00212         char c[10];
00213         //According to RFC1738,RFC3986 the semicolon is a reserved character and must be encoded
00214         f = dados->get_CossineValue(i);
00215         sprintf(c,"%08X",*(unsigned int*)&f);
00216         strcat(str,c);
00217         if (i < (Settings::get_MaxHarmonics()-1))
00218             strcat(str, "%3B");
00219     }
00220     strcat(str,"\r\n");
00221     
00222     char len[5];
00223     sprintf(len,"%d",strlen(str));        
00224     
00225     strcat(strfinal, "Content-Length: ");
00226     strcat(strfinal, len);
00227     strcat(strfinal, header2);
00228     strcat(strfinal, str);    
00229     strcat(strfinal, "\r\n");
00230     
00231     //printf("Request=[%s]\n",strfinal);
00232     //printf("Tamanho STR %d\n", strlen(str));
00233     //printf("Tamanho STRFINAL %d\n", strlen(strfinal));       
00234  }