miyago

main.c

May 17th, 2019
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. // C program to illustrate
  2. // non I/O blocking calls
  3.  
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <fcntl.h> // library for fcntl function
  8. #include <errno.h>
  9. #include "logging.h"
  10.  
  11. #include <string.h>
  12.  
  13. #include <time.h>
  14.  
  15. #define MSGSIZE 6
  16. #define UPDATE_INTERVAL 17
  17.  
  18.  
  19.  
  20. int main(int argc, char **argv) {
  21.     int fdPipe[2];          // File descriptor för pipe
  22.    
  23.     pipe(fdPipe);           // Skapar en pipe
  24.    
  25.     pid_t cpid = fork();    // Skapar en till process
  26.     if (fcntl(fdPipe[0], F_SETFL, O_NONBLOCK) < 0)
  27.         exit(2);
  28.    
  29.     switch(cpid) {
  30.        
  31.         // Fel
  32.         case -1: exit(3);
  33.        
  34.         // Child process
  35.         case 0:                            
  36.         dup2(fdPipe[1], STDOUT_FILENO);     // Skriva till stdout, skrivs till pipe istället
  37.         close(fdPipe[1]);                   // Stäng änden som inte används
  38.         mqtt_sub();
  39.         break;
  40.        
  41.         // Parent process
  42.         default:
  43.         dup2(fdPipe[0], STDIN_FILENO);
  44.         close(fdPipe[0]);
  45.         parent_init();
  46.  
  47.     }
  48.     return 0;
  49. }
  50. int parent_init(){
  51.  
  52.     int nrOfEntries = 0;
  53.     LogEntry sensorLog[200];
  54.  
  55.     char updates[3000] = "\0";
  56.  
  57.     time_t omega;
  58.     time_t alpha = time(NULL);
  59.     time_t seconds = UPDATE_INTERVAL; // Hur många sekunder mellan varje thingspeak update
  60.  
  61.     omega = alpha + seconds;    // Detta är början
  62.  
  63.     printf("start time is : %s", ctime(&alpha));
  64.  
  65. // För resten av programmet, läs och logga data och uppdatera thingspeak varje UPDATE_INTERVAL
  66.     char stdinput[MSGSIZE];
  67.     int nread;
  68.     while (1) {
  69.         while (fgets(stdinput, 10, stdin) != NULL) {
  70.  
  71.  
  72.             // text read
  73.             // by default return no. of bytes
  74.             // which read call read at that time
  75.             fprintf(stdout,"MSG = %s", stdinput);
  76.        
  77.         // sensorLogging(sensorLog, &nrOfEntries);
  78.         /* while (alpha < omega){                          // Loggar data under UPDATE_INTERVAL
  79.          
  80.           alpha = time(NULL);
  81.         }
  82.         fprintf(stdout,"En minut har passerat!\n");
  83.         sortLog(sensorLog, &nrOfEntries);               // Slå ihop sensorvärden som har skett samma sekund
  84.         buildUpdates(sensorLog, nrOfEntries);           // Skapar strängar av datum,tid och effekt i CSV format
  85.         buildURI(sensorLog, nrOfEntries, updates);          // Bygger strängen av all sensordata som ska skickas till thingspeak
  86.         if(nrOfEntries > 0){
  87.           curling(updates);
  88.         }
  89.         nrOfEntries = 0;          // Börjar om, obs structen har kvar sina värden tills de skrivs över
  90.         omega = alpha + seconds;  // Detta är den nya början*/
  91.     }
  92.     }
  93.  
  94. }
  95.  
  96. void sensorLogging(LogEntry *sensorLog, int *entries){
  97.  
  98.  
  99.   int nread;
  100.   char line[MSGSIZE];
  101.  
  102.             while( fgets(line, MSGSIZE,stdin) != NULL){
  103.                 fprintf(stdout,"%s",line);
  104.             }
  105.  
  106.             // text read
  107.             // by default return no. of bytes
  108.             // which read call read at that time
  109.             //fprintf(stdout,"MSG = %s, COUNT: %d\n", buf, *entries);
  110.             //loggingParent(sensorLog, buf, entries);
  111. }
Advertisement
Add Comment
Please, Sign In to add comment