kio

Dependencies:   mbed-http

Revision:
0:29132844de96
Child:
1:078a6d9a2ae3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/source/main-http.cpp	Thu Aug 05 00:08:53 2021 +0000
@@ -0,0 +1,423 @@
+#include "select-demo.h"
+#if DEMO == DEMO_HTTP
+#include "mbed.h"
+#include "http_request.h"
+#include "network-helper.h"
+#include "mbed_mem_trace.h"
+#include <iostream>
+#include <cstring>
+#include <Thread.h>
+#include <stdio.h>
+#include "EthernetInterface.h"
+
+DigitalOut PUERTA1(PG_9);
+DigitalOut PUERTA2(PG_14);
+DigitalOut PUERTA3(PF_15);
+DigitalOut PUERTA4(PE_13);
+
+Serial      sim808(PD_5, PD_6,9600);
+int         onetime = 0, i=0, j=0, onedbquery[4];
+const int   kMaxBufferSize = 100;
+char        buffer[kMaxBufferSize];
+string      opendoors = "http://192.168.1.10/ws/ws.php?op=micopendoors&telefono=";
+string      closedoors = "http://192.168.1.10/ws/ws.php?op=micclosedoors&telefono=";
+string      registers = "http://192.168.1.10/ws/ws.php?op=create-register&telefono=";
+char        linkdoors[40]= "http://192.168.1.10/ws/ws.php?op=doors";
+string      linkregister01 = "";
+string      linkregister02;
+string      linkregister03;
+string      linkregister04;
+char        char_linkregister01[55];
+char        char_linkregister02[55];
+char        char_linkregister03[55];
+char        char_linkregister04[55];
+char        linkopendoors[100];
+char        linkclosedoors[100];
+char        linkregisters[100];
+string      link_string;
+string      http_response = "";
+int         len = 0;
+char        telbuf[11];
+int         mensaje=0;
+Timer       t;
+
+void dump_response(HttpResponse* res) {
+    http_response = res->get_body_as_string().c_str();
+}
+
+int main() {
+    
+//-------------------------------------------------------------------- INICIALIZACION DE PINES -------------------------------------------------------------------------------------------
+
+    PUERTA1 = 1;
+    PUERTA2 = 1;
+    PUERTA3 = 1;
+    PUERTA4 = 1;
+    linkregister01 = "http://192.168.1.10/ws/ws.php?op=create-registeri&id=1";
+    linkregister02 = "http://192.168.1.10/ws/ws.php?op=create-registeri&id=2";
+    linkregister03 = "http://192.168.1.10/ws/ws.php?op=create-registeri&id=3";
+    linkregister04 = "http://192.168.1.10/ws/ws.php?op=create-registeri&id=4";
+    
+    int m = linkregister01.length();
+ 
+    // declaring character array
+    char link_char4[m + 1];
+    char link_char5[m + 1];
+    char link_char6[m + 1];
+    char link_char7[m + 1];
+ 
+    // copying the contents of the
+    // string to char array
+    strcpy(link_char4, linkregister01.c_str());
+    strcpy(link_char5, linkregister02.c_str());
+    strcpy(link_char6, linkregister03.c_str());
+    strcpy(link_char7, linkregister04.c_str());
+ 
+    for (int i2 = 0; i2 < m; i2++){
+        cout << link_char4[i2];
+        cout << link_char5[i2];
+        cout << link_char6[i2];
+        cout << link_char7[i2];
+    }
+    
+    for(int k2 = 0; k2 < 54 ; k2++){
+            char_linkregister01[k2] = linkregister01[k2];
+            char_linkregister02[k2] = linkregister02[k2];
+            char_linkregister03[k2] = linkregister03[k2];
+            char_linkregister04[k2] = linkregister04[k2];
+    }
+
+//-------------------------------------------------------------------- INICIALIZACION DE TARJETA DE RED -------------------------------------------------------------------------------------------
+
+
+    NetworkInterface* network = connect_to_default_network_interface();
+    if (!network) {
+        printf("Cannot connect to the network, see serial output\n");
+        return 1;
+    }
+    
+//------------------------------------------------------------------- RECEPCION Y ALMACENAMIENTO DE TELEFONO --------------------------------------------------------------------------------------
+    t.start();
+    unsigned long int T1, T2;
+    T1 = t.read();
+    buffer[0] = '\0';
+    
+    while (true) {
+        
+        T2 = t.read();
+        if(T2 > (T1 + 2)){
+            T1 = T2;
+            {HttpRequest* get_req = new HttpRequest(network, HTTP_GET, linkdoors);
+            HttpResponse* get_res = get_req->send();
+            if (!get_res) {
+                printf("HttpRequest failed (error code %d)\n", get_req->get_error());
+                return 1;
+            }
+            dump_response(get_res);
+            delete get_req;}
+            
+            char doors_ddbb[4];
+            doors_ddbb[0] = http_response[0];
+            doors_ddbb[1] = http_response[1];
+            doors_ddbb[2] = http_response[2];
+            doors_ddbb[3] = http_response[3];
+            
+            char logicone[0];
+            logicone[0] = '1';            
+            
+            if(doors_ddbb[0] == logicone[0]){
+                PUERTA1 = 1;
+                onedbquery[0] = 0;
+            }
+            else if (doors_ddbb[0] != logicone[0] && onedbquery[0] == 0){
+                PUERTA1 = 0;
+                {HttpRequest* get_req = new HttpRequest(network, HTTP_GET, char_linkregister01);
+                HttpResponse* get_res = get_req->send();
+                if (!get_res) {
+                printf("HttpRequest failed (error code %d)\n", get_req->get_error());
+                return 1;}
+                dump_response(get_res);
+                delete get_req;
+                printf("[aXessKontrol] Se detecto cambio en la base de datos, agregando registro...  Repuesta de DDBB: %s \n", http_response);}
+                onedbquery[0]++;
+                mensaje = 0;
+            }
+            if(doors_ddbb[1] == logicone[0]){
+                PUERTA2 = 1;
+                onedbquery[1] = 0;
+            }
+            else if (doors_ddbb[1] != logicone[0] && onedbquery[1] == 0){
+                PUERTA2 = 0;
+                {HttpRequest* get_req = new HttpRequest(network, HTTP_GET, char_linkregister02);
+                HttpResponse* get_res = get_req->send();
+                if (!get_res) {
+                printf("HttpRequest failed (error code %d)\n", get_req->get_error());
+                return 1;}
+                dump_response(get_res);
+                delete get_req;
+                printf("[aXessKontrol] Se detecto cambio en la base de datos, agregando registro...  Repuesta de DDBB: %s \n", http_response);}
+                onedbquery[1]++;
+                mensaje = 0;
+            }
+            if(doors_ddbb[2] == logicone[0]){
+                PUERTA3 = 1;
+                onedbquery[2] = 0;
+    
+            }
+            else if (doors_ddbb[2] != logicone[0] && onedbquery[2] == 0){
+                PUERTA3 = 0;
+                {HttpRequest* get_req = new HttpRequest(network, HTTP_GET, char_linkregister03);
+                HttpResponse* get_res = get_req->send();
+                if (!get_res) {
+                printf("HttpRequest failed (error code %d)\n", get_req->get_error());
+                return 1;}
+                dump_response(get_res);
+                delete get_req;
+                printf("[aXessKontrol]Se detecto cambio en la base de datos, agregando registro...  Repuesta de DDBB: %s \n", http_response);}
+                onedbquery[2]++;
+                mensaje = 0;
+            }
+            if(doors_ddbb[3] == logicone[0]){
+                PUERTA4 = 1;
+            }
+            else if (doors_ddbb[3] != logicone[0] && onedbquery[3] == 0){
+                PUERTA4 = 0;
+                {HttpRequest* get_req = new HttpRequest(network, HTTP_GET, char_linkregister04);
+                HttpResponse* get_res = get_req->send();
+                if (!get_res) {
+                printf("HttpRequest failed (error code %d)\n", get_req->get_error());
+                return 1;}
+                dump_response(get_res);
+                delete get_req;
+                printf("[aXessKontrol]Se detecto cambio en la base de datos, agregando registro...  Repuesta de DDBB: %s \n", http_response);}
+                onedbquery[3]++;
+                mensaje = 0;
+            }
+        }
+        HOME:
+        
+        if(mensaje == 0){
+            printf("\n[aXessKontrol] Bievenido a la consola de aXessKontrol, aqui se visualiza informacion importante sobre el estado actual del sistema \n");
+            printf("[aXessKontrol] El sistema esta disponible para recibir solicitudes \n\n");
+            mensaje++;
+        }
+        //----------------------------------------------- GET VERIFICACION DE ESTADO DE PINES EN BASE DE DATOS -----------------------------------------------------------------------------
+            
+        while (sim808.readable()) {
+            char new_char = sim808.getc();
+            if(new_char == '0' || new_char == '1' || new_char == '2' || new_char == '3' || new_char == '4' || new_char == '5' || new_char == '6' || new_char == '7' || new_char == '8' || new_char == '9'){
+                buffer[len] = new_char;
+                len++;
+                if(len == 10 && onetime == 0){
+                    for(j=0;j<10;j++){
+                        telbuf[j] = buffer[j];
+                        printf("[aXessKontrol] Se almaceno el numero %c en la posicion j %d de TELBUF\n", telbuf[j], j);
+                    }
+                    onetime++;
+                    goto A;
+                }          
+        }
+            
+            // new message received, handle
+            if (new_char == '\n'){
+                len = 0;
+            }
+        }
+    }
+    A:
+    printf("\n[aXessKontrol] Solicitud recibida.\n[aXessKontrol] El telefono del usuario actual es : %s \n\n", telbuf);
+    
+    sim808.printf("ATA\n");
+    
+//-------------------------------------------CONVERSION Y CONCATENACION DE STRING PARA FORMAR LINK------------------------------------------------------------------------
+
+
+    //--------------------------------------------------CREACION DE LINK OPENDOOR-----------------------------------------------------------------------------------------
+    int n = opendoors.length();
+ 
+    // declaring character array
+    char link_char[n + 1];
+ 
+    // copying the contents of the
+    // string to char array
+    strcpy(link_char, opendoors.c_str());
+ 
+    for (int i = 0; i < n; i++){
+        cout << link_char[i];
+    }
+    
+    for(int k = 0; k < 65 ; k++){
+        if(k < 55){
+            linkopendoors[k] = opendoors[k];
+        }
+        else if (k >= 55) {
+            linkopendoors[k] = telbuf[k-55];
+        }
+    }
+    printf("[aXessKontrol] Iniciando generacion de URLs...\n");
+    printf("[aXessKontrol] El URL para abrir creado es el siguiente:  %s \n", linkopendoors);
+    
+    
+    //-------------------------------------------------- CREACION DE LINK CLOSEDOOR ---------------------------------------------------------------------------------------
+    
+    int n2 = closedoors.length();
+ 
+    // declaring character array
+    char link_char2[n2 + 1];
+ 
+    // copying the contents of the
+    // string to char array
+    strcpy(link_char2, closedoors.c_str());
+ 
+    for (int z = 0; z < n2; z++){
+        cout << link_char2[z];
+    }
+    
+    for(int x = 0; x < 66 ; x++){
+        if(x < 56){
+            linkclosedoors[x] = closedoors[x];
+        }
+        else if (x >= 56) {
+            linkclosedoors[x] = telbuf[x-56];
+        }
+    }
+    
+    printf("[aXessKontrol] El URL para cerrar creado es el siguiente:  %s \n", linkclosedoors);
+    
+    
+        //-------------------------------------------------- CREACION DE LINK REGISTRO ---------------------------------------------------------------------------------------
+    
+    int n3 = registers.length();
+ 
+    // declaring character array
+    char link_char3[n3 + 1];
+ 
+    // copying the contents of the
+    // string to char array
+    strcpy(link_char3, registers.c_str());
+ 
+    for (int z = 0; z < n3; z++){
+        cout << link_char2[z];
+    }
+    
+    for(int x = 0; x < 68 ; x++){
+        if(x < 58){
+            linkregisters[x] = registers[x];
+        }
+        else if (x >= 58) {
+            linkregisters[x] = telbuf[x-58];
+        }
+    }
+    
+    printf("[aXessKontrol] El URL del registro es el siguiente:  %s \n", linkregisters);
+    
+//-------------------------------------------------------------------- CREACION DEL GET HTTP ------------------------------------------------------------------------------------------------------
+    
+//-------------------------------------------------------------------- APERTURA DE PUERTA --------------------------------------------------------------------------------------------------
+    printf("[aXessKontrol] Se generaron correctamente las URLs...\n\n");
+    wait(0.7);   
+    sim808.printf("ATH\n");
+    
+    
+    {
+        HttpRequest* get_req = new HttpRequest(network, HTTP_GET, linkopendoors);
+        
+        HttpResponse* get_res = get_req->send();
+        if (!get_res) {
+            printf("HttpRequest failed (error code %d)\n", get_req->get_error());
+            return 1;
+        }
+        
+        //IMPRESION DE RESPUESTA HTTP
+        dump_response(get_res);
+        delete get_req;
+         
+        printf("[aXessKontrol] La puerta fue abierta, el id del usuario es: %s\n", http_response);
+    }
+    char compare[0];
+    compare[0] = http_response[0];
+    char primero[0];
+    char segundo[0];
+    char tercero[0];
+    char cuarto[0];
+    
+    primero[0] = '1';
+    segundo[0] = '2';
+    tercero[0] = '3';
+    cuarto[0] = '4';
+    
+    if(compare[0] == primero[0]){
+        PUERTA1 = 0;
+    }
+    else if(compare[0] == segundo[0]){
+        PUERTA2 = 0;
+    }
+    else if(compare[0] == tercero[0]){
+        PUERTA3 = 0;
+    }
+    else if(compare[0] == cuarto[0]){
+        PUERTA4 = 0;
+    }
+    
+    //-------------------------------------------------------------------- CREACION DE REGISTRO --------------------------------------------------------------------------------------------------
+    
+    {
+        HttpRequest* get_req = new HttpRequest(network, HTTP_GET, linkregisters);
+        
+        HttpResponse* get_res = get_req->send();
+        if (!get_res) {
+            printf("HttpRequest failed (error code %d)\n", get_req->get_error());
+            return 1;
+        }
+        
+        //IMPRESION DE RESPUESTA HTTP
+        dump_response(get_res);
+        delete get_req;
+         
+        printf("[aXessKontrol] Enviando registro de acceso... Respuesta de DDBB: %s\n", http_response);
+        printf("\n[aXessKontrol] Iniciando cierre en 10 segundos...\n");
+    }
+    
+    wait(10);
+    
+    if(compare[0] == primero[0]){
+        PUERTA1 = 1;
+    }
+    else if(compare[0] == segundo[0]){
+        PUERTA2 = 1;
+    }
+    else if(compare[0] == tercero[0]){
+        PUERTA3 = 1;
+    }
+    else if(compare[0] == cuarto[0]){
+        PUERTA4 = 1;
+    }
+//-------------------------------------------------------------------- CIERRE DE PUERTA --------------------------------------------------------------------------------------------------
+    printf("\n[aXessKontrol] Cerrando la puerta... \n");
+    {
+        HttpRequest* get_req = new HttpRequest(network, HTTP_GET, linkclosedoors);
+        
+        HttpResponse* get_res = get_req->send();
+        if (!get_res) {
+            printf("HttpRequest failed (error code %d)\n", get_req->get_error());
+            return 1;
+        }
+        
+        //IMPRESION DE RESPUESTA HTTP
+        dump_response(get_res);
+        delete get_req;
+         
+        printf("[aXessKontrol] La puerta fue cerrada, actualizando campo. Respuesta de DDBB: %s\n", http_response);
+    }
+    
+    printf("\n[aXessKontrol] Preparando recepcion de nuevas solicitudes... \n");
+    onetime = 0;
+    len = 0;
+    mensaje = 0;
+    opendoors = "http://192.168.1.10/ws/ws.php?op=micopendoors&telefono=";
+    closedoors = "http://192.168.1.10/ws/ws.php?op=micclosedoors&telefono=";
+    registers = "http://192.168.1.10/ws/ws.php?op=create-register&telefono=";
+    goto HOME;
+}
+
+#endif
\ No newline at end of file