Commit 4f743bd8f1a88889f9fb02f2fcde895fde78d55b

Authored by
1 parent 1a71f2a1

Server Raspberry, sans la liaison série

Showing 1 changed file with 121 additions and 0 deletions   Show diff stats
server2.c 0 → 100644
@@ -0,0 +1,121 @@ @@ -0,0 +1,121 @@
  1 +#include <stdio.h>
  2 +#include <stdlib.h>
  3 +#include <libwebsockets.h>
  4 +#include <unistd.h>
  5 +#include <termios.h>
  6 +#define MAX_FRAME_SIZE 1024
  7 +#define WAIT_DELAY 50
  8 +#define TAILLE_MAX_BUFFER 50
  9 +#define TAILLE_MOT 8
  10 +
  11 +static int callback_http(struct libwebsocket_context * this,
  12 + struct libwebsocket *wsi,
  13 + enum libwebsocket_callback_reasons reason, void *user,
  14 + void *in, size_t len)
  15 +{
  16 + return 0;
  17 +}
  18 +
  19 +static int callback_dumb_increment(struct libwebsocket_context * this,
  20 + struct libwebsocket *wsi,
  21 + enum libwebsocket_callback_reasons reason,
  22 + void *user, char *in, size_t len)
  23 +{
  24 +
  25 + switch (reason) {
  26 + case LWS_CALLBACK_ESTABLISHED: // just log message that someone is connecting
  27 + printf("connection established\n");
  28 + break;
  29 + case LWS_CALLBACK_RECEIVE: {
  30 + // Création du buffer pour contenir notre réponse
  31 +
  32 + char* out[4];
  33 + out[1] = strtok((char*)in,",");
  34 + out[2] = strtok(NULL,",");
  35 + out[3] = strtok(NULL,",");
  36 + out[4] = strtok(NULL,",");
  37 +
  38 +
  39 + char commande[50];
  40 + sprintf(commande, "python get_data.py %s %s %s %s", out[1],out[2],out[3],out[4]);
  41 +
  42 +
  43 + FILE *file = popen(commande,"r");
  44 +
  45 + char a[TAILLE_MAX_BUFFER], b[TAILLE_MAX_BUFFER], c[TAILLE_MAX_BUFFER];
  46 + char heure[TAILLE_MOT],minute[TAILLE_MOT],temperature[TAILLE_MOT];
  47 + fscanf(file,"%s",a);
  48 + fscanf(file,"%s",b);
  49 + fscanf(file,"%s",c);
  50 + pclose(file);
  51 +
  52 + int i;
  53 +
  54 + for(i=0;i<8;i++){
  55 + heure[i]=a[i+2];
  56 + minute[i]=b[i+1];
  57 + temperature[i]=c[i+1];
  58 + }
  59 +
  60 + heure[8]='\0';
  61 + minute[8]='\0';
  62 + temperature[8]='\0';
  63 +
  64 +
  65 +
  66 + printf("%s\n",heure);
  67 + printf("%s\n",minute);
  68 + printf("%s\n",temperature);
  69 +
  70 + // release memory back into the wild
  71 +
  72 + break;
  73 + }
  74 + default:
  75 + break;
  76 + }
  77 +
  78 +
  79 + return 0;
  80 +}
  81 +
  82 +static struct libwebsocket_protocols protocols[] = {
  83 + /* first protocol must always be HTTP handler */
  84 + {
  85 + "http-only", // name
  86 + callback_http, // callback
  87 + 0 // per_session_data_size
  88 + },
  89 + {
  90 + "dumb-increment-protocol", // protocol name - very important!
  91 + callback_dumb_increment, // callback
  92 + 0 // we don't use any per session data
  93 +
  94 + },
  95 + {
  96 + NULL, NULL, 0 /* End of list */
  97 + }
  98 +};
  99 +
  100 +
  101 +
  102 +int main(void) {
  103 +int port=9000;
  104 +struct lws_context_creation_info info;
  105 +memset(&info,0,sizeof info);
  106 +info.port=port;
  107 +info.protocols=protocols;
  108 +info.gid=-1;
  109 +info.uid=-1;
  110 +struct libwebsocket_context *context=libwebsocket_create_context(&info);
  111 +if(context==NULL){
  112 + fprintf(stderr, "libwebsocket init failed\n");
  113 + return -1;
  114 + }
  115 +printf("starting server...\n");
  116 +while(1){
  117 + libwebsocket_service(context,WAIT_DELAY);
  118 + }
  119 +libwebsocket_context_destroy(context);
  120 +return 0;
  121 +}