miyago icon

main.c

miyago | PRO | 05/17/19 09:10:47 AM UTC | 0 ⭐ | 513 👁️ | Never ⏰ | []
C |

2.82 KB

|

None

|

0 👍

/

0 👎

// C program to illustrate
// non I/O blocking calls
 
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h> // library for fcntl function
#include <errno.h>
#include "logging.h"
 
#include <string.h>
 
#include <time.h>
 
#define MSGSIZE 6
#define UPDATE_INTERVAL 17
 
 
 
int main(int argc, char **argv) {
    int fdPipe[2];          // File descriptor för pipe
    
    pipe(fdPipe);           // Skapar en pipe
    
    pid_t cpid = fork();    // Skapar en till process
    if (fcntl(fdPipe[0], F_SETFL, O_NONBLOCK) < 0)
        exit(2);
    
    switch(cpid) {
        
        // Fel
        case -1: exit(3);
        
        // Child process
        case 0:                             
        dup2(fdPipe[1], STDOUT_FILENO);     // Skriva till stdout, skrivs till pipe istället
        close(fdPipe[1]);                   // Stäng änden som inte används
        mqtt_sub();
        break;
        
        // Parent process
        default:
        dup2(fdPipe[0], STDIN_FILENO);
        close(fdPipe[0]);
        parent_init();
 
    }
    return 0;
}
int parent_init(){
 
    int nrOfEntries = 0;
    LogEntry sensorLog[200];
 
    char updates[3000] = "\0";
 
    time_t omega;
    time_t alpha = time(NULL);
    time_t seconds = UPDATE_INTERVAL; // Hur många sekunder mellan varje thingspeak update
 
    omega = alpha + seconds;    // Detta är början
 
    printf("start time is : %s", ctime(&alpha));
 
// För resten av programmet, läs och logga data och uppdatera thingspeak varje UPDATE_INTERVAL
    char stdinput[MSGSIZE];
    int nread;
    while (1) {
        while (fgets(stdinput, 10, stdin) != NULL) {
 
 
            // text read
            // by default return no. of bytes
            // which read call read at that time
            fprintf(stdout,"MSG = %s", stdinput);
        
        // sensorLogging(sensorLog, &nrOfEntries);
        /* while (alpha < omega){                          // Loggar data under UPDATE_INTERVAL
          
          alpha = time(NULL);
        }
        fprintf(stdout,"En minut har passerat!\n");
        sortLog(sensorLog, &nrOfEntries);               // Slå ihop sensorvärden som har skett samma sekund
        buildUpdates(sensorLog, nrOfEntries);           // Skapar strängar av datum,tid och effekt i CSV format
        buildURI(sensorLog, nrOfEntries, updates);          // Bygger strängen av all sensordata som ska skickas till thingspeak
        if(nrOfEntries > 0){
          curling(updates);
        }
        nrOfEntries = 0;          // Börjar om, obs structen har kvar sina värden tills de skrivs över
        omega = alpha + seconds;  // Detta är den nya början*/
    }
    }
 
}
 
void sensorLogging(LogEntry *sensorLog, int *entries){
 
 
  int nread;
  char line[MSGSIZE];
 
            while( fgets(line, MSGSIZE,stdin) != NULL){
                fprintf(stdout,"%s",line);
            }
 
            // text read
            // by default return no. of bytes
            // which read call read at that time
            //fprintf(stdout,"MSG = %s, COUNT: %d\n", buf, *entries);
            //loggingParent(sensorLog, buf, entries);
}

Comments