C - Timer logic for error condition -


i'm working on project have deal several sensors, , print out error values textfile. i'm doing random generators, generate dummy values. thing here same errors stored textfile, since code running fast , in loop. i'm thinking of making timer handle last stored data current sensor. i'm not sure if idea, since doesn't sound easy, because in end have handle 30 different sensors. idea how can implement such logic code?

struct arg_struct{ //store these data textfile     char *sensorname;     double sensorvalue;     int faultcode;  };  //insert data textfile arguments detect_error void *write_sens_error(void *arguments){      struct arg_struct *args = arguments;     struct tm *local;     time_t t;     t = time(null);     local = localtime(&t);     file *fp;     fp = fopen("/home/sal/desktop/sensors.txt","a");     fprintf(fp, "%s %s %f %s %d %s %s", args -> sensorname, "\t",args -> sensorvalue, "\t", args -> faultcode,"\t",asctime(local));     fclose(fp);     pthread_exit(null);     return null; }  //create new thread, second thread handle errorvalue storage void detect_error(char *sensorname, double sensorvalue, int faultcode){     pthread_t thread_error1;     struct arg_struct args;     args.sensorname = malloc(7);     strcpy(args.sensorname, sensorname);     args.sensorvalue = sensorvalue;     args.faultcode = faultcode;     pthread_create(&thread_error1, null, &write_sens_error, (void *)&args);     pthread_join(thread_error1, null);     free(args.sensorname); } //generate random values , check error void rand_temperature_sensor(double exts[8], int fts, int cts, int ots){     int i;     (i = 0; < 8; i++){         exts[i] = rand() % 10 + 820;         if(exts[i]>800){             char name[6];             sprintf(name, "exts%d", i+1);             detect_error(name,exts[i],((exts[i]>850) ? 2 : 1));         }     }     printf("%s,%f,%f,%f,%f,%f,%f,%f,%f\n","temp",exts[0],exts[1],exts[2],exts[3],exts[4],exts[5],exts[6],exts[7]; } 

and here snippet of log code generating. seen log, same sensors stored 3 times in 1 second. want avoid, error sensors stored after let's 5 seconds after previous error catch.

exts0    823.000000      1   tue apr 28 23:43:57 2015 exts1    820.000000      1   tue apr 28 23:43:57 2015 exts2    826.000000      1   tue apr 28 23:43:57 2015 exts3    827.000000      1   tue apr 28 23:43:57 2015 exts4    829.000000      1   tue apr 28 23:43:57 2015 exts5    826.000000      1   tue apr 28 23:43:57 2015 exts6    820.000000      1   tue apr 28 23:43:57 2015 exts7    823.000000      1   tue apr 28 23:43:57 2015 exts0    827.000000      1   tue apr 28 23:43:57 2015 exts1    828.000000      1   tue apr 28 23:43:57 2015 exts2    822.000000      1   tue apr 28 23:43:57 2015 exts3    826.000000      1   tue apr 28 23:43:57 2015 exts4    822.000000      1   tue apr 28 23:43:57 2015 exts5    822.000000      1   tue apr 28 23:43:57 2015 exts6    829.000000      1   tue apr 28 23:43:57 2015 exts7    826.000000      1   tue apr 28 23:43:57 2015 exts0    821.000000      1   tue apr 28 23:43:57 2015 exts1    823.000000      1   tue apr 28 23:43:57 2015 exts2    822.000000      1   tue apr 28 23:43:57 2015 exts3    824.000000      1   tue apr 28 23:43:57 2015 exts4    826.000000      1   tue apr 28 23:43:57 2015 exts5    826.000000      1   tue apr 28 23:43:57 2015 exts6    821.000000      1   tue apr 28 23:43:57 2015 exts7    824.000000      1   tue apr 28 23:43:57 2015 

i not hope question confusing. please let me know if have clear out something. full code pasted here.

i suggest add "pthread_mutex_t" handle file writing:
1. declare mutex_writing global variable
2. is_writing[exts_limit] global variable //for each sensor:boolean indicating writing status
3. writingtime[exts_limit]//store last writing time given sensor
4. index //added arg_struct in order identify sensor , modify is_writing
5. @ writing time lock mutex_writing unlock

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #include <time.h>  #define exts_limit 1000 #define cps_limit 200 #define maf1_limit 700 #define maf2_limit 130 #define ks_limit 20 #define fts_limit 100 #define cts_limit 105 #define ots_limit 100 #define ops_limit 11 #define fps_limit 11 #define map_limit 5    /****/  #define    millisecondes  1000  #define    fivesecs   5000*millisecondes  pthread_mutex_t mutex_writing = pthread_mutex_initializer;  int is_writing[exts_limit];   clock_t writingtime[exts_limit];  /***/   struct arg_struct{         char *sensorname;         double sensorvalue;         int faultcode;     int index; };  void *write_sens_error(void *arguments) {      pthread_mutex_lock(&mutex_writing);     struct arg_struct *args = arguments;         struct tm *local;         time_t t;         t = time(null);         local = localtime(&t);      file *fp;     fp = fopen("/home/sal/desktop/sensor_data.txt","a"); //path textfile.     fprintf(fp, "%s %s %f %s %d %s %s", args -> sensorname, "\t",args -> sensorvalue, "\t", args -> faultcode,"\t",asctime(local));         fclose(fp);     is_writing[args->index]=0;     pthread_mutex_unlock(&mutex_writing);     pthread_exit(null);     return null; }  void detect_error(char *sensorname, double sensorvalue, int faultcode, int index){         pthread_t thread_error1;         struct arg_struct args;         args.sensorname = malloc(7);         strcpy(args.sensorname, sensorname);         args.sensorvalue = sensorvalue;         args.faultcode = faultcode;     args.index=index;          clock_t t=clock();     if(is_writing[index]==0 && ((float)t-(float)writingtime[index])>fivesecs)     {     is_writing[index]=1;             writingtime[index]=clock();              pthread_create(&thread_error1, null, &write_sens_error, (void *)&args);              pthread_join(thread_error1, null);         }         free(args.sensorname); }  void rand_temperature_sensor(double exts[8], int fts, int cts, int ots){         int i;         (i = 0; < 8; i++){                 exts[i] = rand() % 10 + 820;                 if(exts[i]>exts_limit){                         char name[6];                         sprintf(name, "exts%d", i+1);                         detect_error(name,exts[i],((exts[i]>1050) ? 2 : 1),i);                 }         }         fts = rand() % 30 + 91;                 if(fts > fts_limit) detect_error("fts",fts,((fts>120) ? 2 : 1),8);         cts = rand() % 3 + 90;                 if(cts > cts_limit) detect_error("cts",cts,((cts>110) ? 2 : 1),9);         ots = rand() % 30 + 91;                 if(ots > ots_limit) detect_error("ots",ots,((ots>120) ? 2 : 1),10);         printf("%s,%f,%f,%f,%f,%f,%f,%f,%f,%d,%d,%d\n","temp",exts[0],exts[1],exts[2],exts[3],exts[4],exts[5],exts[6],exts[7],fts,cts,ots); }  void rand_cps_ls_ks_acc(int cps[8], int ls[2], int ks[2],int ts[2]){         int i;         (i = 0; < 8; i++){                 cps[i] = rand() % 2 + 20;         }         for(i=0; < 2; i++){                 ls[i] = rand() % 3 + 30;                 ks[i] = rand() % 3 + 40;                 ts[i] = rand() % 40 + 100;         }          printf("%s,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n","cps_ls_ks_ts", cps[0],cps[1],cps[2],cps[3],cps[4],cps[5],cps[6],cps[7],ls[0],ls[1],ks[0],ks[1],ts[0],ts[1]); }  void rand_cam_acc(int cam[4], int acc_[2]){         int i;         (i = 0; < 4; i++){                 cam[i] = rand() % 7 + 10;         }                 acc_[1] = rand() % 3 + 20;                 acc_[2] = rand() % 1 + 1;         printf("%s,%d,%d,%d,%d,%d,%d\n","cam_acc", cam[0],cam[1],cam[2],cam[3],acc_[1],acc_[2]); }  void rand_map_maf_ffs_crank(int dash_kmt, int map, int maf, int ffs, int crank, int ops, int fps){         dash_kmt = rand() % 6 + 60;         map = rand() % 10 + 30;         maf = rand() % 5 + 20;         ffs = rand() % 2 + 20;         crank = rand() % 3 + 2;         ops = rand() % 3 + 2;         fps = 25;         printf("%s,%d,%d,%d,%d,%d,%d,%d\n","dash", dash_kmt,map,maf,ffs,crank,ops,fps); }  int main() {  /*init global variables*/ int count=0; for(count=0;count<exts_limit;count++) {     is_writing[count]=0; writingtime[count]=0; } /****/       int i;     setbuf(stdout, null); // disable output buffering.     //setbuf(stdin, null); //disable input buffering          srand ( time(null) ); //generate different random values          double exts[8]; int cps[8],cam[4],ls[2],ks[2],ts[2],acc_[2],fts, cts, ots;         int dash_kmt,map,maf,ffs,crank,ops,fps;         int state = 1;         while(state == 1){                 rand_temperature_sensor(exts,fts,cts,ots);                 rand_cps_ls_ks_acc(cps,ls,ks,ts);                 rand_cam_acc(cam,acc_);                 rand_map_maf_ffs_crank(dash_kmt,map,maf,ffs,crank,ops,fps);                 usleep(600);         }          //pthread_join(thread_error1, null);      return 0; } 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -