server2.c 3.08 KB
#include <stdio.h>
#include <stdlib.h>
#include <libwebsockets.h>
#include <unistd.h>
#include <termios.h>
#define MAX_FRAME_SIZE  1024
#define WAIT_DELAY      50
#define TAILLE_MAX_BUFFER 50
#define TAILLE_MOT 8

static int callback_http(struct libwebsocket_context * this,
                         struct libwebsocket *wsi,
                         enum libwebsocket_callback_reasons reason, void *user,
                         void *in, size_t len)
{
    return 0;
}

static int callback_dumb_increment(struct libwebsocket_context * this,
                                   struct libwebsocket *wsi,
                                   enum libwebsocket_callback_reasons reason,
                                   void *user, char *in, size_t len)
{

    switch (reason) {
        case LWS_CALLBACK_ESTABLISHED: // just log message that someone is connecting
            printf("connection established\n");
            break;
        case LWS_CALLBACK_RECEIVE: {
            // Création du buffer pour contenir notre réponse

            char* out[4];
            out[1] = strtok((char*)in,",");
            out[2] = strtok(NULL,",");
            out[3] = strtok(NULL,",");
            out[4] = strtok(NULL,",");


            char commande[50];
            sprintf(commande, "python get_data.py %s %s %s %s", out[1],out[2],out[3],out[4]);


            FILE *file = popen(commande,"r");

            char a[TAILLE_MAX_BUFFER], b[TAILLE_MAX_BUFFER], c[TAILLE_MAX_BUFFER];
            char heure[TAILLE_MOT],minute[TAILLE_MOT],temperature[TAILLE_MOT];
            fscanf(file,"%s",a);
            fscanf(file,"%s",b);
            fscanf(file,"%s",c);
            pclose(file);

            int i;

            for(i=0;i<8;i++){
                heure[i]=a[i+2];
                minute[i]=b[i+1];
                temperature[i]=c[i+1];
            }

            heure[8]='\0';
            minute[8]='\0';
            temperature[8]='\0';



            printf("%s\n",heure);
            printf("%s\n",minute);
            printf("%s\n",temperature);

            // release memory back into the wild

            break;
        }
        default:
            break;
    }


    return 0;
}

static struct libwebsocket_protocols protocols[] = {
    /* first protocol must always be HTTP handler */
    {
        "http-only",   // name
        callback_http, // callback
        0              // per_session_data_size
    },
    {
        "dumb-increment-protocol", // protocol name - very important!
        callback_dumb_increment,   // callback
        0                          // we don't use any per session data

    },
    {
        NULL, NULL, 0   /* End of list */
    }
};



int main(void) {
int port=9000;
struct lws_context_creation_info info;
memset(&info,0,sizeof info);
info.port=port;
info.protocols=protocols;
info.gid=-1;
info.uid=-1;
struct libwebsocket_context *context=libwebsocket_create_context(&info);
if(context==NULL){
  fprintf(stderr, "libwebsocket init failed\n");
  return -1;
  }
printf("starting server...\n");
while(1){
  libwebsocket_service(context,WAIT_DELAY);
  }
libwebsocket_context_destroy(context);
return 0;
}